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
|
|
|
"""
|
|
|
|
Code to manage the creation and SQL rendering of 'where' constraints.
|
|
|
|
"""
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.utils import tree
|
|
|
|
from django.db.models.fields import Field
|
|
|
|
from django.db.models.query_utils import QueryWrapper
|
|
|
|
from datastructures import EmptyResultSet, FullResultSet
|
|
|
|
|
|
|
|
# Connection types
|
|
|
|
AND = 'AND'
|
|
|
|
OR = 'OR'
|
|
|
|
|
2009-01-05 19:47:14 +08:00
|
|
|
class EmptyShortCircuit(Exception):
|
|
|
|
"""
|
|
|
|
Internal exception used to indicate that a "matches nothing" node should be
|
|
|
|
added to the where-clause.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
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 WhereNode(tree.Node):
|
|
|
|
"""
|
|
|
|
Used to represent the SQL where-clause.
|
|
|
|
|
|
|
|
The class is tied to the Query class that created it (in order to create
|
2008-06-12 11:37:13 +08:00
|
|
|
the correct SQL).
|
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 children in this tree are usually either Q-like objects or lists of
|
2008-06-27 11:27:20 +08:00
|
|
|
[table_alias, field_name, db_type, lookup_type, value_annotation,
|
|
|
|
params]. However, a child could also be any class with as_sql() and
|
|
|
|
relabel_aliases() methods.
|
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
|
|
|
"""
|
|
|
|
default = AND
|
|
|
|
|
2008-07-04 14:42:58 +08:00
|
|
|
def add(self, data, connector):
|
|
|
|
"""
|
|
|
|
Add a node to the where-tree. If the data is a list or tuple, it is
|
|
|
|
expected to be of the form (alias, col_name, field_obj, lookup_type,
|
|
|
|
value), which is then slightly munged before being stored (to avoid
|
|
|
|
storing any reference to field objects). Otherwise, the 'data' is
|
|
|
|
stored unchanged and can be anything with an 'as_sql()' method.
|
|
|
|
"""
|
|
|
|
if not isinstance(data, (list, tuple)):
|
|
|
|
super(WhereNode, self).add(data, connector)
|
|
|
|
return
|
|
|
|
|
2009-01-05 19:47:14 +08:00
|
|
|
obj, lookup_type, value = data
|
2009-03-08 11:32:16 +08:00
|
|
|
if hasattr(value, '__iter__') and hasattr(value, 'next'):
|
|
|
|
# Consume any generators immediately, so that we can determine
|
|
|
|
# emptiness and transform any non-empty values correctly.
|
|
|
|
value = list(value)
|
2009-01-05 19:47:14 +08:00
|
|
|
|
|
|
|
# The "annotation" parameter is used to pass auxilliary information
|
|
|
|
# about the value(s) to the query construction. Specifically, datetime
|
|
|
|
# and empty values need special handling. Other types could be used
|
|
|
|
# here in the future (using Python types is suggested for consistency).
|
2008-07-04 14:42:58 +08:00
|
|
|
if isinstance(value, datetime.datetime):
|
|
|
|
annotation = datetime.datetime
|
2009-01-08 13:16:21 +08:00
|
|
|
elif hasattr(value, 'value_annotation'):
|
|
|
|
annotation = value.value_annotation
|
2008-07-04 14:42:58 +08:00
|
|
|
else:
|
|
|
|
annotation = bool(value)
|
2009-01-05 19:47:14 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
if hasattr(obj, "prepare"):
|
|
|
|
value = obj.prepare(lookup_type, value)
|
|
|
|
super(WhereNode, self).add((obj, lookup_type, annotation, value),
|
|
|
|
connector)
|
|
|
|
return
|
|
|
|
|
|
|
|
super(WhereNode, self).add((obj, lookup_type, annotation, value),
|
2009-01-05 19:47:14 +08:00
|
|
|
connector)
|
2008-07-04 14:42:58 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def as_sql(self, qn, connection):
|
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
|
|
|
"""
|
|
|
|
Returns the SQL version of the where clause and the value to be
|
|
|
|
substituted in. Returns None, None if this node is empty.
|
|
|
|
|
|
|
|
If 'node' is provided, that is the root of the SQL generation
|
|
|
|
(generally not needed except by the internal implementation for
|
|
|
|
recursion).
|
|
|
|
"""
|
2008-07-04 14:42:58 +08:00
|
|
|
if not self.children:
|
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
|
|
|
return None, []
|
|
|
|
result = []
|
|
|
|
result_params = []
|
|
|
|
empty = True
|
2008-07-04 14:42:58 +08:00
|
|
|
for child in self.children:
|
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
|
|
|
try:
|
|
|
|
if hasattr(child, 'as_sql'):
|
2009-12-22 23:18:51 +08:00
|
|
|
sql, params = child.as_sql(qn=qn, connection=connection)
|
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
|
|
|
else:
|
2008-07-04 14:42:58 +08:00
|
|
|
# A leaf node in the tree.
|
2009-12-22 23:18:51 +08:00
|
|
|
sql, params = self.make_atom(child, qn, connection)
|
2009-01-29 18:46:36 +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
|
|
|
except EmptyResultSet:
|
2008-07-04 14:42:58 +08:00
|
|
|
if self.connector == AND and not self.negated:
|
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
|
|
|
# We can bail out early in this particular case (only).
|
|
|
|
raise
|
2008-07-04 14:42:58 +08:00
|
|
|
elif self.negated:
|
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
|
|
|
empty = False
|
|
|
|
continue
|
|
|
|
except FullResultSet:
|
|
|
|
if self.connector == OR:
|
2008-07-04 14:42:58 +08:00
|
|
|
if self.negated:
|
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
|
|
|
empty = True
|
|
|
|
break
|
|
|
|
# We match everything. No need for any constraints.
|
|
|
|
return '', []
|
2008-07-04 14:42:58 +08:00
|
|
|
if self.negated:
|
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
|
|
|
empty = True
|
|
|
|
continue
|
2009-01-29 18:46:36 +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
|
|
|
empty = False
|
|
|
|
if sql:
|
2008-07-04 14:42:58 +08:00
|
|
|
result.append(sql)
|
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
|
|
|
result_params.extend(params)
|
|
|
|
if empty:
|
|
|
|
raise EmptyResultSet
|
2008-07-04 14:42:58 +08:00
|
|
|
|
|
|
|
conn = ' %s ' % self.connector
|
|
|
|
sql_string = conn.join(result)
|
|
|
|
if sql_string:
|
|
|
|
if self.negated:
|
|
|
|
sql_string = 'NOT (%s)' % sql_string
|
|
|
|
elif len(self.children) != 1:
|
|
|
|
sql_string = '(%s)' % sql_string
|
|
|
|
return sql_string, result_params
|
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
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def make_atom(self, child, qn, connection):
|
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
|
|
|
"""
|
2008-07-04 14:42:58 +08:00
|
|
|
Turn a tuple (table_alias, column_name, db_type, lookup_type,
|
2008-06-27 11:27:20 +08:00
|
|
|
value_annot, params) into valid SQL.
|
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
|
|
|
|
|
|
|
Returns the string for the SQL fragment and the parameters to use for
|
|
|
|
it.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
lvalue, lookup_type, value_annot, params_or_value = child
|
|
|
|
if hasattr(lvalue, 'process'):
|
|
|
|
try:
|
|
|
|
lvalue, params = lvalue.process(lookup_type, params_or_value, connection)
|
|
|
|
except EmptyShortCircuit:
|
|
|
|
raise EmptyResultSet
|
|
|
|
else:
|
|
|
|
params = Field().get_db_prep_lookup(lookup_type, params_or_value,
|
|
|
|
connection=connection, prepared=True)
|
2009-01-05 19:47:14 +08:00
|
|
|
if isinstance(lvalue, tuple):
|
|
|
|
# A direct database column lookup.
|
2009-12-22 23:18:51 +08:00
|
|
|
field_sql = self.sql_for_columns(lvalue, qn, connection)
|
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
|
|
|
else:
|
2009-01-05 19:47:14 +08:00
|
|
|
# A smart object with an as_sql() method.
|
2009-12-22 23:18:51 +08:00
|
|
|
field_sql = lvalue.as_sql(qn, connection)
|
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
|
|
|
|
2008-06-27 11:27:20 +08:00
|
|
|
if value_annot is datetime.datetime:
|
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
|
|
|
cast_sql = connection.ops.datetime_cast_sql()
|
|
|
|
else:
|
|
|
|
cast_sql = '%s'
|
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
if hasattr(params, 'as_sql'):
|
2009-12-22 23:18:51 +08:00
|
|
|
extra, params = params.as_sql(qn, connection)
|
2009-01-29 18:46:36 +08:00
|
|
|
cast_sql = ''
|
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
|
|
|
else:
|
|
|
|
extra = ''
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
if (len(params) == 1 and params[0] == '' and lookup_type == 'exact'
|
|
|
|
and connection.features.interprets_empty_strings_as_nulls):
|
|
|
|
lookup_type = 'isnull'
|
|
|
|
value_annot = True
|
|
|
|
|
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
|
|
|
if lookup_type in connection.operators:
|
2009-02-08 19:14:56 +08:00
|
|
|
format = "%s %%s %%s" % (connection.ops.lookup_cast(lookup_type),)
|
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
|
|
|
return (format % (field_sql,
|
2009-02-08 19:14:56 +08:00
|
|
|
connection.operators[lookup_type] % cast_sql,
|
|
|
|
extra), params)
|
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
|
|
|
|
|
|
|
if lookup_type == 'in':
|
2008-06-27 11:27:20 +08:00
|
|
|
if not value_annot:
|
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
|
|
|
raise EmptyResultSet
|
|
|
|
if extra:
|
|
|
|
return ('%s IN %s' % (field_sql, extra), params)
|
2008-06-27 11:27:20 +08:00
|
|
|
return ('%s IN (%s)' % (field_sql, ', '.join(['%s'] * len(params))),
|
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
|
|
|
params)
|
|
|
|
elif lookup_type in ('range', 'year'):
|
|
|
|
return ('%s BETWEEN %%s and %%s' % field_sql, params)
|
2009-02-08 13:08:06 +08:00
|
|
|
elif lookup_type in ('month', 'day', 'week_day'):
|
|
|
|
return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, field_sql),
|
|
|
|
params)
|
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
|
|
|
elif lookup_type == 'isnull':
|
2008-06-27 11:27:20 +08:00
|
|
|
return ('%s IS %sNULL' % (field_sql,
|
|
|
|
(not value_annot and 'NOT ' or '')), ())
|
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
|
|
|
elif lookup_type == 'search':
|
|
|
|
return (connection.ops.fulltext_search_sql(field_sql), params)
|
|
|
|
elif lookup_type in ('regex', 'iregex'):
|
|
|
|
return connection.ops.regex_lookup(lookup_type) % (field_sql, cast_sql), params
|
|
|
|
|
|
|
|
raise TypeError('Invalid lookup_type: %r' % lookup_type)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def sql_for_columns(self, data, qn, connection):
|
2009-01-05 19:47:14 +08:00
|
|
|
"""
|
|
|
|
Returns the SQL fragment used for the left-hand side of a column
|
|
|
|
constraint (for example, the "T1.foo" portion in the clause
|
|
|
|
"WHERE ... T1.foo = 6").
|
|
|
|
"""
|
|
|
|
table_alias, name, db_type = data
|
|
|
|
if table_alias:
|
|
|
|
lhs = '%s.%s' % (qn(table_alias), qn(name))
|
|
|
|
else:
|
|
|
|
lhs = qn(name)
|
|
|
|
return connection.ops.field_cast_sql(db_type) % lhs
|
|
|
|
|
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
|
|
|
def relabel_aliases(self, change_map, node=None):
|
|
|
|
"""
|
|
|
|
Relabels the alias values of any children. 'change_map' is a dictionary
|
|
|
|
mapping old (current) alias values to the new values.
|
|
|
|
"""
|
|
|
|
if not node:
|
|
|
|
node = self
|
|
|
|
for pos, child in enumerate(node.children):
|
|
|
|
if hasattr(child, 'relabel_aliases'):
|
|
|
|
child.relabel_aliases(change_map)
|
|
|
|
elif isinstance(child, tree.Node):
|
|
|
|
self.relabel_aliases(change_map, child)
|
|
|
|
else:
|
2009-02-23 22:47:59 +08:00
|
|
|
if isinstance(child[0], (list, tuple)):
|
|
|
|
elt = list(child[0])
|
|
|
|
if elt[0] in change_map:
|
|
|
|
elt[0] = change_map[elt[0]]
|
|
|
|
node.children[pos] = (tuple(elt),) + child[1:]
|
|
|
|
else:
|
|
|
|
child[0].relabel_aliases(change_map)
|
|
|
|
|
2009-01-29 18:46:36 +08:00
|
|
|
# Check if the query value also requires relabelling
|
|
|
|
if hasattr(child[3], 'relabel_aliases'):
|
|
|
|
child[3].relabel_aliases(change_map)
|
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 EverythingNode(object):
|
|
|
|
"""
|
|
|
|
A node that matches everything.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def as_sql(self, qn=None, connection=None):
|
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
|
|
|
raise FullResultSet
|
|
|
|
|
|
|
|
def relabel_aliases(self, change_map, node=None):
|
|
|
|
return
|
2008-07-23 14:12:15 +08:00
|
|
|
|
|
|
|
class NothingNode(object):
|
|
|
|
"""
|
|
|
|
A node that matches nothing.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
def as_sql(self, qn=None, connection=None):
|
2008-07-23 14:12:15 +08:00
|
|
|
raise EmptyResultSet
|
|
|
|
|
|
|
|
def relabel_aliases(self, change_map, node=None):
|
|
|
|
return
|
|
|
|
|
2009-01-05 19:47:14 +08:00
|
|
|
class Constraint(object):
|
|
|
|
"""
|
|
|
|
An object that can be passed to WhereNode.add() and knows how to
|
|
|
|
pre-process itself prior to including in the WhereNode.
|
|
|
|
"""
|
|
|
|
def __init__(self, alias, col, field):
|
|
|
|
self.alias, self.col, self.field = alias, col, field
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def prepare(self, lookup_type, value):
|
|
|
|
if self.field:
|
|
|
|
return self.field.get_prep_lookup(lookup_type, value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def process(self, lookup_type, value, connection):
|
2009-01-05 19:47:14 +08:00
|
|
|
"""
|
|
|
|
Returns a tuple of data suitable for inclusion in a WhereNode
|
|
|
|
instance.
|
|
|
|
"""
|
|
|
|
# Because of circular imports, we need to import this here.
|
|
|
|
from django.db.models.base import ObjectDoesNotExist
|
|
|
|
try:
|
|
|
|
if self.field:
|
2009-12-22 23:18:51 +08:00
|
|
|
params = self.field.get_db_prep_lookup(lookup_type, value,
|
|
|
|
connection=connection, prepared=True)
|
|
|
|
db_type = self.field.db_type(connection=connection)
|
2009-01-05 19:47:14 +08:00
|
|
|
else:
|
|
|
|
# This branch is used at times when we add a comparison to NULL
|
|
|
|
# (we don't really want to waste time looking up the associated
|
|
|
|
# field object at the calling location).
|
2009-12-22 23:18:51 +08:00
|
|
|
params = Field().get_db_prep_lookup(lookup_type, value,
|
|
|
|
connection=connection, prepared=True)
|
2009-01-05 19:47:14 +08:00
|
|
|
db_type = None
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise EmptyShortCircuit
|
|
|
|
|
|
|
|
return (self.alias, self.col, db_type), params
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def relabel_aliases(self, change_map):
|
|
|
|
if self.alias in change_map:
|
|
|
|
self.alias = change_map[self.alias]
|