diff --git a/obfuscate_and_normalize_test.go b/obfuscate_and_normalize_test.go index b7e8e9b..56fda7c 100644 --- a/obfuscate_and_normalize_test.go +++ b/obfuscate_and_normalize_test.go @@ -253,6 +253,20 @@ multiline comment */ WithDBMS(DBMSOracle), }, }, + { + input: `SELECT * FROM users WHERE id = ? # this is a comment`, + expected: `SELECT * FROM users WHERE id = ?`, + statementMetadata: StatementMetadata{ + Tables: []string{"users"}, + Comments: []string{"# this is a comment"}, + Commands: []string{"SELECT"}, + Procedures: []string{}, + Size: 30, + }, + lexerOpts: []lexerOption{ + WithDBMS(DBMSMySQL), + }, + }, } obfuscator := NewObfuscator( diff --git a/sqllexer.go b/sqllexer.go index 6651849..7aa7178 100644 --- a/sqllexer.go +++ b/sqllexer.go @@ -149,7 +149,10 @@ func (s *Lexer) Scan() Token { fallthrough case ch == '#': if s.config.DBMS == DBMSSQLServer { - return s.scanIdentifier('#') + return s.scanIdentifier(ch) + } else if s.config.DBMS == DBMSMySQL { + // MySQL treats # as a comment + return s.scanSingleLineComment() } fallthrough case isOperator(ch): diff --git a/sqllexer_test.go b/sqllexer_test.go index 4e0bed7..6f98bee 100644 --- a/sqllexer_test.go +++ b/sqllexer_test.go @@ -604,6 +604,22 @@ func TestLexer(t *testing.T) { }, lexerOpts: []lexerOption{WithDBMS(DBMSSQLServer)}, }, + { + name: "MySQL comment", + input: `SELECT * FROM users # comment`, + expected: []Token{ + {IDENT, "SELECT"}, + {WS, " "}, + {WILDCARD, "*"}, + {WS, " "}, + {IDENT, "FROM"}, + {WS, " "}, + {IDENT, "users"}, + {WS, " "}, + {COMMENT, "# comment"}, + }, + lexerOpts: []lexerOption{WithDBMS(DBMSMySQL)}, + }, } for _, tt := range tests {