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, default_time_zone: str = '')¶
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.
Dates and timestamps are compared by the date of the timestamp. Strings compared with a date or timestamp column are read as dates or timestamps.
- default_time_zone = ''¶
- 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.A temporal value is built as
a >= b AND a <= c, seecompare.- 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_float(args)¶
Build a float literal.
- Parameters:
args – A list with the value.
- Returns:
A bound parameter.
- build_in(args)¶
Build an
INpredicate.A temporal value is built as
a = b OR a = c ..., seecompare.- 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_neg(args)¶
Build a negation.
- Parameters:
args – A list with the operand node.
- Returns:
The negated expression.
- 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 a comparison or an arithmetic operator.
- Parameters:
op – Operator, e.g.
>or+.args – Two operand nodes.
- Returns:
The expression. Comparisons are built by
compare.- Raises:
BuildError – If the operator is not implemented.
- 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_wkt(args)¶
Build a geometry literal.
- Parameters:
args – A list with the WKT string.
- Returns:
A geometry in WGS84.
- compare(op, x: _Operand, y: _Operand)¶
Build a comparison.
A string compared with a temporal operand is read as a date or a timestamp. A date and a timestamp are compared by the date of the timestamp.
- Parameters:
op – Comparison operator.
x – Left operand.
y – Right operand.
- Returns:
A condition.
- Raises:
BuildError – If a string is not a valid date or timestamp.
- compare_with_day(op, t, d: datetime.date)¶
Compare a timestamp with a date, using the day boundaries.
- Parameters:
op – Comparison operator.
t – Timestamp expression.
d – Date.
- Returns:
A condition on
t, e.g.t >= d0 AND t < d1for=.
- date_literal(d: datetime.date)¶
Wrap a date as a bound parameter.
- Parameters:
d – Date.
- Returns:
A
datebound parameter.
- date_value(e) datetime.date¶
Parse a date string node.
- Parameters:
e – String node.
- Returns:
A
date.- Raises:
BuildError – If the node is not a string or not a valid date.
- day_bounds(d: datetime.date) tuple[datetime.datetime, datetime.datetime]¶
Compute the start of a day and of the next day.
- Parameters:
d – Date.
- Returns:
A tuple of two aware datetimes in the default time zone.
- 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 – A date string node, e.g.
'2021-06-15'.- Returns:
A
datebound parameter.- Raises:
BuildError – If the argument is not a valid date string.
- 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 timestamp string node, the zone is optional.
- Returns:
A
timestamptzbound parameter.- Raises:
BuildError – If the argument is not a valid timestamp string.
- literal(val)¶
Wrap a python value as a bound parameter.
- Parameters:
val – Value.
- Returns:
A literal expression.
- operand(e) _Operand¶
Wrap a node as an operand.
Date and timestamp functions are evaluated, strings are left pending, other nodes are built.
- Parameters:
e – Node.
- Returns:
The operand.
- Raises:
BuildError – If a date or timestamp is invalid.
- operand_sql(o: _Operand)¶
Build the expression of an operand.
- Parameters:
o – Operand.
- Returns:
The built expression, or a bound parameter for a literal.
- sql_day_start(x, days=0)¶
Build the start of the day of a date expression.
- Parameters:
x –
dateexpression.days – Number of days to add.
- Returns:
A
timestamptzexpression, in the default time zone.
- sql_to_date(x)¶
Build the date of a timestamp expression.
- Parameters:
x –
timestamptzexpression.- Returns:
A
dateexpression, in the default time zone.
- temporal_operand(o: _Operand) _Operand¶
Read a pending string operand as a date or a timestamp.
- Parameters:
o – String operand.
- Returns:
A date operand if the string has no time part, a timestamp operand otherwise.
- Raises:
BuildError – If the string is not a valid date or timestamp.
- 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.A timestamp becomes a degenerate range, a date the half-open range of its day. 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 date or timestamp node.- Returns:
A
tstzrangeexpression, null if a bound is null.
- temporal_span(e, unbounded)¶
Build the start and end of an instant or an interval bound.
- Parameters:
e – Date, timestamp or string node. In an interval, the string
'..'means an open bound.unbounded – Value for an open bound,
-infinityorinfinity, empty outside of intervals.
- Returns:
A tuple of the start and end expressions and a flag that tells whether the end is open.
- Raises:
BuildError – If the node is not a date or a timestamp.
- time_zone_name()¶
Get the name of the default time zone.
- Returns:
A time zone name for postgres, e.g.
Europe/Berlin.
- timestamp_literal(t: datetime.datetime)¶
Wrap a datetime as a bound parameter.
- Parameters:
t – Aware datetime.
- Returns:
A
timestamptzbound parameter.
- timestamp_value(e) datetime.datetime¶
Parse a timestamp string node, in the default time zone if it has no zone.
- Parameters:
e – String node.
- Returns:
An aware
datetime.- Raises:
BuildError – If the node is not a string or not a valid timestamp.
- to_date(t: datetime.datetime) datetime.date¶
Get the date of a timestamp.
- Parameters:
t – Aware datetime.
- Returns:
The date in the default time zone.