Skip to content

fix(engine/sqlite): fixed to be able to find relation from WITH clause #2444

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

Merged
merged 3 commits into from
Jul 24, 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
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/select_cte/sqlite/go/db.go

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

7 changes: 7 additions & 0 deletions internal/endtoend/testdata/select_cte/sqlite/go/models.go

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

40 changes: 40 additions & 0 deletions internal/endtoend/testdata/select_cte/sqlite/go/query.sql.go

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

6 changes: 6 additions & 0 deletions internal/endtoend/testdata/select_cte/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

-- name: ListAuthors :many
WITH abc AS (
SELECT 1 AS n
)
SELECT * FROM abc;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_cte/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "sqlite",
"schema": "query.sql",
"queries": "query.sql",
"name": "querytest"
}
]
}
22 changes: 22 additions & 0 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,25 @@ func (c *cc) convertMultiSelect_stmtContext(n *parser.Select_stmtContext) ast.No
var where ast.Node
var groups = []ast.Node{}
var having ast.Node
var ctes []ast.Node

if ct := n.Common_table_stmt(); ct != nil {
recursive := ct.RECURSIVE_() != nil
for _, cte := range ct.AllCommon_table_expression() {
tableName := identifier(cte.Table_name().GetText())
var cteCols ast.List
for _, col := range cte.AllColumn_name() {
cteCols.Items = append(cteCols.Items, NewIdentifer(col.GetText()))
}
ctes = append(ctes, &ast.CommonTableExpr{
Ctename: &tableName,
Ctequery: c.convert(cte.Select_stmt()),
Location: cte.GetStart().GetStart(),
Cterecursive: recursive,
Ctecolnames: &cteCols,
})
}
}

for _, icore := range n.AllSelect_core() {
core, ok := icore.(*parser.Select_coreContext)
Expand Down Expand Up @@ -377,6 +396,9 @@ func (c *cc) convertMultiSelect_stmtContext(n *parser.Select_stmtContext) ast.No
LimitCount: limitCount,
LimitOffset: limitOffset,
ValuesLists: &ast.List{},
WithClause: &ast.WithClause{
Ctes: &ast.List{Items: ctes},
},
}
}

Expand Down