class documentation

class SQLCompiler:

Known subclasses: django.db.backends.mysql.compiler.SQLCompiler, django.db.models.sql.compiler.SQLAggregateCompiler, django.db.models.sql.compiler.SQLDeleteCompiler, django.db.models.sql.compiler.SQLInsertCompiler, django.db.models.sql.compiler.SQLUpdateCompiler

View In Hierarchy

Undocumented

Method __init__ Undocumented
Method ​_order​_by​_pairs Undocumented
Method ​_setup​_joins Helper method for get_order_by() and get_distinct().
Method apply​_converters Undocumented
Method as​_sql Create the SQL for this query. Return the SQL string and list of parameters.
Method as​_subquery​_condition Undocumented
Method collapse​_group​_by Undocumented
Method compile Undocumented
Method 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.
Method execute​_sql 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.
Method explain​_query Undocumented
Method find​_ordering​_name 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'.
Method get​_combinator​_sql Undocumented
Method get​_converters Undocumented
Method get​_default​_columns No summary
Method get​_distinct Return a quoted list of fields to use in DISTINCT ON part of the query.
Method get​_extra​_select Undocumented
Method get​_from​_clause No summary
Method get​_group​_by Return a list of 2-tuples of form (sql, params).
Method get​_order​_by Return a list of 2-tuples of the form (expr, (sql, params, is_ref)) for the ORDER BY clause.
Method get​_related​_selections No summary
Method get​_select No summary
Method get​_select​_for​_update​_of​_arguments Return a quoted list of arguments for the SELECT FOR UPDATE OF part of the query.
Method has​_results Backends (e.g. NoSQL) can override this in order to use optimized versions of "query has any results."
Method 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.
Method quote​_name​_unless​_alias 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).
Method results​_iter Return an iterator over the results from executing this query.
Method setup​_query Undocumented
Class Variable ordering​_parts Undocumented
Instance Variable ​_meta​_ordering Undocumented
Instance Variable annotation​_col​_map Undocumented
Instance Variable col​_count Undocumented
Instance Variable connection Undocumented
Instance Variable elide​_empty Undocumented
Instance Variable has​_extra​_select Undocumented
Instance Variable having Undocumented
Instance Variable klass​_info Undocumented
Instance Variable query Undocumented
Instance Variable quote​_cache Undocumented
Instance Variable select Undocumented
Instance Variable using Undocumented
Instance Variable where Undocumented
def __init__(self, query, connection, using, elide_empty=True):

Undocumented

def _order_by_pairs(self):

Undocumented

def _setup_joins(self, pieces, opts, alias):

Helper method for get_order_by() and get_distinct().

get_ordering() and get_distinct() must produce same target columns on same input, as the prefixes of get_ordering() and get_distinct() must match. Executing SQL where this is not true is an error.

def apply_converters(self, rows, converters):

Undocumented

def as_sql(self, with_limits=True, with_col_aliases=False):
def as_subquery_condition(self, alias, columns, compiler):
def collapse_group_by(self, expressions, having):

Undocumented

def compile(self, node):

Undocumented

def deferred_to_columns(self):
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.
def execute_sql(self, result_type=MULTI, chunked_fetch=False, chunk_size=GET_ITERATOR_CHUNK_SIZE):

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.

def explain_query(self):

Undocumented

def find_ordering_name(self, 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'.
def get_combinator_sql(self, combinator, all):

Undocumented

def get_converters(self, expressions):

Undocumented

def get_default_columns(self, 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).

def get_distinct(self):

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().

def get_extra_select(self, order_by, select):

Undocumented

def get_from_clause(self):

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.

def get_group_by(self, 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".

def get_order_by(self):

Return a list of 2-tuples of the 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).

def get_related_selections(self, select, opts=None, root_alias=None, cur_depth=1, requested=None, restricted=None):
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).
def get_select(self):

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.

def get_select_for_update_of_arguments(self):
Return a quoted list of arguments for the SELECT FOR UPDATE OF part of the query.
def has_results(self):
Backends (e.g. NoSQL) can override this in order to use optimized versions of "query has any results."
def pre_sql_setup(self):
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.
def quote_name_unless_alias(self, 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).
def results_iter(self, results=None, tuple_expected=False, chunked_fetch=False, chunk_size=GET_ITERATOR_CHUNK_SIZE):
Return an iterator over the results from executing this query.
def setup_query(self):

Undocumented

ordering_parts =

Undocumented

_meta_ordering =

Undocumented

annotation_col_map =

Undocumented

col_count =
connection =

Undocumented

elide_empty =

Undocumented

has_extra_select =

Undocumented

having =

Undocumented

klass_info =

Undocumented

query =

Undocumented

quote_cache: dict[str, str] =

Undocumented

select =

Undocumented

using =

Undocumented

where =

Undocumented