aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
Diffstat (limited to 'Source')
-rw-r--r--Source/SPDatabaseCopy.m14
-rw-r--r--Source/SPExportController.m2
-rw-r--r--Source/SPTableCopy.h15
-rw-r--r--Source/SPTableCopy.m36
4 files changed, 49 insertions, 18 deletions
diff --git a/Source/SPDatabaseCopy.m b/Source/SPDatabaseCopy.m
index 66b6ba1b..c2371f3d 100644
--- a/Source/SPDatabaseCopy.m
+++ b/Source/SPDatabaseCopy.m
@@ -64,21 +64,15 @@
return NO;
}
- BOOL success = [self createDatabase:targetDatabaseName];
+ //abort here if database creation failed
+ if(![self createDatabase:targetDatabaseName])
+ return NO;
SPTableCopy *dbActionTableCopy = [[SPTableCopy alloc] init];
[dbActionTableCopy setConnection:connection];
- for (NSString *currentTable in tables) {
- if ([dbActionTableCopy copyTable:currentTable
- from:sourceDatabaseName
- to:targetDatabaseName
- withContent:copyWithContent]) {
- } else {
- success = NO;
- }
- }
+ BOOL success = [dbActionTableCopy copyTables:tables from:sourceDatabaseName to:targetDatabaseName withContent:copyWithContent];
[dbActionTableCopy release];
diff --git a/Source/SPExportController.m b/Source/SPExportController.m
index 0ff78c1f..b22d2c32 100644
--- a/Source/SPExportController.m
+++ b/Source/SPExportController.m
@@ -639,7 +639,7 @@ static const NSString *SPSQLExportDropEnabled = @"SQLExportDropEnabled";
// Check whether to save the export filename. Save it if it's not blank and contains at least one
// token - this suggests it's not a one-off filename
- if (![exportCustomFilenameTokenField stringValue]) {
+ if ([[exportCustomFilenameTokenField stringValue] length] < 1) {
[prefs removeObjectForKey:SPExportFilenameFormat];
} else {
BOOL saveFilename = NO;
diff --git a/Source/SPTableCopy.h b/Source/SPTableCopy.h
index 32eadcfc..56e1da8c 100644
--- a/Source/SPTableCopy.h
+++ b/Source/SPTableCopy.h
@@ -35,6 +35,7 @@
* @param name name of the table in the source database
* @param sourceDB name of the source database
* @param targetDB name of the target database
+ * @return YES on success, NO on any kind of error (unspecified)
*/
- (BOOL)copyTable:(NSString *)name from: (NSString *)sourceDB to: (NSString *)targetDB;
@@ -54,7 +55,21 @@
* @param sourceDB name of the source database
* @param targetDB name of the target database
* @param copyWithContent whether to copy the content too, otherwise only structure
+ * @return YES on success, NO on any kind of error (unspecified)
*/
- (BOOL)copyTable:(NSString *)tableName from: (NSString *)sourceDB to: (NSString *)targetDB withContent:(BOOL)copyWithContent;
+/**
+ * This method copies a bunch of tables including their data from one db to another.
+ *
+ * @param tableArray array of NSStrings with the table names in the sourceDB
+ * @param sourceDB name of the source database
+ * @param targetDB name of the target database
+ * @param copyWithContent whether to copy the content too, otherwise only structure
+ * @return YES on success, NO on any kind of error (unspecified)
+ *
+ * This method is able to copy InnoDB tables with foreign key constraints.
+ */
+- (BOOL)copyTables:(NSArray *)tablesArray from:(NSString *)sourceDB to:(NSString *)targetDB withContent:(BOOL)copyWithContent;
+
@end
diff --git a/Source/SPTableCopy.m b/Source/SPTableCopy.m
index d390243c..5aa55df1 100644
--- a/Source/SPTableCopy.m
+++ b/Source/SPTableCopy.m
@@ -55,7 +55,7 @@
[createTableStatement release];
- return [connection queryErrored];
+ return ![connection queryErrored];
}
[createTableStatement release];
@@ -66,10 +66,10 @@
- (BOOL)copyTable:(NSString *)tableName from:(NSString *)sourceDB to:(NSString *)targetDB withContent:(BOOL)copyWithContent
{
// Copy the table structure
- BOOL structureCopyResult = [self copyTable:tableName from:sourceDB to:targetDB];
+ BOOL structureCopySuccess = [self copyTable:tableName from:sourceDB to:targetDB];
// Optionally copy the table data using an insert select
- if (structureCopyResult && structureCopyResult && copyWithContent) {
+ if (structureCopySuccess && copyWithContent) {
NSString *copyDataStatement = [NSString stringWithFormat:@"INSERT INTO %@.%@ SELECT * FROM %@.%@",
[targetDB backtickQuotedString],
@@ -80,12 +80,34 @@
[connection queryString:copyDataStatement];
- if ([connection queryErrored]) return NO;
-
- return YES;
+ return ![connection queryErrored];
}
- return structureCopyResult;
+ return structureCopySuccess;
+}
+
+- (BOOL)copyTables:(NSArray *)tablesArray from:(NSString *)sourceDB to:(NSString *)targetDB withContent:(BOOL)copyWithContent
+{
+ BOOL success = YES;
+
+ //disable foreign key checks
+ [connection queryString:@"/*!32352 SET foreign_key_checks=0 */"];
+ if([connection queryErrored])
+ success = NO;
+
+ //copy tables
+ for(NSString *tableName in tablesArray) {
+ if(![self copyTable:tableName from:sourceDB to:targetDB withContent:copyWithContent])
+ success = NO;
+ }
+
+ //enable foreign key checks
+ [connection queryString:@"/*!32352 SET foreign_key_checks=1 */"];
+ if([connection queryErrored])
+ success = NO;
+
+ //done
+ return success;
}
- (BOOL)moveTable:(NSString *)tableName from:(NSString *)sourceDB to:(NSString *)targetDB