Ticket #9055: 9055-3.diff

File 9055-3.diff, 6.8 KB (added by Walter Doekes, 12 years ago)
  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index 958982b..928311e 100644
    a b class BaseDatabaseOperations(object):  
    684684        to_unicode = lambda s: force_text(s, strings_only=True, errors='replace')
    685685        if isinstance(params, (list, tuple)):
    686686            u_params = tuple(to_unicode(val) for val in params)
     687        elif params is None:
     688            u_params = ()
    687689        else:
    688690            u_params = dict((to_unicode(k), to_unicode(v)) for k, v in params.items())
    689691
  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index fc2ff31..bc1699f 100644
    a b class CursorWrapper(object):  
    115115
    116116    def execute(self, query, args=None):
    117117        try:
     118            # args is None means no string interpolation
    118119            return self.cursor.execute(query, args)
    119120        except Database.IntegrityError as e:
    120121            six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  • django/db/backends/oracle/base.py

    diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py
    index a9ae025..f4b099d 100644
    a b class FormatStylePlaceholderCursor(object):  
    748748            params = []
    749749        else:
    750750            params = self._format_params(params)
    751         args = [(':arg%d' % i) for i in range(len(params))]
     751            args = [(':arg%d' % i) for i in range(len(params))]
    752752        # cx_Oracle wants no trailing ';' for SQL statements.  For PL/SQL, it
    753753        # it does want a trailing ';' but not a trailing '/'.  However, these
    754754        # characters must be included in the original query in case the query
    755755        # is being passed to SQL*Plus.
    756756        if query.endswith(';') or query.endswith('/'):
    757757            query = query[:-1]
    758         query = convert_unicode(query % tuple(args), self.charset)
     758        if params is None:
     759            query = convert_unicode(query, self.charset)
     760        else:
     761            query = convert_unicode(query % tuple(args), self.charset)
    759762        self._guess_input_sizes([params])
    760763        try:
    761764            return self.cursor.execute(query, self._param_generator(params))
  • django/db/backends/postgresql_psycopg2/base.py

    diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
    index 85a0991..b791835 100644
    a b class CursorWrapper(object):  
    5151
    5252    def execute(self, query, args=None):
    5353        try:
     54            # args is None means no string interpolation               
    5455            return self.cursor.execute(query, args)
    5556        except Database.IntegrityError as e:
    5657            six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  • django/db/backends/sqlite3/base.py

    diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
    index 7ddaaf8..02c3a2b 100644
    a b class SQLiteCursorWrapper(Database.Cursor):  
    396396    This fixes it -- but note that if you want to use a literal "%s" in a query,
    397397    you'll need to use "%%s".
    398398    """
    399     def execute(self, query, params=()):
    400         query = self.convert_query(query)
     399    def execute(self, query, params=None):
     400        if params is not None:     
     401            query = self.convert_query(query)
    401402        try:
     403            if params is None:
     404                return Database.Cursor.execute(self, query)
    402405            return Database.Cursor.execute(self, query, params)
    403406        except Database.IntegrityError as e:
    404407            six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
    class SQLiteCursorWrapper(Database.Cursor):  
    415418            six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
    416419
    417420    def convert_query(self, query):
    418         return FORMAT_QMARK_REGEX.sub('?', query).replace('%%','%')
     421        return FORMAT_QMARK_REGEX.sub('?', query).replace('%%', '%')
    419422
    420423def _sqlite_date_extract(lookup_type, dt):
    421424    if dt is None:
  • django/db/backends/util.py

    diff --git a/django/db/backends/util.py b/django/db/backends/util.py
    index 1ba2306..5adf940 100644
    a b class CursorWrapper(object):  
    3434
    3535class CursorDebugWrapper(CursorWrapper):
    3636
    37     def execute(self, sql, params=()):
     37    def execute(self, sql, params=None):
    3838        self.set_dirty()
    3939        start = time()
    4040        try:
     41            if params is None:
     42                # params default might be backend specific
     43               return self.cursor.execute(sql)
    4144            return self.cursor.execute(sql, params)
    4245        finally:
    4346            stop = time()
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index 0e1700b..ce185e3 100644
    a b class ConnectionCreatedSignalTest(TestCase):  
    353353
    354354
    355355class EscapingChecks(TestCase):
     356    """ Each test is run once with normal cursor, and once with
     357        CursorDebugWrapper activated by setting DEBUG to True
     358    """
     359    def test_paramless_no_escaping(self):
     360        cursor = connection.cursor()
     361        cursor.execute("SELECT '%s'")
     362        self.assertEquals(cursor.fetchall()[0][0], '%s')
     363
     364    @override_settings(DEBUG=True)
     365    def test_paramless_no_escaping_debug(self):
     366        self.test_paramless_no_escaping()
     367
     368    def test_parameter_escaping(self):
     369        cursor = connection.cursor()
     370        cursor.execute("SELECT '%%', %s", ('%d',))
     371        self.assertEquals(cursor.fetchall()[0], ('%', '%d'))
     372
     373    @override_settings(DEBUG=True)
     374    def test_parameter_escaping_debug(self):
     375        self.test_parameter_escaping()
    356376
    357377    @unittest.skipUnless(connection.vendor == 'sqlite',
    358378                         "This is a sqlite-specific issue")
    359     def test_parameter_escaping(self):
     379    def test_sqlite_parameter_escaping(self):
    360380        #13648: '%s' escaping support for sqlite3
    361381        cursor = connection.cursor()
    362         response = cursor.execute(
    363             "select strftime('%%s', date('now'))").fetchall()[0][0]
    364         self.assertNotEqual(response, None)
     382        cursor.execute("select strftime('%s', date('now'))")
     383        response = cursor.fetchall()[0][0]
    365384        # response should be an non-zero integer
    366385        self.assertTrue(int(response))
    367386
     387    @unittest.skipUnless(connection.vendor == 'sqlite',
     388                         "This is a sqlite-specific issue")
     389    @override_settings(DEBUG=True)
     390    def test_sqlite_parameter_escaping_debug(self):
     391        self.test_sqlite_parameter_escaping()
     392
    368393
    369394class SqlliteAggregationTests(TestCase):
    370395    """
Back to Top