Ticket #12923: last_executed_query.patch
File last_executed_query.patch, 2.9 KB (added by , 15 years ago) |
---|
-
django/db/backends/mysql/base.py
224 224 second = '%s-12-31 23:59:59.99' 225 225 return [first % value, second % value] 226 226 227 def construct_query(self, cursor, sql, params): 228 return sql % tuple(cursor.connection.literal(param) for param in params) 229 227 230 class DatabaseWrapper(BaseDatabaseWrapper): 228 231 229 232 operators = { -
django/db/backends/__init__.py
212 212 else: 213 213 u_params = dict([(to_unicode(k), to_unicode(v)) for k, v in params.items()]) 214 214 215 return s mart_unicode(sql) % u_params215 return self.construct_query(cursor, smart_unicode(sql), u_params) 216 216 217 def construct_query(self, cursor, sql, params): 218 """ 219 Returns a string of the query that results of composing the 220 parameters into the given sql template. 221 222 `sql` is the raw query containing placeholders, and `params` is the 223 sequence of parameters. The result is the combination of both. 224 Additionally, `cursor` is provided for use by backends that may 225 need it. 226 """ 227 return sql % tuple(['"' + param.replace('"', '""') + '"' for param in params]) 228 217 229 def last_insert_id(self, cursor, table_name, pk_name): 218 230 """ 219 231 Given a cursor object that has just performed an INSERT statement into -
tests/regressiontests/backends/tests.py
34 34 c.execute('DROP TABLE ltext') 35 35 self.assertEquals(long_str, row[0].read()) 36 36 37 class LastExecutedQuery(unittest.TestCase): 38 39 def test_last_executed_query(self): 40 c = connection.cursor() 41 c.execute('CREATE TABLE last_executed_query (example1 VARCHAR(255), example2 VARCHAR(255))') 42 query = 'INSERT INTO last_executed_query (example1, example2) VALUES (%s, %s)' 43 args = ('dangerous"string', "more'danger") 44 c.execute(query, args) 45 46 expectation = { 47 'django.db.backends.sqlite3': u'INSERT INTO last_executed_query (example1, example2) VALUES ("dangerous""string", "more\'danger")', 48 'django.db.backends.mysql': u"INSERT INTO last_executed_query (example1, example2) VALUES ('dangerous\\\"string', 'more\\'danger')", 49 }[settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE']] 50 51 self.assertEquals(expectation, connection.ops.last_executed_query(c, query, args)) 52 53 37 54 def connection_created_test(sender, **kwargs): 38 55 print 'connection_created signal' 39 56