sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Fix `ORDER BY` arg parsing in sqlite

Open andriygm opened this issue 7 months ago • 2 comments

sqlc currently doesn't handle bound ORDER BY clauses in sqlite, nor does it have tests for this behavior. This PR attempts for rectify this. Closes https://github.com/sqlc-dev/sqlc/issues/3788.

andriygm avatar May 14 '25 14:05 andriygm

currently, base behavior works but mixing params like so:

-- name: ListAuthorsColumnSortDirection :many
SELECT * FROM authors
WHERE id > ?
ORDER BY
    CASE
        WHEN @order_by = 'asc' THEN name
    END ASC,
    CASE
        WHEN @order_by = 'desc' OR @order_by IS NULL THEN name
    END DESC;

produces this incorrect output (question marks in CASE clauses should instead be same variable)

-- name: ListAuthorsColumnSortDirection :many
SELECT id, name, bio FROM authors
WHERE id > ?
ORDER BY
    CASE
        WHEN ? = 'asc' THEN name
    END ASC,
    CASE
        WHEN ? = 'desc' OR ? IS NULL THEN name
    END DESC

this can still be worked around by using named params in this case.

andriygm avatar May 15 '25 20:05 andriygm

How can we get this PR merged? I'm currently running into this issue :(

ilikeorangutans avatar Oct 16 '25 21:10 ilikeorangutans