@@ -88,7 +88,7 @@ - (void)exportOperation
8888{
8989 sqlTableDataInstance = [[[SPTableData alloc ] init ] autorelease ];
9090 [sqlTableDataInstance setConnection: connection];
91-
91+
9292 SPMySQLResult *queryResult;
9393
9494 NSString *tableName;
@@ -111,18 +111,18 @@ - (void)exportOperation
111111 NSMutableString *sqlString = [[NSMutableString alloc ] init ];
112112
113113 NSMutableDictionary *viewSyntaxes = [NSMutableDictionary dictionary ];
114-
114+
115115 // Check that we have all the required info before starting the export
116116 if ((![self sqlExportTables ]) || ([[self sqlExportTables ] count ] == 0 ) ||
117- (![self sqlDatabaseHost ]) || ([[self sqlDatabaseHost ] isEqualToString: @" " ]) ||
118- (![self sqlDatabaseName ]) || ([[self sqlDatabaseName ] isEqualToString: @" " ]) ||
119- (![self sqlDatabaseVersion ] || ([[self sqlDatabaseName ] isEqualToString: @" " ])))
117+ (![self sqlDatabaseHost ]) || ([[self sqlDatabaseHost ] isEqualToString: @" " ]) ||
118+ (![self sqlDatabaseName ]) || ([[self sqlDatabaseName ] isEqualToString: @" " ]) ||
119+ (![self sqlDatabaseVersion ] || ([[self sqlDatabaseName ] isEqualToString: @" " ])))
120120 {
121121 [errors release ];
122122 [sqlString release ];
123123 return ;
124124 }
125-
125+
126126 // Inform the delegate that the export process is about to begin
127127 [delegate performSelectorOnMainThread: @selector (sqlExportProcessWillBegin: ) withObject: self waitUntilDone: NO ];
128128
@@ -159,7 +159,7 @@ - (void)exportOperation
159159
160160 [targetArray addObject: item];
161161 }
162-
162+
163163 // If required write the UTF-8 Byte Order Mark (BOM)
164164 if ([self sqlOutputIncludeUTF8BOM ]) {
165165 [metaString appendString: @" \xef\xbb\xbf " ];
@@ -194,13 +194,44 @@ - (void)exportOperation
194194 // TODO we should link to a website explaining the risk here
195195 [metaString appendString: @" SET NAMES utf8mb4;\n " ];
196196 }
197-
197+
198198 [metaString appendString: @" /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n " ];
199199 [metaString appendString: @" /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n " ];
200+ /* A note on SQL_MODE:
201+ *
202+ * BEFORE 3.23.6
203+ * No supported
204+ *
205+ * FROM 3.23.6
206+ * There is a "--ansi" / "-a" CLI argument to mysqld, which is the predecessor to SQL_MODE.
207+ * It can be queried via "SHOW VARIABLES" -> "ansi_mode" = "OFF" | "ON"
208+ *
209+ * FROM 3.23.41
210+ * There is a "--sql-mode=[opt[,opt[...]]]" CLI argument to mysqld.
211+ * It can be queried via "SHOW VARIABLES" -> "sql_mode" but the result will be a bitfield value with
212+ * #define MODE_REAL_AS_FLOAT 1 = "REAL_AS_FLOAT"
213+ * #define MODE_PIPES_AS_CONCAT 2 = "PIPES_AS_CONCAT"
214+ * #define MODE_ANSI_QUOTES 4 = "ANSI_QUOTES"
215+ * #define MODE_IGNORE_SPACE 8 = "IGNORE_SPACE"
216+ * #define MODE_SERIALIZABLE 16 = "SERIALIZE" (!)
217+ * #define MODE_ONLY_FULL_GROUP_BY 32 = "ONLY_FULL_GROUP_BY"
218+ * The "--ansi" switch is still supported but mostly equivalent to setting all of the options above
219+ * (it will also set the transaction isolation level to SERIALIZABLE).
220+ * "ansi_mode" is no longer returned by SHOW VARIABLES.
221+ *
222+ * FROM 4.1.0
223+ * - "sql_mode" can be changed at runtime (global or per session).
224+ * - "SHOW VARIABLES" now returns a CSV list of named options
225+ *
226+ * FROM 4.1.1
227+ * - "SERIALIZE" is no longer supported (must be changed via "SET TRANSACTION ISOLATION LEVEL")
228+ * (trivia: internally it has become MODE_NOT_USED: 16 = "?")
229+ *
230+ */
200231 [metaString appendString: @" /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n\n " ];
201232
202233 [self writeString: metaString];
203-
234+
204235 // Loop through the selected tables
205236 for (NSArray *table in tables)
206237 {
@@ -254,7 +285,7 @@ - (void)exportOperation
254285
255286 [tableDetails release ];
256287 }
257-
288+
258289 if ([connection queryErrored ]) {
259290 [errors appendFormat: @" %@ \n " , [connection lastErrorMessage ]];
260291
@@ -283,20 +314,20 @@ - (void)exportOperation
283314 [self writeUTF8String: createTableSyntax];
284315 [self writeUTF8String: @" ;\n\n " ];
285316 }
286-
317+
287318 // Add the table content if required
288319 if (sqlOutputIncludeContent && (tableType == SPTableTypeTable)) {
289320
290321 // Retrieve the table details via the data class, and use it to build an array containing column numeric status
291322 tableDetails = [NSDictionary dictionaryWithDictionary: [sqlTableDataInstance informationForTable: tableName]];
292-
323+
293324 NSUInteger colCount = [[tableDetails objectForKey: @" columns" ] count ];
294325 NSMutableArray *rawColumnNames = [NSMutableArray arrayWithCapacity: colCount];
295326 NSMutableArray *queryColumnDetails = [NSMutableArray arrayWithCapacity: colCount];
296327
297328 BOOL *useRawDataForColumnAtIndex = calloc (colCount, sizeof (BOOL ));
298329 BOOL *useRawHexDataForColumnAtIndex = calloc (colCount, sizeof (BOOL ));
299-
330+
300331 // Determine whether raw data can be used for each column during processing - safe numbers and hex-encoded data.
301332 for (j = 0 ; j < colCount; j++)
302333 {
@@ -332,7 +363,7 @@ - (void)exportOperation
332363 [queryColumnDetails addObject: [[theColumnDetail objectForKey: @" name" ] mySQLBacktickQuotedString ]];
333364 }
334365 }
335-
366+
336367 // Retrieve the number of rows in the table for progress bar drawing
337368 NSArray *rowArray = [[connection queryString: [NSString stringWithFormat: @" SELECT COUNT(1) FROM %@ " , [tableName backtickQuotedString ]]] getRowAsArray ];
338369
@@ -345,7 +376,7 @@ - (void)exportOperation
345376 }
346377
347378 NSUInteger rowCount = [NSArrayObjectAtIndex(rowArray, 0 ) integerValue ];
348-
379+
349380 if (rowCount) {
350381
351382 // Set up a result set in streaming mode
@@ -468,7 +499,7 @@ - (void)exportOperation
468499 if ([self sqlOutputEncodeBLOBasHex ]) {
469500 [sqlString appendString: [connection escapeAndQuoteData: object]];
470501 }
471- else {
502+ else {
472503 NSString *data = [[NSString alloc ] initWithData: object encoding: [self exportOutputEncoding ]];
473504
474505 if (data == nil ) {
@@ -493,7 +524,7 @@ - (void)exportOperation
493524
494525 [sqlString appendString: @" )" ];
495526 queryLength += [sqlString length ];
496-
527+
497528 // Write this row to the file
498529 [self writeUTF8String: sqlString];
499530
@@ -563,14 +594,13 @@ - (void)exportOperation
563594
564595 [metaString appendFormat: @" /*!50003 SET SESSION SQL_MODE=\" %@ \" */;;\n /*!50003 CREATE */ " , [triggers objectForKey: @" sql_mode" ]];
565596 [metaString appendFormat: @" /*!50017 DEFINER=%@ @%@ */ /*!50003 TRIGGER %@ %@ %@ ON %@ FOR EACH ROW %@ */;;\n " ,
566- [NSArrayObjectAtIndex(triggersDefiner, 0 ) backtickQuotedString ],
567- [NSArrayObjectAtIndex(triggersDefiner, 1 ) backtickQuotedString ],
568- [[triggers objectForKey: @" Trigger" ] backtickQuotedString ],
569- [triggers objectForKey: @" Timing" ],
570- [triggers objectForKey: @" Event" ],
571- [[triggers objectForKey: @" Table" ] backtickQuotedString ],
572- [triggers objectForKey: @" Statement" ]
573- ];
597+ [NSArrayObjectAtIndex(triggersDefiner, 0 ) backtickQuotedString ],
598+ [NSArrayObjectAtIndex(triggersDefiner, 1 ) backtickQuotedString ],
599+ [[triggers objectForKey: @" Trigger" ] backtickQuotedString ],
600+ [triggers objectForKey: @" Timing" ],
601+ [triggers objectForKey: @" Event" ],
602+ [[triggers objectForKey: @" Table" ] backtickQuotedString ],
603+ [triggers objectForKey: @" Statement" ]];
574604
575605 [triggers release ];
576606 }
@@ -633,17 +663,16 @@ - (void)exportOperation
633663
634664 // Retrieve the definitions
635665 queryResult = [connection queryString: [NSString stringWithFormat: @" /*!50003 SHOW %@ STATUS WHERE `Db` = %@ */" , procedureType,
636- [[self sqlDatabaseName ] tickQuotedString ]]];
666+ [[self sqlDatabaseName ] tickQuotedString ]]];
637667
638668 [queryResult setReturnDataAsStrings: YES ];
639669
640670 if ([queryResult numberOfRows ]) {
641671
642672 [metaString setString: @" \n " ];
643673 [metaString appendFormat: @" --\n -- Dumping routines (%@ ) for database %@ \n --\n DELIMITER ;;\n\n " , procedureType,
644- [[self sqlDatabaseName ] tickQuotedString ]];
645-
646-
674+ [[self sqlDatabaseName ] tickQuotedString ]];
675+
647676 // Loop through the definitions, exporting if enabled
648677 for (s = 0 ; s < [queryResult numberOfRows ]; s++)
649678 {
@@ -688,7 +717,7 @@ - (void)exportOperation
688717 // Add the 'DROP' command if required
689718 if (sqlOutputIncludeDropSyntax) {
690719 [metaString appendFormat: @" /*!50003 DROP %@ IF EXISTS %@ */;;\n " , procedureType,
691- [procedureName backtickQuotedString ]];
720+ [procedureName backtickQuotedString ]];
692721 }
693722
694723 // Only continue if the 'CREATE SYNTAX' is required
@@ -699,14 +728,13 @@ - (void)exportOperation
699728
700729 // Definer is user@host but we need to escape it to `user`@`host`
701730 NSArray *procedureDefiner = [[proceduresList objectForKey: @" Definer" ] componentsSeparatedByString: @" @" ];
702-
703- NSString *escapedDefiner = [NSString stringWithFormat: @" %@ @%@ " ,
704- [NSArrayObjectAtIndex(procedureDefiner, 0 ) backtickQuotedString ],
705- [NSArrayObjectAtIndex(procedureDefiner, 1 ) backtickQuotedString ]
706- ];
731+
732+ NSString *escapedDefiner = [NSString stringWithFormat: @" %@ @%@ " ,
733+ [NSArrayObjectAtIndex(procedureDefiner, 0 ) backtickQuotedString ],
734+ [NSArrayObjectAtIndex(procedureDefiner, 1 ) backtickQuotedString ]];
707735
708736 SPMySQLResult *createProcedureResult = [connection queryString: [NSString stringWithFormat: @" /*!50003 SHOW CREATE %@ %@ */" , procedureType,
709- [procedureName backtickQuotedString ]]];
737+ [procedureName backtickQuotedString ]]];
710738 [createProcedureResult setReturnDataAsStrings: YES ];
711739 if ([connection queryErrored ]) {
712740 [errors appendFormat: @" %@ \n " , [connection lastErrorMessage ]];
@@ -779,10 +807,10 @@ - (void)exportOperation
779807
780808 // Write footer-type information to the file
781809 [self writeUTF8String: metaString];
782-
810+
783811 // Set export errors
784812 [self setSqlExportErrors: errors];
785-
813+
786814 [errors release ];
787815 [sqlString release ];
788816
0 commit comments