Skip to content

fix(engine/mysql): Fix missing binds in ORDER BY #1273

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 1 commit into from
Nov 6, 2021
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
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/order_by_binds/mysql/go/db.go

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/order_by_binds/mysql/go/models.go

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

71 changes: 71 additions & 0 deletions internal/endtoend/testdata/order_by_binds/mysql/go/query.sql.go

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

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/order_by_binds/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE authors (
id BIGINT PRIMARY KEY,
name text NOT NULL,
bio text
);

-- name: ListAuthorsColumnSort :many
SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
ORDER BY CASE WHEN sqlc.arg(sort_column) = 'name' THEN name END;

-- name: ListAuthorsNameSort :many
SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
ORDER BY name ASC;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/order_by_binds/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "mysql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/order_by_binds/postgresql/go/db.go

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/order_by_binds/postgresql/go/models.go

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.

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/order_by_binds/postgresql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

-- name: ListAuthorsColumnSort :many
SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
ORDER BY CASE WHEN sqlc.arg(sort_column) = 'name' THEN name END;

-- name: ListAuthorsNameSort :many
SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
ORDER BY name ASC;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/order_by_binds/postgresql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
61 changes: 52 additions & 9 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,21 @@ func (c *cc) convertSelectField(n *pcast.SelectField) *ast.ResTarget {
}

func (c *cc) convertSelectStmt(n *pcast.SelectStmt) *ast.SelectStmt {
windowClause := &ast.List{Items: make([]ast.Node, 0)}
orderByClause := c.convertOrderByClause(n.OrderBy)
if orderByClause != nil {
windowClause.Items = append(windowClause.Items, orderByClause)
}

op, all := c.convertSetOprType(n.AfterSetOperator)
stmt := &ast.SelectStmt{
TargetList: c.convertFieldList(n.Fields),
FromClause: c.convertTableRefsClause(n.From),
WhereClause: c.convert(n.Where),
WithClause: c.convertWithClause(n.With),
Op: op,
All: all,
TargetList: c.convertFieldList(n.Fields),
FromClause: c.convertTableRefsClause(n.From),
WhereClause: c.convert(n.Where),
WithClause: c.convertWithClause(n.With),
WindowClause: windowClause,
Op: op,
All: all,
}
if n.Limit != nil {
stmt.LimitCount = c.convert(n.Limit.Count)
Expand Down Expand Up @@ -656,7 +663,18 @@ func (c *cc) convertByItem(n *pcast.ByItem) ast.Node {
}

func (c *cc) convertCaseExpr(n *pcast.CaseExpr) ast.Node {
return todo(n)
if n == nil {
return nil
}
list := &ast.List{Items: []ast.Node{}}
for _, n := range n.WhenClauses {
list.Items = append(list.Items, c.convertWhenClause(n))
}
return &ast.CaseExpr{
Args: list,
Defresult: c.convert(n.ElseClause),
Location: n.OriginTextPosition(),
}
}

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

func (c *cc) convertOrderByClause(n *pcast.OrderByClause) ast.Node {
return todo(n)
if n == nil {
return nil
}
list := &ast.List{Items: []ast.Node{}}
for _, item := range n.Items {
switch item.Expr.(type) {
case *pcast.CaseExpr:
list.Items = append(list.Items, &ast.CaseWhen{
Expr: c.convert(item.Expr),
Location: item.Expr.OriginTextPosition(),
})
case *pcast.ColumnNameExpr:
list.Items = append(list.Items, &ast.CaseExpr{
Xpr: c.convert(item.Expr),
Location: item.Expr.OriginTextPosition(),
})
}
}
return list
}

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

func (c *cc) convertWhenClause(n *pcast.WhenClause) ast.Node {
return todo(n)
if n == nil {
return nil
}
return &ast.CaseWhen{
Expr: c.convert(n.Expr),
Result: c.convert(n.Result),
Location: n.OriginTextPosition(),
}
}

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