diff --git a/acceptance/experimental/postgres/query/trailing-comment/out.test.toml b/acceptance/experimental/postgres/query/trailing-comment/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/experimental/postgres/query/trailing-comment/output.txt b/acceptance/experimental/postgres/query/trailing-comment/output.txt new file mode 100644 index 00000000000..f14586a2906 --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/output.txt @@ -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 diff --git a/acceptance/experimental/postgres/query/trailing-comment/q.sql b/acceptance/experimental/postgres/query/trailing-comment/q.sql new file mode 100644 index 00000000000..ec0021d89c4 --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/q.sql @@ -0,0 +1,2 @@ +SELECT 1; +-- end of file diff --git a/acceptance/experimental/postgres/query/trailing-comment/script b/acceptance/experimental/postgres/query/trailing-comment/script new file mode 100644 index 00000000000..dbc871eb4f3 --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/script @@ -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" diff --git a/acceptance/experimental/postgres/query/trailing-comment/test.toml b/acceptance/experimental/postgres/query/trailing-comment/test.toml new file mode 100644 index 00000000000..0ddf42ff007 --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/test.toml @@ -0,0 +1,8 @@ +[[Server]] +Pattern = "GET /api/2.0/database/instances/my-instance" +Response.Body = ''' +{ + "name": "my-instance", + "state": "STOPPED" +} +''' diff --git a/experimental/postgres/cmd/multistatement.go b/experimental/postgres/cmd/multistatement.go index 12032d1608b..4913f40762d 100644 --- a/experimental/postgres/cmd/multistatement.go +++ b/experimental/postgres/cmd/multistatement.go @@ -12,13 +12,14 @@ 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 @@ -26,20 +27,20 @@ var errMultipleStatements = errors.New("input contains multiple statements (a '; // 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 '\'': @@ -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 } diff --git a/experimental/postgres/cmd/multistatement_test.go b/experimental/postgres/cmd/multistatement_test.go index baeec020dd9..8df5d8aca75 100644 --- a/experimental/postgres/cmd/multistatement_test.go +++ b/experimental/postgres/cmd/multistatement_test.go @@ -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},