-
Notifications
You must be signed in to change notification settings - Fork 47
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
Implement named args and binding #104
Conversation
Would it be possible for |
I did a first test with this design and it is very nice and clean, and this way I can use it with |
It is easy to create a However, it would have to use its own entry function, e.g. |
The usecase is that I have some dynamic queries that can have more or less parameters depending on filters, otherwise I would need to create a struct with all possible parameters beforehand. |
Thank you. I'm looking forward to the in-depth test to help iron out any issues with the design. |
But how would this work since the actual query will expect the same number of |
For these queries I don't use if filter.Email != nil {
query.Apply(
sm.Where(`email = ?`, bob.Named("email")),
)
params["email"] = *filter.Email
} |
If you're rebuilding the query every time, isn't it just easier to do this? if filter.Email != nil {
query.Apply(
sm.Where(psql.Quote("email").EQ(*filter.Email)),
)
} This way you wouldn't even need to rebind the parameters. |
Will this generate an SQL argument or the text will be hardcoded on the SQL? I tried to test it, but it seems that calling
|
My bad 🤦🏾 . It should be like this to make it an arg. (the old code wouldn't even compile). if filter.Email != nil {
query.Apply(
sm.Where(psql.Quote("email").EQ(psql.Arg(*filter.Email))),
)
}
It shouldn't. Any unnamed arg will be "saved" and returned in the final list after binding. |
Seems to be not working like this: query := psql.Select(
sm.Columns("id", "name"),
sm.From("users"),
sm.Where(psql.Raw("id >= ?", bob.Named("id1"))),
sm.Where(psql.Raw("id <= ?", 12)),
)
type paramst struct {
Id1 int
}
queryBound, err := bob.BindNamed[paramst](query)
if err != nil {
panic(err)
}
queryStr, args, err := queryBound.Bind(paramst{
Id1: 400,
}).Build()
if err != nil {
panic(err)
}
|
Deploying with Cloudflare Pages
|
Try now. |
This seems fixed now, will resume my tests. |
I'm finding that not having map parameters is somewhat limiting for the majority of queries, for example, query := psql.Select(
sm.Columns(getDefaultTenantColumns()...),
sm.From("tenant"),
sm.Where(psql.Raw(`tenant_id = ?`, bob.Named("tenant_id"))),
sm.Where(psql.Raw(`deleted_at IS NULL`)),
) In this case I would need to create a top level struct (as it needs to be declared on the type of I don't thing arguments are reusable enough to require they always be a top-level declared struct, arguments vary a lot between queries. |
@RangelReale after more than a year, I have returned to working on this 😅 See details in #298 |
Here is another attempt at binding named args.
Using it will look something like this:
@RangelReale let me know your thougts on this design.
bob.InTx
method can be used to get a transaction specific version.