Permalink
Browse files

Sequel Pro will now actually change the SQL_MODE of the connection du…

…ring SQL export to match the generated file (#2955)
  • Loading branch information...
dmoagx committed Jun 4, 2018
1 parent bc3b8ab commit 148a5ec11d18b71987170b14cd16446fb100896b
Showing with 53 additions and 43 deletions.
  1. +53 −43 Source/SPSQLExporter.m
View
@@ -112,15 +112,15 @@ - (void)exportOperation
NSMutableDictionary *viewSyntaxes = [NSMutableDictionary dictionary];
NSString *oldSqlMode = nil;
// Check that we have all the required info before starting the export
if ((![self sqlExportTables]) || ([[self sqlExportTables] count] == 0) ||
(![self sqlDatabaseHost]) || ([[self sqlDatabaseHost] isEqualToString:@""]) ||
(![self sqlDatabaseName]) || ([[self sqlDatabaseName] isEqualToString:@""]) ||
(![self sqlDatabaseVersion] || ([[self sqlDatabaseName] isEqualToString:@""])))
{
[errors release];
[sqlString release];
return;
goto end_cleanup;
}
// Inform the delegate that the export process is about to begin
@@ -138,11 +138,7 @@ - (void)exportOperation
for (NSArray *item in [self sqlExportTables])
{
// Check for cancellation flag
if ([self isCancelled]) {
[errors release];
[sqlString release];
return;
}
if ([self isCancelled]) goto end_cleanup;
switch ([NSArrayObjectAtIndex(item, 4) intValue]) {
case SPTableTypeProc:
@@ -196,7 +192,7 @@ - (void)exportOperation
}
[metaString appendString:@"/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n"];
[metaString appendString:@"/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n"];
/* A note on SQL_MODE:
*
* BEFORE 3.23.6
@@ -228,6 +224,40 @@ - (void)exportOperation
* (trivia: internally it has become MODE_NOT_USED: 16 = "?")
*
*/
BOOL sqlModeIsValid = NO;
//fetch old sql mode to restore it later
{
SPMySQLResult *result = [connection queryString:@"SHOW VARIABLES LIKE 'sql_mode'"];
if(![connection queryErrored]) {
[result setReturnDataAsStrings:YES];
NSArray *row = [result getRowAsArray];
oldSqlMode = [[row objectAtIndex:1] unboxNull];
}
}
//set sql mode for export
if([@"" isEqualToString:oldSqlMode]) {
// the current sql_mode is already the one we want (empty string), no need to change+revert it
oldSqlMode = nil;
sqlModeIsValid = YES;
}
else {
[connection queryString:@"SET SQL_MODE=''"]; //mysqldump uses a conditional comment for 40100 here, but we want to see the error, since it can't simply be ignored (also ANSI mode is supported before 4.1)
if (![connection queryErrored]) {
sqlModeIsValid = YES;
}
else {
[errors appendFormat:@"%@ (%@)\n", NSLocalizedString(@"The server's SQL_MODE could not be changed to one suitable for export. The export may be missing important details or may not be importable at all!", @"sql export : 'set @@sql_mode' query failed message"), [connection lastErrorMessage]];
[self writeUTF8String:[NSString stringWithFormat:@"# SET SQL_MODE Error: %@\n\n\n", [connection lastErrorMessage]]];
//if we couldn't change it, we don't need to restore it either
oldSqlMode = nil;
}
}
// there is no point in writing out that the file should use a specific SQL mode when we don't even know which one was active during export
if(sqlModeIsValid) {
[metaString appendString:@"/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n"];
}
[metaString appendString:@"/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n\n"];
[self writeString:metaString];
@@ -236,11 +266,7 @@ - (void)exportOperation
for (NSArray *table in tables)
{
// Check for cancellation flag
if ([self isCancelled]) {
[errors release];
[sqlString release];
return;
}
if ([self isCancelled]) goto end_cleanup;
[self setSqlCurrentTableExportIndex:[self sqlCurrentTableExportIndex]+1];
tableName = NSArrayObjectAtIndex(table, 0);
@@ -415,12 +441,10 @@ - (void)exportOperation
[streamingResult cancelResultLoad];
[streamingResult release];
[sqlExportPool release];
[errors release];
[sqlString release];
free(useRawDataForColumnAtIndex);
free(useRawHexDataForColumnAtIndex);
return;
goto end_cleanup;
}
// Update the progress
@@ -581,11 +605,7 @@ - (void)exportOperation
for (s = 0; s < [queryResult numberOfRows]; s++)
{
// Check for cancellation flag
if ([self isCancelled]) {
[errors release];
[sqlString release];
return;
}
if ([self isCancelled]) goto end_cleanup;
NSDictionary *triggers = [[NSDictionary alloc] initWithDictionary:[queryResult getRowAsDictionary]];
@@ -627,11 +647,7 @@ - (void)exportOperation
for (tableName in viewSyntaxes)
{
// Check for cancellation flag
if ([self isCancelled]) {
[errors release];
[sqlString release];
return;
}
if ([self isCancelled]) goto end_cleanup;
[metaString setString:@"\n\n"];
@@ -647,11 +663,7 @@ - (void)exportOperation
for (NSString *procedureType in @[@"PROCEDURE", @"FUNCTION"])
{
// Check for cancellation flag
if ([self isCancelled]) {
[errors release];
[sqlString release];
return;
}
if ([self isCancelled]) goto end_cleanup;
// Retrieve the array of selected procedures or functions, and skip export if not selected
NSMutableArray *items;
@@ -677,11 +689,7 @@ - (void)exportOperation
for (s = 0; s < [queryResult numberOfRows]; s++)
{
// Check for cancellation flag
if ([self isCancelled]) {
[errors release];
[sqlString release];
return;
}
if ([self isCancelled]) goto end_cleanup;
NSDictionary *proceduresList = [[NSDictionary alloc] initWithDictionary:[queryResult getRowAsDictionary]];
NSString *procedureName = [NSString stringWithFormat:@"%@", [proceduresList objectForKey:@"Name"]];
@@ -693,9 +701,7 @@ - (void)exportOperation
// Check for cancellation flag
if ([self isCancelled]) {
[proceduresList release];
[errors release];
[sqlString release];
return;
goto end_cleanup;
}
if ([NSArrayObjectAtIndex(item, 0) isEqualToString:procedureName]) {
@@ -811,9 +817,6 @@ - (void)exportOperation
// Set export errors
[self setSqlExportErrors:errors];
[errors release];
[sqlString release];
// Close the file
[[self exportOutputFile] close];
@@ -822,6 +825,13 @@ - (void)exportOperation
// Inform the delegate that the export process is complete
[delegate performSelectorOnMainThread:@selector(sqlExportProcessComplete:) withObject:self waitUntilDone:NO];
end_cleanup:
if(oldSqlMode) {
[connection queryString:[NSString stringWithFormat:@"SET SQL_MODE=%@",[oldSqlMode tickQuotedString]]];
}
[errors release];
[sqlString release];
}
/**

0 comments on commit 148a5ec

Please sign in to comment.