Ticket #9136: rownum.patch

File rownum.patch, 2.3 KB (added by Guillaume Taglang <guillaume.taglang@…>, 16 years ago)

patch replacing row_number() with row

  • django/db/backends/oracle/query.py

     
    9797                sql, params = super(OracleQuery, self).as_sql(with_limits=False,
    9898                        with_col_aliases=with_col_aliases)
    9999            else:
    100                 # `get_columns` needs to be called before `get_ordering` to
    101                 # populate `_select_alias`.
    102                 self.pre_sql_setup()
    103                 self.get_columns()
    104                 ordering = self.get_ordering()
     100                self.extra_where = list(self.extra_where)
     101                self.extra_where.extend(('ROWNUM > %d' % self.low_mark, ))
     102                if self.high_mark is not None:
     103                    self.extra_where.extend(('ROWNUM <= %d' % self.high_mark, ))
     104                self.extra_where = tuple(self.extra_where)
    105105
    106                 # Oracle's ROW_NUMBER() function requires an ORDER BY clause.
    107                 if ordering:
    108                     rn_orderby = ', '.join(ordering)
    109                 else:
    110                     # Create a default ORDER BY since none was specified.
    111                     qn = self.quote_name_unless_alias
    112                     opts = self.model._meta
    113                     rn_orderby = '%s.%s' % (qn(opts.db_table),
    114                         qn(opts.fields[0].db_column or opts.fields[0].column))
    115 
    116                 # Ensure the base query SELECTs our special "_RN" column
    117                 self.extra_select['_RN'] = ('ROW_NUMBER() OVER (ORDER BY %s)'
    118                                             % rn_orderby, '')
    119106                sql, params = super(OracleQuery, self).as_sql(with_limits=False,
    120107                                                        with_col_aliases=True)
    121108
    122                 # Wrap the base query in an outer SELECT * with boundaries on
    123                 # the "_RN" column.  This is the canonical way to emulate LIMIT
    124                 # and OFFSET on Oracle.
    125                 sql = 'SELECT * FROM (%s) WHERE "_RN" > %d' % (sql, self.low_mark)
    126                 if self.high_mark is not None:
    127                     sql = '%s AND "_RN" <= %d' % (sql, self.high_mark)
    128 
    129109            return sql, params
    130110
    131111        def set_limits(self, low=None, high=None):
Back to Top