Ticket #13835: django-trunk-rev13294-for-johnny-cache.patch
File django-trunk-rev13294-for-johnny-cache.patch, 7.8 KB (added by , 14 years ago) |
---|
-
django/contrib/gis/db/models/sql/where.py
44 44 lvalue, lookup_type, value_annot, params_or_value = child 45 45 if isinstance(lvalue, GeoConstraint): 46 46 data, params = lvalue.process(lookup_type, params_or_value, connection) 47 tables = [] 48 if hasattr(params, 'tables'): 49 tables.extend(params.tables) 47 50 spatial_sql = connection.ops.spatial_lookup_sql(data, lookup_type, params_or_value, lvalue.field, qn) 48 return spatial_sql, params 51 return spatial_sql, params, tables 49 52 else: 50 53 return super(GeoWhereNode, self).make_atom(child, qn, connection) 51 54 -
django/db/models/query_utils.py
133 133 A type that indicates the contents are an SQL fragment and the associate 134 134 parameters. Can be used to pass opaque data to a where-clause, for example. 135 135 """ 136 def __init__(self, sql, params ):136 def __init__(self, sql, params, tables): 137 137 self.data = sql, params 138 self.tables = tables 138 139 139 140 def as_sql(self, qn=None, connection=None): 140 141 return self.data -
django/db/models/fields/__init__.py
315 315 sql, params = value.as_sql() 316 316 else: 317 317 sql, params = value._as_sql(connection=connection) 318 return QueryWrapper(('(%s)' % sql), params) 318 tables = [] 319 if hasattr(value, 'query'): 320 tables = value.query.tables 321 return QueryWrapper(('(%s)' % sql), params, tables) 319 322 320 323 if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'): 321 324 return [value] -
django/db/models/fields/related.py
152 152 sql, params = value.as_sql() 153 153 else: 154 154 sql, params = value._as_sql(connection=connection) 155 return QueryWrapper(('(%s)' % sql), params) 155 tables = [] 156 if hasattr(value, 'query'): 157 tables = value.query.tables 158 return QueryWrapper(('(%s)' % sql), params, tables) 156 159 157 160 # FIXME: lt and gt are explicitly allowed to make 158 161 # get_(next/prev)_by_date work; other lookups are not allowed since that -
django/db/models/sql/where.py
84 84 return None, [] 85 85 result = [] 86 86 result_params = [] 87 result_tables = [] 87 88 empty = True 88 89 for child in self.children: 89 90 try: 91 # child can be 'WhereNode' (or Nothing/Everything Node) or tuple 92 # tuples do not have as_sql method, and theirs tables are 93 # processed in make_atom 94 # WhereNode provides tables in result_tables parameter 95 # constructed in the 'as_sql' method call 96 # NothingNode and EverythingNode do not provide result_tables 90 97 if hasattr(child, 'as_sql'): 91 98 sql, params = child.as_sql(qn=qn, connection=connection) 99 if hasattr(child, 'result_tables'): 100 tables = child.result_tables 92 101 else: 93 102 # A leaf node in the tree. 94 sql, params = self.make_atom(child, qn, connection)103 sql, params, tables = self.make_atom(child, qn, connection) 95 104 96 105 except EmptyResultSet: 97 106 if self.connector == AND and not self.negated: … … 115 124 if sql: 116 125 result.append(sql) 117 126 result_params.extend(params) 127 result_tables.extend(tables) 118 128 if empty: 119 129 raise EmptyResultSet 120 130 … … 125 135 sql_string = 'NOT (%s)' % sql_string 126 136 elif len(self.children) != 1: 127 137 sql_string = '(%s)' % sql_string 138 self.result_tables = result_tables 128 139 return sql_string, result_params 129 140 130 141 def make_atom(self, child, qn, connection): … … 136 147 it. 137 148 """ 138 149 lvalue, lookup_type, value_annot, params_or_value = child 150 tables = [] 139 151 if hasattr(lvalue, 'process'): 140 152 try: 141 153 lvalue, params = lvalue.process(lookup_type, params_or_value, connection) … … 150 162 else: 151 163 # A smart object with an as_sql() method. 152 164 field_sql = lvalue.as_sql(qn, connection) 165 # lvalue with as_sql() method is an aggregate object 166 # (django.db.models.sql.aggregates.*) 167 # its join tables should appear in query.tables 153 168 154 169 if value_annot is datetime.datetime: 155 170 cast_sql = connection.ops.datetime_cast_sql() … … 157 172 cast_sql = '%s' 158 173 159 174 if hasattr(params, 'as_sql'): 175 # params with an as_sql method is a QueryWrapper or SQLEvaluator instance 176 # join tables of SQLEvaluator should appear in query.tables 177 # join tables of QueryWrapper are stored as tables instance variable 178 if hasattr(params, 'tables'): 179 tables.extend(params.tables) 160 180 extra, params = params.as_sql(qn, connection) 161 181 cast_sql = '' 162 182 else: … … 171 191 format = "%s %%s %%s" % (connection.ops.lookup_cast(lookup_type),) 172 192 return (format % (field_sql, 173 193 connection.operators[lookup_type] % cast_sql, 174 extra), params )194 extra), params, tables) 175 195 176 196 if lookup_type == 'in': 177 197 if not value_annot: 178 198 raise EmptyResultSet 179 199 if extra: 180 return ('%s IN %s' % (field_sql, extra), params )200 return ('%s IN %s' % (field_sql, extra), params, tables) 181 201 return ('%s IN (%s)' % (field_sql, ', '.join(['%s'] * len(params))), 182 params )202 params, tables) 183 203 elif lookup_type in ('range', 'year'): 184 return ('%s BETWEEN %%s and %%s' % field_sql, params )204 return ('%s BETWEEN %%s and %%s' % field_sql, params, tables) 185 205 elif lookup_type in ('month', 'day', 'week_day'): 186 206 return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, field_sql), 187 params )207 params, tables) 188 208 elif lookup_type == 'isnull': 189 209 return ('%s IS %sNULL' % (field_sql, 190 (not value_annot and 'NOT ' or '')), () )210 (not value_annot and 'NOT ' or '')), (), tables) 191 211 elif lookup_type == 'search': 192 return (connection.ops.fulltext_search_sql(field_sql), params )212 return (connection.ops.fulltext_search_sql(field_sql), params, tables) 193 213 elif lookup_type in ('regex', 'iregex'): 194 return connection.ops.regex_lookup(lookup_type) % (field_sql, cast_sql), params 214 return connection.ops.regex_lookup(lookup_type) % (field_sql, cast_sql), params, tables 195 215 196 216 raise TypeError('Invalid lookup_type: %r' % lookup_type) 197 217