sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Field name and Ordering type (ASC/DESC) as arguments

Open mdanialr opened this issue 3 years ago • 2 comments

What do you want to change?

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY $1 $2;
  • Example 1:
SELECT * FROM authors
ORDER BY updated_at ASC;
  • Example 2:
SELECT * FROM authors
ORDER BY created_at DESC;

It seems currently this kind of syntax is not supported yet. I just wanna accommodate many cases such as Example 1 and Example 2 with only one function, so that's why I need the field name and the ordering value (ASC/DESC) as arguments not hard coded in the sql.

What database engines need to be changed?

PostgreSQL

What programming language backends need to be changed?

Go

mdanialr avatar May 30 '22 23:05 mdanialr

Furthermore, I think there should be first class support for NULLS FIRST or NULLS LAST for the exact same reasons as ASC and DESC

kennedyjustin avatar Jun 12 '22 20:06 kennedyjustin

Right now this is possible with some modified version of https://github.com/kyleconroy/sqlc/issues/830#issuecomment-827489986, but it gets super ugly super fast.

kennedyjustin avatar Jun 12 '22 20:06 kennedyjustin

@mdanialr @kennedyjustin : do you guys have any other solution for this?

Using this solution https://github.com/sqlc-dev/sqlc/issues/830#issuecomment-827489986, I got error ERROR: CASE types text and timestamp with time zone cannot be matched (SQLSTATE 42804) or ERROR: CASE types text and integer cannot be matched (SQLSTATE 42804) because my columns are not the same type

piavgh avatar Nov 19 '23 04:11 piavgh