Changes between Initial Version and Version 1 of S3/FieldSelectors

05/01/14 10:55:13 (8 years ago)
Dominic König



  • S3/FieldSelectors

    v1 v1  
     1= Field Selectors =
     3Field selectors are a string syntax to specify fields in an S3Resource.
     5== Basic Syntax ==
     7The fundamental syntax of a field selector is:
     13The path specifies the table that contains the field.
     15=== Fields in the Master Table ===
     17In the simplest case, the path is just the master table of the resource which is represented by the tilde followed by a period:
     23The master table path is mandatory only in URLs but can otherwise be omitted, i.e. the simplest field selector is simply the field name.
     25It is also possible to use the name of the master table without module prefix (e.g. "organisation" for org_organisation) instead of the tilde to refer to the master table.
     27=== Fields in Components ===
     29If the field is in a component resource, then the path is the alias of that component followed by a period:
     35By default, the alias is the name of the component table without its module prefix (e.g. "contact" for pr_contact).
     37The alias can also be set explicitly in add_component (e.g. for filtered components).
     39If the alias can not be resolved, an AttributeError will be raised.
     41=== Fields in Referenced Tables ===
     43If the field is in a table that is referenced by a foreign key in the master table, then this can be expressed like:
     49The $ sign indicates that the field is in the table referenced by this foreign key field.
     51The foreign key field must have a real foreign key constraint, i.e. it must not be a list:reference or an integer pseudo-reference.
     53Similar, if the foreign key is in a component:
     59=== Fields in Linked Tables ===
     61If the field is in a table that references the master table, but is not a component of it, this can be expressed like:
     67In this case, the <foreign_key> is the field in <linked_table> that references the master table (i.e. the so-called "left key").
     69Similar, if the linked table references a component instead of the master table:
     75=== Combinations ===
     77It is possible to chain path expressions. Every part of the path is relative to the table specified by any previous parts:
     83...specifies that the field is in a component <alias> of the table referenced by <foreign_key>
     89...specifies a field in table that is referenced by <foreign_key2> in a table that is referenced by <foreign_key1> in a component <alias> of the master resource.
     91One should though keep in mind that using multi-table paths requires multi-table joins or multiple sub-queries when filtering data. It should therefore be avoided (e.g. through better modelling) where performance is critical, or generally where it is possible.
     93=== Context Paths ===
     95Context paths allow to use the same filter expression for multiple resources even if they have different relationships to the target field.
     97Consider the following case:
     99For project_project, the location reference is in a component project_location:
     104For org_office, the location reference is in the master table:
     109Obviously, the field specified by both selectors is the same - just the path to the field is specific for each resource. Due to these different paths, though, filter queries with either selector can not be re-used for the other resource:
     112# Filter query specific for project_project
     113query = S3FieldSelector("location.location_id$name") == "Example"
     115# Filter query specific for org_office
     116query = S3FieldSelector("~.location_id$name") == "Example"
     119To overcome this, we can configure the resource-specific part of the path as "context" path, using s3db.configure:
     122# Configure the "location" context for project_project:
     124               context={"location": "location.location_id"},
     125               )
     127# Configure the "location" context for org_office:
     129               context={"location": "~.location_id"},
     130               )
     133Now we can use this "location" context to replace the resource-specific part of the path:
     138Through this, we can re-use the filter query for both resources:
     140# Filter expression valid for both project_project and org_office:
     141query = S3FieldSelector("(location)$name") == "Example
     144Only when the field selector gets resolved against each resource (e.g. during add_filter), the "(location)" part is translated according to the respective "context" configuration.