Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

=== Trailing line comment after ';'

>>> musterr [CLI] experimental postgres query --target my-instance SELECT 1; -- done
Error: database instance "my-instance" is not ready for accepting connections (state: STOPPED)

=== Trailing block comment after ';'

>>> musterr [CLI] experimental postgres query --target my-instance SELECT 1; /* done */
Error: database instance "my-instance" is not ready for accepting connections (state: STOPPED)

=== File ending with a comment line after the statement

>>> musterr [CLI] experimental postgres query --target my-instance --file q.sql
Error: database instance "my-instance" is not ready for accepting connections (state: STOPPED)

=== Two statements are still rejected

>>> musterr [CLI] experimental postgres query --target my-instance SELECT 1; -- one SELECT 1; SELECT 2
Error: argv[2]: input contains multiple statements (a ';' separates two or more statements)
This command runs one statement per input. To run multiple statements:
- Pass each as a separate positional: query "SELECT 1" "SELECT 2"
- Pass each in its own --file: query --file q1.sql --file q2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT 1;
-- end of file
16 changes: 16 additions & 0 deletions acceptance/experimental/postgres/query/trailing-comment/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# A single statement followed by a comment after its terminating ';' is one
# statement as far as Postgres is concerned, so the pre-flight check must let
# it through. The instance is STOPPED, so an accepted input fails at target
# resolution instead of the multi-statement check.

title "Trailing line comment after ';'"
trace musterr $CLI experimental postgres query --target my-instance "SELECT 1; -- done"

title "Trailing block comment after ';'"
trace musterr $CLI experimental postgres query --target my-instance "SELECT 1; /* done */"

title "File ending with a comment line after the statement"
trace musterr $CLI experimental postgres query --target my-instance --file q.sql

title "Two statements are still rejected"
trace musterr $CLI experimental postgres query --target my-instance "SELECT 1; -- one" "SELECT 1; SELECT 2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[Server]]
Pattern = "GET /api/2.0/database/instances/my-instance"
Response.Body = '''
{
"name": "my-instance",
"state": "STOPPED"
}
'''
54 changes: 40 additions & 14 deletions experimental/postgres/cmd/multistatement.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,35 @@ import (
var errMultipleStatements = errors.New("input contains multiple statements (a ';' separates two or more statements)")

// checkSingleStatement walks sql and returns errMultipleStatements if a
// statement-terminating ';' is found anywhere except trailing whitespace.
// statement-terminating ';' is followed by anything other than whitespace,
// comments and further ';'. Postgres drops the empty statements those form.
//
// The scanner ignores ';' inside:
// - single-quoted strings ('a;b', SQL standard doubled-quote escape)
// - double-quoted identifiers ("col;name")
// - line comments (-- ... \n)
// - block comments (/* ... */, non-nesting)
// - line comments (-- ... up to \n or \r)
// - block comments (/* ... */, nesting as in Postgres)
// - dollar-quoted bodies ($tag$ ... $tag$, optional tag)
//
// Over-rejection on weird syntactic edge cases is acceptable: users get a
// clear error and can split into multiple input units. v2 may swap this for
// a real Postgres tokenizer.
func checkSingleStatement(sql string) error {
s := sql
// Trim trailing whitespace once so a single trailing ';' is allowed.
end := len(strings.TrimRight(s, " \t\r\n"))
end := len(s)

terminated := false
i := 0
for i < end {
c := s[i]

if terminated && !isSQLSpace(c) && c != ';' && !startsComment(s, i, end) {
return errMultipleStatements
}

switch c {
case ';':
// A ';' that's not at end-of-trimmed-input is a separator.
if i < end-1 {
return errMultipleStatements
}
// Trailing ';' is fine.
terminated = true
i++

case '\'':
Expand Down Expand Up @@ -103,21 +104,46 @@ func scanQuoted(s string, start, end int, quote byte) int {
return end
}

// isSQLSpace reports whether c is whitespace to the Postgres lexer.
func isSQLSpace(c byte) bool {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v'
}

// startsComment reports whether a "--" or "/*" comment starts at s[i].
func startsComment(s string, i, end int) bool {
return i+1 < end && ((s[i] == '-' && s[i+1] == '-') || (s[i] == '/' && s[i+1] == '*'))
}

// scanLineComment advances past a "--" comment opened at s[start]. Like the
// Postgres lexer, it ends the comment at either '\n' or '\r'.
func scanLineComment(s string, start, end int) int {
i := start + 2
for i < end && s[i] != '\n' {
for i < end && s[i] != '\n' && s[i] != '\r' {
i++
}
return i
}

// scanBlockComment advances past a "/*" comment opened at s[start]. Postgres
// nests block comments, so the comment ends at the "*/" that closes the
// outermost "/*". Returns end if unterminated.
func scanBlockComment(s string, start, end int) int {
depth := 1
i := start + 2
for i+1 < end {
if s[i] == '*' && s[i+1] == '/' {
return i + 2
switch {
case s[i] == '/' && s[i+1] == '*':
depth++
i += 2
case s[i] == '*' && s[i+1] == '/':
depth--
i += 2
if depth == 0 {
return i
}
default:
i++
}
i++
}
return end
}
Expand Down
16 changes: 16 additions & 0 deletions experimental/postgres/cmd/multistatement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ func TestCheckSingleStatement(t *testing.T) {
{name: "semi inside line comment then real semi", input: "SELECT 1 -- ; \n; SELECT 2", wantErr: true},
{name: "semi inside dollar then real semi", input: "SELECT $$a;b$$; SELECT 2", wantErr: true},

{name: "trailing line comment after semi", input: "SELECT 1; -- done", wantErr: false},
{name: "trailing block comment after semi", input: "SELECT 1; /* done */\n", wantErr: false},
{name: "comment line after semi", input: "SELECT 1;\n-- end\n", wantErr: false},
{name: "comment then second statement", input: "SELECT 1; -- x\nSELECT 2", wantErr: true},
{name: "block comment then second statement", input: "SELECT 1; /* x */ SELECT 2", wantErr: true},
{name: "CR ends line comment after semi", input: "SELECT 1; -- x\rSELECT 2", wantErr: true},
{name: "CR ends line comment before semi", input: "SELECT 1 -- x\r; SELECT 2", wantErr: true},
{name: "repeated trailing semicolons", input: "SELECT 1;;", wantErr: false},
{name: "spaced trailing semicolons", input: "SELECT 1; ;", wantErr: false},
{name: "semicolon after trailing comment", input: "SELECT 1; -- a\n;", wantErr: false},
{name: "form feed after semi", input: "SELECT 1;\f", wantErr: false},
{name: "nested block comment after semi", input: "SELECT 1; /* /* */ */", wantErr: false},
{name: "nested block comment hides line comment after semi", input: "SELECT 1; /* /* */--*/ SELECT 2", wantErr: true},
{name: "nested block comment hides line comment before semi", input: "SELECT 1 /* /* */--*/; SELECT 2", wantErr: true},
{name: "semicolon in nested block comment", input: "SELECT 1 /* a /* ; */ ; */", wantErr: false},

{name: "leading whitespace", input: " ;", wantErr: false},
{name: "empty input", input: "", wantErr: false},
{name: "only whitespace", input: " \n\t ", wantErr: false},
Expand Down
Loading