-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Currently the value of a composition is static which greatly limits query re-usability. The first attempts at increasing this re-usability were the :count() and :union() style commands but they were fairly limited and greatly increase code complexity (it generates SQL which requires knowing which dialect of SQL a driver wants.
After a fair amount of using these compositions I have found it would be much easier (as well as more flexible) to write a query with a subquery that could easily be swapped out. For example writing a generic count query that works similar to the :count() macro as follows:
SELECT COUNT(base.id) AS base_count FROM (:reference(ref_name)) base (referred to below as count_base.cql).
This query would compose whatever query ref_name is set to and count whatever column was specified as id.
This base.cql template could be used as follows:
SELECT base_count FROM :compose(count_base.cql SETTING ref_name TO :compose(active_users.cql)
Assuming simple.cql was SELECT id, name FROM users WHERE active = 1 this would result in:
SELECT COUNT(base.id) FROM (SELECT id, name FROM users WHERE active = 1) base allowing for a count query that is reusable for any other query with an id column and without having to alter the active_users.cql query in order to count these users.
If a query has a reference that is not provided by the calling macro using SETTING composition should fail.
If the SETTING keyword is used while composing a query without any references composition should also fail.