From f1e829f98f50d9e75e7ba111361a625f7aab802d Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Fri, 13 Aug 2010 16:48:59 +0000 Subject: Tidy up database renaming and copying by removing the use of alert dialogs within loops to prevent locking up the main thread. These operations ideally should also be threaded. --- Source/SPTableCopy.m | 87 +++++++++++++++++++--------------------------------- 1 file changed, 31 insertions(+), 56 deletions(-) (limited to 'Source/SPTableCopy.m') 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; } -- cgit v1.2.3