Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(normalizer): INSERT stmt space normalization #56

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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