Skip to content

Commit

Permalink
fix: uppercase=false does not lowercase the query (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
wugeer authored Sep 17, 2024
1 parent 4fcfa0d commit fc8fa52
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 37 deletions.
94 changes: 61 additions & 33 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,30 @@ impl<'a> Formatter<'a> {
{
self.trim_spaces_end(query);
}
if self.options.uppercase
&& !self
.options
.ignore_case_convert
.as_ref()
.map(|values| values.contains(&token.value))
.unwrap_or(false)
{
query.push_str(&token.value.to_uppercase());
} else {
query.push_str(token.value);

let value = match (
self.options.uppercase,
self.options.ignore_case_convert.as_ref(),
) {
(Some(uppercase), Some(values)) if !values.contains(&token.value) => {
if uppercase {
Cow::Owned(token.value.to_uppercase())
} else {
Cow::Owned(token.value.to_lowercase())
}
}
(Some(uppercase), None) => {
if uppercase {
Cow::Owned(token.value.to_uppercase())
} else {
Cow::Owned(token.value.to_lowercase())
}
}
_ => Cow::Borrowed(token.value),
};

query.push_str(&value);

self.inline_block.begin_if_possible(self.tokens, self.index);

if !self.inline_block.is_active() {
Expand All @@ -210,18 +221,27 @@ impl<'a> Formatter<'a> {
// Closing parentheses decrease the block indent level
fn format_closing_parentheses(&mut self, token: &Token<'_>, query: &mut String) {
let mut token = token.clone();
let value = if self.options.uppercase
&& !self
.options
.ignore_case_convert
.as_ref()
.map(|values| values.contains(&token.value))
.unwrap_or(false)
{
token.value.to_uppercase()
} else {
token.value.to_string()
let value = match (
self.options.uppercase,
self.options.ignore_case_convert.as_ref(),
) {
(Some(uppercase), Some(values)) if !values.contains(&token.value) => {
if uppercase {
Cow::Owned(token.value.to_uppercase())
} else {
Cow::Owned(token.value.to_lowercase())
}
}
(Some(uppercase), None) => {
if uppercase {
Cow::Owned(token.value.to_uppercase())
} else {
Cow::Owned(token.value.to_lowercase())
}
}
_ => Cow::Borrowed(token.value),
};

token.value = &value;

if self.inline_block.is_active() {
Expand Down Expand Up @@ -317,17 +337,25 @@ impl<'a> Formatter<'a> {
}

fn format_reserved_word<'t>(&self, token: &'t str) -> Cow<'t, str> {
if self.options.uppercase
&& !self
.options
.ignore_case_convert
.as_ref()
.map(|values| values.contains(&token))
.unwrap_or(false)
{
Cow::Owned(token.to_uppercase())
} else {
Cow::Borrowed(token)
match (
self.options.uppercase,
self.options.ignore_case_convert.as_ref(),
) {
(Some(uppercase), Some(values)) if !values.contains(&token) => {
if uppercase {
Cow::Owned(token.to_uppercase())
} else {
Cow::Owned(token.to_lowercase())
}
}
(Some(uppercase), None) => {
if uppercase {
Cow::Owned(token.to_uppercase())
} else {
Cow::Owned(token.to_lowercase())
}
}
_ => Cow::Borrowed(token),
}
}

Expand Down
52 changes: 48 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct FormatOptions<'a> {
/// When set, changes reserved keywords to ALL CAPS
///
/// Default: false
pub uppercase: bool,
pub uppercase: Option<bool>,
/// Controls the number of line breaks after a query
///
/// Default: 1
Expand All @@ -47,7 +47,7 @@ impl<'a> Default for FormatOptions<'a> {
fn default() -> Self {
FormatOptions {
indent: Indent::Spaces(2),
uppercase: false,
uppercase: None,
lines_between_queries: 1,
ignore_case_convert: None,
}
Expand Down Expand Up @@ -741,7 +741,7 @@ mod tests {
fn it_converts_keywords_to_uppercase_when_option_passed_in() {
let input = "select distinct * frOM foo left join bar WHERe cola > 1 and colb = 3";
let options = FormatOptions {
uppercase: true,
uppercase: Some(true),
..FormatOptions::default()
};
let expected = indoc!(
Expand Down Expand Up @@ -1663,7 +1663,7 @@ mod tests {
fn it_uses_given_ignore_case_convert_config() {
let input = "select count(*),Column1 from Table1;";
let options = FormatOptions {
uppercase: true,
uppercase: Some(true),
ignore_case_convert: Some(vec!["from"]),
..FormatOptions::default()
};
Expand Down Expand Up @@ -1717,4 +1717,48 @@ mod tests {

assert_eq!(format(input, &QueryParams::None, &options), expected);
}

#[test]
fn it_converts_keywords_to_lowercase_when_option_passed_in() {
let input = "select distinct * frOM foo left join bar WHERe cola > 1 and colb = 3";
let options = FormatOptions {
uppercase: Some(false),
..FormatOptions::default()
};
let expected = indoc!(
"
select
distinct *
from
foo
left join bar
where
cola > 1
and colb = 3"
);

assert_eq!(format(input, &QueryParams::None, &options), expected);
}

#[test]
fn it_converts_keywords_nothing_when_no_option_passed_in() {
let input = "select distinct * frOM foo left join bar WHERe cola > 1 and colb = 3";
let options = FormatOptions {
uppercase: None,
..FormatOptions::default()
};
let expected = indoc!(
"
select
distinct *
frOM
foo
left join bar
WHERe
cola > 1
and colb = 3"
);

assert_eq!(format(input, &QueryParams::None, &options), expected);
}
}

0 comments on commit fc8fa52

Please sign in to comment.