Skip to content

Commit 8a15b9f

Browse files
authored
fix(engine/sqlite): fixed to be able to find relation from WITH clause (#2444)
* fix(engine/sqlite): fixed to be able to find relation from WITH clause fix #2136 * test: add endtoend * chore: v1.19.1
1 parent ebe15a7 commit 8a15b9f

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

internal/endtoend/testdata/select_cte/sqlite/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/select_cte/sqlite/go/models.go

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

internal/endtoend/testdata/select_cte/sqlite/go/query.sql.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
-- name: ListAuthors :many
3+
WITH abc AS (
4+
SELECT 1 AS n
5+
)
6+
SELECT * FROM abc;
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+
"path": "go",
6+
"engine": "sqlite",
7+
"schema": "query.sql",
8+
"queries": "query.sql",
9+
"name": "querytest"
10+
}
11+
]
12+
}

internal/engine/sqlite/convert.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,25 @@ func (c *cc) convertMultiSelect_stmtContext(n *parser.Select_stmtContext) ast.No
331331
var where ast.Node
332332
var groups = []ast.Node{}
333333
var having ast.Node
334+
var ctes []ast.Node
335+
336+
if ct := n.Common_table_stmt(); ct != nil {
337+
recursive := ct.RECURSIVE_() != nil
338+
for _, cte := range ct.AllCommon_table_expression() {
339+
tableName := identifier(cte.Table_name().GetText())
340+
var cteCols ast.List
341+
for _, col := range cte.AllColumn_name() {
342+
cteCols.Items = append(cteCols.Items, NewIdentifer(col.GetText()))
343+
}
344+
ctes = append(ctes, &ast.CommonTableExpr{
345+
Ctename: &tableName,
346+
Ctequery: c.convert(cte.Select_stmt()),
347+
Location: cte.GetStart().GetStart(),
348+
Cterecursive: recursive,
349+
Ctecolnames: &cteCols,
350+
})
351+
}
352+
}
334353

335354
for _, icore := range n.AllSelect_core() {
336355
core, ok := icore.(*parser.Select_coreContext)
@@ -377,6 +396,9 @@ func (c *cc) convertMultiSelect_stmtContext(n *parser.Select_stmtContext) ast.No
377396
LimitCount: limitCount,
378397
LimitOffset: limitOffset,
379398
ValuesLists: &ast.List{},
399+
WithClause: &ast.WithClause{
400+
Ctes: &ast.List{Items: ctes},
401+
},
380402
}
381403
}
382404

0 commit comments

Comments
 (0)