Skip to content

Commit 66b434e

Browse files
committed
fix(compiler): correctly construct query catalog for insert-with-select queries (all engines)
1 parent 3da0b82 commit 66b434e

31 files changed

+549
-1
lines changed

internal/compiler/query_catalog.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embed
2020
case *ast.DeleteStmt:
2121
with = n.WithClause
2222
case *ast.InsertStmt:
23-
with = n.WithClause
23+
if selectStmt, ok := n.SelectStmt.(*ast.SelectStmt); ok && selectStmt.WithClause != nil {
24+
with = selectStmt.WithClause
25+
} else {
26+
with = n.WithClause
27+
}
2428
case *ast.UpdateStmt:
2529
with = n.WithClause
2630
case *ast.SelectStmt:

internal/endtoend/testdata/insert_cte_select/mysql/go/db.go

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

internal/endtoend/testdata/insert_cte_select/mysql/go/models.go

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

internal/endtoend/testdata/insert_cte_select/mysql/go/query.sql.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- name: CreateAuthor :exec
2+
insert into authors (id, name, bio)
3+
with potential_authors as (
4+
select id, name, bio
5+
from dummy
6+
)
7+
select id, name, bio
8+
from potential_authors;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
create table authors (
2+
id integer primary key,
3+
name text not null,
4+
bio text
5+
);
6+
7+
create table dummy (
8+
id integer primary key,
9+
name text not null,
10+
bio text
11+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "mysql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/insert_cte_select/postgresql/pgx/v4/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/insert_cte_select/postgresql/pgx/v4/go/models.go

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

internal/endtoend/testdata/insert_cte_select/postgresql/pgx/v4/go/query.sql.go

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

0 commit comments

Comments
 (0)