Commits

NOKIA Development committed 5312828

[refactoring] creating constant ENCODING to facilitate change base.py

  • Participants
  • Parent commits b8e59f6

Comments (0)

Files changed (1)

File sql_server/pyodbc/base.py

 from django.db.backends.signals import connection_created
 from django.conf import settings
 from django import VERSION as DjangoVersion
-if DjangoVersion[:2] == (1,2) :
+if DjangoVersion[:2] == (1, 2) :
     from django import get_version
     version_str = get_version()
     if 'SVN' in version_str and int(version_str.split('SVN-')[-1]) < 11952: # django trunk revision 11952 Added multiple database support.
         _DJANGO_VERSION = 11
     else:
         _DJANGO_VERSION = 12
-elif DjangoVersion[:2] == (1,1):
+elif DjangoVersion[:2] == (1, 1):
     _DJANGO_VERSION = 11
-elif DjangoVersion[:2] == (1,0):
+elif DjangoVersion[:2] == (1, 0):
     _DJANGO_VERSION = 10
 elif DjangoVersion[0] == 1:
     _DJANGO_VERSION = 13
 else:
     _DJANGO_VERSION = 9
-    
+
 from sql_server.pyodbc.operations import DatabaseOperations
 from sql_server.pyodbc.client import DatabaseClient
 from sql_server.pyodbc.creation import DatabaseCreation
 import os
 import warnings
 
+ENCODING = 'utf-8'
+
 warnings.filterwarnings('error', 'The DATABASE_ODBC.+ is deprecated', DeprecationWarning, __name__, 0)
 
 collation = 'Latin1_General_CI_AS'
         'exact': '= %s',
         'iexact': "= UPPER(%s)",
         'contains': "LIKE %s ESCAPE '\\' COLLATE " + collation,
-        'icontains': "LIKE UPPER(%s) ESCAPE '\\' COLLATE "+ collation,
+        'icontains': "LIKE UPPER(%s) ESCAPE '\\' COLLATE " + collation,
         'gt': '> %s',
         'gte': '>= %s',
         'lt': '< %s',
             else:
                 # Only append DRIVER if DATABASE_ODBC_DSN hasn't been set
                 cstr_parts.append('DRIVER={%s}' % driver)
-                
+
                 if os.name == 'nt' or driver == 'FreeTDS' and \
                         options.get('host_is_server', False):
                     if port_str:
 
             if self.MARS_Connection:
                 cstr_parts.append('MARS_Connection=yes')
-                
+
             if 'extra_params' in options:
                 cstr_parts.append(options['extra_params'])
 
         if self.driver_needs_utf8 and isinstance(sql, unicode):
             # FreeTDS (and other ODBC drivers?) doesn't support Unicode
             # yet, so we need to encode the SQL clause itself in utf-8
-            sql = sql.encode('utf-8')
+            sql = sql.encode(ENCODING)
         # pyodbc uses '?' instead of '%s' as parameter placeholder.
         if n_params is not None:
             sql = sql % tuple('?' * n_params)
                 if self.driver_needs_utf8:
                     # FreeTDS (and other ODBC drivers?) doesn't support Unicode
                     # yet, so we need to encode parameters in utf-8
-                    fp.append(p.encode('utf-8'))
+                    fp.append(p.encode(ENCODING))
                 else:
                     fp.append(p)
             elif isinstance(p, str):
                 if self.driver_needs_utf8:
                     # TODO: use system encoding when calling decode()?
-                    fp.append(p.decode('utf-8').encode('utf-8'))
+                    fp.append(p.decode(ENCODING).encode(ENCODING))
                 else:
                     fp.append(p)
             elif isinstance(p, type(True)):
         fr = []
         for row in rows:
             if isinstance(row, str):
-                fr.append(row.decode('utf-8'))
+                fr.append(row.decode(ENCODING))
             else:
                 fr.append(row)
         return tuple(fr)
         if attr in self.__dict__:
             return self.__dict__[attr]
         return getattr(self.cursor, attr)
-    
+
     def __iter__(self):
-        return iter(self.cursor)
+        return iter(self.cursor)