Skip to content
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

Add support for named parameters #227

Merged
merged 4 commits into from
Jun 6, 2024

Conversation

ajzo90
Copy link
Contributor

@ajzo90 ajzo90 commented Jun 5, 2024

This PR adds support for named parameters.
It tries to keeps old behaviour intact, but introduces some vague interpretation since it can mix named and non-named parameters. Possibly a better alternative is to check whether named or positional parameters are used and do strict validation.

stmt, _ := db.Prepare("SELECT $foo, $bar")
_ = stmt.QueryRow(sql.Named("foo", 1), sql.Named("bar", "baz"))`

Copy link
Owner

@marcboeker marcboeker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajzo90 Thanks for this PR. Please see my comments.

Comment on lines +65 to +77
// override with ordinal if set
for _, v := range args {
if v.Ordinal == i+1 {
arg = v
}
}

// override with name if set
for _, v := range args {
if v.Name == paramName {
arg = v
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we merge these two loops?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in 3 passes to set different priorities. For example in case the client code or a wrapper library is setting/generating NamedValues with both a Name and an Ordinal as a fallback mechanism.

statement.go Outdated Show resolved Hide resolved
statement_test.go Outdated Show resolved Hide resolved
@marcboeker marcboeker merged commit 0d97dd3 into marcboeker:main Jun 6, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants