Skip to content

Bind query Order By with params? #830

Closed as not planned
Closed as not planned
@grippy

Description

@grippy

Maybe this exists and I couldn't find anything in the docs: Is it possible to control the sorting on a :many query using the input params?

When I run the following query, I don't see the ability to pass OrderBy in the getStuffParams:

const getStuff = `-- name: GetStuff :many
SELECT *
FROM stuff
ORDER BY $3
OFFSET $1 LIMIT $2
`
type getStuffParams struct {
	Offset int32 `json:"offset"`
	Limit  int32 `json:"limit"`
}

Since the order by value is a $variable it we be nice if getStruffParams would generate something like this, instead:

type OrderBy struct {
     col string
     order string
}

type getStuffParams struct {
	Offset int32 `json:"offset"`
	Limit  int32 `json:"limit"`
        OrderBy []OrderBy `json:"order_by"`
}

In the case where order by is hard-coded in the query, then there wouldn't be a need to add "OrderBy" to the params.
It should be possible to mix both: order by x desc, $1 or order by $1, x desc.

This pattern would also enable dynamically generating HAVING and GROUP BY clauses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions