Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: uppercase=false does not lowercase the query #50

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,4 +1,4 @@
//! This crate is a port of https://github.com/kufii/sql-formatter-plus

Check warning on line 1 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build-test-unix (latest-stable)

this URL is not a hyperlink
//! written in Rust. It is intended to be usable as a pure-Rust library
//! for formatting SQL queries.

Expand Down Expand Up @@ -32,7 +32,7 @@
/// When set, changes reserved keywords to ALL CAPS
///
/// Default: false
wugeer marked this conversation as resolved.
Show resolved Hide resolved
pub uppercase: bool,
pub uppercase: Option<bool>,
/// Controls the number of line breaks after a query
///
/// Default: 1
Expand All @@ -47,7 +47,7 @@
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 @@
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 @@
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 @@

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);
}
}
Loading