Skip to content

Templated queries #1392

Open
Open

Description

@lukehutch

Is your feature request related to a problem? Please describe.

@SandPod I just came across a need in my project that overlaps with your rework of the query framework.

I have a table-backed class that has hundreds of columns, called Profile. I want to be able to pass a sparsely-populated instance of Profile to the server to serve as filter criteria (basically, as a query template). For any column value in the query template that is not unset (this could mean null in the general case, but in the case of my table, it means '' or false, depending on whether the column is String or bool, since my columns are not nullable), I want to add a term to a query for the column, of the form t.columnName.equals(filterCriteria.columnName). The goal is to find records that have the same values as values set in the query template, while allowing sparsity in the query template (not all values have to be set).

The problem I have is that Dart doesn't have built-in introspection, so I can't just iterate through the fields of the query template, looking for fields that are set, and then automatically build a query term for that field/column, concatenating the terms using a fold with the & operator to join all the clauses together. Currently I have to write the entire huge query out by hand, and if I add a column, I have to remember to add the query clause.

Describe the solution you'd like

I would love to see a query type added of the form Profile.find(session, filteredBy: profileQueryTemplate). This would be equivalent to Profile.find(session, where: (t) => Profile.buildFilter(t, profileQueryTemplate)), where Profile.buildFilter(t, profileQueryTemplate) builds the query from the template. This would require the code generator to generate the Profile.buildFilter function.

As far as determining which query template fields are treated as being set (triggering filtering on the value provided in the query template) and which query template fields are treated as being unset: this could use null as the unset value, for nullable columns, and the additive identity ('', false, 0) as the unset value, for non-nullable columns.

Activity

  1. added
    enhancement
    on Sep 25, 2023
  2. SandPod commented on Sep 25, 2023

    @SandPod
    Contributor

    Interesting, I certainly understand the need behind this and I like the simplicity for creating queries that It would enable.

    I can't jump on this as part of the work at the moment but I'll keep it in mind for our planning after are are done with relations.

    We recently added copyWith that provides a solution for distinguishing between an unsent value and a null value, so there is probably some inspiration that can be taken from there.

  3. added
    area: database
    on Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: databaseenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions