From 4778aa05b499be2039c6366fae72b67530b44dfb Mon Sep 17 00:00:00 2001 From: kevin <5299031+kevin9327@users.noreply.github.com> Date: Mon, 5 Oct 2026 11:59:13 +0900 Subject: [PATCH] fix(sql): match quote and dash-dash rules in cursor statement split --- crates/dbx-sql-core/src/sql.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/dbx-sql-core/src/sql.rs b/crates/dbx-sql-core/src/sql.rs index 701417f727..cb01535152 100644 --- a/crates/dbx-sql-core/src/sql.rs +++ b/crates/dbx-sql-core/src/sql.rs @@ -1056,7 +1056,10 @@ fn split_sql_statement_ranges_with_options(sql: &str, options: SqlParsingOptions } if !in_single_quote && !in_double_quote && !in_backtick { - if ch == '-' && next == Some('-') { + if ch == '-' + && next == Some('-') + && dash_dash_starts_line_comment(options.profile, next_char_at(sql, i + 2)) + { in_line_comment = true; i += 2; continue; @@ -1118,11 +1121,11 @@ fn split_sql_statement_ranges_with_options(sql: &str, options: SqlParsingOptions } match ch { - '\'' if !in_double_quote && !in_backtick && !is_escaped_single_quote(sql, i) => { + '\'' if !in_double_quote && !in_backtick && !has_odd_trailing_backslashes(&sql[start..i]) => { in_single_quote = !in_single_quote; i += ch.len_utf8(); } - '"' if !in_single_quote && !in_backtick => { + '"' if !in_single_quote && !in_backtick && !has_odd_trailing_backslashes(&sql[start..i]) => { in_double_quote = !in_double_quote; i += ch.len_utf8(); } @@ -1253,10 +1256,6 @@ fn next_char_len(sql: &str, index: usize) -> usize { next_char(sql, index).len_utf8() } -fn is_escaped_single_quote(sql: &str, index: usize) -> bool { - index > 0 && sql.as_bytes().get(index - 1) == Some(&b'\\') -} - fn is_on_delimiter_line(sql: &str, range_start: usize, index: usize, options: SqlParsingOptions) -> bool { let line_start = sql[range_start..index].rfind('\n').map_or(range_start, |pos| range_start + pos + 1); sql[line_start..index].trim_start().as_bytes().get(..9).is_some_and(|prefix| { @@ -3357,6 +3356,22 @@ mod tests { ); } + #[test] + fn cursor_statement_closes_mysql_string_after_escaped_backslash() { + let sql = "SELECT 'a\\\\';\nSELECT 2;"; + let cursor = sql.find("SELECT 2").unwrap() + 3; + assert_eq!(find_statement_at_cursor_for_database(sql, cursor, DatabaseType::Mysql), "SELECT 2"); + assert_eq!(find_statement_at_cursor_for_database(sql, 3, DatabaseType::Mysql), "SELECT 'a\\\\'"); + } + + #[test] + fn cursor_statement_treats_mysql_dash_dash_without_space_as_minus() { + let sql = "SELECT 5--1;\nSELECT 2;"; + let cursor = sql.find("SELECT 2").unwrap() + 3; + assert_eq!(find_statement_at_cursor_for_database(sql, cursor, DatabaseType::Mysql), "SELECT 2"); + assert_eq!(find_statement_at_cursor_for_database(sql, 3, DatabaseType::Mysql), "SELECT 5--1"); + } + #[test] fn keeps_mysql_string_open_after_odd_trailing_backslashes() { let sql = r#"INSERT INTO notes VALUES ('it\'s; still one value'); SELECT 1;"#;