Skip to content

MySQL queries that use the same parameter in two places #777

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

Closed
alecbz opened this issue Nov 10, 2020 · 2 comments
Closed

MySQL queries that use the same parameter in two places #777

alecbz opened this issue Nov 10, 2020 · 2 comments
Labels
📚 mysql bug Something isn't working

Comments

@alecbz
Copy link
Contributor

alecbz commented Nov 10, 2020

A MySQL query like:

SELECT * FROM things WHERE foo = sqlc.arg(foo) OR old_foo = sqlc.arg(foo)

gets compiled into:

const query = `SELECT * FROM things WHERE foo = ? OR old_foo = ?`

func (q *Queries) GetThingByFoo(ctx context.Context, foo string) (Thing, error) {
  row := q.db.QueryRowContext(ctx, query, foo)
  ...
}

which causes MySQL to complain because only one argument is supplied when it was expecting two.


It seems like we should at least error out during code generation in this case, but even better would be to do:

row := q.db.QueryRowContext(ctx, query, foo, foo)
@kyleconroy kyleconroy added bug Something isn't working 📚 mysql labels Nov 17, 2020
@kyleconroy kyleconroy added this to the v2.0.0 milestone Nov 17, 2020
@kyleconroy kyleconroy modified the milestones: v1.6.0, v1.7.0 Nov 23, 2020
@mightyguava
Copy link
Contributor

I remember explicitly solving for something very similar in the Postgres Kotlin codegen when numbered parameters are used. It was, 0f5afd3, done by tracking paramRefs by order of occurrence (allowing duplicates) instead of keeping them as a sorted set. This code still seems to work in Dolphin! Maybe something similar can be done to solve the problem with numbered parameters?

@kyleconroy kyleconroy removed this from the v1.7.0 milestone Feb 28, 2021
@kyleconroy
Copy link
Collaborator

Fixed in #1296

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 mysql bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants