Django

Code

Changeset 3370

Show
Ignore:
Timestamp:
07/18/06 21:46:18 (2 years ago)
Author:
jpellerin
Message:

[multi-db] Began updating models.db.query to access connection through manager (work in progress).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/multiple-db-support/django/db/models/query.py

    r3355 r3370  
    167167        # undefined, so we convert it to a list of tuples. 
    168168        extra_select = self._select.items() 
    169         cursor = self.model._meta.connection.cursor() 
     169        cursor = self.model._default_manager.db.connection.cursor() 
    170170        select, sql, params = self._get_sql_clause() 
    171171        cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 
     
    187187    def count(self): 
    188188        "Performs a SELECT COUNT() and returns the number of records as an integer." 
    189         info = self.model._meta.connection_info 
    190         backend = info.backend 
    191         connection = info.connection         
     189        db = self.model._default_manager.db 
     190        backend = db.backend 
     191        connection = db.connection         
    192192        counter = self._clone() 
    193193        counter._order_by = () 
     
    436436    def _get_sql_clause(self): 
    437437        opts = self.model._meta 
    438         backend = opts.connection_info.backend 
     438        backend = self.model._default_manager.db.backend 
     439        qn = backend.quote_name 
    439440        # Construct the fundamental parts of the query: SELECT X FROM Y WHERE Z. 
    440         select = ["%s.%s" % (backend.quote_name(opts.db_table), backend.quote_name(f.column)) for f in opts.fields] 
     441        select = ["%s.%s" % (qn(opts.db_table), qn(f.column)) 
     442                  for f in opts.fields] 
    441443        tables = [quote_only_if_word(t) for t in self._tables] 
    442444        joins = SortedDict() 
     
    456458        # Add any additional SELECTs. 
    457459        if self._select: 
    458             select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select.items()]) 
     460            select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), qn(s[0])) 
     461                           for s in self._select.items()]) 
    459462 
    460463        # Start composing the body of the SQL statement. 
    461         sql = [" FROM", backend.quote_name(opts.db_table)] 
     464        sql = [" FROM", qn(opts.db_table)] 
    462465 
    463466        # Compose the join dictionary into SQL describing the joins. 
     
    492495                if "." in col_name: 
    493496                    table_prefix, col_name = col_name.split('.', 1) 
    494                     table_prefix = backend.quote_name(table_prefix) + '.' 
     497                    table_prefix = qn(table_prefix) + '.' 
    495498                else: 
    496499                    # Use the database table as a column prefix if it wasn't given, 
    497500                    # and if the requested column isn't a custom SELECT. 
    498501                    if "." not in col_name and col_name not in (self._select or ()): 
    499                         table_prefix = backend.quote_name(opts.db_table) + '.' 
     502                        table_prefix = qn(opts.db_table) + '.' 
    500503                    else: 
    501504                        table_prefix = '' 
    502                 order_by.append('%s%s %s' % (table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order)) 
     505                order_by.append('%s%s %s' % (table_prefix, qn(orderfield2column(col_name, opts)), order)) 
    503506        if order_by: 
    504507            sql.append("ORDER BY " + ", ".join(order_by)) 
     
    526529            field_names = [f.attname for f in self.model._meta.fields] 
    527530 
    528         info = self.model._meta.connection_info 
    529         backend = info.backend 
    530         connection = info.connection 
     531        db = self.model._default_manager.db 
     532        backend = db.backend 
     533        qn = backend.quote_name 
     534        connection = db.connection 
    531535        cursor = connection.cursor() 
    532536        select, sql, params = self._get_sql_clause() 
    533         select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] 
     537        select = ['%s.%s' % (qn(self.model._meta.db_table), qn(c)) 
     538                  for c in columns] 
    534539        cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 
    535540        while 1: 
     
    548553    def iterator(self): 
    549554        from django.db.backends.util import typecast_timestamp 
    550         info = self.model._meta.connection_info 
    551         backend = info.backend 
    552         connection = info.connection 
     555        db = self.model._default_manager.db 
     556        backend = db.backend 
     557        qn = backend.quote_name 
     558        connection = db.connection 
    553559         
    554560        self._order_by = () # Clear this because it'll mess things up otherwise. 
    555561        if self._field.null: 
    556562            self._where.append('%s.%s IS NOT NULL' % \ 
    557                 (backend.quote_name(self.model._meta.db_table), backend.quote_name(self._field.column))) 
     563                (qn(self.model._meta.db_table), qn(self._field.column))) 
    558564        select, sql, params = self._get_sql_clause() 
    559565        sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1 %s' % \ 
    560             (backend.get_date_trunc_sql(self._kind, '%s.%s' % (backend.quote_name(self.model._meta.db_table), 
    561             backend.quote_name(self._field.column))), sql, self._order) 
     566            (backend.get_date_trunc_sql(self._kind, '%s.%s' % (qn(self.model._meta.db_table), 
     567            qn(self._field.column))), sql, self._order) 
    562568        cursor = connection.cursor() 
    563569        cursor.execute(sql, params)