staab / janet-pqutils

A utility library for Janet and janet-pq.
MIT License
8 stars 0 forks source link

Add query dsl #6

Open staab opened 4 years ago

staab commented 4 years ago

Three layers:

  1. Something data-oriented like {:select [:x :y] :where [{:x := 1}]}, which allows us to target clauses by type when compiling the query. This might be redundant and unnecessary given representation number 2.
  2. Also data-oriented, but as a collection, like [{:t :select :v :x} {:t :select :v :y} {:t :where :k :x :op := :v 1}]. This allows the user to easily compose/modify queries, since a query is a (recursive, with subqueries) collection of clauses.
  3. A convenience layer, e.g. (q (select x y z) (where= x 1)). The q function should compile directly to the collection representation above, flattening values returned from query building functions/macros into a single list. This should recur into subqueries as well.

A compile function should take any of the three representations above and turn it into a string, escaping literals and identifiers as necessary. If the input is a string already, it should just return it.

Execution functions shouldn't know about this layer, but should expose a configuration option for preparing queries, which can be configured by setting it to compile.

staab commented 4 years ago

It might be worth replacing unpack with something in this layer for creating aliases. This would: