Skip to content

Commit dc311e7

Browse files
authored
fix(mysql): Variables not resolving in ORDER BY statements (#3115)
1 parent d812709 commit dc311e7

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

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

Lines changed: 28 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/query.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ SELECT * FROM authors
33
WHERE id > sqlc.arg(min_id)
44
ORDER BY CASE WHEN sqlc.arg(sort_column) = 'name' THEN name END;
55

6+
-- name: ListAuthorsColumnSortFnWtihArg :many
7+
SELECT * FROM authors
8+
ORDER BY MOD(id, sqlc.arg(mod_arg));
9+
610
-- name: ListAuthorsNameSort :many
711
SELECT * FROM authors
812
WHERE id > sqlc.arg(min_id)

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

Lines changed: 28 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/query.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ SELECT * FROM authors
33
WHERE id > sqlc.arg(min_id)
44
ORDER BY CASE WHEN sqlc.arg(sort_column) = 'name' THEN name END;
55

6+
-- name: ListAuthorsColumnSortFnWtihArg :many
7+
SELECT * FROM authors
8+
ORDER BY MOD(id, $1);
9+
610
-- name: ListAuthorsNameSort :many
711
SELECT * FROM authors
812
WHERE id > sqlc.arg(min_id)

internal/engine/dolphin/convert.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,18 +1025,7 @@ func (c *cc) convertOrderByClause(n *pcast.OrderByClause) ast.Node {
10251025
}
10261026
list := &ast.List{Items: []ast.Node{}}
10271027
for _, item := range n.Items {
1028-
switch item.Expr.(type) {
1029-
case *pcast.CaseExpr:
1030-
list.Items = append(list.Items, &ast.CaseWhen{
1031-
Expr: c.convert(item.Expr),
1032-
Location: item.Expr.OriginTextPosition(),
1033-
})
1034-
case *pcast.ColumnNameExpr:
1035-
list.Items = append(list.Items, &ast.CaseExpr{
1036-
Xpr: c.convert(item.Expr),
1037-
Location: item.Expr.OriginTextPosition(),
1038-
})
1039-
}
1028+
list.Items = append(list.Items, c.convert(item.Expr))
10401029
}
10411030
return list
10421031
}

0 commit comments

Comments
 (0)