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

preparedstatements? #164

Open
rdp opened this issue Apr 5, 2022 · 8 comments
Open

preparedstatements? #164

rdp opened this issue Apr 5, 2022 · 8 comments

Comments

@rdp
Copy link

rdp commented Apr 5, 2022

Do they exist? If not, they are convenient at times, thanks!

@straight-shoota
Copy link
Member

crystal-db implicitly uses prepared statements by default, unless configured otherwise.

@rdp
Copy link
Author

rdp commented Apr 6, 2022 via email

@straight-shoota
Copy link
Member

By default, yes (and if the driver supports it, but AFAIK all existing drivers do). You can disable prepared statements in the connection config via prepared_statements=false. Or you can explicitly run unprepared statements.
More details are available in #25, but we're really lacking documentation on this.

@rdp
Copy link
Author

rdp commented Apr 6, 2022

Yeah that wasn't super clear...I may do a PR for it.

Having "prepared statement" objects might be nice for named parameters, setting parameters separately, etc. but query parameters will work for now for me, thanks! Prepared statements might help with speed on occasion too I'm not sure.

In more general feedback, it's also a bit odd that one driver has a different syntax for query parameters (i.e. prepared statements) than others, as well, though I guess that might be OK for now since few use different DB's. Cheers!

@straight-shoota
Copy link
Member

it's also a bit odd that one driver has a different syntax for query parameters (i.e. prepared statements) than others, as well, though I guess that might be OK for now since few use different DB's.

Well, that syntax directly depends on the database. The drivers don't interpreter query parameters, they just pass the raw query to the server, thus it must use the specific parameter syntax of the respective database.

@straight-shoota
Copy link
Member

Having "prepared statement" objects might be nice for named parameters, setting parameters separately

You can use Database#build or Connection#build to create a statement instance.

Prepared statements might help with speed on occasion too I'm not sure.

Definitely! Prepared statements are cached, so the server doesn't need to re-interpret the same query again.

@rdp
Copy link
Author

rdp commented Apr 11, 2022

For followers, it's like this in Postgres:

b = db.build("select $1::int")
b.query("2") # no query_one method? Error: undefined method 'query_one' for PG::Statement (compile-time type is DB::Statement+)

Thanks.

beta-ziliani pushed a commit to crystal-lang/crystal-book that referenced this issue May 26, 2022
Add example of query parameters in a query, mention there are more helper methods available for query, mention preparedstatements are at play (crystal-lang/crystal-db#164).
@rdp
Copy link
Author

rdp commented Oct 20, 2022

Might be nice if prepared statements could be created in "block form" for auto-closing but maybe since they're manual that's not needed?

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

No branches or pull requests

2 participants