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

Reference Composition #26

Open
soulstompp opened this issue Apr 17, 2021 · 1 comment
Open

Reference Composition #26

soulstompp opened this issue Apr 17, 2021 · 1 comment
Assignees

Comments

@soulstompp
Copy link
Owner

soulstompp commented Apr 17, 2021

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.

@soulstompp soulstompp self-assigned this Apr 17, 2021
@spazm
Copy link
Collaborator

spazm commented Apr 26, 2021

noob questions: :)

  1. when you say 'value of a composition is static', do you mean that the results are fixed at parsing, or do you mean it has a 'static lifetime? Or the fact that the composer produces the actual SQL for the backend rather than having some sort of re-usable type that can be converted to SQL in a jit manner at the end.
  2. Is one of the goals to rework :count and :union internally to use the :setting keyword?

anyways, now that I'm looking at the code again, going to see if I remember and finally understand ComposerTrait, and most of the crate types, SqlComposition, SqlCompositionAlias, ParsedItem etc.

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