Skip to content

Commit

Permalink
fix variables not resolving in ORDER BY statements
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlaff committed Jan 9, 2024
1 parent c9d826b commit 4fb8011
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
28 changes: 28 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.

4 changes: 4 additions & 0 deletions internal/endtoend/testdata/order_by_binds/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
ORDER BY CASE WHEN sqlc.arg(sort_column) = 'name' THEN name END;

-- name: ListAuthorsColumnSortFnWtihArg :many
SELECT * FROM authors
ORDER BY MOD(id, sqlc.arg(mod_arg));

-- name: ListAuthorsNameSort :many
SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
ORDER BY CASE WHEN sqlc.arg(sort_column) = 'name' THEN name END;

-- name: ListAuthorsColumnSortFnWtihArg :many
SELECT * FROM authors
ORDER BY MOD(id, $1);

-- name: ListAuthorsNameSort :many
SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
Expand Down
13 changes: 1 addition & 12 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,18 +1025,7 @@ func (c *cc) convertOrderByClause(n *pcast.OrderByClause) ast.Node {
}
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(),
})
}
list.Items = append(list.Items, c.convert(item.Expr))
}
return list
}
Expand Down

0 comments on commit 4fb8011

Please sign in to comment.