From d31012f4e7942ed2068a2307ef1e31db626c70de Mon Sep 17 00:00:00 2001 From: cp-20 Date: Tue, 4 Feb 2025 00:38:05 +0900 Subject: [PATCH] fix(normalizer): INSERT stmt space normalization --- normalizer/query_normalizer.go | 4 ++++ normalizer/query_normalizer_test.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/normalizer/query_normalizer.go b/normalizer/query_normalizer.go index e83e8a6..196f343 100644 --- a/normalizer/query_normalizer.go +++ b/normalizer/query_normalizer.go @@ -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*)+\?\)`) @@ -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 (?)") diff --git a/normalizer/query_normalizer_test.go b/normalizer/query_normalizer_test.go index 9d4b0e2..dc30cc8 100644 --- a/normalizer/query_normalizer_test.go +++ b/normalizer/query_normalizer_test.go @@ -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 (?);", }, {