Skip to content

Commit

Permalink
fix(normalizer): INSERT stmt space normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-20 committed Feb 3, 2025
1 parent ed8d415 commit d31012f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions normalizer/query_normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

var spaceRegex = regexp.MustCompile(`\s+`)
var insertRegex = regexp.MustCompile(`INSERT INTO (\w+)\s*\(`)
var inRegex = regexp.MustCompile(`IN\s*\((\?,\s*)+\?\)`)
var valuesRegex = regexp.MustCompile(`VALUES\s*\((\?,\s*)+\?\)`)

Expand All @@ -20,6 +21,9 @@ func NormalizeQuery(query string) string {
// remove backquotes
query = strings.ReplaceAll(query, "`", "")

// INSERT INTO table(... -> INSERT INTO table (...
query = insertRegex.ReplaceAllString(query, "INSERT INTO $1 (")

// IN (?, ?, ?) -> IN (?)
query = inRegex.ReplaceAllString(query, "IN (?)")

Expand Down
2 changes: 1 addition & 1 deletion normalizer/query_normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestNormalizeQuery(t *testing.T) {
expected: "SELECT id from table;",
},
{
query: "INSERT INTO table (name, col) VALUES (?, ?);",
query: "INSERT INTO table(name, col) VALUES (?, ?);",
expected: "INSERT INTO table (name, col) VALUES (?);",
},
{
Expand Down

0 comments on commit d31012f

Please sign in to comment.