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 to func_<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, or build_operator for 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 None if 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: Exception

A 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: Builder

Builds 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, Text for 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 AND expression.

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 BETWEEN predicate.

A temporal value is built as a >= b AND a <= c, see compare.

Parameters:

args – The value, lower bound and upper bound nodes.

Returns:

A BETWEEN expression.

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 IN predicate.

A temporal value is built as a = b OR a = c ..., see compare.

Parameters:

args – The value node, followed by the list item nodes.

Returns:

An IN expression.

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 NULL predicate.

Parameters:

args – A list with the value node.

Returns:

An IS NULL expression.

build_like(args)

Build a LIKE predicate.

Parameters:

args – The value and pattern nodes.

Returns:

A LIKE expression.

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 NOT expression.

build_not_between(args)

Build a NOT BETWEEN predicate.

Parameters:

args – The value, lower bound and upper bound nodes.

Returns:

A negated BETWEEN expression.

build_not_in(args)

Build a NOT IN predicate.

Parameters:

args – The value node, followed by the list item nodes.

Returns:

A negated IN expression.

build_not_like(args)

Build a NOT LIKE predicate.

Parameters:

args – The value and pattern nodes.

Returns:

A negated LIKE expression.

build_not_null(args)

Build an IS NOT NULL predicate.

Parameters:

args – A list with the value node.

Returns:

An IS NOT NULL expression.

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 OR expression.

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 < d1 for =.

date_literal(d: datetime.date)

Wrap a date as a bound parameter.

Parameters:

d – Date.

Returns:

A date bound 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_CONTAINEDBY array 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_CONTAINS array 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_EQUALS array 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_OVERLAPS array predicate.

Parameters:

args – Two array nodes.

Returns:

A condition that is true if the arrays have a common element.

func_accenti(args)

Build the ACCENTI function.

Parameters:

args – A string node.

Returns:

The value without accents, using the postgres unaccent extension.

func_bbox(args)

Build the BBOX function.

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 CASEI function.

Parameters:

args – A string node.

Returns:

The lowercased value.

func_date(args)

Build the DATE function.

Parameters:

args – A date string node, e.g. '2021-06-15'.

Returns:

A date bound parameter.

Raises:

BuildError – If the argument is not a valid date string.

func_interval(args)

Reject INTERVAL outside of temporal predicates.

Parameters:

args – Interval bound nodes.

Raises:

BuildError – Always.

func_s_contains(args)

Build the S_CONTAINS spatial predicate.

Parameters:

args – Two geometry nodes.

Returns:

An ST_Contains expression.

func_s_crosses(args)

Build the S_CROSSES spatial predicate.

Parameters:

args – Two geometry nodes.

Returns:

An ST_Crosses expression.

func_s_disjoint(args)

Build the S_DISJOINT spatial predicate.

Parameters:

args – Two geometry nodes.

Returns:

An ST_Disjoint expression.

func_s_equals(args)

Build the S_EQUALS spatial predicate.

Parameters:

args – Two geometry nodes.

Returns:

An ST_Equals expression.

func_s_intersects(args)

Build the S_INTERSECTS spatial predicate.

Parameters:

args – Two geometry nodes.

Returns:

An ST_Intersects expression.

func_s_overlaps(args)

Build the S_OVERLAPS spatial predicate.

Parameters:

args – Two geometry nodes.

Returns:

An ST_Overlaps expression.

func_s_touches(args)

Build the S_TOUCHES spatial predicate.

Parameters:

args – Two geometry nodes.

Returns:

An ST_Touches expression.

func_s_within(args)

Build the S_WITHIN spatial predicate.

Parameters:

args – Two geometry nodes.

Returns:

An ST_Within expression.

func_t_after(args)

Build the T_AFTER temporal 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_BEFORE temporal 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_CONTAINS temporal 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_DISJOINT temporal 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_DURING temporal 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_EQUALS temporal 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_FINISHEDBY temporal 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_FINISHES temporal 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_INTERSECTS temporal 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_MEETS temporal 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_METBY temporal 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_OVERLAPPEDBY temporal 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_OVERLAPS temporal 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_STARTEDBY temporal 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_STARTS temporal 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 TIMESTAMP function.

Parameters:

args – An ISO timestamp string node, the zone is optional.

Returns:

A timestamptz bound 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 – date expression.

  • days – Number of days to add.

Returns:

A timestamptz expression, in the default time zone.

sql_to_date(x)

Build the date of a timestamp expression.

Parameters:

x – timestamptz expression.

Returns:

A date expression, 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 tstzrange expressions.

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 INTERVAL function node, or a date or timestamp node.

Returns:

A tstzrange expression, 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, -infinity or infinity, 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 timestamptz bound 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.