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

stream_query with parameters #933

Open
sulakm opened this issue Jan 17, 2025 · 2 comments
Open

stream_query with parameters #933

sulakm opened this issue Jan 17, 2025 · 2 comments

Comments

@sulakm
Copy link

sulakm commented Jan 17, 2025

Hello,

Is it possible to simply replace "query" with "stream" in this construct ?

for (auto[...] : tx.query<...>("select ... =$1", params{key}) { ... }

=> for (auto[...] : tx.stream<...>("select ... =$1", params{key}) { ... }

Select with tx.query works fine.
With tx.stream code compiles but I get this exception:
ERROR: bind message supplies 1 parameters, but prepared statement "" requires 0

I took a peek at your source code and both methods internally calls exec(sql,params) with one difference: stream wraps sql query in copy to stdout.
So I tried tx.query("copy (select ... =$1) to stdout", params{key) and this time I got the same exception as with tx.stream.
I quess prepared statements with "copy" are not supported either in libpq or the server.

@jtv
Copy link
Owner

jtv commented Jan 18, 2025

The error message suggests to me that the implementation of parameterised statements in libpq uses a prepared statement. (The empty name has a special meaning — basically that's the single-use prepared statement.)

Streaming a parameterised statement ought to work, from libpqxx's perspective. But apparently the frontend/backend protocol prevents it! I guess I never thought to write a test for this because it's so simple and regular, API-wise, that I didn't think of it as a separate test case.

@jtv
Copy link
Owner

jtv commented Jan 19, 2025

It's a bit painful — you'll have to insert the parameters into the query string yourself. Just always remember to quote and escape any string values using the connection's quote() function.

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