summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-10-14 02:16:57 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-10-14 02:16:57 +0000
commit90f2893aea6dc943c3dca88bec3c5195fb79fa03 (patch)
tree0a405a33e01bdd2ec19f09c79c8307db2f85ee87
parent0ebb752e89b312436b424065da05f2b1f8e22a22 (diff)
queryset-refactor: Fixed some quoting problems with aliases.
These showed up for PostgreSQL, but this approach is a bit more robust in general, I suspect. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@6498 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/models/sql/query.py21
-rw-r--r--django/db/models/sql/where.py2
2 files changed, 17 insertions, 6 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index dbbe47f23a..c6a6997118 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -104,6 +104,16 @@ class Query(object):
sql, params = self.as_sql()
return sql % params
+ 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).
+ """
+ if name != self.alias_map.get(name, [name])[0]:
+ return name
+ return self.connection.ops.quote_name(name)
+
def clone(self, klass=None, **kwargs):
"""
Creates a copy of the current instance. The 'kwargs' parameter can be
@@ -119,6 +129,7 @@ class Query(object):
obj.select = self.select[:]
obj.tables = self.tables[:]
obj.where = copy.deepcopy(self.where)
+ obj.where.query = obj
obj.having = self.having[:]
obj.group_by = self.group_by[:]
obj.order_by = self.order_by[:]
@@ -319,7 +330,7 @@ class Query(object):
columns have been specified, returns all columns relating to fields in
the model.
"""
- qn = self.connection.ops.quote_name
+ qn = self.quote_name_unless_alias
result = []
if self.select or self.extra_select:
for col in self.select:
@@ -348,7 +359,7 @@ class Query(object):
a "select", for example (e.g. CountQuery).
"""
result = []
- qn = self.connection.ops.quote_name
+ qn = self.quote_name_unless_alias
for alias in self.tables:
if not self.alias_map[alias][ALIAS_REFCOUNT]:
continue
@@ -368,7 +379,7 @@ class Query(object):
"""
Returns a tuple representing the SQL elements in the "group by" clause.
"""
- qn = self.connection.ops.quote_name
+ qn = self.quote_name_unless_alias
result = []
for col in self.group_by:
if isinstance(col, (list, tuple)):
@@ -384,7 +395,7 @@ class Query(object):
Returns a tuple representing the SQL elements in the "order by" clause.
"""
ordering = self.order_by or self.model._meta.ordering
- qn = self.connection.ops.quote_name
+ qn = self.quote_name_unless_alias
opts = self.model._meta
result = []
for field in ordering:
@@ -942,7 +953,7 @@ class UpdateQuery(Query):
"Can only update one table at a time."
result = ['UPDATE %s' % self.tables[0]]
result.append('SET')
- qn = self.connection.ops.quote_name
+ qn = self.quote_name_unless_alias
values = ['%s = %s' % (qn(v[0]), v[1]) for v in self.values]
result.append(', '.join(values))
where, params = self.where.as_sql()
diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py
index 4058752ed6..eb50639a3a 100644
--- a/django/db/models/sql/where.py
+++ b/django/db/models/sql/where.py
@@ -91,7 +91,7 @@ class WhereNode(tree.Node):
"""
table_alias, name, field, lookup_type, value = child
conn = self.query.connection
- qn = conn.ops.quote_name
+ qn = self.query.quote_name_unless_alias
if table_alias:
lhs = '%s.%s' % (qn(table_alias), qn(name))
else: