Skip to content

Fix ORDER BY arg parsing in sqlite #3962

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

andriygm
Copy link

@andriygm andriygm commented May 14, 2025

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 #3788.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang labels May 14, 2025
@andriygm andriygm marked this pull request as draft May 14, 2025 14:46
@andriygm andriygm marked this pull request as ready for review May 14, 2025 15:50
@andriygm
Copy link
Author

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 andriygm changed the title Fix ORDER BY clause in sqlite Fix ORDER BY arg parsing in sqlite May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Args in sqlite ORDER BY not respected
1 participant