Skip to content

Commit

Permalink
Fix an issue with SQL exports when configured to create an new INSERT…
Browse files Browse the repository at this point in the history
… statement for every single row (#2671)
  • Loading branch information
dmoagx committed Jan 27, 2017
1 parent 4eb2987 commit a13b067
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions Source/SPSQLExporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ - (void)exportOperation
SPTableType tableType = SPTableTypeTable;

id createTableSyntax = nil;
NSUInteger j, k, t, s, rowCount, queryLength, lastProgressValue, cleanAutoReleasePool = NO;
NSUInteger j, t, s, rowCount, queryLength, lastProgressValue, cleanAutoReleasePool = NO;

BOOL sqlOutputIncludeStructure;
BOOL sqlOutputIncludeContent;
Expand Down Expand Up @@ -369,7 +369,8 @@ - (void)exportOperation
[self writeUTF8String:[NSString stringWithFormat:@"INSERT INTO %@ (%@)\nVALUES", [tableName backtickQuotedString], [rawColumnNames componentsJoinedAndBacktickQuoted]]];

// Iterate through the rows to construct a VALUES group for each
j = 0, k = 0;
NSUInteger rowsWrittenForTable = 0;
NSUInteger rowsWrittenForCurrentStmt = 0;

NSAutoreleasePool *sqlExportPool = [[NSAutoreleasePool alloc] init];

Expand All @@ -392,11 +393,8 @@ - (void)exportOperation
return;
}

j++;
k++;

// Update the progress
NSUInteger progress = (NSUInteger)(j * ([self exportMaxProgress] / rowCount));
NSUInteger progress = (NSUInteger)((rowsWrittenForTable + 1) * ([self exportMaxProgress] / rowCount));

if (progress > lastProgressValue) {
[self setExportProgressValue:progress];
Expand All @@ -410,20 +408,20 @@ - (void)exportOperation
// Set up the new row as appropriate. If a new INSERT statement should be created,
// set one up; otherwise, set up a new row
if ((([self sqlInsertDivider] == SPSQLInsertEveryNDataBytes) && (queryLength >= ([self sqlInsertAfterNValue] * 1024))) ||
(([self sqlInsertDivider] == SPSQLInsertEveryNRows) && (k == [self sqlInsertAfterNValue])))
(([self sqlInsertDivider] == SPSQLInsertEveryNRows) && (rowsWrittenForCurrentStmt == [self sqlInsertAfterNValue])))
{
[sqlString setString:@";\n\nINSERT INTO "];
[sqlString appendString:[tableName backtickQuotedString]];
[sqlString appendString:@" ("];
[sqlString appendString:[rawColumnNames componentsJoinedAndBacktickQuoted]];
[sqlString appendString:@")\nVALUES\n\t("];

queryLength = 0, k = 0;
queryLength = 0, rowsWrittenForCurrentStmt = 0;

// Use the opportunity to drain and reset the autorelease pool at the end of this row
cleanAutoReleasePool = YES;
}
else if (j == 1) {
else if (rowsWrittenForTable == 0) {
[sqlString setString:@"\n\t("];
}
else {
Expand Down Expand Up @@ -506,6 +504,9 @@ - (void)exportOperation
sqlExportPool = [[NSAutoreleasePool alloc] init];
cleanAutoReleasePool = NO;
}

rowsWrittenForTable++;
rowsWrittenForCurrentStmt++;
}

// Complete the command
Expand Down

0 comments on commit a13b067

Please sign in to comment.