Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: filter out empty sql (#4233)

p0ny2c40fec4ee27
diff --git a/plugin/db/spanner/statement_parser.go b/plugin/db/spanner/statement_parser.go
index b56c820e72..9d5dfd8711 100644
--- a/plugin/db/spanner/statement_parser.go
+++ b/plugin/db/spanner/statement_parser.go
@@ -164,7 +164,10 @@ func splitStatement(sql string) ([]string, error) {
}
_, _ = res.WriteRune(c)
} else if c == delimiter {
- stmts = append(stmts, strings.Trim(res.String(), " \n\t"))
+ stmt := strings.Trim(res.String(), " \n\t")
+ if stmt != "" {
+ stmts = append(stmts, stmt)
+ }
res.Reset()
res.Grow(len(sql))
} else {
@@ -177,11 +180,15 @@ func splitStatement(sql string) ([]string, error) {
return nil, spanner.ToSpannerError(status.Errorf(codes.InvalidArgument, "statement contains an unclosed literal: %s", sql))
}
if res.Len() > 0 {
- stmts = append(stmts, strings.Trim(res.String(), " \n\t"))
+ stmt := strings.Trim(res.String(), " \n\t")
+ if stmt != "" {
+ stmts = append(stmts, stmt)
+ }
}
return stmts, nil
}
+// sanitizeSQL removes comments, splits the sql by `;` and returns the trimmed sql statement array.
func sanitizeSQL(sql string) ([]string, error) {
query, err := removeCommentsAndTrim(sql)
if err != nil {
@@ -204,6 +211,7 @@ func isDDL(query string) bool {
return false
}
+// isSelect returns true if the given sql string is a SELECT statement.
func isSelect(query string) bool {
for keyword := range selectStatements {
if len(query) >= len(keyword) && strings.EqualFold(query[:len(keyword)], keyword) {
diff --git a/plugin/db/spanner/statement_parser_test.go b/plugin/db/spanner/statement_parser_test.go
index c92ca72d31..4a5fe78d3d 100644
--- a/plugin/db/spanner/statement_parser_test.go
+++ b/plugin/db/spanner/statement_parser_test.go
@@ -477,6 +477,14 @@ CREATE INDEX bytebase_idx_migration_history_namespace_created ON migration_histo
`,
want: []string{"SELECT 1", "SELECT '2;3;4;'", "SELECT 2"},
},
+ {
+ input: `
+ SELECT 1;;;;
+ SELECT '2;3;4;';;
+ SELECT 2;;
+ `,
+ want: []string{"SELECT 1", "SELECT '2;3;4;'", "SELECT 2"},
+ },
}
a := require.New(t)
for _, tc := range tests {