forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check column references in ORDER BY (sqlc-dev#1411)
- Loading branch information
1 parent
4837b07
commit baf228d
Showing
5 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package compiler | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/kyleconroy/sqlc/internal/config" | ||
"github.com/kyleconroy/sqlc/internal/engine/dolphin" | ||
"github.com/kyleconroy/sqlc/internal/engine/postgresql" | ||
"github.com/kyleconroy/sqlc/internal/engine/sqlite" | ||
"github.com/kyleconroy/sqlc/internal/opts" | ||
) | ||
|
||
func Test_ParseQueryErrors(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
engine config.Engine | ||
createStmt string | ||
selectStmt string | ||
parser Parser | ||
wantErr error | ||
}{ | ||
{ | ||
name: "unreferenced order column postgresql", | ||
engine: config.EnginePostgreSQL, | ||
createStmt: `CREATE TABLE authors (id INT);`, | ||
selectStmt: `SELECT id FROM authors ORDER BY foo;`, | ||
parser: postgresql.NewParser(), | ||
wantErr: fmt.Errorf(`column reference "foo" not found`), | ||
}, | ||
{ | ||
name: "unreferenced order column mysql", | ||
engine: config.EngineMySQL, | ||
createStmt: `CREATE TABLE authors (id INT);`, | ||
selectStmt: `SELECT id FROM authors ORDER BY foo;`, | ||
parser: dolphin.NewParser(), | ||
wantErr: fmt.Errorf(`column reference "foo" not found`), | ||
}, | ||
{ | ||
name: "unreferenced order column sqlite", | ||
engine: config.EngineSQLite, | ||
createStmt: `CREATE TABLE authors (id INT);`, | ||
selectStmt: `SELECT id FROM authors ORDER BY foo;`, | ||
parser: sqlite.NewParser(), | ||
wantErr: fmt.Errorf(`column reference "foo" not found`), | ||
}, | ||
{ | ||
name: "unreferenced group column postgresql", | ||
engine: config.EnginePostgreSQL, | ||
createStmt: `CREATE TABLE authors ( id INT );`, | ||
selectStmt: `SELECT id FROM authors GROUP BY foo;`, | ||
parser: postgresql.NewParser(), | ||
wantErr: fmt.Errorf(`column reference "foo" not found`), | ||
}, | ||
{ | ||
name: "unreferenced group column mysql", | ||
engine: config.EngineMySQL, | ||
createStmt: `CREATE TABLE authors (id INT);`, | ||
selectStmt: `SELECT id FROM authors GROUP BY foo;`, | ||
parser: dolphin.NewParser(), | ||
wantErr: fmt.Errorf(`column reference "foo" not found`), | ||
}, | ||
{ | ||
name: "unreferenced group column sqlite", | ||
engine: config.EngineSQLite, | ||
createStmt: `CREATE TABLE authors (id INT);`, | ||
selectStmt: `SELECT id FROM authors GROUP BY foo;`, | ||
parser: sqlite.NewParser(), | ||
wantErr: fmt.Errorf(`column reference "foo" not found`), | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
conf := config.SQL{ | ||
Engine: tc.engine, | ||
} | ||
combo := config.CombinedSettings{} | ||
comp := NewCompiler(conf, combo) | ||
stmts, err := tc.parser.Parse(strings.NewReader(tc.createStmt)) | ||
if err != nil { | ||
t.Fatalf("cannot parse test catalog: %v", err) | ||
} | ||
err = comp.catalog.Update(stmts[0], comp) | ||
if err != nil { | ||
t.Fatalf("cannot update test catalog: %v", err) | ||
} | ||
stmts, err = tc.parser.Parse(strings.NewReader(tc.selectStmt)) | ||
if err != nil { | ||
t.Errorf("Parse failed: %v", err) | ||
} | ||
if len(stmts) != 1 { | ||
t.Errorf("expected one statement, got %d", len(stmts)) | ||
} | ||
|
||
_, err = comp.parseQuery(stmts[0].Raw, tc.selectStmt, opts.Parser{}) | ||
if err == nil { | ||
t.Fatalf("expected parseQuery to return an error, got nil") | ||
} | ||
if err.Error() != tc.wantErr.Error() { | ||
t.Errorf("error message: want %s, got %s", tc.wantErr, err.Error()) | ||
} | ||
}) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
internal/endtoend/testdata/order_by_non_existing_column/postgresql/query.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- Example queries for sqlc | ||
CREATE TABLE authors ( | ||
id INT | ||
); | ||
|
||
-- name: ListAuthors :many | ||
SELECT id FROM authors | ||
ORDER BY adfadsf; |
7 changes: 7 additions & 0 deletions
7
internal/endtoend/testdata/order_by_non_existing_column/postgresql/sqlc.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: 1 | ||
packages: | ||
- path: "go" | ||
name: "querytest" | ||
engine: "postgresql" | ||
schema: "query.sql" | ||
queries: "query.sql" |
2 changes: 2 additions & 0 deletions
2
internal/endtoend/testdata/order_by_non_existing_column/postgresql/stderr.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# package querytest | ||
query.sql:8:10: column reference "adfadsf" not found |