Skip to content

Commit 63f2f65

Browse files
committed
trim spaces and empty statements in sql/parse.parse
1 parent d32bf37 commit 63f2f65

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

sql/parse/parse.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ func parse(ctx *sql.Context, query string, multi bool) (sql.Node, string, string
8484
defer span.End()
8585

8686
s := strings.TrimSpace(query)
87-
if strings.HasSuffix(s, ";") {
88-
s = s[:len(s)-1]
89-
}
87+
// trim spaces and empty statements
88+
s = strings.TrimRightFunc(s, func(r rune) bool {
89+
return r == ';' || unicode.IsSpace(r)
90+
})
9091

9192
var stmt sqlparser.Statement
9293
var err error
@@ -102,9 +103,10 @@ func parse(ctx *sql.Context, query string, multi bool) (sql.Node, string, string
102103
if ri != 0 && ri < len(s) {
103104
parsed = s[:ri]
104105
parsed = strings.TrimSpace(parsed)
105-
if strings.HasSuffix(parsed, ";") {
106-
parsed = parsed[:len(parsed)-1]
107-
}
106+
// trim spaces and empty statements
107+
parsed = strings.TrimRightFunc(parsed, func(r rune) bool {
108+
return r == ';' || unicode.IsSpace(r)
109+
})
108110
remainder = s[ri:]
109111
}
110112
}

sql/parse/parse_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5252,19 +5252,19 @@ func TestParseOne(t *testing.T) {
52525252
},
52535253
{
52545254
"SELECT 1 /* testing */ ;",
5255-
[]string{"SELECT 1 /* testing */ "},
5255+
[]string{"SELECT 1 /* testing */"},
52565256
},
52575257
{
52585258
"SELECT 1 -- this is a test",
52595259
[]string{"SELECT 1 -- this is a test"},
52605260
},
52615261
{
52625262
"-- empty statement with comment\n; SELECT 1; SELECT 2",
5263-
[]string{"-- empty statement with comment\n", "SELECT 1", "SELECT 2"},
5263+
[]string{"-- empty statement with comment", "SELECT 1", "SELECT 2"},
52645264
},
52655265
{
52665266
"SELECT 1; -- empty statement with comment\n; SELECT 2",
5267-
[]string{"SELECT 1", "-- empty statement with comment\n", "SELECT 2"},
5267+
[]string{"SELECT 1", "-- empty statement with comment", "SELECT 2"},
52685268
},
52695269
{
52705270
"SELECT 1; SELECT 2; -- empty statement with comment\n",

0 commit comments

Comments
 (0)