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

Yeshql cannot handle :: cast in postgresql #7

Open
noxecane opened this issue Oct 8, 2018 · 2 comments
Open

Yeshql cannot handle :: cast in postgresql #7

noxecane opened this issue Oct 8, 2018 · 2 comments

Comments

@noxecane
Copy link

noxecane commented Oct 8, 2018

Expected Behaviour

[yesh|
  -- name: getMytable :: ()
  select *, now() - '1 hour ago'::interval from mytable;
|]

should generate

select *, now() - '1 hour ago'::interval from mytable;

Actual Behaviour

select *, now() - '1 hour ago'

My guess is yeshql is detecting :: in queries. I thought it would only use those in comments(--)
For now I am using cast directly.
I would have liked to send a patch but I know close to nothing of neither template-haskell not QuasiQuoting

@tdammers
Copy link
Owner

tdammers commented Oct 8, 2018

You are right, this doesn't work - :: inside queries is yeshql magic, interpreted as a type annotation, as an alternative to annotating parameters in comments. That is, instead of this:

-- name: getMytable :: ()
-- :id :: Int
SELECT * FROM mytable WHERE id = :id

...you could also write:

-- name: getMytable :: ()
SELECT * FROM mytable WHERE id = :id :: Int

Both forms are equivalent, however, the first form allows you to order parameters independently from the order in which they are found inside the query.

I'm not entirely sure how to fix this clash with PostgreSQL syntax; possibilities would include:

  • Removing this feature, allowing type annotations only in comments. This is the most thorough solution, but sacrifices a potentially useful feature.
  • Add a switch somewhere to opt into or out of this feature. This would complicate the API a little, but could be worth it.
  • Add an option somewhere to change the syntax for type annotations. This would complicate the API a little more though.
  • Make the parser more picky - we only really want to accept such type annotations after named parameters, e.g. :id :: Int, but not after anything else, so 'foo' :: text should not parse as a type annotation, but be left alone. This of course still leaves us with queries where you want to have a parameter as one type, but then have PostgreSQL cast it to another, which can be worked around, but it'd still be a somewhat surprising wart.

@noxecane
Copy link
Author

noxecane commented Oct 8, 2018

I think the second option balances things. I would have preferred the first one, but since this is a postgresql only syntax it'll be better to use an opt-out switch. The other two just bring complexity that'll be too much for a simple library

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