gws.lib.cql.builder¶
Build database expressions from CQL2 parse trees.
Source code: gws.lib.cql.builder
Module Contents¶
- class gws.lib.cql.builder.Builder¶
Walks a CQL2 parse tree and dispatches nodes to handler methods.
Node types go to
build_<type>methods, standard functions tofunc_<name>methods (names are lowercased). This class implements no node types; subclasses add handlers.- build(e)¶
Build an expression from a parse tree node.
Calls
build_<type>with the node arguments, orbuild_operatorfor operators without one.- Parameters:
e – Parse tree node.
- Returns:
The expression returned by the handler.
- Raises:
BuildError – If the node type is not implemented.
- build_function(args)¶
Build a standard function call by calling
func_<name>with the argument nodes.- Parameters:
args – Function name, followed by the argument nodes.
- Returns:
The expression returned by the handler.
- Raises:
BuildError – If the function is not implemented.
- build_operator(op, args)¶
Build an operator expression.
- Parameters:
op – Operator, e.g.
>or+.args – Operand nodes.
- Returns:
The expression. Not implemented in this class.
- Raises:
BuildError – Always, in this class.
- build_user_function(args)¶
Build a non-standard function call. Subclasses handle their own functions here.
- Parameters:
args – Function name as written, followed by the argument nodes.
- Returns:
The expression. Not implemented in this class.
- Raises:
BuildError – Always, in this class.
- get_method(name)¶
Find a handler method.
- Parameters:
name – Method name, case-insensitive.
- Returns:
The method, or
Noneif the builder does not implement it.
- value(e) Any¶
Unwrap a literal node into a plain python value.
- Parameters:
e – Literal node. Array nodes are unwrapped recursively.
- Returns:
The value, a list for arrays.
- Raises:
BuildError – If the node is not a literal.
- exception gws.lib.cql.builder.BuildError¶
Bases:
ExceptionA parse tree node or function is not supported by the builder, or its arguments are invalid.
- class gws.lib.cql.builder.SqlBuilder(table: gws.lib.sa.Table)¶
Bases:
BuilderBuilds SQLAlchemy expressions for a postgis table.
Names refer to table columns, literals become bound parameters, geometries are in WGS84. All standard CQL2 functions are implemented, user functions are not.
- table¶
- array_element_type(vals)¶
Infer the element type of an array literal from its first element.
- Parameters:
vals – Array values.
- Returns:
An SQLAlchemy type,
Textfor an empty array or non-numeric values.
- array_operand(e)¶
Build an array expression, an array literal becoming a typed parameter.
- Parameters:
e – Array literal or another node.
- Returns:
An array expression.
- array_pair(args)¶
Coerce both operands of an array predicate to arrays.
- Parameters:
args – Two array nodes.
- Returns:
A tuple of two array expressions.
- build_and(args)¶
Build a conjunction.
- Parameters:
args – Operand nodes.
- Returns:
An
ANDexpression.
- build_array(args)¶
Build an array.
- Parameters:
args – Element nodes.
- Returns:
A list of expressions.
- build_bbox(args)¶
Build a bounding box geometry.
- Parameters:
args – Four numbers: min x, min y, max x, max y.
- Returns:
A rectangle geometry in WGS84.
- build_between(args)¶
Build a
BETWEENpredicate.- Parameters:
args – The value, lower bound and upper bound nodes.
- Returns:
A
BETWEENexpression.
- build_bool(args)¶
Build a boolean literal.
- Parameters:
args – A list with the value.
- Returns:
A bound parameter.
- build_date(args)¶
Build a date literal.
- Parameters:
args – A list with the date value.
- Returns:
A
DATEexpression.
- build_float(args)¶
Build a float literal.
- Parameters:
args – A list with the value.
- Returns:
A bound parameter.
- build_in(args)¶
Build an
INpredicate.- Parameters:
args – The value node, followed by the list item nodes.
- Returns:
An
INexpression.
- build_int(args)¶
Build a integer literal.
- Parameters:
args – A list with the value.
- Returns:
A bound parameter.
- build_is_null(args)¶
Build an
IS NULLpredicate.- Parameters:
args – A list with the value node.
- Returns:
An
IS NULLexpression.
- build_like(args)¶
Build a
LIKEpredicate.- Parameters:
args – The value and pattern nodes.
- Returns:
A
LIKEexpression.
- build_name(args)¶
Build a column reference.
- Parameters:
args – Name parts, only the first is used as the column name.
- Returns:
The table column.
- Raises:
BuildError – If the table has no such column.
- build_not(args)¶
Build a negation.
- Parameters:
args – A list with the operand node.
- Returns:
A
NOTexpression.
- build_not_between(args)¶
Build a
NOT BETWEENpredicate.- Parameters:
args – The value, lower bound and upper bound nodes.
- Returns:
A negated
BETWEENexpression.
- build_not_in(args)¶
Build a
NOT INpredicate.- Parameters:
args – The value node, followed by the list item nodes.
- Returns:
A negated
INexpression.
- build_not_like(args)¶
Build a
NOT LIKEpredicate.- Parameters:
args – The value and pattern nodes.
- Returns:
A negated
LIKEexpression.
- build_not_null(args)¶
Build an
IS NOT NULLpredicate.- Parameters:
args – A list with the value node.
- Returns:
An
IS NOT NULLexpression.
- build_operator(op, args)¶
Build an operator expression.
- Parameters:
op – Operator, e.g.
>or+.args – Operand nodes.
- Returns:
The expression. Not implemented in this class.
- Raises:
BuildError – Always, in this class.
- build_or(args)¶
Build a disjunction.
- Parameters:
args – Operand nodes.
- Returns:
An
ORexpression.
- build_string(args)¶
Build a string literal.
- Parameters:
args – A list with the value.
- Returns:
A bound parameter.
- build_timestamp(args)¶
Build a timestamp literal.
- Parameters:
args – A list with the datetime value.
- Returns:
A
timestamptzexpression.
- build_wkt(args)¶
Build a geometry literal.
- Parameters:
args – A list with the WKT string.
- Returns:
A geometry in WGS84.
- func_a_containedby(args)¶
Build the
A_CONTAINEDBYarray predicate.- Parameters:
args – Two array nodes.
- Returns:
A condition that is true if all elements of the first array are in the second.
- func_a_contains(args)¶
Build the
A_CONTAINSarray predicate.- Parameters:
args – Two array nodes.
- Returns:
A condition that is true if the first array contains all elements of the second.
- func_a_equals(args)¶
Build the
A_EQUALSarray predicate.- Parameters:
args – Two array nodes.
- Returns:
A condition that is true if both arrays contain the same elements, ignoring order and duplicates.
- func_a_overlaps(args)¶
Build the
A_OVERLAPSarray predicate.- Parameters:
args – Two array nodes.
- Returns:
A condition that is true if the arrays have a common element.
- func_accenti(args)¶
Build the
ACCENTIfunction.- Parameters:
args – A string node.
- Returns:
The value without accents, using the postgres
unaccentextension.
- func_bbox(args)¶
Build the
BBOXfunction.- Parameters:
args – Four number literal nodes: min x, min y, max x, max y.
- Returns:
A rectangle geometry in WGS84.
- Raises:
BuildError – If an argument is not a literal.
- func_casei(args)¶
Build the
CASEIfunction.- Parameters:
args – A string node.
- Returns:
The lowercased value.
- func_date(args)¶
Build the
DATEfunction.- Parameters:
args – An ISO date string literal node.
- Returns:
A
DATEexpression.- Raises:
BuildError – If the argument is not a literal.
- func_interval(args)¶
Reject
INTERVALoutside of temporal predicates.- Parameters:
args – Interval bound nodes.
- Raises:
BuildError – Always.
- func_s_contains(args)¶
Build the
S_CONTAINSspatial predicate.- Parameters:
args – Two geometry nodes.
- Returns:
An
ST_Containsexpression.
- func_s_crosses(args)¶
Build the
S_CROSSESspatial predicate.- Parameters:
args – Two geometry nodes.
- Returns:
An
ST_Crossesexpression.
- func_s_disjoint(args)¶
Build the
S_DISJOINTspatial predicate.- Parameters:
args – Two geometry nodes.
- Returns:
An
ST_Disjointexpression.
- func_s_equals(args)¶
Build the
S_EQUALSspatial predicate.- Parameters:
args – Two geometry nodes.
- Returns:
An
ST_Equalsexpression.
- func_s_intersects(args)¶
Build the
S_INTERSECTSspatial predicate.- Parameters:
args – Two geometry nodes.
- Returns:
An
ST_Intersectsexpression.
- func_s_overlaps(args)¶
Build the
S_OVERLAPSspatial predicate.- Parameters:
args – Two geometry nodes.
- Returns:
An
ST_Overlapsexpression.
- func_s_touches(args)¶
Build the
S_TOUCHESspatial predicate.- Parameters:
args – Two geometry nodes.
- Returns:
An
ST_Touchesexpression.
- func_s_within(args)¶
Build the
S_WITHINspatial predicate.- Parameters:
args – Two geometry nodes.
- Returns:
An
ST_Withinexpression.
- func_t_after(args)¶
Build the
T_AFTERtemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the first range starts after the second ends.
- func_t_before(args)¶
Build the
T_BEFOREtemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the first range ends before the second starts.
- func_t_contains(args)¶
Build the
T_CONTAINStemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the second range lies strictly inside the first.
- func_t_disjoint(args)¶
Build the
T_DISJOINTtemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the ranges have no common point.
- func_t_during(args)¶
Build the
T_DURINGtemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the first range lies strictly inside the second.
- func_t_equals(args)¶
Build the
T_EQUALStemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if both ranges are equal.
- func_t_finishedby(args)¶
Build the
T_FINISHEDBYtemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if both ranges end together and the first starts earlier.
- func_t_finishes(args)¶
Build the
T_FINISHEStemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if both ranges end together and the first starts later.
- func_t_intersects(args)¶
Build the
T_INTERSECTStemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the ranges have a common point.
- func_t_meets(args)¶
Build the
T_MEETStemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the first range ends where the second starts.
- func_t_metby(args)¶
Build the
T_METBYtemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the first range starts where the second ends.
- func_t_overlappedby(args)¶
Build the
T_OVERLAPPEDBYtemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the first range starts inside the second and ends after it.
- func_t_overlaps(args)¶
Build the
T_OVERLAPStemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if the first range starts before the second and ends inside it.
- func_t_startedby(args)¶
Build the
T_STARTEDBYtemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if both ranges start together and the first ends later.
- func_t_starts(args)¶
Build the
T_STARTStemporal predicate.- Parameters:
args – Two temporal nodes, instants or intervals.
- Returns:
A condition that is true if both ranges start together and the first ends earlier.
- func_timestamp(args)¶
Build the
TIMESTAMPfunction.- Parameters:
args – An ISO date-time string literal node.
- Returns:
A
timestamptzexpression.- Raises:
BuildError – If the argument is not a literal.
- literal(val)¶
Wrap a python value as a bound parameter.
- Parameters:
val – Value.
- Returns:
A literal expression.
- temporal_bound(e, unbounded)¶
Build an interval bound.
- Parameters:
e – Bound node. The string
'..'means an open bound.unbounded – Value for an open bound,
-infinityorinfinity.
- Returns:
A
timestamptzexpression.
- temporal_pair(args)¶
Coerce both operands of a temporal predicate to ranges.
- Parameters:
args – Two temporal nodes.
- Returns:
A tuple of two
tstzrangeexpressions.
- temporal_range(e)¶
Coerce a temporal expression to a
tstzrange, an instant becomes a degenerate range.A null bound is unbounded in postgres, therefore null inputs must yield a null range, otherwise a null column would match everything.
- Parameters:
e – An
INTERVALfunction node, or a node that builds a timestamp.- Returns:
A
tstzrangeexpression with inclusive bounds, null if a bound is null.
- timestamp_value(e)¶
Coerce an expression to a
timestamptz, naive values are assumed to be UTC.- Parameters:
e – Node.
- Returns:
A
timestamptzexpression.