fix(sql): handle comment-only SQL files

This commit is contained in:
lewis authored and GitHub committed 2026-07-22 17:17:50 +08:00
1 parent ffbd32d203
commit c180c40b36
1 file changed
+23
+23
View File
@@ -1888,6 +1888,10 @@ fn leading_executable_sql_with_options(sql: &str, options: SqlParsingOptions) ->
i += 1;
}
if i >= bytes.len() {
break;
}
if i + 1 < bytes.len() && bytes[i] == b'-' && bytes[i + 1] == b'-' {
i += 2;
while i < bytes.len() && bytes[i] != b'\n' {
@@ -1931,6 +1935,10 @@ fn first_executable_sql_token_with_options(sql: &str, options: SqlParsingOptions
i += 1;
}
if i >= bytes.len() {
break;
}
if i + 1 < bytes.len() && bytes[i] == b'-' && bytes[i + 1] == b'-' {
i += 2;
while i < bytes.len() && bytes[i] != b'\n' {
@@ -2419,6 +2427,21 @@ mod tests {
);
}
#[test]
fn mysql_split_skips_comment_only_statement() {
assert!(split_sql_statements_for_database("-- DBX SQL preview crash reproducer\n\n;", DatabaseType::Mysql)
.is_empty());
}
#[test]
fn mysql_keyword_detection_skips_comment_only_input() {
assert!(!starts_with_executable_sql_keyword_for_database(
"-- DBX SQL preview crash reproducer\n\n",
&["SELECT"],
DatabaseType::Mysql,
));
}
#[test]
fn decodes_utf8_bom_sql_file_bytes_without_bom_statement_prefix() {
let sql = decode_sql_file_bytes(b"\xEF\xBB\xBFCREATE TABLE t(id int);").unwrap();