Operations API

class django_spanner.operations.DatabaseOperations(connection)[source]

A Spanner-specific version of Django database operations.

adapt_datefield_value(value)[source]

Cast date argument into Spanner DB API DateStr format.

Parameters

value (object) – A date argument.

Return type

DateStr

Returns

Formatted Date.

adapt_datetimefield_value(value)[source]

Reformat time argument into Cloud Spanner.

Parameters

value (object) – A time argument.

Return type

TimestampStr

Returns

Formatted Time.

adapt_decimalfield_value(value, max_digits=None, decimal_places=None)[source]

Convert value from decimal.Decimal to spanner compatible value. Since spanner supports Numeric storage of decimal and python spanner takes care of the conversion so this is a no-op method call.

Parameters
  • value (decimal.Decimal) – A decimal field value.

  • max_digits (int) – (Optional) A maximum number of digits.

  • decimal_places (int) – (Optional) The number of decimal places to store with the number.

Return type

decimal.Decimal

Returns

decimal value.

adapt_ipaddressfield_value(value)

Transform a string representation of an IP address into the expected type for the backend driver.

adapt_timefield_value(value)[source]

Transform a time value to an object compatible with what is expected by the backend driver for time columns.

Parameters

value (datetime.datetime) – A time field value.

Return type

TimestampStr

Returns

Formatted Time.

adapt_unknown_value(value)

Transform a value to something compatible with the backend driver.

This method only depends on the type of the value. It’s designed for cases where the target type isn’t known, such as .raw() SQL queries. As a consequence it may not work perfectly in all circumstances.

autoinc_sql(table, column)

Return any SQL needed to support auto-incrementing primary keys, or None if no SQL is necessary.

This SQL is executed when a table is created.

binary_placeholder_sql(value)

Some backends require special syntax to insert binary content (MySQL for example uses ‘_binary %s’).

bulk_batch_size(fields, objs)[source]

Override the base class method. Returns the maximum number of the query parameters.

Parameters
  • fields (list) – Currently not used.

  • objs (list) – Currently not used.

Return type

int

Returns

The maximum number of query parameters (constant).

bulk_insert_sql(fields, placeholder_rows)[source]

A helper method that stitches multiple values into a single SQL record.

Parameters
  • fields (list) – Currently not used.

  • placeholder_rows (list) – Data “rows” containing values to combine.

Return type

str

Returns

A SQL statement (a VALUES command).

cache_key_culling_sql()

Return an SQL query that retrieves the first cache key greater than the n smallest.

This is used by the ‘db’ cache backend to determine where to start culling.

check_expression_support(expression)

Check that the backend supports the provided expression.

This is used on specific backends to rule out known expressions that have problematic or nonexistent implementations. If the expression has a known problem, the backend should raise NotSupportedError.

combine_duration_expression(connector, sub_expressions)[source]

Combine duration expressions into single one using connector.

Parameters
  • connector (str) – A type of connector operation.

  • sub_expressions (list) – A list of expressions to combine.

Raises

DatabaseError

Return type

str

Returns

A SQL statement for combining.

combine_expression(connector, sub_expressions)[source]

Recurrently combine expressions into single one using connector.

Parameters
  • connector (str) – A type of connector operation.

  • sub_expressions (list) – A list of expressions to combine.

Return type

str

Returns

A SQL statement for combining.

compiler(compiler_name)

Return the SQLCompiler class corresponding to the given name, in the namespace corresponding to the compiler_module attribute on this backend.

convert_binaryfield_value(value, expression, connection)[source]

Convert Spanner BinaryField value for Django.

Parameters
  • value (bytes) – A base64-encoded binary field value.

  • expression (django.db.models.expressions.BaseExpression) – A query expression.

  • connection (Connection) – Reference to a Spanner database connection.

Return type

bytes

Returns

A base64 encoded bytes.

convert_datetimefield_value(value, expression, connection)[source]

Convert Spanner DateTimeField value for Django.

Parameters
  • value (DatetimeWithNanoseconds) – A datetime field value.

  • expression (django.db.models.expressions.BaseExpression) – A query expression.

  • connection (Connection) – Reference to a Spanner database connection.

Return type

datetime

Returns

A TZ-aware datetime.

convert_timefield_value(value, expression, connection)[source]

Convert Spanner TimeField value for Django.

Parameters
  • value (DatetimeWithNanoseconds) – A datetime/time field.

  • expression (django.db.models.expressions.BaseExpression) – A query expression.

  • connection (Connection) – Reference to a Spanner database connection.

Return type

datetime.time

Returns

A converted datetime.

convert_uuidfield_value(value, expression, connection)[source]

Convert a UUID field to Cloud Spanner.

Parameters
  • value (str) – A UUID-valued str.

  • expression (django.db.models.expressions.BaseExpression) – A query expression.

  • connection (Connection) – Reference to a Spanner database connection.

Return type

uuid.UUID

Returns

A converted UUID.

date_extract_sql(lookup_type, field_name)[source]

Extract date from the lookup.

Parameters
  • lookup_type (str) – A type of the lookup.

  • field_name (str) – The name of the field.

Return type

str

Returns

A SQL statement for extracting.

date_interval_sql(timedelta)[source]

Get a date interval in microseconds.

Parameters

timedelta (datetime) – A time delta for the interval.

Return type

str

Returns

A SQL statement.

date_trunc_sql(lookup_type, field_name, tzname=None)[source]

Truncate date in the lookup.

Parameters
  • lookup_type (str) – A type of the lookup.

  • field_name (str) – The name of the field.

  • tzname (str) – The name of the timezone. This is ignored because

Spanner does not support Timezone conversion in DATE_TRUNC function.

Return type

str

Returns

A SQL statement for truncating.

datetime_cast_date_sql(field_name, tzname)[source]

Cast date in the lookup.

Parameters
  • field_name (str) – The name of the field.

  • tzname (str) – The time zone name. If using of time zone is not allowed in settings default will be UTC.

Return type

str

Returns

A SQL statement for casting.

datetime_cast_time_sql(field_name, tzname)[source]

Cast time in the lookup.

Parameters
  • field_name (str) – The name of the field.

  • tzname (str) – The time zone name. If using of time zone is not allowed in settings default will be UTC.

Return type

str

Returns

A SQL statement for casting.

datetime_extract_sql(lookup_type, field_name, tzname)[source]

Extract datetime from the lookup.

Parameters
  • lookup_type (str) – A type of the lookup.

  • field_name (str) – The name of the field.

  • tzname (str) – The time zone name. If using of time zone is not allowed in settings default will be UTC.

Return type

str

Returns

A SQL statement for extracting.

datetime_trunc_sql(lookup_type, field_name, tzname='UTC')[source]

Truncate datetime in the lookup.

Parameters
  • lookup_type (str) – A type of the lookup.

  • field_name (str) – The name of the field.

  • tzname (str) – The name of the timezone.

Return type

str

Returns

A SQL statement for truncating.

deferrable_sql()

Return the SQL to make a constraint “initially deferred” during a CREATE TABLE statement.

distinct_sql(fields, params)

Return an SQL DISTINCT clause which removes duplicate rows from the result set. If any fields are given, only check the given fields for duplicates.

end_transaction_sql(success=True)

Return the SQL statement required to end a transaction.

execute_sql_flush(using, sql_list)

Execute a list of SQL statements to flush the database.

fetch_returned_insert_id(cursor)

Given a cursor object that has just performed an INSERT…RETURNING statement into a table that has an auto-incrementing ID, return the newly created ID.

field_cast_sql(db_type, internal_type)

Given a column type (e.g. ‘BLOB’, ‘VARCHAR’) and an internal type (e.g. ‘GenericIPAddressField’), return the SQL to cast it before using it in a WHERE statement. The resulting string should contain a ‘%s’ placeholder for the column being searched against.

for_update_sql(nowait=False, skip_locked=False, of=())

Return the FOR UPDATE SQL clause to lock rows for an update operation.

force_no_ordering()

Return a list used in the “ORDER BY” clause to force no ordering at all. Return an empty list to include nothing in the ordering.

format_for_duration_arithmetic(sql)[source]

Do nothing since formatting is handled in the custom function.

Parameters

sql (str) – A SQL statement.

Return type

str

Returns

A SQL statement.

get_db_converters(expression)[source]

Get a list of functions needed to convert field data.

Parameters

expression (django.db.models.expressions.BaseExpression) – A query expression to convert.

Return type

list

Returns

Converter functions to apply to Spanner field values.

integer_field_range(internal_type)

Given an integer field internal type (e.g. ‘PositiveIntegerField’), return a tuple of the (min_value, max_value) form representing the range of the column type bound to the field.

last_executed_query(cursor, sql, params)

Return a string of the query last executed by the given cursor, with placeholders replaced with actual values.

sql is the raw query containing placeholders and params is the sequence of parameters. These are used by default, but this method exists for database backends to provide a better implementation according to their own quoting schemes.

last_insert_id(cursor, table_name, pk_name)

Given a cursor object that has just performed an INSERT statement into a table that has an auto-incrementing ID, return the newly created ID.

pk_name is the name of the primary-key column.

limit_offset_sql(low_mark, high_mark)

Return LIMIT/OFFSET SQL clause.

lookup_cast(lookup_type, internal_type=None)[source]

Cast text lookups to string to allow things like filter(x__contains=4).

Parameters
  • lookup_type (str) – A type of the lookup.

  • internal_type (str) – (Optional)

Return type

str

Returns

A SQL statement.

max_in_list_size()

Return the maximum number of items that can be passed in a single ‘IN’ list condition, or None if the backend does not impose a limit.

max_name_length()[source]

Get the maximum length of Spanner table and column names.

See also: https://cloud.google.com/spanner/quotas#tables

Return type

int

Returns

Maximum length of the name of the table.

modify_insert_params(placeholder, params)

Allow modification of insert parameters. Needed for Oracle Spatial backend due to #10888.

no_limit_value()[source]

The largest INT64: (2**63) - 1

Return type

int

Returns

The largest INT64.

pk_default_value()

Return the value to use during an INSERT statement to specify that the field should use its default value.

prep_for_iexact_query(x)

Lookups that use this method use REGEXP_CONTAINS instead of LIKE.

Parameters

x (str) – A query to prepare.

Return type

str

Returns

A prepared query.

prep_for_like_query(x)[source]

Lookups that use this method use REGEXP_CONTAINS instead of LIKE.

Parameters

x (str) – A query to prepare.

Return type

str

Returns

A prepared query.

prepare_sql_script(sql)

Take an SQL script that may contain multiple lines and return a list of statements to feed to successive cursor.execute() calls.

Since few databases are able to process raw SQL scripts in a single cursor.execute() call and PEP 249 doesn’t talk about this use case, the default implementation is conservative.

process_clob(value)

Return the value of a CLOB column, for backends that return a locator object that requires additional processing.

quote_name(name)[source]

Return a quoted version of the given table or column name. Also, applies backticks to the name that either contain ‘-’ or ‘ ‘, or is a Cloud Spanner’s reserved keyword.

Spanner says “column name not valid” if spaces or hyphens are present (although according to the documantation, any character should be allowed in quoted identifiers). Replace problematic characters when running the Django tests to prevent crashes. (Don’t modify names in normal operation to prevent the possibility of colliding with another column.)

See: https://github.com/googleapis/python-spanner-django/issues/204

Parameters

name (str) – The Quota name.

Return type

str

Returns

Name escaped if it has to be escaped.

random_function_sql()

Return an SQL expression that returns a random value.

regex_lookup(lookup_type)

Return the string to use in a query when performing regular expression lookups (using “regex” or “iregex”). It should contain a ‘%s’ placeholder for the column being searched against.

If the feature is not supported (or part of it is not supported), raise NotImplementedError.

return_insert_id()

For backends that support returning the last insert ID as part of an insert query, return the SQL and params to append to the INSERT query. The returned fragment should contain a format string to hold the appropriate column.

savepoint_commit_sql(sid)

Return the SQL for committing the given savepoint.

savepoint_create_sql(sid)

Return the SQL for starting a new savepoint. Only required if the “uses_savepoints” feature is True. The “sid” parameter is a string for the savepoint id.

savepoint_rollback_sql(sid)

Return the SQL for rolling back the given savepoint.

sequence_reset_by_name_sql(style, sequences)

Return a list of the SQL statements required to reset sequences passed in sequences.

The style argument is a Style object as returned by either color_style() or no_style() in django.core.management.color.

sequence_reset_sql(style, model_list)

Return a list of the SQL statements required to reset sequences for the given models.

The style argument is a Style object as returned by either color_style() or no_style() in django.core.management.color.

set_time_zone_sql()

Return the SQL that will set the connection’s time zone.

Return ‘’ if the backend doesn’t support time zones.

sql_flush(style, tables, reset_sequences=False, allow_cascade=False)[source]

Override the base class method. Returns a list of SQL statements required to remove all data from the given database tables (without actually removing the tables themselves).

Parameters
  • style (Style) – (Currently not used) An object as returned by either color_style() or no_style().

  • tables (list) – A collection of Cloud Spanner Tables

  • reset_sequences (bool) – (Optional) Currently not used.

  • allow_cascade (bool) – (Optional) Currently not used.

Return type

list

Returns

A list of SQL statements required to remove all data from

the given database tables.

start_transaction_sql()

Return the SQL statement required to start a transaction.

tablespace_sql(tablespace, inline=False)

Return the SQL that will be used in a query to define the tablespace.

Return ‘’ if the backend doesn’t support tablespaces.

If inline is True, append the SQL to a row; otherwise append it to the entire CREATE TABLE or CREATE INDEX statement.

time_extract_sql(lookup_type, field_name)[source]

Extract time from the lookup.

Parameters
  • lookup_type (str) – A type of the lookup.

  • field_name (str) – The name of the field.

Return type

str

Returns

A SQL statement for extracting.

time_trunc_sql(lookup_type, field_name, tzname='UTC')[source]

Truncate time in the lookup.

Parameters
  • lookup_type (str) – A type of the lookup.

  • field_name (str) – The name of the field.

  • tzname (str) – The name of the timezone. Defaults to ‘UTC’ For backward compatability.

Return type

str

Returns

A SQL statement for truncating.

unification_cast_sql(output_field)

Given a field instance, return the SQL that casts the result of a union to that type. The resulting string should contain a ‘%s’ placeholder for the expression being cast.

validate_autopk_value(value)

Certain backends do not accept some values for “serial” fields (for example zero in MySQL). Raise a ValueError if the value is invalid, otherwise return the validated value.

window_frame_rows_start_end(start=None, end=None)

Return SQL for start and end points in an OVER clause window frame.

year_lookup_bounds_for_date_field(value)

Return a two-elements list with the lower and upper bound to be used with a BETWEEN operator to query a DateField value using a year lookup.

value is an int, containing the looked-up year.

year_lookup_bounds_for_datetime_field(value)

Return a two-elements list with the lower and upper bound to be used with a BETWEEN operator to query a DateTimeField value using a year lookup.

value is an int, containing the looked-up year.