sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

sqlite: nullif(?,"") (and other builtin functions) does not generate the correct type

Open avalonbits opened this issue 2 years ago • 0 comments

Version

1.15.0

What happened?

Tried both on v1.15.0 and HEAD (as of August 26 2022).

sqlite schema:

-- name: UpdateChild :one
CREATE TABLE IF NOT EXISTS Child(
    ID INTEGER NOT NULL PRIMARY KEY,
    PublicID TEXT NOT NULL,
    Name TEXT NOT NULL,
    DOB TEXT NOT NULL, 
    Photo BLOB
);

Given the following query:

-- name: UpdateChild :one
UPDATE Child SET
    name  = COALESCE(nullif(?,""), name),
    dob   = COALESCE(nullif(?,""), dob),
    photo = COALESCE(?,photo)
    WHERE
        publicid = ?
    RETURNING *;

sqlc generates:

type UpdateChildParams struct{
  NULLIF   interface{}
  NULLIF_2 interface{}
  Photo    []byte
  Publicid string
}

It should instead generate:

type UpdateChildParams struct{
  Name     interface{}
  Dob      interface{}
  Photo    []byte
  Publicid string
}

Which it does generate if I remove nullif:

-- name: UpdateChild :one
UPDATE Child SET
    name  = COALESCE(?, name),
    dob   = COALESCE(?, dob),
    photo = COALESCE(?,photo)
    WHERE
        publicid = ?
    RETURNING *;

I need the nullif because COALESCE only works with NULL values.

Relevant log output

No response

Database schema

No response

SQL queries

No response

Configuration

No response

Playground URL

https://play.sqlc.dev/p/7208959872393525fe74f3f7250180226cda8da6e1d3002578a017b923ee3a11

What operating system are you using?

Linux

What database engines are you using?

No response

What type of code are you generating?

Go

avalonbits avatar Aug 26 '22 21:08 avalonbits