Skip to content

Commit 805759d

Browse files
authored
fix(engine/mysql): Fix missing binds in ORDER BY (#1273)
1 parent 4663aab commit 805759d

File tree

11 files changed

+332
-9
lines changed

11 files changed

+332
-9
lines changed

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

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

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

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

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

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE authors (
2+
id BIGINT PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
6+
7+
-- name: ListAuthorsColumnSort :many
8+
SELECT * FROM authors
9+
WHERE id > sqlc.arg(min_id)
10+
ORDER BY CASE WHEN sqlc.arg(sort_column) = 'name' THEN name END;
11+
12+
-- name: ListAuthorsNameSort :many
13+
SELECT * FROM authors
14+
WHERE id > sqlc.arg(min_id)
15+
ORDER BY name ASC;
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": "mysql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/order_by_binds/postgresql/go/db.go

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

internal/endtoend/testdata/order_by_binds/postgresql/go/models.go

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

internal/endtoend/testdata/order_by_binds/postgresql/go/query.sql.go

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE authors (
2+
id BIGSERIAL PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
6+
7+
-- name: ListAuthorsColumnSort :many
8+
SELECT * FROM authors
9+
WHERE id > sqlc.arg(min_id)
10+
ORDER BY CASE WHEN sqlc.arg(sort_column) = 'name' THEN name END;
11+
12+
-- name: ListAuthorsNameSort :many
13+
SELECT * FROM authors
14+
WHERE id > sqlc.arg(min_id)
15+
ORDER BY name ASC;
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": "postgresql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/dolphin/convert.go

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,21 @@ func (c *cc) convertSelectField(n *pcast.SelectField) *ast.ResTarget {
452452
}
453453

454454
func (c *cc) convertSelectStmt(n *pcast.SelectStmt) *ast.SelectStmt {
455+
windowClause := &ast.List{Items: make([]ast.Node, 0)}
456+
orderByClause := c.convertOrderByClause(n.OrderBy)
457+
if orderByClause != nil {
458+
windowClause.Items = append(windowClause.Items, orderByClause)
459+
}
460+
455461
op, all := c.convertSetOprType(n.AfterSetOperator)
456462
stmt := &ast.SelectStmt{
457-
TargetList: c.convertFieldList(n.Fields),
458-
FromClause: c.convertTableRefsClause(n.From),
459-
WhereClause: c.convert(n.Where),
460-
WithClause: c.convertWithClause(n.With),
461-
Op: op,
462-
All: all,
463+
TargetList: c.convertFieldList(n.Fields),
464+
FromClause: c.convertTableRefsClause(n.From),
465+
WhereClause: c.convert(n.Where),
466+
WithClause: c.convertWithClause(n.With),
467+
WindowClause: windowClause,
468+
Op: op,
469+
All: all,
463470
}
464471
if n.Limit != nil {
465472
stmt.LimitCount = c.convert(n.Limit.Count)
@@ -656,7 +663,18 @@ func (c *cc) convertByItem(n *pcast.ByItem) ast.Node {
656663
}
657664

658665
func (c *cc) convertCaseExpr(n *pcast.CaseExpr) ast.Node {
659-
return todo(n)
666+
if n == nil {
667+
return nil
668+
}
669+
list := &ast.List{Items: []ast.Node{}}
670+
for _, n := range n.WhenClauses {
671+
list.Items = append(list.Items, c.convertWhenClause(n))
672+
}
673+
return &ast.CaseExpr{
674+
Args: list,
675+
Defresult: c.convert(n.ElseClause),
676+
Location: n.OriginTextPosition(),
677+
}
660678
}
661679

662680
func (c *cc) convertChangeStmt(n *pcast.ChangeStmt) ast.Node {
@@ -924,7 +942,25 @@ func (c *cc) convertOnUpdateOpt(n *pcast.OnUpdateOpt) ast.Node {
924942
}
925943

926944
func (c *cc) convertOrderByClause(n *pcast.OrderByClause) ast.Node {
927-
return todo(n)
945+
if n == nil {
946+
return nil
947+
}
948+
list := &ast.List{Items: []ast.Node{}}
949+
for _, item := range n.Items {
950+
switch item.Expr.(type) {
951+
case *pcast.CaseExpr:
952+
list.Items = append(list.Items, &ast.CaseWhen{
953+
Expr: c.convert(item.Expr),
954+
Location: item.Expr.OriginTextPosition(),
955+
})
956+
case *pcast.ColumnNameExpr:
957+
list.Items = append(list.Items, &ast.CaseExpr{
958+
Xpr: c.convert(item.Expr),
959+
Location: item.Expr.OriginTextPosition(),
960+
})
961+
}
962+
}
963+
return list
928964
}
929965

930966
func (c *cc) convertParenthesesExpr(n *pcast.ParenthesesExpr) ast.Node {
@@ -1249,7 +1285,14 @@ func (c *cc) convertVariableExpr(n *pcast.VariableExpr) ast.Node {
12491285
}
12501286

12511287
func (c *cc) convertWhenClause(n *pcast.WhenClause) ast.Node {
1252-
return todo(n)
1288+
if n == nil {
1289+
return nil
1290+
}
1291+
return &ast.CaseWhen{
1292+
Expr: c.convert(n.Expr),
1293+
Result: c.convert(n.Result),
1294+
Location: n.OriginTextPosition(),
1295+
}
12531296
}
12541297

12551298
func (c *cc) convertWindowFuncExpr(n *pcast.WindowFuncExpr) ast.Node {

0 commit comments

Comments
 (0)