ehmpathy / sql-code-generator

Generate code from your SQL schema and queries for type safety and development speed.
MIT License
28 stars 3 forks source link

formal support for OR statements on query input variable type definitions #49

Open uladkasach opened 2 years ago

uladkasach commented 2 years ago

OR statements are important - but not trivial to support.

the reason they're not trivial is because they open the topic that one query may attempt to use the same token in different ways:

consider the following cases

  1. optional filter

    WHERE (:until IS null OR chat_message.created_at <= :until)
  2. broader search

    WHERE (
    user.first_name = :name
    OR
    user.last_name = :name
    )
  3. impossible criteria

    WHERE (
    user.id = :name -- => type = number
    AND
    user.first_name = :name -- => type = string
    )

case 1 and 2 are no problem to support, just union the types

case 3 is a problem to support, because we have to intersect the types

how do we distinguish between intersection cases and union cases?:

how do we merge intersections?:

ok... how do we identify unions though?

ok cool

and how do we distinguish in the data model unions vs intersections?

ok so that means, when we're outputting the types


how do we handle identifying these cases then, functionally?


this seems pretty tractable!

uladkasach commented 2 years ago

:thinking: - we can probably start with just treating everything as a union

e.g., still suffix each token so that we identify the type of every position of the token without explicitly considering whether its a Union or Intersection - and then when we go to output the types just treat everything as a union

this is not ideal as it removes some type saftey (i.e., if we did consider intersections, we would help notify users sooner that their query is not going to work)

but it is ideal in the sense that it is way faster to implement and way more convinient to use


lets start there - and we can create another ticket for intersections and explicit AND -vs- OR support when we need it / when the benefit is justified