diff options
Diffstat (limited to 'Source/SPTableCopy.m')
-rw-r--r-- | Source/SPTableCopy.m | 87 |
1 files changed, 31 insertions, 56 deletions
diff --git a/Source/SPTableCopy.m b/Source/SPTableCopy.m index 514eaeb1..c965350a 100644 --- a/Source/SPTableCopy.m +++ b/Source/SPTableCopy.m @@ -27,49 +27,35 @@ @implementation SPTableCopy -- (NSString *)getCreateTableStatementFor: (NSString *)tableName inDB: (NSString *)sourceDB { - NSString *showCreateTableStatment = [NSString stringWithFormat:@"SHOW CREATE TABLE %@.%@", - [sourceDB backtickQuotedString], - [tableName backtickQuotedString] - ]; +- (NSString *)getCreateTableStatementFor:(NSString *)tableName inDB:(NSString *)sourceDB +{ + NSString *showCreateTableStatment = [NSString stringWithFormat:@"SHOW CREATE TABLE %@.%@", [sourceDB backtickQuotedString], [tableName backtickQuotedString]]; + MCPResult *theResult = [connection queryString:showCreateTableStatment]; - if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Failed to show create table statement", @"show create table error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to retrieve the create table statement for a table.\n\nMySQL said: %@", - @"show create table error informative message"), - [connection getLastErrorMessage]]); - } if ([theResult numOfRows] != 0) { return [[theResult fetchRowAsArray] objectAtIndex:1]; } + return @""; } -- (BOOL)copyTable:(NSString *)tableName from: (NSString *)sourceDB to: (NSString *)targetDB { - +- (BOOL)copyTable:(NSString *)tableName from:(NSString *)sourceDB to:(NSString *)targetDB +{ NSString *createTableResult = [self getCreateTableStatementFor:tableName inDB:sourceDB]; NSMutableString *createTableStatement = [[NSMutableString alloc] initWithString:createTableResult]; if ([[createTableStatement substringToIndex:12] isEqualToString:@"CREATE TABLE"]) { - // adding the target DB name and the separator dot after "CREATE TABLE ". + + // Add the target DB name and the separator dot after "CREATE TABLE ". [createTableStatement insertString:@"." atIndex:13]; [createTableStatement insertString:[targetDB backtickQuotedString] atIndex:13]; [connection queryString:createTableStatement]; - if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Failed to copy table", @"copy table error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to copy a table.\n\nMySQL said: %@", - @"copy table error informative message"), - [connection getLastErrorMessage]]); - return NO; - } + if ([connection queryErrored]) return NO; + return YES; - } else { - NSLog(@"Could not copy non-table/view %@", tableName); } [createTableStatement release]; @@ -77,55 +63,44 @@ return NO; } -- (BOOL)copyTable:(NSString *)tableName from: (NSString *)sourceDB to: (NSString *)targetDB withContent:(BOOL)copyWithContent{ - // copy the table structure +- (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]; - // optionally copy the table data using an insert select - if (structureCopyResult && copyWithContent) { + // Optionally copy the table data using an insert select + if (structureCopyResult && structureCopyResult && copyWithContent) { + NSString *copyDataStatement = [NSString stringWithFormat:@"INSERT INTO %@.%@ SELECT * FROM %@.%@", - [targetDB backtickQuotedString], - [tableName backtickQuotedString], - [sourceDB backtickQuotedString], - [tableName backtickQuotedString] - ]; + [targetDB backtickQuotedString], + [tableName backtickQuotedString], + [sourceDB backtickQuotedString], + [tableName backtickQuotedString] + ]; + [connection queryString:copyDataStatement]; - if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Failed to copy table data", @"copy table data error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to copy a table's data.\n\nMySQL said: %@", - @"copy table data error informative message"), - [connection getLastErrorMessage]]); - return NO; - } - return YES; + if ([connection queryErrored]) return NO; + return YES; } + return structureCopyResult; } -- (BOOL)moveTable:(NSString *)tableName from: (NSString *)sourceDB to: (NSString *)targetDB { - +- (BOOL)moveTable:(NSString *)tableName from:(NSString *)sourceDB to:(NSString *)targetDB +{ NSString *moveStatement = [NSString stringWithFormat:@"RENAME TABLE %@.%@ TO %@.%@", [sourceDB backtickQuotedString], [tableName backtickQuotedString], [targetDB backtickQuotedString], [tableName backtickQuotedString] ]; - // moving the table - DLog(@"Moving table %@ from %@ to %@", tableName, sourceDB, targetDB); - DLog(@"Moving table: %@", moveStatement); + // Move the table [connection queryString:moveStatement]; - if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Failed to move table", @"move table error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to move a table.\n\nMySQL said: %@", - @"move table error informative message"), - [connection getLastErrorMessage]]); - return NO; - } + if ([connection queryErrored]) return NO; + return YES; } |