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

Format error in an UPDATE statement #98

Closed
CGenie opened this issue Sep 2, 2022 · 5 comments
Closed

Format error in an UPDATE statement #98

CGenie opened this issue Sep 2, 2022 · 5 comments
Labels
invalid This doesn't seem right question Further information is requested

Comments

@CGenie
Copy link

CGenie commented Sep 2, 2022

Hello,

I have this query:

[sql| UPDATE x
                    SET a = ?
                    WHERE b = ? AND c = ? AND d IN (SELECT id FROM y WHERE A = ?) |]

This works perfectly fine when I substitute some values and execute as a raw SQL statement.

However when I try to Database.PostgreSQL.Simple.execute this query with some values, I'm getting this error:

*** Exception: FormatError {fmtMessage = "syntax error in multi-row template", fmtQuery = "UPDATE x SET a = ? WHERE b = ? AND c = ? AND d IN (SELECT id FROM y WHERE A = ?)", fmtParams = []}

Apparently the internal parser for SQL for postgresql-simple can't handle this query properly.

@phadej
Copy link
Collaborator

phadej commented Sep 2, 2022

are you sure you are calling execute and not executeMany?

A standalone reproducer using formatQuery would be great.

@CGenie
Copy link
Author

CGenie commented Sep 2, 2022

Yes, it's executeMany but I thought they're just like a map over execute and I wanted to simplify my example.

@phadej
Copy link
Collaborator

phadej commented Sep 2, 2022

I thought they're just like a map over execute and I wanted to simplify my example.

it's not. It expands VALUS (?,?) into many tuples, so only one statement is executed.

from formatMany doc:

-- The query string must contain exactly one substitution group,
-- identified by the SQL keyword \"@VALUES@\" (case insensitive)
-- followed by an \"@(@\" character, a series of one or more \"@?@\"
-- characters separated by commas, and a \"@)@\" character. White
-- space in a substitution group is permitted.

@CGenie
Copy link
Author

CGenie commented Sep 2, 2022

Ah OK I get it. So I should do mapM (execute c query) params in my case instead of executeMany directly?

@CGenie
Copy link
Author

CGenie commented Sep 2, 2022

Yeah, mapM worked for me. Thanks for solving this!

@CGenie CGenie closed this as completed Sep 2, 2022
@phadej phadej added invalid This doesn't seem right question Further information is requested labels Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants