Skip to content

fix(compiler): insert-with-select statements #4006

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion internal/compiler/query_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embed
case *ast.DeleteStmt:
with = n.WithClause
case *ast.InsertStmt:
with = n.WithClause
if selectStmt, ok := n.SelectStmt.(*ast.SelectStmt); ok && selectStmt.WithClause != nil {
with = selectStmt.WithClause
} else {
with = n.WithClause
}
case *ast.UpdateStmt:
with = n.WithClause
case *ast.SelectStmt:
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/insert_cte_select/mysql/go/db.go

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

21 changes: 21 additions & 0 deletions internal/endtoend/testdata/insert_cte_select/mysql/go/models.go

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

25 changes: 25 additions & 0 deletions internal/endtoend/testdata/insert_cte_select/mysql/go/query.sql.go

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

8 changes: 8 additions & 0 deletions internal/endtoend/testdata/insert_cte_select/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- name: CreateAuthor :exec
insert into authors (id, name, bio)
with potential_authors as (
select id, name, bio
from dummy
)
select id, name, bio
from potential_authors;
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/insert_cte_select/mysql/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create table authors (
id integer primary key,
name text not null,
bio text
);

create table dummy (
id integer primary key,
name text not null,
bio text
);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/insert_cte_select/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "mysql",
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}

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,8 @@
-- name: CreateAuthor :exec
insert into authors (id, name, bio)
with potential_authors as (
select id, name, bio
from dummy
)
select id, name, bio
from potential_authors;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create table authors (
id integer primary key,
name text not null,
bio text
);

create table dummy (
id integer primary key,
name text not null,
bio text
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}

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,8 @@
-- name: CreateAuthor :exec
insert into authors (id, name, bio)
with potential_authors as (
select id, name, bio
from dummy
)
select id, name, bio
from potential_authors;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create table authors (
id integer primary key,
name text not null,
bio text
);

create table dummy (
id integer primary key,
name text not null,
bio text
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v5",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}

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

Loading
Loading