sqlc-dev / sqlc

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

Forcing the parameter type for MySQL #3226

Open Jille opened 8 months ago

Jille commented 8 months ago

What do you want to change?

With postgresql one can write $1:: integer and sqlc will make the generated type an integer. MySQL seems to lack such a type cast/assertion.

Having that would allow people to work around missing/incorrect type inference.

I propose to use sqlc.arg(foo, int) and sqlc.narg(foo, int).

What database engines need to be changed?

MySQL

What programming language backends need to be changed?

No response

simonklee commented 8 months ago

Isn't it possible to use CAST for this?

https://play.sqlc.dev/p/754393cb25dc32e04cb7daf4102621418a45ee448e9d00ba91a6e8fe10afb79d

Jille commented 8 months ago

Maybe? GetAuthorParams.Name is an interface{} in your example, where I expected a string.

simonklee commented 8 months ago

Yes, it didn’t seem to handle CHAR cast, but some of the other types worked.

simonklee commented 8 months ago

:: is simply the postgres specific CAST syntax.

Jille commented 8 months ago

:: is simply the postgres specific CAST syntax.

Yeah, but I had failed to convince MySQL's CAST to do what I wanted. (Maybe because I accidentally tried INTEGER and STRING/VARCHAR rather than trying UNSIGNED and CHAR?)

I would prefer having the syntax to be stripped out by sqlc rather than being sent to the database engine by the way, which is why I like the sqlc.arg(foo, int) approach.

kyleconroy commented 8 months ago

@Jille Our database-agnostic solution for this is https://github.com/sqlc-dev/sqlc/issues/2800. Would that work for you?

seanlaff commented 7 months ago

Related: https://github.com/sqlc-dev/sqlc/issues/3127