paurkedal / ocaml-caqti

Cooperative-threaded access to relational data
https://paurkedal.github.io/ocaml-caqti/index.html
GNU Lesser General Public License v3.0
307 stars 36 forks source link

Parse error disallows ?::typecast #101

Closed anmonteiro closed 1 year ago

anmonteiro commented 1 year ago

Caqti doesn't parse the following query:

WITH data(name, age) AS (
  VALUES (?::varchar, 25)
)

insert into ...

It errors with:

Fatal error: exception Failure("Parse error at byte 50: ':' is not allowed after parameter reference '?'")

Though e.g. Postgres should have no issue with it (e.g. a github search yields many results for this pattern https://github.com/search?q=%3F%3A%3Avarchar&type=code )

paurkedal commented 1 year ago

The think the only PostgreSQL syntax for unnamed parameters is $1::varchar. The liked code is maybe using a system which translates the ? to $n, like Caqti, but without the restriction on what can follow directly after a ?? The error can be avoided by adding a space between the ? and the first :. The reason for the restriction is to avoid accepting something which we in a future version might want to interpret, but I can be convinced to lift it.

paurkedal commented 1 year ago

Fixed by making an exception to the exception: ? can now be followed by a colon if that colon is followed by another one.

anmonteiro commented 1 year ago

Works great after testing, thanks!