Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
2014-04-27 01:18:45 +08:00
|
|
|
Useful auxiliary data structures for query construction. Not useful outside
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
the SQL domain.
|
|
|
|
"""
|
2016-05-26 03:00:20 +08:00
|
|
|
# for backwards-compatibility in Django 1.11
|
|
|
|
from django.core.exceptions import EmptyResultSet # NOQA: F401
|
2014-11-17 16:26:10 +08:00
|
|
|
from django.db.models.sql.constants import INNER, LOUTER
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
class MultiJoin(Exception):
|
|
|
|
"""
|
|
|
|
Used by join construction code to indicate the point at which a
|
|
|
|
multi-valued join was attempted (if the caller wants to treat that
|
|
|
|
exceptionally).
|
|
|
|
"""
|
2013-02-18 07:56:24 +08:00
|
|
|
def __init__(self, names_pos, path_with_names):
|
|
|
|
self.level = names_pos
|
|
|
|
# The path travelled, this includes the path to the multijoin.
|
|
|
|
self.names_with_path = path_with_names
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Empty:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
pass
|
2014-11-17 16:26:10 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Join:
|
2014-11-17 16:26:10 +08:00
|
|
|
"""
|
|
|
|
Used by sql.Query and sql.SQLCompiler to generate JOIN clauses into the
|
|
|
|
FROM entry. For example, the SQL generated could be
|
|
|
|
LEFT OUTER JOIN "sometable" T1 ON ("othertable"."sometable_id" = "sometable"."id")
|
|
|
|
|
|
|
|
This class is primarily used in Query.alias_map. All entries in alias_map
|
|
|
|
must be Join compatible by providing the following attributes and methods:
|
|
|
|
- table_name (string)
|
|
|
|
- table_alias (possible alias for the table, can be None)
|
|
|
|
- join_type (can be None for those entries that aren't joined from
|
|
|
|
anything)
|
|
|
|
- parent_alias (which table is this join's parent, can be None similarly
|
|
|
|
to join_type)
|
|
|
|
- as_sql()
|
|
|
|
- relabeled_clone()
|
|
|
|
"""
|
|
|
|
def __init__(self, table_name, parent_alias, table_alias, join_type,
|
2017-09-22 23:53:17 +08:00
|
|
|
join_field, nullable, filtered_relation=None):
|
2014-11-17 16:26:10 +08:00
|
|
|
# Join table
|
|
|
|
self.table_name = table_name
|
|
|
|
self.parent_alias = parent_alias
|
|
|
|
# Note: table_alias is not necessarily known at instantiation time.
|
|
|
|
self.table_alias = table_alias
|
|
|
|
# LOUTER or INNER
|
|
|
|
self.join_type = join_type
|
|
|
|
# A list of 2-tuples to use in the ON clause of the JOIN.
|
|
|
|
# Each 2-tuple will create one join condition in the ON clause.
|
|
|
|
self.join_cols = join_field.get_joining_columns()
|
2013-11-09 20:25:15 +08:00
|
|
|
# Along which field (or ForeignObjectRel in the reverse join case)
|
2014-11-17 16:26:10 +08:00
|
|
|
self.join_field = join_field
|
|
|
|
# Is this join nullabled?
|
|
|
|
self.nullable = nullable
|
2017-09-22 23:53:17 +08:00
|
|
|
self.filtered_relation = filtered_relation
|
2014-11-17 16:26:10 +08:00
|
|
|
|
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Generate the full
|
2014-11-17 16:26:10 +08:00
|
|
|
LEFT OUTER JOIN sometable ON sometable.somecol = othertable.othercol, params
|
|
|
|
clause for this join.
|
|
|
|
"""
|
2015-07-10 15:52:22 +08:00
|
|
|
join_conditions = []
|
2014-11-17 16:26:10 +08:00
|
|
|
params = []
|
|
|
|
qn = compiler.quote_name_unless_alias
|
|
|
|
qn2 = connection.ops.quote_name
|
2015-07-10 15:52:22 +08:00
|
|
|
|
|
|
|
# Add a join condition for each pair of joining columns.
|
2018-08-14 06:41:24 +08:00
|
|
|
for lhs_col, rhs_col in self.join_cols:
|
2015-07-10 15:52:22 +08:00
|
|
|
join_conditions.append('%s.%s = %s.%s' % (
|
2014-11-17 16:26:10 +08:00
|
|
|
qn(self.parent_alias),
|
|
|
|
qn2(lhs_col),
|
|
|
|
qn(self.table_alias),
|
|
|
|
qn2(rhs_col),
|
|
|
|
))
|
2015-07-10 15:52:22 +08:00
|
|
|
|
|
|
|
# Add a single condition inside parentheses for whatever
|
|
|
|
# get_extra_restriction() returns.
|
2014-11-17 16:26:10 +08:00
|
|
|
extra_cond = self.join_field.get_extra_restriction(
|
|
|
|
compiler.query.where_class, self.table_alias, self.parent_alias)
|
|
|
|
if extra_cond:
|
|
|
|
extra_sql, extra_params = compiler.compile(extra_cond)
|
2015-07-10 15:52:22 +08:00
|
|
|
join_conditions.append('(%s)' % extra_sql)
|
2014-11-17 16:26:10 +08:00
|
|
|
params.extend(extra_params)
|
2017-09-22 23:53:17 +08:00
|
|
|
if self.filtered_relation:
|
|
|
|
extra_sql, extra_params = compiler.compile(self.filtered_relation)
|
|
|
|
if extra_sql:
|
|
|
|
join_conditions.append('(%s)' % extra_sql)
|
|
|
|
params.extend(extra_params)
|
2015-07-10 15:52:22 +08:00
|
|
|
if not join_conditions:
|
|
|
|
# This might be a rel on the other end of an actual declared field.
|
|
|
|
declared_field = getattr(self.join_field, 'field', self.join_field)
|
|
|
|
raise ValueError(
|
|
|
|
"Join generated an empty ON clause. %s did not yield either "
|
|
|
|
"joining columns or extra restrictions." % declared_field.__class__
|
|
|
|
)
|
|
|
|
on_clause_sql = ' AND '.join(join_conditions)
|
|
|
|
alias_str = '' if self.table_alias == self.table_name else (' %s' % self.table_alias)
|
|
|
|
sql = '%s %s%s ON (%s)' % (self.join_type, qn(self.table_name), alias_str, on_clause_sql)
|
|
|
|
return sql, params
|
2014-11-17 16:26:10 +08:00
|
|
|
|
|
|
|
def relabeled_clone(self, change_map):
|
|
|
|
new_parent_alias = change_map.get(self.parent_alias, self.parent_alias)
|
|
|
|
new_table_alias = change_map.get(self.table_alias, self.table_alias)
|
2017-09-22 23:53:17 +08:00
|
|
|
if self.filtered_relation is not None:
|
|
|
|
filtered_relation = self.filtered_relation.clone()
|
|
|
|
filtered_relation.path = [change_map.get(p, p) for p in self.filtered_relation.path]
|
|
|
|
else:
|
|
|
|
filtered_relation = None
|
2014-11-17 16:26:10 +08:00
|
|
|
return self.__class__(
|
|
|
|
self.table_name, new_parent_alias, new_table_alias, self.join_type,
|
2017-09-22 23:53:17 +08:00
|
|
|
self.join_field, self.nullable, filtered_relation=filtered_relation,
|
|
|
|
)
|
|
|
|
|
|
|
|
def equals(self, other, with_filtered_relation):
|
|
|
|
return (
|
|
|
|
isinstance(other, self.__class__) and
|
|
|
|
self.table_name == other.table_name and
|
|
|
|
self.parent_alias == other.parent_alias and
|
|
|
|
self.join_field == other.join_field and
|
|
|
|
(not with_filtered_relation or self.filtered_relation == other.filtered_relation)
|
|
|
|
)
|
2014-11-17 16:26:10 +08:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2017-09-22 23:53:17 +08:00
|
|
|
return self.equals(other, with_filtered_relation=True)
|
2014-11-17 16:26:10 +08:00
|
|
|
|
|
|
|
def demote(self):
|
|
|
|
new = self.relabeled_clone({})
|
|
|
|
new.join_type = INNER
|
|
|
|
return new
|
|
|
|
|
|
|
|
def promote(self):
|
|
|
|
new = self.relabeled_clone({})
|
|
|
|
new.join_type = LOUTER
|
|
|
|
return new
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class BaseTable:
|
2014-11-17 16:26:10 +08:00
|
|
|
"""
|
|
|
|
The BaseTable class is used for base table references in FROM clause. For
|
|
|
|
example, the SQL "foo" in
|
|
|
|
SELECT * FROM "foo" WHERE somecond
|
|
|
|
could be generated by this class.
|
|
|
|
"""
|
|
|
|
join_type = None
|
|
|
|
parent_alias = None
|
2017-09-22 23:53:17 +08:00
|
|
|
filtered_relation = None
|
2014-11-17 16:26:10 +08:00
|
|
|
|
|
|
|
def __init__(self, table_name, alias):
|
|
|
|
self.table_name = table_name
|
|
|
|
self.table_alias = alias
|
|
|
|
|
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
alias_str = '' if self.table_alias == self.table_name else (' %s' % self.table_alias)
|
|
|
|
base_sql = compiler.quote_name_unless_alias(self.table_name)
|
|
|
|
return base_sql + alias_str, []
|
|
|
|
|
|
|
|
def relabeled_clone(self, change_map):
|
|
|
|
return self.__class__(self.table_name, change_map.get(self.table_alias, self.table_alias))
|
2017-09-22 23:53:17 +08:00
|
|
|
|
|
|
|
def equals(self, other, with_filtered_relation):
|
|
|
|
return (
|
|
|
|
isinstance(self, other.__class__) and
|
|
|
|
self.table_name == other.table_name and
|
|
|
|
self.table_alias == other.table_alias
|
|
|
|
)
|