sqlc-dev / sqlc

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

feat(compiler): Improve type inference for parameters compared to constants only #2742

Open orisano opened 1 year ago

orisano commented 1 year ago

What do you want to change?

We would like to improve the type inference of parameters compared to constants only. Currently, it is absolutely inferred that interface{}.

https://play.sqlc.dev/p/9ba65e1196da80daf0543327f12974275bf7d83b6ae5ce32a0c0f75034d2528d

query:

-- name: Many :many
SELECT id FROM foo WHERE @param = 'foo';

generated:

func (q *Queries) Many(ctx context.Context, param interface{}) ([]int64, error) {

What type of param should be inferred, sql.NullString or string?

What database engines need to be changed?

PostgreSQL, MySQL, SQLite

What programming language backends need to be changed?

Go

kyleconroy commented 1 year ago

In this case I think string would be the correct option, but it's not always going to be the right answer. It depends on a query-by-query basis.

orisano commented 1 year ago

What is the case of a constant-only comparison where the parameter type is sql.NullString? What is the difference between sqlc.narg and sqlc.arg?