fix(sql): fix SQL keyword detection failure caused by leading parentheses

This commit is contained in:
ptma 2026-06-25 22:27:55 +08:00 committed by GitHub
parent c63c9e451c
commit c2a3f491cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 1 deletions

View File

@ -1596,7 +1596,7 @@ fn first_executable_sql_token_with_options(sql: &str, options: SqlParsingOptions
let mut i = 0;
while i < bytes.len() {
while i < bytes.len() && bytes[i].is_ascii_whitespace() {
while i < bytes.len() && (bytes[i].is_ascii_whitespace() || bytes[i] == b'(') {
i += 1;
}
@ -2123,6 +2123,16 @@ mod tests {
assert!(starts_with_executable_sql_keyword("-- comment\nDESC users", &["DESCRIBE"]));
}
#[test]
fn detects_keyword_after_parentheses() {
assert!(starts_with_executable_sql_keyword("(SELECT 1)", &["SELECT"]));
assert!(starts_with_executable_sql_keyword(" ( SELECT 1 )", &["SELECT"]));
assert!(starts_with_executable_sql_keyword("((SELECT 1))", &["SELECT"]));
assert!(starts_with_executable_sql_keyword("(SELECT * FROM users)", &["SELECT"]));
assert!(starts_with_executable_sql_keyword("(INSERT INTO users VALUES (1))", &["INSERT"]));
assert!(starts_with_executable_sql_keyword("/* comment */(UPDATE users SET name = 'test')", &["UPDATE"]));
}
#[test]
fn mysql_hash_comments_are_ignored_for_keyword_detection() {
assert!(starts_with_executable_sql_keyword_for_database(