25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(sql): handle MySQL dash comments correctly
Melody Ruch880d635961b7
diff --git a/crates/dbx-core/src/sql.rs b/crates/dbx-core/src/sql.rsindex 4b115b86ca..73298b4ebd 100644--- a/crates/dbx-core/src/sql.rs+++ b/crates/dbx-core/src/sql.rs@@ -118,6 +118,7 @@ struct SqlDialectProfile {supports_go_batch_separator: bool,keeps_sqlserver_module_batch_at_cursor: bool,preserves_tdsql_proxy_directive: bool,+ requires_whitespace_after_line_comment_dashes: bool,}impl Default for SqlDialectProfile {@@ -134,6 +135,7 @@ impl Default for SqlDialectProfile {supports_go_batch_separator: false,keeps_sqlserver_module_batch_at_cursor: false,preserves_tdsql_proxy_directive: false,+ requires_whitespace_after_line_comment_dashes: false,}}}@@ -168,7 +170,12 @@ impl SqlDialectProfile {}fn mysql_compatible() -> Self {- Self { supports_hash_line_comments: true, supports_mysql_routine_blocks: true, ..Self::default() }+ Self {+ supports_hash_line_comments: true,+ supports_mysql_routine_blocks: true,+ requires_whitespace_after_line_comment_dashes: true,+ ..Self::default()+ }}fn mysql() -> Self {@@ -288,6 +295,18 @@ fn sql_file_encoding_error() -> String {.to_string()}+/// Whether a `--` sequence starts a line comment. MySQL (and MySQL-wire-compatible+/// dialects) requires the second dash to be followed by whitespace, a control character,+/// or end-of-input —+/// `5--1` is subtraction (`5 - -1`), not a comment. Other dialects (Postgres, Oracle/DM,+/// SQL Server, ...) treat `--` as a comment opener unconditionally.+fn dash_dash_starts_line_comment(profile: SqlDialectProfile, char_after_dashes: Option<char>) -> bool {+ if !profile.requires_whitespace_after_line_comment_dashes {+ return true;+ }+ char_after_dashes.is_none_or(|ch| ch.is_whitespace() || ch.is_control())+}+#[derive(Default)]pub struct SqlStatementSplitter {buffer: String,@@ -299,6 +318,7 @@ pub struct SqlStatementSplitter {dollar_quote_tag: Option<String>,postgres_dollar_quoted_routine: bool,previous: Option<char>,+ pending_mysql_line_comment_dashes: bool,custom_delimiter: Option<String>,options: SqlParsingOptions,}@@ -313,6 +333,13 @@ impl SqlStatementSplitter {let chars = chunk.chars().collect::<Vec<_>>();let mut i = 0;+ if self.pending_mysql_line_comment_dashes {+ if let Some(first) = chars.first().copied() {+ self.in_line_comment = dash_dash_starts_line_comment(self.options.profile, Some(first));+ self.pending_mysql_line_comment_dashes = false;+ }+ }+while i < chars.len() {if let Some(tag) = &self.dollar_quote_tag {let tag_chars = tag.chars().collect::<Vec<_>>();@@ -358,11 +385,20 @@ impl SqlStatementSplitter {if !self.in_single_quote && !self.in_double_quote && !self.in_backtick {if self.previous == Some('-') && ch == '-' {- self.in_line_comment = true;- self.buffer.push(ch);- self.previous = Some(ch);- i += 1;- continue;+ if self.options.profile.requires_whitespace_after_line_comment_dashes && next.is_none() {+ self.pending_mysql_line_comment_dashes = true;+ self.buffer.push(ch);+ self.previous = Some(ch);+ i += 1;+ continue;+ }+ if dash_dash_starts_line_comment(self.options.profile, next) {+ self.in_line_comment = true;+ self.buffer.push(ch);+ self.previous = Some(ch);+ i += 1;+ continue;+ }}if self.previous == Some('/') && ch == '*' {self.in_block_comment = true;@@ -371,13 +407,6 @@ impl SqlStatementSplitter {i += 1;continue;}- if ch == '-' && next == Some('-') {- self.in_line_comment = true;- self.buffer.push(ch);- self.previous = Some(ch);- i += 1;- continue;- }if self.options.profile.supports_hash_line_comments && ch == '#' {self.in_line_comment = true;self.buffer.push(ch);@@ -521,6 +550,10 @@ impl SqlStatementSplitter {pub fn finish(mut self) -> Vec<String> {let mut statements = Vec::new();+ if self.pending_mysql_line_comment_dashes {+ self.in_line_comment = true;+ self.pending_mysql_line_comment_dashes = false;+ }let trimmed = self.buffer.trim();let last_line = trimmed.rsplit('\n').next().unwrap_or(trimmed).trim();if self.options.profile.supports_custom_delimiter_commands && parse_delimiter_command(last_line).is_some() {@@ -552,6 +585,7 @@ impl SqlStatementSplitter {self.buffer.clear();self.postgres_dollar_quoted_routine = false;self.previous = None;+ self.pending_mysql_line_comment_dashes = false;}fn on_delimiter_line(&self) -> bool {@@ -2589,10 +2623,10 @@ mod tests {use crate::models::connection::DatabaseType;use super::{- contains_or_fuzzy_match, decode_sql_file_bytes, find_statement_at_cursor_for_database, fuzzy_filter_enabled,- fuzzy_like_pattern_with_escape, fuzzy_subsequence_match, optimize_sql_file_import_statements,- prepare_sql_file_statement, split_sql_script, split_sql_statement_ranges_with_options,- split_sql_statements_for_database, starts_with_executable_sql_keyword,+ contains_or_fuzzy_match, dash_dash_starts_line_comment, decode_sql_file_bytes,+ find_statement_at_cursor_for_database, fuzzy_filter_enabled, fuzzy_like_pattern_with_escape,+ fuzzy_subsequence_match, optimize_sql_file_import_statements, prepare_sql_file_statement, split_sql_script,+ split_sql_statement_ranges_with_options, split_sql_statements_for_database, starts_with_executable_sql_keyword,starts_with_executable_sql_keyword_for_database, SqlDialectProfile, SqlFileStatementAction, SqlParsingOptions,SqlStatementSplitter,};@@ -2633,6 +2667,77 @@ mod tests {.is_empty());}+ #[test]+ fn mysql_dash_dash_without_trailing_space_is_not_a_comment_per_issue_5382() {+ // MySQL requires `--` to be followed by whitespace to start a line comment;+ // `5--1` is subtraction (5 - -1), not a comment.+ assert_eq!(split_sql_statements_for_database("SELECT 5--1;", DatabaseType::Mysql), vec!["SELECT 5--1"]);+ }++ #[test]+ fn mysql_dash_dash_without_trailing_space_does_not_merge_following_statements_per_issue_5382() {+ let statements = split_sql_statements_for_database(+ "INSERT INTO t VALUES (1, 5--1);\nINSERT INTO t VALUES (2, 10);",+ DatabaseType::Mysql,+ );+ assert_eq!(statements, vec!["INSERT INTO t VALUES (1, 5--1)", "INSERT INTO t VALUES (2, 10)"]);+ }++ #[test]+ fn mysql_dash_dash_with_trailing_space_still_starts_a_line_comment() {+ // The comment text itself is preserved verbatim in the following statement's+ // buffer (comments are never stripped, just protected from delimiter parsing) —+ // this only asserts the embedded `;` inside the comment doesn't split early.+ let statements = split_sql_statements_for_database(+ "INSERT INTO t VALUES (1); -- trailing comment; with semicolon\nINSERT INTO t VALUES (2);",+ DatabaseType::Mysql,+ );+ assert_eq!(+ statements,+ vec!["INSERT INTO t VALUES (1)", "-- trailing comment; with semicolon\nINSERT INTO t VALUES (2)"]+ );+ }++ #[test]+ fn dash_dash_requires_space_only_for_mysql_compatible_profiles() {+ let mysql_profile = SqlDialectProfile::mysql();+ assert!(dash_dash_starts_line_comment(mysql_profile, Some(' ')));+ assert!(dash_dash_starts_line_comment(mysql_profile, Some('\n')));+ assert!(dash_dash_starts_line_comment(mysql_profile, Some('\u{1}')));+ assert!(dash_dash_starts_line_comment(mysql_profile, None));+ assert!(!dash_dash_starts_line_comment(mysql_profile, Some('1')));++ // Postgres, Oracle/DM, SQL Server, ... treat `--` as a comment opener unconditionally.+ let default_profile = SqlDialectProfile::default();+ assert!(dash_dash_starts_line_comment(default_profile, Some('1')));+ assert!(dash_dash_starts_line_comment(default_profile, None));+ }++ #[test]+ fn mysql_dash_dash_opener_without_space_can_span_chunks() {+ let mut splitter = SqlStatementSplitter::with_options(SqlParsingOptions::mysql_compatible());++ assert_eq!(splitter.push_chunk("SELECT 5-"), Vec::<String>::new());+ assert_eq!(splitter.push_chunk("-1;"), vec!["SELECT 5--1"]);+ assert_eq!(splitter.finish(), Vec::<String>::new());+ }++ #[test]+ fn mysql_complete_dash_dash_sequence_waits_for_the_next_chunk() {+ let mut splitter = SqlStatementSplitter::with_options(SqlParsingOptions::mysql_compatible());++ assert_eq!(splitter.push_chunk("SELECT 5--"), Vec::<String>::new());+ assert_eq!(splitter.push_chunk("1;\nSELECT 2;"), vec!["SELECT 5--1", "SELECT 2"]);+ assert_eq!(splitter.finish(), Vec::<String>::new());+ }++ #[test]+ fn mysql_control_character_after_dashes_starts_a_line_comment() {+ let statements = split_sql_statements_for_database("SELECT 1--\u{1}; hidden\nSELECT 2;", DatabaseType::Mysql);++ assert_eq!(statements, vec!["SELECT 1--\u{1}; hidden\nSELECT 2"]);+ }+#[test]fn mysql_keyword_detection_skips_comment_only_input() {assert!(!starts_with_executable_sql_keyword_for_database(