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"

0 commit comments

Comments
 (0)