Skip to content

Commit e47d31f

Browse files
authored
Small fixes (#108)
This PR adds support for PostgreSQL's row-level locking syntax (FOR UPDATE, FOR SHARE, etc.) as top-level keywords and fixes a tokenizer bug where type specifiers weren't recognized after numeric literals.
1 parent 9385670 commit e47d31f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,27 @@ mod tests {
443443
assert_eq!(format(input, &QueryParams::None, &options), expected);
444444
}
445445

446+
#[test]
447+
fn it_formats_select_with_for_update_of() {
448+
let input: &'static str = "SELECT id FROM users WHERE disabled_at IS NULL FOR UPDATE OF users SKIP LOCKED LIMIT 1";
449+
let options = FormatOptions::default();
450+
let expected = indoc!(
451+
"
452+
SELECT
453+
id
454+
FROM
455+
users
456+
WHERE
457+
disabled_at IS NULL
458+
FOR UPDATE
459+
OF users SKIP LOCKED
460+
LIMIT
461+
1"
462+
);
463+
464+
assert_eq!(format(input, &QueryParams::None, &options), expected);
465+
}
466+
446467
#[test]
447468
fn it_formats_limit_with_two_comma_separated_values_on_single_line() {
448469
let input = "LIMIT 5, 10;";

src/tokenizer.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ fn get_type_specifier_token<'i>(
157157
TokenKind::Placeholder,
158158
TokenKind::Reserved,
159159
TokenKind::String,
160+
TokenKind::Number,
160161
TokenKind::TypeSpecifier,
161162
TokenKind::Word,
162163
]
@@ -461,7 +462,7 @@ fn get_top_level_reserved_token<'a>(
461462
last_reserved_top_level_token: Option<Token<'a>>,
462463
) -> impl Parser<&'a str, Token<'a>, ContextError> {
463464
move |input: &mut &'a str| {
464-
let uc_input: String = get_uc_words(input, 3);
465+
let uc_input: String = get_uc_words(input, 4);
465466
let mut uc_input = uc_input.as_str();
466467

467468
// First peek at the first character to determine which group to check
@@ -502,6 +503,14 @@ fn get_top_level_reserved_token<'a>(
502503
'F' => alt((
503504
terminated("FETCH FIRST", end_of_word),
504505
terminated("FROM", end_of_word),
506+
terminated(
507+
(
508+
"FOR ",
509+
alt(("UPDATE", "NO KEY UPDATE", "SHARE", "KEY SHARE")),
510+
)
511+
.take(),
512+
end_of_word,
513+
),
505514
))
506515
.parse_next(&mut uc_input),
507516

0 commit comments

Comments
 (0)