Commits

Olav Andreas Lindekleiv  committed 1c18adf

Output SQL in a more readable format, and split into queries of 50 rows.

  • Participants
  • Parent commits dd4ae5e

Comments (0)

Files changed (1)

File rptparser.py

                     new_columns.append(None)
             columns = new_columns
 
-        # Insert query:
-        query = ['INSERT INTO %s (' % table]
-
-        # Comma separated column names:
-        query_columns = []
-        for column in columns:
-            if column:
-                query_columns.append(column)
-        query.append(', '.join(query_columns))
-
-        # Rows:
-        query.append(') VALUES ')
+        queries = []
+        row_offset = 0
+        rows_per_query = 50
         rows = self.parsed['rows']
+        while row_offset <= len(rows):
+            # Insert query:
+            query = ['INSERT INTO %s (' % table]
 
-        query_rows = []
-        for row in rows:
-            query_row = []
-            i = 0
-            for value in row:
-                # Skip values for Null columns as specified in map_dict:
-                i += 1
-                if not columns[i - 1]:
-                    continue
-
-                # Add non-numbers with quotes:
-                try:
-                    int(value)
-                except ValueError:
-                    # Don't put NULL in quotes:
-                    if value != 'NULL':
-                        value = '"%s"' % value
-                query_row.append(str(value))
-
-            # Add semicolon on last row:
-            new_row = ', '.join(query_row)
-            if row != rows[-1]:
-                row = '(%s),' % new_row
-            else:
-                row = '(%s);' % new_row
-            query_rows.append(row)
-
-        query.append(' '.join(query_rows))
-        return ''.join(query)
+            # Comma separated column names:
+            query_columns = []
+            for column in columns:
+                if column:
+                    query_columns.append(column)
+            query.append(', '.join(query_columns))
+
+            # Rows:
+            query.append(') VALUES\n')
+            rows = self.parsed['rows']
+
+            query_rows = []
+            for i in range(rows_per_query):
+                row = rows[row_offset + i]
+                is_last_row = row == rows[-1]
+
+                query_row = []
+                j = 0
+                for value in row:
+                    # Skip values for Null columns as specified in map_dict:
+                    j += 1
+                    if not columns[j - 1]:
+                        continue
+
+                    # Add non-numbers with quotes:
+                    try:
+                        int(value)
+                    except ValueError:
+                        # Don't put NULL in quotes:
+                        if value != 'NULL':
+                            value = '"%s"' % value
+                    query_row.append(str(value))
+
+                # Add semicolon on last row in query:
+                new_row = ', '.join(query_row)
+                if i == 49 or is_last_row:
+                    query_rows.append('(%s);\n' % new_row)
+                else:
+                    query_rows.append('(%s),' % new_row)
+
+                if is_last_row:
+                    break
+
+            query.append('\n'.join(query_rows))
+            queries.append(''.join(query))
+            row_offset += rows_per_query
+
+        return ' '.join(queries)