From cf581e8191dfff7c244a864765ba6df664385d40 Mon Sep 17 00:00:00 2001 From: Zhengda Lu Date: Mon, 11 Sep 2023 16:18:01 -0400 Subject: [PATCH] =?UTF-8?q?special=20case=20treatment=20=3D=3F?= --- normalizer_test.go | 1 + sqllexer.go | 5 ++++- sqllexer_test.go | 20 ++++++++++++++++++++ sqllexer_utils.go | 2 +- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/normalizer_test.go b/normalizer_test.go index b5a05a5..3c440ff 100644 --- a/normalizer_test.go +++ b/normalizer_test.go @@ -388,6 +388,7 @@ func TestNormalizerFormatting(t *testing.T) { queries: []string{ "SELECT id,name, address FROM users where id = ?", "SELECT id, name, address FROM users where id = ?", + "SELECT id,name, address FROM users where id =?", "SELECT id as ID, name as Name, address FROM users where id = ?", }, expected: "SELECT id, name, address FROM users where id = ?", diff --git a/sqllexer.go b/sqllexer.go index c1de46f..113177f 100644 --- a/sqllexer.go +++ b/sqllexer.go @@ -320,8 +320,11 @@ func (s *Lexer) scanWhitespace() Token { func (s *Lexer) scanOperator() Token { s.start = s.cursor + lastCh := s.peek() ch := s.next() - for isOperator(ch) { + for isOperator(ch) && !(lastCh == '=' && ch == '?') { + // hack: we don't want to treat "=?" as an single operator + lastCh = ch ch = s.next() } return Token{OPERATOR, s.src[s.start:s.cursor]} diff --git a/sqllexer_test.go b/sqllexer_test.go index 14e13fc..f6f827f 100644 --- a/sqllexer_test.go +++ b/sqllexer_test.go @@ -449,6 +449,26 @@ func TestLexer(t *testing.T) { {STRING, "'j\\'s'"}, }, }, + { + name: "select with escaped string", + input: "SELECT * FROM users where id =?", + expected: []Token{ + {IDENT, "SELECT"}, + {WS, " "}, + {WILDCARD, "*"}, + {WS, " "}, + {IDENT, "FROM"}, + {WS, " "}, + {IDENT, "users"}, + {WS, " "}, + {IDENT, "where"}, + {WS, " "}, + {IDENT, "id"}, + {WS, " "}, + {OPERATOR, "="}, + {OPERATOR, "?"}, + }, + }, } for _, tt := range tests { diff --git a/sqllexer_utils.go b/sqllexer_utils.go index ba8924c..2e097b6 100644 --- a/sqllexer_utils.go +++ b/sqllexer_utils.go @@ -67,7 +67,7 @@ func isLeadingSign(ch rune) bool { } func isLetter(ch rune) bool { - return unicode.IsLetter(ch) || ch == '_' || ch == '#' + return unicode.IsLetter(ch) || ch == '_' } func isDoubleQuote(ch rune) bool {