Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add support for bind params#42

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
blakeembrey wants to merge1 commit intomain
base:main
Choose a base branch
Loading
frombe/bind-param
Open

Conversation

@blakeembrey
Copy link
Owner

Follow up from#41 (comment), prototyping a backward compatible way to add bind parameters.

TL;DR addsBIND_PARAM symbol, when seen it increments a counter, counter is used in a new.bind method to output a values list.

@blakeembrey
Copy link
OwnerAuthor

@PhilippSalvisberg here's a simple prototype that's backward compatible.

* A SQL instance can be nested within each other to build SQL strings.
*/
exportclassSql{
readonlybindParams=0;
Copy link
OwnerAuthor

Choose a reason for hiding this comment

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

The only question to myself is whethervalues should now become a getter that throws whenbindParams > 0? That would ensure the instance isn't incorrectly used without binding params.

returnvalue;
}

bind(...params:Value[]){
Copy link
OwnerAuthor

Choose a reason for hiding this comment

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

Doesn't technically need this method if you know what you're doing, but the array length safety and merging with any non-params seems like a good pattern to enforce.

/**
* A param that's expected to be bound later of included in `values`.
*/
exportconstBIND_PARAM=Symbol("BIND_PARAM");
Copy link
OwnerAuthor

Choose a reason for hiding this comment

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

Name options:

  • BIND_PARAMETER
  • BIND_VALUE
  • BIND
  • BIND_KEY
  • PARAM

@PhilippSalvisberg
Copy link
Contributor

Thanks for the follow-up.

AFAIU the cases I mentioned in#41 (comment) (bind definitions e.g. for output parameters andexecuteMany) are not simplified by this change.

I can see that this change simplifies some code for a series of similar SQL statements (bind/execute in a loop). However, usingexecuteMany in OracleDB or a single SQL statement would probably be more efficient.

Maybe I didn't understand the intention and the use case.

@blakeembrey
Copy link
OwnerAuthor

However, using executeMany in OracleDB or a single SQL statement would probably be more efficient.

Good question. I was trying to make it easier to useexecuteMany. You'd be able to do something like:

constquery=sql`UPDATE x SET y =${BIND_PARAM} WHERE z =${BIND_PARAM}`;constresult=db.executeMany(query.statement,[[1,1],[2,2],[3,4]]);

However, if you wanted better safety for the params:

constresult=db.executeMany(query.statement,[query.bind(1,1),query.bind(2,2),query.bind(3,3)]);

@blakeembrey
Copy link
OwnerAuthor

blakeembrey commentedApr 22, 2024
edited
Loading

You are right that it's a very marginal improvement, and you could just use1 or something and ignorevalues anyway and it'd work the same as the first example. The only difference would be if you happened to have something like this mixing positions you want to bind later and bind now:

constquery=sql`UPDATE x SET y =${true} WHERE z =${BIND_PARAM}`;constresult=db.executeMany(query.statement,[query.bind(1),query.bind(2),query.bind(3)]);// Would be `[[true, 1], [true, 2], [true, 3]]`.

@PhilippSalvisberg
Copy link
Contributor

(...)

const query= sql`UPDATE x SET y = ${true} WHERE z = ${BIND_PARAM}`;const result=db.executeMany(query.statement, [query.bind(1),query.bind(2),query.bind(3)]);// Would be`[[true, 1], [true, 2], [true, 3]]`.

Excellent example. Thanks for the explanation. I agree. This new feature can simplify the code, especially with many "fixed" bind values and a few delayed bindings.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@blakeembrey@PhilippSalvisberg

[8]ページ先頭

©2009-2025 Movatter.jp