Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add more database analyzer test cases #2854

Merged
merged 4 commits into from
Oct 16, 2023
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
3 changes: 3 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
if err != nil {
return nil, err
}
if qc == nil {
return nil, fmt.Errorf("query catalog is empty")
}
table, cerr := qc.GetTable(fqn)
if cerr != nil {
// TODO: Update error location
Expand Down
5 changes: 4 additions & 1 deletion internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
}
table, err := c.GetTable(fqn)
if err != nil {
// If the table name doesn't exist, fisrt check if it's a CTE
if qc == nil {
continue
}
// If the table name doesn't exist, first check if it's a CTE
if _, qcerr := qc.GetTable(fqn); qcerr != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions internal/endtoend/testdata/cte_update/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/1515
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["managed-db"]
}
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/cte_update/postgresql/pgx/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/endtoend/testdata/cte_update/postgresql/pgx/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- name: UpdateAttribute :one
with updated_attribute as (UPDATE attribute_value
SET
val = CASE WHEN @filter_value::bool THEN @value ELSE val END
WHERE attribute_value.id = @id
RETURNING id,attribute,val)
select updated_attribute.id, val, name
from updated_attribute
left join attribute on updated_attribute.attribute = attribute.id;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/cte_update/postgresql/pgx/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create table attribute_value
(
id bigserial not null,
val text not null,
attribute bigint not null
);

create table attribute
(
id bigserial not null,
name text not null
);
10 changes: 10 additions & 0 deletions internal/endtoend/testdata/cte_update/postgresql/pgx/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"
1 change: 1 addition & 0 deletions internal/endtoend/testdata/func_return_table/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/1322
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["managed-db"]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: Foo :one
SELECT * FROM register_account('a', 'b');
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE TABLE accounts (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);

-- this is a useless and horrifying function cause we don't hash
-- the password, this is just to repro the bug in sqlc
CREATE OR REPLACE FUNCTION register_account(
_username TEXT,
_password VARCHAR(70)
)
RETURNS TABLE (
account_id INTEGER
)
AS $$
BEGIN
INSERT INTO accounts (username, password)
VALUES (
_username,
_password
)
RETURNING id INTO account_id;

RETURN NEXT;
END;
$$ LANGUAGE plpgsql;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"
1 change: 1 addition & 0 deletions internal/endtoend/testdata/join_using/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/sqlc-dev/sqlc/issues/1425
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["managed-db"]
}
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/join_using/postgresql/pgx/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/join_using/postgresql/pgx/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: SelectJoinUsing :many
select t1.fk, sum(t2.fk) from t1 join t2 using (fk) group by fk;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create table t1 (
fk integer not null unique
);
create table t2 (
fk integer not null references t1(fk)
);
10 changes: 10 additions & 0 deletions internal/endtoend/testdata/join_using/postgresql/pgx/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"
Loading