Commits

Michael Manfre committed 92494bb

Convert % format strings to using format in preparation for Python 3

  • Participants
  • Parent commits eb91d25

Comments (0)

Files changed (2)

File sqlserver_ado/base.py

             port = int(settings.PORT)
         except ValueError:
             raise ImproperlyConfigured("DATABASE PORT must be a number.")
-        db_host = '%s,%i;Network Library=DBMSSOCN' % (db_host, port)
+        db_host = '{0},{1};Network Library=DBMSSOCN'.format(db_host, port)
 
     # If no user is specified, use integrated security.
     if settings.USER != '':
-        auth_string = "UID=%s;PWD=%s" % (settings.USER, settings.PASSWORD)
+        auth_string = 'UID={0};PWD={1}'.format(settings.USER, settings.PASSWORD)
     else:
-        auth_string = "Integrated Security=SSPI"
+        auth_string = 'Integrated Security=SSPI'
 
     parts = [
-        "DATA SOURCE=%s" % (db_host,),
-        "Initial Catalog=%s" % (db_name,),
+        'DATA SOURCE={0};Initial Catalog={1}'.format(db_host, db_name),
         auth_string
     ]
 
         "startswith": "LIKE %s ESCAPE '\\'",
         "endswith": "LIKE %s ESCAPE '\\'",
         "istartswith": "LIKE %s ESCAPE '\\'",
-        "iendswith": "LIKE %s ESCAPE '\\'",
+        "iendswith": "LIKE %S ESCAPE '\\'",
     }
 
     def __init__(self, *args, **kwargs):

File sqlserver_ado/compiler.py

             if self.query.distinct:
                 _select += ' DISTINCT'
             
-            sql = re.sub(r'(?i)^%s' % _select, '%s TOP %s' % (_select, self.query.high_mark), raw_sql, 1)
+            sql = re.sub(r'(?i)^{0}'.format(_select), '{0} TOP {1}'.format(_select, self.query.high_mark), raw_sql, 1)
             return sql, fields
             
         # Else we have limits; rewrite the query using ROW_NUMBER()
         if order is None:
             meta = self.query.get_meta()                
             column = meta.pk.db_column or meta.pk.get_attname()
-            order = '%s.%s ASC' % (inner_table_name, qn(column))
+            order = '{0}.{1} ASC'.format(inner_table_name, qn(column))
         else:
             # remap order for injected subselect
             new_order = []
                     tbl, col = x.rsplit('.', 1)
                 else:
                     col = x
-                new_order.append('%s.%s' % (inner_table_name, col))
+                new_order.append('{0}.{1}'.format(inner_table_name, col))
             order = ', '.join(new_order)
         
-        where_row_num = "%s < _row_num" % (self.query.low_mark)
+        where_row_num = '{0} < _row_num'.format(self.query.low_mark)
         if self.query.high_mark:
-            where_row_num += " and _row_num <= %s" % (self.query.high_mark)
+            where_row_num += ' and _row_num <= {0}'.format(self.query.high_mark)
             
         # Lop off ORDER... and the initial "SELECT"
         inner_select = _remove_order_limit_offset(raw_sql)
                 tbl, col = x.rsplit('.', 1)
             else:
                 col = x
-            f.append('%s.%s' % (inner_table_name, col.strip()))
+            f.append('{0}.{1}'.format(inner_table_name, col.strip()))
         
         
         # inject a subselect to get around OVER requiring ORDER BY to come from FROM
-        inner_select = '%s FROM ( SELECT %s ) AS %s'\
-             % (', '.join(f), inner_select, inner_table_name)
+        inner_select = '{fields} FROM ( SELECT {inner} ) AS {inner_as}'.format(
+            fields=', '.join(f),
+            inner=inner_select,
+            inner_as=inner_table_name,
+        )
         
-        sql = "SELECT _row_num, %s FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY %s) as _row_num, %s) as QQQ where %s"\
-             % (outer_fields, order, inner_select, where_row_num)
+        sql = "SELECT _row_num, {outer} FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY {order}) as _row_num, {inner}) as QQQ where {where}".format(
+            outer=outer_fields,
+            order=order,
+            inner=inner_select,
+            where=where_row_num,
+        )
         
         return sql, fields
 
                 col_key = col_name.lower()
 
                 if col_key in names_seen:
-                    alias = qn('%s___%s' % (col_name, names_seen.count(col_key)))
+                    alias = qn('{0}___{1}'.format(col_name, names_seen.count(col_key)))
                     outer.append(alias)
             
                     col = _replace_sub(col)
             
-                    inner.append("%s as %s" % (col, alias))
+                    inner.append('{0} as {1}'.format(col, alias))
                 else:
                     replaced = _replace_sub(col)
                             
                 params = []
             elif auto_in_fields:
                 # wrap with identity insert
-                sql = "SET IDENTITY_INSERT %s ON;%s;SET IDENTITY_INSERT %s OFF" %\
-                    (quoted_table, sql, quoted_table)
+                sql = 'SET IDENTITY_INSERT {table} ON;{sql};SET IDENTITY_INSERT {table} OFF'.format(
+                    table=quoted_table,
+                    sql=sql,
+                )
 
         return sql, params