Compiler API¶
- class django_spanner.compiler.SQLAggregateCompiler(query, connection, using)[source]¶
A wrapper class for compatibility with Django specifications.
- deferred_to_columns()¶
Convert the self.deferred_loading data structure to mapping of table names to sets of column names which are to be loaded. Return the dictionary.
- execute_sql(result_type='multi', chunked_fetch=False, chunk_size=100)¶
Run the query against the database and return the result(s). The return value is a single data item if result_type is SINGLE, or an iterator over the results if the result_type is MULTI.
result_type is either MULTI (use fetchmany() to retrieve all rows), SINGLE (only retrieve a single row), or None. In this last case, the cursor is returned if any query is executed, since it’s used by subclasses such as InsertQuery). It’s possible, however, that no query is needed, as the filters describe an empty set. In that case, None is returned, to avoid any unnecessary database interaction.
- find_ordering_name(name, opts, alias=None, default_order='ASC', already_seen=None)¶
Return the table alias (the name might be ambiguous, the alias will not be) and column name for ordering by the given ‘name’ parameter. The ‘name’ is of the form ‘field1__field2__…__fieldN’.
- get_combinator_sql(combinator, all)¶
Override the native Django method.
- Copied from the base class except for:
combinator_sql += ‘ ALL’ if all else ‘ DISTINCT’
Cloud Spanner requires ALL or DISTINCT.
- get_default_columns(start_alias=None, opts=None, from_parent=None)¶
Compute the default columns for selecting every field in the base model. Will sometimes be called to pull in related models (e.g. via select_related), in which case “opts” and “start_alias” will be given to provide a starting point for the traversal.
Return a list of strings, quoted appropriately for use in SQL directly, as well as a set of aliases used in the select statement (if ‘as_pairs’ is True, return a list of (alias, col_name) pairs instead of strings as the first component and None as the second component).
- get_distinct()¶
Return a quoted list of fields to use in DISTINCT ON part of the query.
This method can alter the tables in the query, and thus it must be called before get_from_clause().
- get_from_clause()¶
Return a list of strings that are joined together to go after the “FROM” part of the query, as well as a list any extra parameters that need to be included. Subclasses, can override this to create a from-clause via a “select”.
This should only be called after any SQL construction methods that might change the tables that are needed. This means the select columns, ordering, and distinct must be done first.
- get_group_by(select, order_by)¶
Return a list of 2-tuples of form (sql, params).
The logic of what exactly the GROUP BY clause contains is hard to describe in other words than “if it passes the test suite, then it is correct”.
- get_order_by()¶
Return a list of 2-tuples of form (expr, (sql, params, is_ref)) for the ORDER BY clause.
The order_by clause can alter the select clause (for example it can add aliases to clauses that do not yet have one, or it can add totally new select clauses).
Fill in the information needed for a select_related query. The current depth is measured as the number of connections away from the root model (for example, cur_depth=1 means we are looking at models with direct connections to the root model).
- get_select()¶
Return three values: - a list of 3-tuples of (expression, (sql, params), alias) - a klass_info structure, - a dictionary of annotations
The (sql, params) is what the expression will produce, and alias is the “AS alias” for the column (possibly None).
The klass_info structure contains the following information: - The base model of the query. - Which columns for that model are present in the query (by
position of the select clause).
related_klass_infos: [f, klass_info] to descent into
The annotations is a dictionary of {‘attname’: column position} values.
- get_select_for_update_of_arguments()¶
Return a quoted list of arguments for the SELECT FOR UPDATE OF part of the query.
- has_results()¶
Backends (e.g. NoSQL) can override this in order to use optimized versions of “query has any results.”
- ordering_parts = <SimpleLazyObject: re.compile('^(.*)\\s(?:ASC|DESC).*', re.MULTILINE|re.DOTALL)>¶
- pre_sql_setup()¶
Do any necessary class setup immediately prior to producing SQL. This is for things that can’t necessarily be done in __init__ because we might not have all the pieces in place at that time.
- quote_name_unless_alias(name)¶
A wrapper around connection.ops.quote_name that doesn’t quote aliases for table names. This avoids problems with some SQL dialects that treat quoted strings specially (e.g. PostgreSQL).
- results_iter(results=None, tuple_expected=False, chunked_fetch=False, chunk_size=100)¶
Return an iterator over the results from executing this query.
- class django_spanner.compiler.SQLCompiler(query, connection, using)[source]¶
A variation of the Django SQL compiler, adjusted for Spanner-specific functionality.
- as_sql(with_limits=True, with_col_aliases=False)[source]¶
Create the SQL for this query. Return the SQL string and list of parameters.
If ‘with_limits’ is False, any limit/offset information is not included in the query.
- deferred_to_columns()[source]¶
Convert the self.deferred_loading data structure to mapping of table names to sets of column names which are to be loaded. Return the dictionary.
- execute_sql(result_type='multi', chunked_fetch=False, chunk_size=100)[source]¶
Run the query against the database and return the result(s). The return value is a single data item if result_type is SINGLE, or an iterator over the results if the result_type is MULTI.
result_type is either MULTI (use fetchmany() to retrieve all rows), SINGLE (only retrieve a single row), or None. In this last case, the cursor is returned if any query is executed, since it’s used by subclasses such as InsertQuery). It’s possible, however, that no query is needed, as the filters describe an empty set. In that case, None is returned, to avoid any unnecessary database interaction.
- find_ordering_name(name, opts, alias=None, default_order='ASC', already_seen=None)[source]¶
Return the table alias (the name might be ambiguous, the alias will not be) and column name for ordering by the given ‘name’ parameter. The ‘name’ is of the form ‘field1__field2__…__fieldN’.
- get_combinator_sql(combinator, all)[source]¶
Override the native Django method.
- Copied from the base class except for:
combinator_sql += ‘ ALL’ if all else ‘ DISTINCT’
Cloud Spanner requires ALL or DISTINCT.
- get_default_columns(start_alias=None, opts=None, from_parent=None)[source]¶
Compute the default columns for selecting every field in the base model. Will sometimes be called to pull in related models (e.g. via select_related), in which case “opts” and “start_alias” will be given to provide a starting point for the traversal.
Return a list of strings, quoted appropriately for use in SQL directly, as well as a set of aliases used in the select statement (if ‘as_pairs’ is True, return a list of (alias, col_name) pairs instead of strings as the first component and None as the second component).
- get_distinct()[source]¶
Return a quoted list of fields to use in DISTINCT ON part of the query.
This method can alter the tables in the query, and thus it must be called before get_from_clause().
- get_from_clause()[source]¶
Return a list of strings that are joined together to go after the “FROM” part of the query, as well as a list any extra parameters that need to be included. Subclasses, can override this to create a from-clause via a “select”.
This should only be called after any SQL construction methods that might change the tables that are needed. This means the select columns, ordering, and distinct must be done first.
- get_group_by(select, order_by)[source]¶
Return a list of 2-tuples of form (sql, params).
The logic of what exactly the GROUP BY clause contains is hard to describe in other words than “if it passes the test suite, then it is correct”.
- get_order_by()[source]¶
Return a list of 2-tuples of form (expr, (sql, params, is_ref)) for the ORDER BY clause.
The order_by clause can alter the select clause (for example it can add aliases to clauses that do not yet have one, or it can add totally new select clauses).
Fill in the information needed for a select_related query. The current depth is measured as the number of connections away from the root model (for example, cur_depth=1 means we are looking at models with direct connections to the root model).
- get_select()[source]¶
Return three values: - a list of 3-tuples of (expression, (sql, params), alias) - a klass_info structure, - a dictionary of annotations
The (sql, params) is what the expression will produce, and alias is the “AS alias” for the column (possibly None).
The klass_info structure contains the following information: - The base model of the query. - Which columns for that model are present in the query (by
position of the select clause).
related_klass_infos: [f, klass_info] to descent into
The annotations is a dictionary of {‘attname’: column position} values.
- get_select_for_update_of_arguments()[source]¶
Return a quoted list of arguments for the SELECT FOR UPDATE OF part of the query.
- has_results()[source]¶
Backends (e.g. NoSQL) can override this in order to use optimized versions of “query has any results.”
- ordering_parts = <SimpleLazyObject: re.compile('^(.*)\\s(?:ASC|DESC).*', re.MULTILINE|re.DOTALL)>¶
- pre_sql_setup()[source]¶
Do any necessary class setup immediately prior to producing SQL. This is for things that can’t necessarily be done in __init__ because we might not have all the pieces in place at that time.
- class django_spanner.compiler.SQLDeleteCompiler(query, connection, using)[source]¶
A wrapper class for compatibility with Django specifications.
- deferred_to_columns()¶
Convert the self.deferred_loading data structure to mapping of table names to sets of column names which are to be loaded. Return the dictionary.
- execute_sql(result_type='multi', chunked_fetch=False, chunk_size=100)¶
Run the query against the database and return the result(s). The return value is a single data item if result_type is SINGLE, or an iterator over the results if the result_type is MULTI.
result_type is either MULTI (use fetchmany() to retrieve all rows), SINGLE (only retrieve a single row), or None. In this last case, the cursor is returned if any query is executed, since it’s used by subclasses such as InsertQuery). It’s possible, however, that no query is needed, as the filters describe an empty set. In that case, None is returned, to avoid any unnecessary database interaction.
- find_ordering_name(name, opts, alias=None, default_order='ASC', already_seen=None)¶
Return the table alias (the name might be ambiguous, the alias will not be) and column name for ordering by the given ‘name’ parameter. The ‘name’ is of the form ‘field1__field2__…__fieldN’.
- get_combinator_sql(combinator, all)¶
Override the native Django method.
- Copied from the base class except for:
combinator_sql += ‘ ALL’ if all else ‘ DISTINCT’
Cloud Spanner requires ALL or DISTINCT.
- get_default_columns(start_alias=None, opts=None, from_parent=None)¶
Compute the default columns for selecting every field in the base model. Will sometimes be called to pull in related models (e.g. via select_related), in which case “opts” and “start_alias” will be given to provide a starting point for the traversal.
Return a list of strings, quoted appropriately for use in SQL directly, as well as a set of aliases used in the select statement (if ‘as_pairs’ is True, return a list of (alias, col_name) pairs instead of strings as the first component and None as the second component).
- get_distinct()¶
Return a quoted list of fields to use in DISTINCT ON part of the query.
This method can alter the tables in the query, and thus it must be called before get_from_clause().
- get_from_clause()¶
Return a list of strings that are joined together to go after the “FROM” part of the query, as well as a list any extra parameters that need to be included. Subclasses, can override this to create a from-clause via a “select”.
This should only be called after any SQL construction methods that might change the tables that are needed. This means the select columns, ordering, and distinct must be done first.
- get_group_by(select, order_by)¶
Return a list of 2-tuples of form (sql, params).
The logic of what exactly the GROUP BY clause contains is hard to describe in other words than “if it passes the test suite, then it is correct”.
- get_order_by()¶
Return a list of 2-tuples of form (expr, (sql, params, is_ref)) for the ORDER BY clause.
The order_by clause can alter the select clause (for example it can add aliases to clauses that do not yet have one, or it can add totally new select clauses).
Fill in the information needed for a select_related query. The current depth is measured as the number of connections away from the root model (for example, cur_depth=1 means we are looking at models with direct connections to the root model).
- get_select()¶
Return three values: - a list of 3-tuples of (expression, (sql, params), alias) - a klass_info structure, - a dictionary of annotations
The (sql, params) is what the expression will produce, and alias is the “AS alias” for the column (possibly None).
The klass_info structure contains the following information: - The base model of the query. - Which columns for that model are present in the query (by
position of the select clause).
related_klass_infos: [f, klass_info] to descent into
The annotations is a dictionary of {‘attname’: column position} values.
- get_select_for_update_of_arguments()¶
Return a quoted list of arguments for the SELECT FOR UPDATE OF part of the query.
- has_results()¶
Backends (e.g. NoSQL) can override this in order to use optimized versions of “query has any results.”
- ordering_parts = <SimpleLazyObject: re.compile('^(.*)\\s(?:ASC|DESC).*', re.MULTILINE|re.DOTALL)>¶
- pre_sql_setup()¶
Do any necessary class setup immediately prior to producing SQL. This is for things that can’t necessarily be done in __init__ because we might not have all the pieces in place at that time.
- quote_name_unless_alias(name)¶
A wrapper around connection.ops.quote_name that doesn’t quote aliases for table names. This avoids problems with some SQL dialects that treat quoted strings specially (e.g. PostgreSQL).
- results_iter(results=None, tuple_expected=False, chunked_fetch=False, chunk_size=100)¶
Return an iterator over the results from executing this query.
- class django_spanner.compiler.SQLInsertCompiler(query, connection, using)[source]¶
A wrapper class for compatibility with Django specifications.
- as_sql()[source]¶
Create the SQL for this query. Return the SQL string and list of parameters.
If ‘with_limits’ is False, any limit/offset information is not included in the query.
- assemble_as_sql(fields, value_rows)[source]¶
Take a sequence of N fields and a sequence of M rows of values, and generate placeholder SQL and parameters for each field and value. Return a pair containing:
a sequence of M rows of N SQL placeholder strings, and
a sequence of M rows of corresponding parameter values.
Each placeholder string may contain any number of ‘%s’ interpolation strings, and each parameter row will contain exactly as many params as the total number of ‘%s’s in the corresponding placeholder row.
- deferred_to_columns()¶
Convert the self.deferred_loading data structure to mapping of table names to sets of column names which are to be loaded. Return the dictionary.
- execute_sql(returning_fields=None)[source]¶
Run the query against the database and return the result(s). The return value is a single data item if result_type is SINGLE, or an iterator over the results if the result_type is MULTI.
result_type is either MULTI (use fetchmany() to retrieve all rows), SINGLE (only retrieve a single row), or None. In this last case, the cursor is returned if any query is executed, since it’s used by subclasses such as InsertQuery). It’s possible, however, that no query is needed, as the filters describe an empty set. In that case, None is returned, to avoid any unnecessary database interaction.
- field_as_sql(field, val)[source]¶
Take a field and a value intended to be saved on that field, and return placeholder SQL and accompanying params. Check for raw values, expressions, and fields with get_placeholder() defined in that order.
When field is None, consider the value raw and use it as the placeholder, with no corresponding parameters returned.
- find_ordering_name(name, opts, alias=None, default_order='ASC', already_seen=None)¶
Return the table alias (the name might be ambiguous, the alias will not be) and column name for ordering by the given ‘name’ parameter. The ‘name’ is of the form ‘field1__field2__…__fieldN’.
- get_combinator_sql(combinator, all)¶
Override the native Django method.
- Copied from the base class except for:
combinator_sql += ‘ ALL’ if all else ‘ DISTINCT’
Cloud Spanner requires ALL or DISTINCT.
- get_default_columns(start_alias=None, opts=None, from_parent=None)¶
Compute the default columns for selecting every field in the base model. Will sometimes be called to pull in related models (e.g. via select_related), in which case “opts” and “start_alias” will be given to provide a starting point for the traversal.
Return a list of strings, quoted appropriately for use in SQL directly, as well as a set of aliases used in the select statement (if ‘as_pairs’ is True, return a list of (alias, col_name) pairs instead of strings as the first component and None as the second component).
- get_distinct()¶
Return a quoted list of fields to use in DISTINCT ON part of the query.
This method can alter the tables in the query, and thus it must be called before get_from_clause().
- get_from_clause()¶
Return a list of strings that are joined together to go after the “FROM” part of the query, as well as a list any extra parameters that need to be included. Subclasses, can override this to create a from-clause via a “select”.
This should only be called after any SQL construction methods that might change the tables that are needed. This means the select columns, ordering, and distinct must be done first.
- get_group_by(select, order_by)¶
Return a list of 2-tuples of form (sql, params).
The logic of what exactly the GROUP BY clause contains is hard to describe in other words than “if it passes the test suite, then it is correct”.
- get_order_by()¶
Return a list of 2-tuples of form (expr, (sql, params, is_ref)) for the ORDER BY clause.
The order_by clause can alter the select clause (for example it can add aliases to clauses that do not yet have one, or it can add totally new select clauses).
Fill in the information needed for a select_related query. The current depth is measured as the number of connections away from the root model (for example, cur_depth=1 means we are looking at models with direct connections to the root model).
- get_select()¶
Return three values: - a list of 3-tuples of (expression, (sql, params), alias) - a klass_info structure, - a dictionary of annotations
The (sql, params) is what the expression will produce, and alias is the “AS alias” for the column (possibly None).
The klass_info structure contains the following information: - The base model of the query. - Which columns for that model are present in the query (by
position of the select clause).
related_klass_infos: [f, klass_info] to descent into
The annotations is a dictionary of {‘attname’: column position} values.
- get_select_for_update_of_arguments()¶
Return a quoted list of arguments for the SELECT FOR UPDATE OF part of the query.
- has_results()¶
Backends (e.g. NoSQL) can override this in order to use optimized versions of “query has any results.”
- ordering_parts = <SimpleLazyObject: re.compile('^(.*)\\s(?:ASC|DESC).*', re.MULTILINE|re.DOTALL)>¶
- pre_save_val(field, obj)[source]¶
Get the given field’s value off the given obj. pre_save() is used for things like auto_now on DateTimeField. Skip it if this is a raw query.
- pre_sql_setup()¶
Do any necessary class setup immediately prior to producing SQL. This is for things that can’t necessarily be done in __init__ because we might not have all the pieces in place at that time.
- prepare_value(field, value)[source]¶
Prepare a value to be used in a query by resolving it if it is an expression and otherwise calling the field’s get_db_prep_save().
- quote_name_unless_alias(name)¶
A wrapper around connection.ops.quote_name that doesn’t quote aliases for table names. This avoids problems with some SQL dialects that treat quoted strings specially (e.g. PostgreSQL).
- results_iter(results=None, tuple_expected=False, chunked_fetch=False, chunk_size=100)¶
Return an iterator over the results from executing this query.
- class django_spanner.compiler.SQLUpdateCompiler(query, connection, using)[source]¶
A wrapper class for compatibility with Django specifications.
- deferred_to_columns()¶
Convert the self.deferred_loading data structure to mapping of table names to sets of column names which are to be loaded. Return the dictionary.
- execute_sql(result_type)[source]¶
Execute the specified update. Return the number of rows affected by the primary update query. The “primary update query” is the first non-empty query that is executed. Row counts for any subsequent, related queries are not available.
- find_ordering_name(name, opts, alias=None, default_order='ASC', already_seen=None)¶
Return the table alias (the name might be ambiguous, the alias will not be) and column name for ordering by the given ‘name’ parameter. The ‘name’ is of the form ‘field1__field2__…__fieldN’.
- get_combinator_sql(combinator, all)¶
Override the native Django method.
- Copied from the base class except for:
combinator_sql += ‘ ALL’ if all else ‘ DISTINCT’
Cloud Spanner requires ALL or DISTINCT.
- get_default_columns(start_alias=None, opts=None, from_parent=None)¶
Compute the default columns for selecting every field in the base model. Will sometimes be called to pull in related models (e.g. via select_related), in which case “opts” and “start_alias” will be given to provide a starting point for the traversal.
Return a list of strings, quoted appropriately for use in SQL directly, as well as a set of aliases used in the select statement (if ‘as_pairs’ is True, return a list of (alias, col_name) pairs instead of strings as the first component and None as the second component).
- get_distinct()¶
Return a quoted list of fields to use in DISTINCT ON part of the query.
This method can alter the tables in the query, and thus it must be called before get_from_clause().
- get_from_clause()¶
Return a list of strings that are joined together to go after the “FROM” part of the query, as well as a list any extra parameters that need to be included. Subclasses, can override this to create a from-clause via a “select”.
This should only be called after any SQL construction methods that might change the tables that are needed. This means the select columns, ordering, and distinct must be done first.
- get_group_by(select, order_by)¶
Return a list of 2-tuples of form (sql, params).
The logic of what exactly the GROUP BY clause contains is hard to describe in other words than “if it passes the test suite, then it is correct”.
- get_order_by()¶
Return a list of 2-tuples of form (expr, (sql, params, is_ref)) for the ORDER BY clause.
The order_by clause can alter the select clause (for example it can add aliases to clauses that do not yet have one, or it can add totally new select clauses).
Fill in the information needed for a select_related query. The current depth is measured as the number of connections away from the root model (for example, cur_depth=1 means we are looking at models with direct connections to the root model).
- get_select()¶
Return three values: - a list of 3-tuples of (expression, (sql, params), alias) - a klass_info structure, - a dictionary of annotations
The (sql, params) is what the expression will produce, and alias is the “AS alias” for the column (possibly None).
The klass_info structure contains the following information: - The base model of the query. - Which columns for that model are present in the query (by
position of the select clause).
related_klass_infos: [f, klass_info] to descent into
The annotations is a dictionary of {‘attname’: column position} values.
- get_select_for_update_of_arguments()¶
Return a quoted list of arguments for the SELECT FOR UPDATE OF part of the query.
- has_results()¶
Backends (e.g. NoSQL) can override this in order to use optimized versions of “query has any results.”
- ordering_parts = <SimpleLazyObject: re.compile('^(.*)\\s(?:ASC|DESC).*', re.MULTILINE|re.DOTALL)>¶
- pre_sql_setup()[source]¶
If the update depends on results from other tables, munge the “where” conditions to match the format required for (portable) SQL updates.
If multiple updates are required, pull out the id values to update at this point so that they don’t change as a result of the progressive updates.
- quote_name_unless_alias(name)¶
A wrapper around connection.ops.quote_name that doesn’t quote aliases for table names. This avoids problems with some SQL dialects that treat quoted strings specially (e.g. PostgreSQL).
- results_iter(results=None, tuple_expected=False, chunked_fetch=False, chunk_size=100)¶
Return an iterator over the results from executing this query.