Ticket #13835: django-trunk-rev13294-for-johnny-cache.patch

File django-trunk-rev13294-for-johnny-cache.patch, 7.8 KB (added by fetzig, 14 years ago)
  • django/contrib/gis/db/models/sql/where.py

     
    4444        lvalue, lookup_type, value_annot, params_or_value = child
    4545        if isinstance(lvalue, GeoConstraint):
    4646            data, params = lvalue.process(lookup_type, params_or_value, connection)
     47            tables = []
     48            if hasattr(params, 'tables'):
     49                tables.extend(params.tables)
    4750            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
    4952        else:
    5053            return super(GeoWhereNode, self).make_atom(child, qn, connection)
    5154
  • django/db/models/query_utils.py

     
    133133    A type that indicates the contents are an SQL fragment and the associate
    134134    parameters. Can be used to pass opaque data to a where-clause, for example.
    135135    """
    136     def __init__(self, sql, params):
     136    def __init__(self, sql, params, tables):
    137137        self.data = sql, params
     138        self.tables = tables
    138139
    139140    def as_sql(self, qn=None, connection=None):
    140141        return self.data
  • django/db/models/fields/__init__.py

     
    315315                sql, params = value.as_sql()
    316316            else:
    317317                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)
    319322
    320323        if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'):
    321324            return [value]
  • django/db/models/fields/related.py

     
    152152                sql, params = value.as_sql()
    153153            else:
    154154                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)
    156159
    157160        # FIXME: lt and gt are explicitly allowed to make
    158161        # get_(next/prev)_by_date work; other lookups are not allowed since that
  • django/db/models/sql/where.py

     
    8484            return None, []
    8585        result = []
    8686        result_params = []
     87        result_tables = []
    8788        empty = True
    8889        for child in self.children:
    8990            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
    9097                if hasattr(child, 'as_sql'):
    9198                    sql, params = child.as_sql(qn=qn, connection=connection)
     99                    if hasattr(child, 'result_tables'):
     100                        tables = child.result_tables
    92101                else:
    93102                    # 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)
    95104
    96105            except EmptyResultSet:
    97106                if self.connector == AND and not self.negated:
     
    115124            if sql:
    116125                result.append(sql)
    117126                result_params.extend(params)
     127                result_tables.extend(tables)
    118128        if empty:
    119129            raise EmptyResultSet
    120130
     
    125135                sql_string = 'NOT (%s)' % sql_string
    126136            elif len(self.children) != 1:
    127137                sql_string = '(%s)' % sql_string
     138        self.result_tables = result_tables
    128139        return sql_string, result_params
    129140
    130141    def make_atom(self, child, qn, connection):
     
    136147        it.
    137148        """
    138149        lvalue, lookup_type, value_annot, params_or_value = child
     150        tables = []
    139151        if hasattr(lvalue, 'process'):
    140152            try:
    141153                lvalue, params = lvalue.process(lookup_type, params_or_value, connection)
     
    150162        else:
    151163            # A smart object with an as_sql() method.
    152164            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
    153168
    154169        if value_annot is datetime.datetime:
    155170            cast_sql = connection.ops.datetime_cast_sql()
     
    157172            cast_sql = '%s'
    158173
    159174        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)
    160180            extra, params = params.as_sql(qn, connection)
    161181            cast_sql = ''
    162182        else:
     
    171191            format = "%s %%s %%s" % (connection.ops.lookup_cast(lookup_type),)
    172192            return (format % (field_sql,
    173193                              connection.operators[lookup_type] % cast_sql,
    174                               extra), params)
     194                              extra), params, tables)
    175195
    176196        if lookup_type == 'in':
    177197            if not value_annot:
    178198                raise EmptyResultSet
    179199            if extra:
    180                 return ('%s IN %s' % (field_sql, extra), params)
     200                return ('%s IN %s' % (field_sql, extra), params, tables)
    181201            return ('%s IN (%s)' % (field_sql, ', '.join(['%s'] * len(params))),
    182                     params)
     202                    params, tables)
    183203        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)
    185205        elif lookup_type in ('month', 'day', 'week_day'):
    186206            return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, field_sql),
    187                     params)
     207                    params, tables)
    188208        elif lookup_type == 'isnull':
    189209            return ('%s IS %sNULL' % (field_sql,
    190                 (not value_annot and 'NOT ' or '')), ())
     210                (not value_annot and 'NOT ' or '')), (), tables)
    191211        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)
    193213        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
    195215
    196216        raise TypeError('Invalid lookup_type: %r' % lookup_type)
    197217
Back to Top