Skip to content

LIMIT parameter type should be sql.NullInt32? #210

Closed

Description

@fwojciec

First of all - thank you for this extremely useful tool!

Sqlc uses int32 type as parameter for LIMIT in queries, for example for this query

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name
LIMIT $1 OFFSET $2;

sqlc will generate this set of params:

type ListAuthorsParams struct {
	Limit  int32
	Offset int32
}

According to Postgres docs it should be also possible to use NULL as the argument to LIMIT to effectively omit the LIMIT clause. This is very useful for queries where LIMIT is optional.

It's possible to work around this by passing some arbitrary large number as the parameter, but that seems like a hack.

Would it be possible to use something like sql.NullInt32 instead of int32 as the parameter for LIMIT so that it's possible to dynamically assign NULL to LIMIT in queries?

Activity

  1. cmoog commented on Dec 22, 2019

    @cmoog
    Contributor

    One possibility solution is to have the Limit be of type *int32. Then the generator would add the logic for injecting "NULL". The aws sdk takes a similar approach for nullable parameters.

  2. EnchanterIO commented on Feb 4, 2021

    @EnchanterIO

    On a similar note. If a table primary key is defined as BIG INT, why the offset and limit generates int32 params @cmoog ? Shouldn't that generate int64?

    Is there a way to specify this in the query annotations or?

    Update: COUNT(*) query generates int64 result:

    const countAll = `-- name: CountAll :one
    SELECT COUNT(*) FROM ...
    `
    
    func (q *Queries) CountAll(ctx context.Context) (int64, error) {
    
  3. EnchanterIO commented on Feb 10, 2021

    @EnchanterIO

    Hi @kyleconroy , do you have any idea how to resolve my above comment and make the sqlc to generate the attributes of type int64 instead of int32 please?

  4. kyleconroy commented on Aug 28, 2021

    @kyleconroy
    Collaborator

    @EnchanterIO You can always cast the parameter to the correct type

    -- name: ListAuthors :many
    SELECT * FROM authors
    ORDER BY name
    LIMIT $1::bigint
    OFFSET $2::bigint;
    
  5. kyleconroy commented on Aug 28, 2021

    @kyleconroy
    Collaborator

    Changing this to default to a NullInt32 is a breaking change I'm not willing to make right now. That said, #1155 will address this for all parameters, so I'm going to close this in favor of that issue.

  6. EnchanterIO commented on Aug 29, 2021

    @EnchanterIO

    Wou I didn't know about this casting $1::bigint THANKS. Will try that tomorrow. That will do for now, I understand breaking changes are only possible in long-term releases. Thanks again @kyleconroy for awesome work on SQLC.

  7. GuillaumeDesforges commented on Nov 27, 2024

    @GuillaumeDesforges

    In combination with #1665, it is effectively not possible to use a nullable parameter in LIMIT:

    • LIMIT sqlc.narg(limit) raises a parse error
    • LIMIT $ makes a non-nullable parameter

    What is the recommended way to go around this?

    EDIT: this works:

    select * from whatever
    limit sqlc.narg('limit')
    offset sqlc.narg('offset')

    It is important to quote in sqlc.narg in order for the parser not to fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions