sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

sqlc.Narg does not work with enums

Open Emyrk opened this issue 2 years ago • 8 comments

Version

1.14.0

What happened?

sqlc.narg is a great addition. Unfortunately, it does not seem to work for enum types. I would really like to be able to use nullable enums, as empty strings are invalid enums and throw an error.

In the example provided, it would be great if the method definition used *Colors to make it nullable:

func (q *Queries) GetBoxes(ctx context.Context, color *Colors) ([]Box, error) {

The actual method does not accept a nullable argument.

func (q *Queries) GetBoxes(ctx context.Context, color Colors) ([]Box, error) {

Relevant log output

No response

Database schema

CREATE TYPE colors AS ENUM (
    'red',
    'blue',
    'green'
);

-- Example queries for sqlc
CREATE TABLE boxs (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  color colors
);

SQL queries

-- name: GetBoxes :many
SELECT * FROM boxs
WHERE 
CASE
	WHEN sqlc.narg('color') :: colors != null THEN
		color = sqlc.narg('color')
	ELSE true
END;

Configuration

No response

Playground URL

https://play.sqlc.dev/p/b269211b4c484293e3e7f8b370d501075e0a355375be9abe1dfb344915601285

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Emyrk avatar Jun 16 '22 00:06 Emyrk

The thing about this behavior you encountered is that there is no way to handle null values for the Colors enum. There is sql.NullString, sql.NullTime, sql.NullBool, but there isn't a NullColors type that can handle null values. Later, I will link a repo showing how to use enums that can be nullable.

eullerpereira94 avatar Jun 24 '22 15:06 eullerpereira94

Would like to see that example!

Emyrk avatar Jun 24 '22 15:06 Emyrk

This repository shows to how to do it: https://github.com/eullerpereira94/sqlc_enum_test

Notice that you have to manually create a type to handle your enums and a second type that implements both sql.Scanner and driver.Valuer to handle your enums when they can be nullable.

Another thing that happens is that sqlc generates a type for your enum regardless of any type that you might provide to it. Maybe I can work a PR so that sqlc might generate the nullable type on its own.

eullerpereira94 avatar Jun 24 '22 16:06 eullerpereira94

@eullerpereira94 Thanks for the example. I will take a look and replicate in my repo if it's better than my current method.

It would be great to autogen it as all enums should be primitive types under the hood!

Emyrk avatar Jun 24 '22 23:06 Emyrk

There are two open MRs for this, see https://github.com/kyleconroy/sqlc/pull/1582 and https://github.com/kyleconroy/sqlc/pull/1485

danielbprice avatar Jun 28 '22 21:06 danielbprice

@danielbprice well, I did'nt know. I saw the problem and tried to fix it. The other two proposals seem to have more work done than mine. And seeing that they have proper functioning code already, I will just wait for when a new version with this feature is released.

eullerpereira94 avatar Jun 28 '22 22:06 eullerpereira94

I'm waiting to get the PR merged as well, I'm starting to get worried it won't get merged anytime soon

sbres avatar Jul 05 '22 09:07 sbres

Never mind it just got merged 🎉

sbres avatar Jul 05 '22 10:07 sbres

Confirmed that this now works :)

https://play.sqlc.dev/p/0ac3182108229aa90ff756658d45e2fc43c865517fa63f42c4339cb2b462a11e

kyleconroy avatar Aug 29 '22 03:08 kyleconroy