sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.58k stars 812 forks source link

[MySQL] sqlc.arg() does not work correctly in an `AND NOT (...)` clause. #3608

Open tomtwinkle opened 2 months ago

tomtwinkle commented 2 months ago

Version

1.27.0

What happened?

The generated code is not what I expected. Originally, the part specified by sqlc.arg() was ? but it is output as it is.

Expected Result

const listTests = `-- name: ListTests :many
SELECT id, created_at FROM tests
WHERE created_at >= ?
AND NOT (created_at = ? AND id <= ?)
ORDER BY created_at ASC
LIMIT ?
`

type ListTestsParams struct {
    ID []byte
    CreatedAt time.Time
    Limit     int32
}

Actual Result

const listTests = `-- name: ListTests :many
SELECT id, created_at FROM tests
WHERE created_at >= ?
AND NOT (created_at = sqlc.arg(created_at) AND id <= sqlc.arg(id))
ORDER BY created_at ASC
LIMIT ?
`

type ListTestsParams struct {
    CreatedAt time.Time
    Limit     int32
}

Relevant log output

No response

Database schema

CREATE TABLE `tests` (
  `id` binary(16) NOT NULL,
  `created_at` datetime(6) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `index_tests_1` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

SQL queries

-- name: ListTests :many
SELECT * FROM tests
WHERE created_at >= sqlc.arg(created_at)
AND NOT (created_at = sqlc.arg(created_at) AND id <= sqlc.arg(id))
ORDER BY created_at ASC
LIMIT ?;

Configuration

version: "2"
sql:
  - engine: "mysql"
    queries: "query.sql"
    schema: "schema.sql"
    gen:
      go:
        package: "sqlc"
        out: "sqlc"
        emit_prepared_queries: true
        emit_empty_slices: true
        emit_json_tags: false
        json_tags_id_uppercase: true
        emit_result_struct_pointers: true
        emit_methods_with_db_argument: false

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go