mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-07-07 00:26:40 +00:00
5468707289
* fix(util): ignore comment only sql input Problem: sqlutil.SplitStatements strips SQL comments while scanning, but when no statements remain it falls back to returning the original query. Inputs that contain only comments are therefore reported as executable SQL statements. Root cause: The no-statements fallback did not distinguish a real single statement from input that had been fully removed by comment filtering. Fix: Remove the original-query fallback and return an explicit empty slice when scanning produces no statements. Reproduction: env GOCACHE=/private/tmp/seaweedfs-go-cache go test ./weed/util/sqlutil -run TestSplitStatements -count=1 failed before the fix because comment-only inputs returned the comment text as a statement. Validation: gofmt -w weed/util/sqlutil/splitter.go weed/util/sqlutil/splitter_test.go; env GOCACHE=/private/tmp/seaweedfs-go-cache go test ./weed/util/sqlutil -run TestSplitStatements -count=1; env GOCACHE=/private/tmp/seaweedfs-go-cache go test ./weed/util/sqlutil -count=1; git diff --check; git diff --cached --check. Duplicate check: Searched /private/tmp/seaweedfs-codex0610-old-branch-index.tsv and existing tests for sqlutil, SplitStatements, comments, and comment-only. Old PostgreSQL query branches cover malformed wire frames and SQL engine numeric parsing, not comment-only statement splitting. Co-authored-by: Codex <noreply@openai.com> * Update weed/util/sqlutil/splitter.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: Codex <noreply@openai.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
158 lines
4.7 KiB
Go
158 lines
4.7 KiB
Go
package sqlutil
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestSplitStatements(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "Simple single statement",
|
|
input: "SELECT * FROM users",
|
|
expected: []string{"SELECT * FROM users"},
|
|
},
|
|
{
|
|
name: "Multiple statements",
|
|
input: "SELECT * FROM users; SELECT * FROM orders;",
|
|
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Semicolon in single quotes",
|
|
input: "SELECT 'hello;world' FROM users; SELECT * FROM orders;",
|
|
expected: []string{"SELECT 'hello;world' FROM users", "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Semicolon in double quotes",
|
|
input: `SELECT "column;name" FROM users; SELECT * FROM orders;`,
|
|
expected: []string{`SELECT "column;name" FROM users`, "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Escaped quotes in strings",
|
|
input: `SELECT 'don''t split; here' FROM users; SELECT * FROM orders;`,
|
|
expected: []string{`SELECT 'don''t split; here' FROM users`, "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Escaped quotes in identifiers",
|
|
input: `SELECT "column""name" FROM users; SELECT * FROM orders;`,
|
|
expected: []string{`SELECT "column""name" FROM users`, "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Single line comment",
|
|
input: "SELECT * FROM users; -- This is a comment\nSELECT * FROM orders;",
|
|
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Single line comment with semicolon",
|
|
input: "SELECT * FROM users; -- Comment with; semicolon\nSELECT * FROM orders;",
|
|
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Multi-line comment",
|
|
input: "SELECT * FROM users; /* Multi-line\ncomment */ SELECT * FROM orders;",
|
|
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Multi-line comment with semicolon",
|
|
input: "SELECT * FROM users; /* Comment with; semicolon */ SELECT * FROM orders;",
|
|
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Complex mixed case",
|
|
input: `SELECT 'test;string', "quoted;id" FROM users; -- Comment; here
|
|
/* Another; comment */
|
|
INSERT INTO users VALUES ('name''s value', "id""field");`,
|
|
expected: []string{
|
|
`SELECT 'test;string', "quoted;id" FROM users`,
|
|
`INSERT INTO users VALUES ('name''s value', "id""field")`,
|
|
},
|
|
},
|
|
{
|
|
name: "Empty statements filtered",
|
|
input: "SELECT * FROM users;;; SELECT * FROM orders;",
|
|
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Whitespace handling",
|
|
input: " SELECT * FROM users ; SELECT * FROM orders ; ",
|
|
expected: []string{"SELECT * FROM users", "SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Single statement without semicolon",
|
|
input: "SELECT * FROM users",
|
|
expected: []string{"SELECT * FROM users"},
|
|
},
|
|
{
|
|
name: "Empty query",
|
|
input: "",
|
|
expected: []string{},
|
|
},
|
|
{
|
|
name: "Only whitespace",
|
|
input: " \n\t ",
|
|
expected: []string{},
|
|
},
|
|
{
|
|
name: "Only single line comment",
|
|
input: "-- just a comment",
|
|
expected: []string{},
|
|
},
|
|
{
|
|
name: "Only multi-line comment",
|
|
input: "/* just a comment; with separator */",
|
|
expected: []string{},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := SplitStatements(tt.input)
|
|
if !reflect.DeepEqual(result, tt.expected) {
|
|
t.Errorf("SplitStatements() = %v, expected %v", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSplitStatements_EdgeCases(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "Nested comments are not supported but handled gracefully",
|
|
input: "SELECT * FROM users; /* Outer /* inner */ comment */ SELECT * FROM orders;",
|
|
expected: []string{"SELECT * FROM users", "comment */ SELECT * FROM orders"},
|
|
},
|
|
{
|
|
name: "Unterminated string (malformed SQL)",
|
|
input: "SELECT 'unterminated string; SELECT * FROM orders;",
|
|
expected: []string{"SELECT 'unterminated string; SELECT * FROM orders;"},
|
|
},
|
|
{
|
|
name: "Unterminated comment (malformed SQL)",
|
|
input: "SELECT * FROM users; /* unterminated comment",
|
|
expected: []string{"SELECT * FROM users"},
|
|
},
|
|
{
|
|
name: "Multiple semicolons in quotes",
|
|
input: "SELECT ';;;' FROM users; SELECT ';;;' FROM orders;",
|
|
expected: []string{"SELECT ';;;' FROM users", "SELECT ';;;' FROM orders"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := SplitStatements(tt.input)
|
|
if !reflect.DeepEqual(result, tt.expected) {
|
|
t.Errorf("SplitStatements() = %v, expected %v", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|