Skip to content

Commit 0cfadb0

Browse files
authored
test(analyzer): Add more database analyzer test cases (#2854)
* Fix #1322 * Fix #1425 * Prevent panics * Fix #1515
1 parent 651cafd commit 0cfadb0

File tree

26 files changed

+355
-1
lines changed

26 files changed

+355
-1
lines changed

internal/compiler/output_columns.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,9 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
608608
if err != nil {
609609
return nil, err
610610
}
611+
if qc == nil {
612+
return nil, fmt.Errorf("query catalog is empty")
613+
}
611614
table, cerr := qc.GetTable(fqn)
612615
if cerr != nil {
613616
// TODO: Update error location

internal/compiler/resolve.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
6363
}
6464
table, err := c.GetTable(fqn)
6565
if err != nil {
66-
// If the table name doesn't exist, fisrt check if it's a CTE
66+
if qc == nil {
67+
continue
68+
}
69+
// If the table name doesn't exist, first check if it's a CTE
6770
if _, qcerr := qc.GetTable(fqn); qcerr != nil {
6871
return nil, err
6972
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/sqlc-dev/sqlc/issues/1515
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["managed-db"]
3+
}

internal/endtoend/testdata/cte_update/postgresql/pgx/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/cte_update/postgresql/pgx/go/models.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/cte_update/postgresql/pgx/go/query.sql.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- name: UpdateAttribute :one
2+
with updated_attribute as (UPDATE attribute_value
3+
SET
4+
val = CASE WHEN @filter_value::bool THEN @value ELSE val END
5+
WHERE attribute_value.id = @id
6+
RETURNING id,attribute,val)
7+
select updated_attribute.id, val, name
8+
from updated_attribute
9+
left join attribute on updated_attribute.attribute = attribute.id;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
create table attribute_value
2+
(
3+
id bigserial not null,
4+
val text not null,
5+
attribute bigint not null
6+
);
7+
8+
create table attribute
9+
(
10+
id bigserial not null,
11+
name text not null
12+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/sqlc-dev/sqlc/issues/1322
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["managed-db"]
3+
}

internal/endtoend/testdata/func_return_table/postgresql/pgx/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/func_return_table/postgresql/pgx/go/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/func_return_table/postgresql/pgx/go/query.sql.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: Foo :one
2+
SELECT * FROM register_account('a', 'b');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
CREATE TABLE accounts (
2+
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
3+
username TEXT NOT NULL UNIQUE,
4+
password TEXT NOT NULL
5+
);
6+
7+
-- this is a useless and horrifying function cause we don't hash
8+
-- the password, this is just to repro the bug in sqlc
9+
CREATE OR REPLACE FUNCTION register_account(
10+
_username TEXT,
11+
_password VARCHAR(70)
12+
)
13+
RETURNS TABLE (
14+
account_id INTEGER
15+
)
16+
AS $$
17+
BEGIN
18+
INSERT INTO accounts (username, password)
19+
VALUES (
20+
_username,
21+
_password
22+
)
23+
RETURNING id INTO account_id;
24+
25+
RETURN NEXT;
26+
END;
27+
$$ LANGUAGE plpgsql;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/sqlc-dev/sqlc/issues/1425
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["managed-db"]
3+
}

internal/endtoend/testdata/join_using/postgresql/pgx/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/join_using/postgresql/pgx/go/models.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/join_using/postgresql/pgx/go/query.sql.go

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: SelectJoinUsing :many
2+
select t1.fk, sum(t2.fk) from t1 join t2 using (fk) group by fk;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
create table t1 (
2+
fk integer not null unique
3+
);
4+
create table t2 (
5+
fk integer not null references t1(fk)
6+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

0 commit comments

Comments
 (0)