Changeset 6601
- Timestamp:
- 10/23/07 14:00:31 (1 year ago)
- Files:
-
- django/trunk/django/db/backends/__init__.py (modified) (1 diff)
- django/trunk/django/db/backends/postgresql_psycopg2/base.py (modified) (2 diffs)
- django/trunk/django/db/backends/util.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/backends/__init__.py
r6195 r6601 128 128 raise NotImplementedError('Full-text search is not implemented for this database backend') 129 129 130 def last_executed_query(self, cursor, sql, params): 131 """ 132 Returns a string of the query last executed by the given cursor, with 133 placeholders replaced with actual values. 134 135 `sql` is the raw query containing placeholders, and `params` is the 136 sequence of parameters. These are used by default, but this method 137 exists for database backends to provide a better implementation 138 according to their own quoting schemes. 139 """ 140 from django.utils.encoding import smart_unicode, force_unicode 141 142 # Convert params to contain Unicode values. 143 to_unicode = lambda s: force_unicode(s, strings_only=True) 144 if isinstance(params, (list, tuple)): 145 u_params = tuple([to_unicode(val) for val in params]) 146 else: 147 u_params = dict([(to_unicode(k), to_unicode(v)) for k, v in params.items()]) 148 149 return smart_unicode(sql) % u_params 150 130 151 def last_insert_id(self, cursor, table_name, pk_name): 131 152 """ django/trunk/django/db/backends/postgresql_psycopg2/base.py
r6012 r6601 6 6 7 7 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures 8 from django.db.backends.postgresql.operations import DatabaseOperations 8 from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations 9 9 try: 10 10 import psycopg2 as Database … … 21 21 class DatabaseFeatures(BaseDatabaseFeatures): 22 22 needs_datetime_string_cast = False 23 24 class DatabaseOperations(PostgresqlDatabaseOperations): 25 def last_executed_query(self, cursor, sql, params): 26 # With psycopg2, cursor objects have a "query" attribute that is the 27 # exact query sent to the database. See docs here: 28 # http://www.initd.org/tracker/psycopg/wiki/psycopg2_documentation#postgresql-status-message-and-executed-query 29 return cursor.query 23 30 24 31 class DatabaseWrapper(BaseDatabaseWrapper): django/trunk/django/db/backends/util.py
r5971 r6601 2 2 import md5 3 3 from time import time 4 from django.utils.encoding import smart_unicode, force_unicode5 4 6 5 try: … … 12 11 def __init__(self, cursor, db): 13 12 self.cursor = cursor 14 self.db = db 13 self.db = db # Instance of a BaseDatabaseWrapper subclass 15 14 16 15 def execute(self, sql, params=()): … … 20 19 finally: 21 20 stop = time() 21 sql = self.db.ops.last_executed_query(self.cursor, sql, params) 22 22 self.db.queries.append({ 23 'sql': s mart_unicode(sql) % convert_args(params),23 'sql': sql, 24 24 'time': "%.3f" % (stop - start), 25 25 }) … … 32 32 stop = time() 33 33 self.db.queries.append({ 34 'sql': ' MANY: ' + sql + ' ' + smart_unicode(tuple(param_list)),34 'sql': '%s times: %s' % (len(param_list), sql), 35 35 'time': "%.3f" % (stop - start), 36 36 }) … … 41 41 else: 42 42 return getattr(self.cursor, attr) 43 44 def convert_args(args):45 """46 Convert sequence or dictionary to contain unicode values.47 """48 to_unicode = lambda s: force_unicode(s, strings_only=True)49 if isinstance(args, (list, tuple)):50 return tuple([to_unicode(val) for val in args])51 else:52 return dict([(to_unicode(k), to_unicode(v)) for k, v in args.items()])53 43 54 44 ###############################################
