Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub(crate) fn format(
.collect::<String>();
s.into()
}
TokenKind::Placeholder => format!("{} / {:?}", token.value, token.key).into(),
_ => Cow::Borrowed(token.value),
};
anstream::eprintln!("{k}{:21}{rk}: {d}{:50}{rd} {line}", kind, value);
Expand Down
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,29 @@ mod tests {
);
}

#[test]
fn it_recognizes_braced_placeholders_with_param_values() {
let input = "SELECT {a}, {b}, {c};";
let params = vec![
("a".to_string(), "first".to_string()),
("b".to_string(), "second".to_string()),
("c".to_string(), "third".to_string()),
];
let options = FormatOptions::default();
let expected = indoc!(
"
SELECT
first,
second,
third;"
);

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

#[test]
fn it_formats_query_with_go_batch_separator() {
let input = "SELECT 1 GO SELECT 2";
Expand Down
19 changes: 18 additions & 1 deletion src/tokenizer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use unicode_categories::UnicodeCategories;
use winnow::ascii::{digit0, digit1, till_line_ending, Caseless};
use winnow::combinator::{alt, dispatch, eof, fail, opt, peek, terminated};
use winnow::combinator::{alt, delimited, dispatch, eof, fail, opt, peek, terminated};
use winnow::error::ContextError;
use winnow::error::ParserError;
use winnow::prelude::*;
Expand Down Expand Up @@ -326,13 +326,15 @@ fn get_placeholder_token<'i>(
get_ident_named_placeholder_token,
|input: &mut _| get_string_named_placeholder_token(input, dialect),
get_indexed_placeholder_token,
get_braced_named_placeholder_token,
))
.parse_next(input)
} else {
alt((
get_indexed_placeholder_token,
get_ident_named_placeholder_token,
|input: &mut _| get_string_named_placeholder_token(input, dialect),
get_braced_named_placeholder_token,
))
.parse_next(input)
}
Expand Down Expand Up @@ -381,6 +383,21 @@ fn get_ident_named_placeholder_token<'i>(input: &mut &'i str) -> Result<Token<'i
})
}

fn get_braced_named_placeholder_token<'i>(input: &mut &'i str) -> Result<Token<'i>> {
delimited('{', take_until(1.., '}'), '}')
.with_taken()
.parse_next(input)
.map(|(index, token)| {
let index = Cow::Borrowed(index);
Token {
kind: TokenKind::Placeholder,
value: token,
key: Some(PlaceholderKind::Named(index)),
alias: token,
}
})
}

fn get_string_named_placeholder_token<'i>(
input: &mut &'i str,
dialect: Dialect,
Expand Down