sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Optional WHERE parameters via $1 IS NULL OR - redeclared in this block - query_parameter_limit

Open sergeishilkocardmarket opened this issue 1 month ago • 2 comments

Version

1.30.0

What happened?

Fail to generate go gode when using named argument >1 for optional SELECT WHERE query case see https://github.com/sqlc-dev/sqlc/issues/200

suggested solution does not work in 1.30.0 in golang

Image

would generate go compile error name redeclared in this block

Relevant log output

Go auto-generated code

// ListUsers
//
//	SELECT name
//	FROM users
//	WHERE
//	 (name = ? OR ? IS NULL)
func (q *Queries) ListUsers(ctx context.Context, name *string, name *string) ([]string, error) {

go build

# query_parameter_limit = 4 (important) will generate  (ISSUE) duplicate func args
# query_parameter_limit = 1 will generate struct, no duplicate  (NO ISSUE)

sqlc/maindb/users.sql.go:25:64: name redeclared in this block
sqlc/maindb/users.sql.go:25:50: other declaration of name

Database schema


CREATE TABLE users (name TEXT NOT NULL);

SQL queries

-- name: ListUsers :many
SELECT *
FROM users
WHERE
 (name = sqlc.narg('name') OR sqlc.narg('name') IS NULL)



Configuration

version: "2"
sql:
  - engine: "mysql"
    queries: "./db/maindb/queries/"
    schema: "./db/maindb/migrations/"
    gen:
      go:
        package: "maindb"
        out: "internal/infra/sqlc/maindb"
        sql_package: "database/sql"
        sql_driver: "github.com/go-sql-driver/mysql"
        emit_interface: true
        emit_json_tags: true
        json_tags_case_style: "camel"
        emit_prepared_queries: true
        emit_exact_table_names: true
        emit_empty_slices: true
        emit_result_struct_pointers: true
        emit_params_struct_pointers: true
        query_parameter_limit: 4
        emit_db_tags: true
        emit_enum_valid_method: true
        emit_all_enum_values: true
        emit_sql_as_comment: true

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

sergeishilkocardmarket avatar Nov 21 '25 10:11 sergeishilkocardmarket

changing query_parameter_limit: 1 emits a proper struct, but when using parameters there is an issue.

Is there chance query_parameter_limit can be overriden on per-query basis?

sergeishilkocardmarket avatar Nov 21 '25 10:11 sergeishilkocardmarket

As a solution to this, ideally an option to override query_parameter_limit per query, or option to always use struct argument per query

-- name: ListUsers :many
-- query_parameter_limit: 0
-- argsType: struct
SELECT *
FROM users
WHERE
 (name = sqlc.narg('name') OR sqlc.narg('name') IS NULL)

sergeishilkocardmarket avatar Nov 21 '25 12:11 sergeishilkocardmarket