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

Support typed prepared statements #1121

Open
mvanderlee opened this issue Feb 21, 2024 · 0 comments
Open

Support typed prepared statements #1121

mvanderlee opened this issue Feb 21, 2024 · 0 comments

Comments

@mvanderlee
Copy link

While datatypes are optional in prepared statements, not using them can results in type errors such as

function sum(text) does not exist

This is easily reproducible in any PSQL client

PREPARE my_query AS
SELECT SUM(CASE WHEN v = 'A' THEN $1 ELSE $2 END)
FROM (VALUES ('A'), ('B')) as q0(v);
-- ERROR: function sum(text) does not exist

But when I specify the types, it works

PREPARE my_query(int, int) AS
SELECT SUM(CASE WHEN v = 'A' THEN $1 ELSE $2 END)
FROM (VALUES ('A'), ('B')) as q0(v);

EXECUTE my_query(1,0);
-- 1

DEALLOCATE my_query;

asyncpg currently does not include types, which causes me to run into the sum(text) error. I'd expect it to either include types, or allow me to specify the types somehow.

Workaround:
Include a cast expression.
i.e.: $1::int or CAST($1 AS INT)

Full example

PREPARE my_query AS
SELECT SUM(CASE WHEN v = 'A' THEN $1::int ELSE $2::int END)
FROM (VALUES ('A'), ('B')) as q0(v);
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

1 participant