Germán M. Bravo avatar Germán M. Bravo committed 14e2c88

Improved SQL debugging output

Comments (0)

Files changed (1)

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 []
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.