1
0
mirror of https://github.com/django/django.git synced 2025-07-06 18:59:13 +00:00

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
This commit is contained in:
Malcolm Tredinnick 2007-10-14 02:16:57 +00:00
parent 0ebb752e89
commit 90f2893aea
2 changed files with 17 additions and 6 deletions

View File

@ -104,6 +104,16 @@ class Query(object):
sql, params = self.as_sql() sql, params = self.as_sql()
return sql % params 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): def clone(self, klass=None, **kwargs):
""" """
Creates a copy of the current instance. The 'kwargs' parameter can be Creates a copy of the current instance. The 'kwargs' parameter can be
@ -119,6 +129,7 @@ class Query(object):
obj.select = self.select[:] obj.select = self.select[:]
obj.tables = self.tables[:] obj.tables = self.tables[:]
obj.where = copy.deepcopy(self.where) obj.where = copy.deepcopy(self.where)
obj.where.query = obj
obj.having = self.having[:] obj.having = self.having[:]
obj.group_by = self.group_by[:] obj.group_by = self.group_by[:]
obj.order_by = self.order_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 columns have been specified, returns all columns relating to fields in
the model. the model.
""" """
qn = self.connection.ops.quote_name qn = self.quote_name_unless_alias
result = [] result = []
if self.select or self.extra_select: if self.select or self.extra_select:
for col in self.select: for col in self.select:
@ -348,7 +359,7 @@ class Query(object):
a "select", for example (e.g. CountQuery). a "select", for example (e.g. CountQuery).
""" """
result = [] result = []
qn = self.connection.ops.quote_name qn = self.quote_name_unless_alias
for alias in self.tables: for alias in self.tables:
if not self.alias_map[alias][ALIAS_REFCOUNT]: if not self.alias_map[alias][ALIAS_REFCOUNT]:
continue continue
@ -368,7 +379,7 @@ class Query(object):
""" """
Returns a tuple representing the SQL elements in the "group by" clause. 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 = [] result = []
for col in self.group_by: for col in self.group_by:
if isinstance(col, (list, tuple)): if isinstance(col, (list, tuple)):
@ -384,7 +395,7 @@ class Query(object):
Returns a tuple representing the SQL elements in the "order by" clause. Returns a tuple representing the SQL elements in the "order by" clause.
""" """
ordering = self.order_by or self.model._meta.ordering 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 opts = self.model._meta
result = [] result = []
for field in ordering: for field in ordering:
@ -942,7 +953,7 @@ class UpdateQuery(Query):
"Can only update one table at a time." "Can only update one table at a time."
result = ['UPDATE %s' % self.tables[0]] result = ['UPDATE %s' % self.tables[0]]
result.append('SET') 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] values = ['%s = %s' % (qn(v[0]), v[1]) for v in self.values]
result.append(', '.join(values)) result.append(', '.join(values))
where, params = self.where.as_sql() where, params = self.where.as_sql()

View File

@ -91,7 +91,7 @@ class WhereNode(tree.Node):
""" """
table_alias, name, field, lookup_type, value = child table_alias, name, field, lookup_type, value = child
conn = self.query.connection conn = self.query.connection
qn = conn.ops.quote_name qn = self.query.quote_name_unless_alias
if table_alias: if table_alias:
lhs = '%s.%s' % (qn(table_alias), qn(name)) lhs = '%s.%s' % (qn(table_alias), qn(name))
else: else: