Functions API

Various math helper functions.

class django_spanner.functions.IfNull(*args, **kwargs)[source]

Represent SQL IFNULL function.

as_sql(compiler, connection, function=None, template=None, arg_joiner=None, **extra_context)

Responsible for returning a (sql, [params]) tuple to be included in the current query.

Different backends can provide their own implementation, by providing an as_{vendor} method and patching the Expression:

``` def override_as_sql(self, compiler, connection):

# custom logic return super().as_sql(compiler, connection)

setattr(Expression, ‘as_’ + connection.vendor, override_as_sql) ```

Parameters
  • compiler (*) – the query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (*) – the database connection used for the current query.

Return: (sql, params)

Where sql is a string containing ordered sql parameters to be replaced with the elements of the list params.

convert_value

Expressions provide their own converters because users have the option of manually specifying the output_field which may be a different type from the one the database returns.

deconstruct()

Return a 3-tuple of class import path, positional arguments, and keyword arguments.

flatten()

Recursively yield this expression and all subexpressions, in depth-first order.

get_source_fields()

Return the underlying field types used by this aggregate.

output_field

Return the output type of this expressions.

resolve_expression(query=None, allow_joins=True, reuse=None, summarize=False, for_save=False)

Provide the chance to do any preprocessing or validation before being added to the query.

Parameters
  • query (*) – the backend query implementation

  • allow_joins (*) – boolean allowing or denying use of joins in this query

  • reuse (*) – a set of reusable joins for multijoins

  • summarize (*) – a terminal aggregate clause

  • for_save (*) – whether this expression about to be used in a save or update

Return: an Expression to be added to the query.

select_format(compiler, sql, params)

Custom format for select clauses. For example, EXISTS expressions need to be wrapped in CASE WHEN on Oracle.

django_spanner.functions.cast(self, compiler, connection, **extra_context)[source]

A method to extend Django Cast class. Cast SQL query for given parameters.

Parameters
  • self (Cast) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.chr_(self, compiler, connection, **extra_context)[source]

A method to extend Django Chr class. Returns a SQL query where the code points are displayed as a string.

Parameters
  • self (Chr) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.concatpair(self, compiler, connection, **extra_context)[source]

A method to extend Django ConcatPair class. Concatenates a SQL query into the sequence of IfNull objects.

Parameters
  • self (ConcatPair) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.cot(self, compiler, connection, **extra_context)[source]

A method to extend Django Cot class. Returns a SQL query of calculated trigonometric cotangent function.

Parameters
  • self (Cot) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.degrees(self, compiler, connection, **extra_context)[source]

A method to extend Django Degress class. Returns a SQL query of the angle converted to degrees.

Parameters
  • self (Degrees) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.left_and_right(self, compiler, connection, **extra_context)[source]

A method to extend Django Left and Right classes.

Parameters
  • self (Left or Right) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.log(self, compiler, connection, **extra_context)[source]

A method to extend Django Log class. Returns a SQL query of calculated logarithm.

Parameters
  • self (Log) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.ord_(self, compiler, connection, **extra_context)[source]

A method to extend Django Ord class. Returns a SQL query of the expression converted to ord.

Parameters
  • self (Ord) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.pi(self, compiler, connection, **extra_context)[source]

A method to extend Django Pi class. Returns a SQL query of the Pi constant.

Parameters
  • self (Pi) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.radians(self, compiler, connection, **extra_context)[source]

A method to extend Django Radians class. Returns a SQL query of the angle converted to radians.

Parameters
  • self (Radians) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.register_functions()[source]

Register the above methods with the corersponding Django classes.

django_spanner.functions.strindex(self, compiler, connection, **extra_context)[source]

A method to extend Django StrIndex class. Returns a SQL query of the string position.

Parameters
  • self (StrIndex) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.

django_spanner.functions.substr(self, compiler, connection, **extra_context)[source]

A method to extend Django Substr class. Returns a SQL query of a substring.

Parameters
  • self (Substr) – the instance of the class that owns this method.

  • compiler (SQLCompilerst) – The query compiler responsible for generating the query. Must have a compile method, returning a (sql, [params]) tuple. Calling compiler(value) will return a quoted value.

  • connection (Connection) – The Spanner database connection used for the current query.

Return type

tuple(str, list)

Returns

A tuple where str is a string containing ordered SQL parameters to be replaced with the elements of the list.