New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configure positional parameter logic #1498
Comments
I am very new to sqlc, given I put some research in it I would like to pick this. |
Just to chime in - I really like the positional argument for only 1 parameter - I would even like positional arguments for more parameters (maybe up to 3?), but only if their types are distinct. That's what I would like out of a Go generator. When there are more than 3 parameters, then a struct is more ergonomic anyway. But when there are <= 3 parameters, of all distinct types, the compiler will protect you from transposing them, and it should be self-documenting enough. Crazy Idea _, _ := queries.CreateAuthor(ctx,
db.Name("Stephen King"),
db.Bio(sql.NullString{Valid: true, String: "wrote books"}))
// or maybe by using some better naming for the package names
_, _ := queries.CreateAuthor(ctx,
params.Name("Stephen King"),
params.Bio(sql.NullString{Valid: true, String: "wrote books"})) This has a few side effects:
Would anyone be interested in something like that? And here is a little template for what I think would be required to do that. // we start with private structs
type createAuthorParams struct {
name string
bio sql.NullString
}
func (c *createAuthorParams) setName(name string) {
c.name = name
}
func (c *createAuthorParams) setBio(bio sql.NullString) {
c.bio = bio
}
type nameParam = func (nameSetter)
type nameSetter interface {
setName(string)
}
type bioParam = func (bioSetter)
type bioSetter interface {
setBio(sql.NullString)
}
func Bio(b sql.NullString) bioParam {
return func (s bioSetter) {
s.setBio(b)
}
}
func Name(name string) nameParam {
return func (s nameSetter) {
s.setName(name)
}
}
func (q *Queries) CreateAuthor(ctx context.Context, name nameParam, bio bioParam) (sql.Result, error) {
var p createAuthorParams
name(&p)
bio(&p)
return q.db.ExecContext(ctx, createAuthor, p.name, p.bio)
} |
I think having the option be configurable would be a great advantage. Reason being - sometimes we want to decode JSON directly into the struct emitted by sqlc, and in that case having the option to set it to 0 would be quite helpful. |
Hi @kyleconroy - Is this still open or has it been resolved? Happy to pick this up and code this as work has given me a bit of time to do open-source work. |
Since the time I wrote the proposal, I think I learned a lot more about my own use cases - and also the thing others are struggling with - with regard to sqlc. I (personally) think that "sqlc core engine" (parser, "typechecking", etc) could use some love before trying to double-down on anything code-gen related. I think I'll "retract" my proposal, so I can help out on those areas first. I'd really want to avoid 'death by 1000 paper cuts', and I don't think my proposal is really limiting anyone from using sqlc - it would just be a nice to have. |
Hey @kyleconroy - any update on this? |
@farazfazli the first proposal is already merged |
@go-mez That's awesome! Good job. |
|
What do you want to change?
For Go, sqlc will use a positional argument for a query if there's a single parameter. If there's more than one, it will generate a parameters struct. While this works for me, a few other folks would like different behavior.
@brentd would like to use positional arguments for up to three parameters (#1496)
@Threpio would like to always use parameter structs (#1373)
How should we do it?
We can solve this using a single configuration parameter called
query_parameter_limit
, which defaults to 1 (the current behavior). We can set the value to a pointer in the configuration YAML / JSON so that zero is a valid option. Anything less than zero should be an error.What programming language backends need to be changed?
Go
The text was updated successfully, but these errors were encountered: