Commits

Germán M. Bravo  committed 14e2c88

Improved SQL debugging output

  • Participants
  • Parent commits 3a364da

Comments (0)

Files changed (1)

File south/db/generic.py

         """
         return self._get_connection().ops.quote_name(name)
 
+    def quote_sql_param(self, param):
+        "Add protective quoting around an SQL string parameter"
+        if isinstance(param, string_types):
+            return "'%s'" % text_type(param).replace("'", "\\'")
+        else:
+            return param
+
     def _print_sql_error(self, e, sql, params=[]):
         print('FATAL ERROR - The following SQL query failed: %s' % sql, file=sys.stderr)
         print('The error was: %s' % e, file=sys.stderr)
-        
+
     def execute(self, sql, params=[], print_all_errors=True):
         """
         Executes the given SQL statement, with optional parameters.
         
         cursor = self._get_connection().cursor()
         if self.debug:
-            print("   = %s" % sql, params)
+            sql_debug = text_type(sql % tuple(self.quote_sql_param(p) for p in params))
+            if not sql_debug.endswith(';'):
+                sql_debug += ';'
+            print("   = %s" % sql_debug)
 
         if self.dry_run:
             return []