Changeset 3370
- Timestamp:
- 07/18/06 21:46:18 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/multiple-db-support/django/db/models/query.py
r3355 r3370 167 167 # undefined, so we convert it to a list of tuples. 168 168 extra_select = self._select.items() 169 cursor = self.model._ meta.connection.cursor()169 cursor = self.model._default_manager.db.connection.cursor() 170 170 select, sql, params = self._get_sql_clause() 171 171 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) … … 187 187 def count(self): 188 188 "Performs a SELECT COUNT() and returns the number of records as an integer." 189 info = self.model._meta.connection_info190 backend = info.backend191 connection = info.connection189 db = self.model._default_manager.db 190 backend = db.backend 191 connection = db.connection 192 192 counter = self._clone() 193 193 counter._order_by = () … … 436 436 def _get_sql_clause(self): 437 437 opts = self.model._meta 438 backend = opts.connection_info.backend 438 backend = self.model._default_manager.db.backend 439 qn = backend.quote_name 439 440 # 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] 441 443 tables = [quote_only_if_word(t) for t in self._tables] 442 444 joins = SortedDict() … … 456 458 # Add any additional SELECTs. 457 459 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()]) 459 462 460 463 # Start composing the body of the SQL statement. 461 sql = [" FROM", backend.quote_name(opts.db_table)]464 sql = [" FROM", qn(opts.db_table)] 462 465 463 466 # Compose the join dictionary into SQL describing the joins. … … 492 495 if "." in col_name: 493 496 table_prefix, col_name = col_name.split('.', 1) 494 table_prefix = backend.quote_name(table_prefix) + '.'497 table_prefix = qn(table_prefix) + '.' 495 498 else: 496 499 # Use the database table as a column prefix if it wasn't given, 497 500 # and if the requested column isn't a custom SELECT. 498 501 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) + '.' 500 503 else: 501 504 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)) 503 506 if order_by: 504 507 sql.append("ORDER BY " + ", ".join(order_by)) … … 526 529 field_names = [f.attname for f in self.model._meta.fields] 527 530 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 531 535 cursor = connection.cursor() 532 536 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] 534 539 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 535 540 while 1: … … 548 553 def iterator(self): 549 554 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 553 559 554 560 self._order_by = () # Clear this because it'll mess things up otherwise. 555 561 if self._field.null: 556 562 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))) 558 564 select, sql, params = self._get_sql_clause() 559 565 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) 562 568 cursor = connection.cursor() 563 569 cursor.execute(sql, params)
