Skip to content

Commit

Permalink
Merge pull request #10 from DataDog/zhengda.lu/operator
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zhengda authored Sep 11, 2023
2 parents 70764ab + cf581e8 commit a3ef011
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?",
Expand Down
5 changes: 4 additions & 1 deletion sqllexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand Down
20 changes: 20 additions & 0 deletions sqllexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion sqllexer_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a3ef011

Please sign in to comment.