diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPDBActionCommons.h | 5 | ||||
-rw-r--r-- | Source/SPDatabaseCopy.h | 4 | ||||
-rw-r--r-- | Source/SPDatabaseCopy.m | 59 | ||||
-rw-r--r-- | Source/SPDatabaseDocument.m | 45 | ||||
-rw-r--r-- | Source/SPDatabaseInfo.m | 28 | ||||
-rw-r--r-- | Source/SPDatabaseRename.h | 9 | ||||
-rw-r--r-- | Source/SPDatabaseRename.m | 94 | ||||
-rw-r--r-- | Source/SPTableCopy.m | 87 |
8 files changed, 156 insertions, 175 deletions
diff --git a/Source/SPDBActionCommons.h b/Source/SPDBActionCommons.h index 1b7185bd..6ff6e11c 100644 --- a/Source/SPDBActionCommons.h +++ b/Source/SPDBActionCommons.h @@ -23,7 +23,6 @@ // More info at <http://code.google.com/p/sequel-pro/> #import <MCPKit/MCPKit.h> -#import "SPAlertSheets.h" @interface SPDBActionCommons : NSObject { @@ -32,12 +31,12 @@ } /** - * @property MCPConnection references the MCPKit connection to MySQL; it has to be set. + * @property connection References the MCPKit connection to MySQL; it has to be set. */ @property (retain) MCPConnection *connection; /** - * @property the NSWindow instance to send message sheets to + * @property messageWindow The NSWindow instance to send message sheets to. */ @property (assign) NSWindow *messageWindow; diff --git a/Source/SPDatabaseCopy.h b/Source/SPDatabaseCopy.h index a146bbf4..56a7e26a 100644 --- a/Source/SPDatabaseCopy.h +++ b/Source/SPDatabaseCopy.h @@ -53,7 +53,7 @@ * @param NSString targetDatabaseName the name of the target database * @result BOOL success */ -- (BOOL)copyDatabaseFrom: (NSString *)sourceDatabaseName to: (NSString *)targetDatabaseName withContent: (BOOL)copyWithContent; +- (BOOL)copyDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName withContent:(BOOL)copyWithContent; /** * This method creates a new database. @@ -61,6 +61,6 @@ * @param NSString newDatabaseName name of the new database to be created * @return BOOL YES on success, otherwise NO */ -- (BOOL) createDatabase: (NSString *)newDatabaseName; +- (BOOL)createDatabase:(NSString *)newDatabaseName; @end diff --git a/Source/SPDatabaseCopy.m b/Source/SPDatabaseCopy.m index 913554c2..8942dee1 100644 --- a/Source/SPDatabaseCopy.m +++ b/Source/SPDatabaseCopy.m @@ -30,41 +30,44 @@ @synthesize dbInfo; -- (SPDatabaseInfo *)getDBInfoObject { +- (SPDatabaseInfo *)getDBInfoObject +{ if (dbInfo != nil) { return dbInfo; - } else { + } + else { dbInfo = [[SPDatabaseInfo alloc] init]; + [dbInfo setConnection:[self connection]]; [dbInfo setMessageWindow:messageWindow]; } + return dbInfo; } -- (BOOL)copyDatabaseFrom: (NSString *)sourceDatabaseName to: (NSString *)targetDatabaseName withContent:(BOOL)copyWithContent { - +- (BOOL)copyDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName withContent:(BOOL)copyWithContent +{ SPDatabaseInfo *databaseInfo = [self getDBInfoObject]; - // check, whether the source database exists and the target database doesn't. + + // Check, whether the source database exists and the target database doesn't. NSArray *tables = [NSArray array]; + BOOL sourceExists = [databaseInfo databaseExists:sourceDatabaseName]; BOOL targetExists = [databaseInfo databaseExists:targetDatabaseName]; + if (sourceExists && !targetExists) { - // retrieve the list of tables/views/funcs/triggers from the source database - - tables = [connection listTablesFromDB:sourceDatabaseName]; - } else { - SPBeginAlertSheet(NSLocalizedString(@"Cannot create existing database", @"create database exists error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to create the target database.\n\nDatabase %@ already exists.", - @"create database error informative message"), - targetDatabaseName]); + // Retrieve the list of tables/views/funcs/triggers from the source database + tables = [connection listTablesFromDB:sourceDatabaseName]; + } + else { return NO; } - DLog(@"list of found tables of source db: %@", tables); - + BOOL success = [self createDatabase:targetDatabaseName]; + SPTableCopy *dbActionTableCopy = [[SPTableCopy alloc] init]; + [dbActionTableCopy setConnection:connection]; for (NSString *currentTable in tables) { @@ -82,26 +85,20 @@ return success; } -- (BOOL) createDatabase: (NSString *)newDatabaseName { - NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", - [newDatabaseName backtickQuotedString]]; +- (BOOL)createDatabase:(NSString *)newDatabaseName +{ + NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", [newDatabaseName backtickQuotedString]]; + [connection queryString:createStatement]; - if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Failed to create database", @"create database error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to create the target database.\n\nMySQL said: %@", - @"create database error informative message"), - [connection getLastErrorMessage]]); - return NO; - } - return YES; - + if ([connection queryErrored]) return NO; + return YES; } -- (void)dealloc { - [dbInfo dealloc]; +- (void)dealloc +{ + [dbInfo release], dbInfo = nil; } diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m index 3a0e029d..27e773b9 100644 --- a/Source/SPDatabaseDocument.m +++ b/Source/SPDatabaseDocument.m @@ -4673,41 +4673,60 @@ @implementation SPDatabaseDocument (PrivateAPI) -- (void)_copyDatabase { +- (void)_copyDatabase +{ if ([[databaseCopyNameField stringValue] isEqualToString:@""]) { SPBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, parentWindow, self, nil, nil, NSLocalizedString(@"Database must have a name.", @"message of panel when no db name is given")); return; } + SPDatabaseCopy *dbActionCopy = [[SPDatabaseCopy alloc] init]; - [dbActionCopy setConnection: [self getConnection]]; - [dbActionCopy setMessageWindow: parentWindow]; + + [dbActionCopy setConnection:[self getConnection]]; + [dbActionCopy setMessageWindow:parentWindow]; BOOL copyWithContent = [copyDatabaseDataButton state] == NSOnState; - if ([dbActionCopy copyDatabaseFrom: [self database] - to: [databaseCopyNameField stringValue] - withContent: copyWithContent]) { + if ([dbActionCopy copyDatabaseFrom:[self database] to:[databaseCopyNameField stringValue] withContent:copyWithContent]) { [self selectDatabase:[databaseCopyNameField stringValue] item:nil]; } + else { + SPBeginAlertSheet(NSLocalizedString(@"Unable to copy database", @"unable to copy database message"), + NSLocalizedString(@"OK", @"OK button"), nil, nil, parentWindow, self, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to copy the database '%@' to '%@'.", @"unable to copy database message informative message"), [self database], [databaseCopyNameField stringValue]]); + } + [dbActionCopy release]; - [self setDatabases: self]; + + // Update DB list + [self setDatabases:self]; } -- (void)_renameDatabase { +- (void)_renameDatabase +{ if ([[databaseRenameNameField stringValue] isEqualToString:@""]) { SPBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, parentWindow, self, nil, nil, NSLocalizedString(@"Database must have a name.", @"message of panel when no db name is given")); return; } + SPDatabaseRename *dbActionRename = [[SPDatabaseRename alloc] init]; - [dbActionRename setConnection: [self getConnection]]; - [dbActionRename setMessageWindow: parentWindow]; - if ([dbActionRename renameDatabaseFrom: [self database] - to: [databaseRenameNameField stringValue]]) { + [dbActionRename setConnection:[self getConnection]]; + [dbActionRename setMessageWindow:parentWindow]; + + if ([dbActionRename renameDatabaseFrom:[self database] to:[databaseRenameNameField stringValue]]) { [self selectDatabase:[databaseRenameNameField stringValue] item:nil]; } + else { + SPBeginAlertSheet(NSLocalizedString(@"Unable to rename database", @"unable to rename database message"), + NSLocalizedString(@"OK", @"OK button"), nil, nil, parentWindow, self, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to rename the database '%@' to '%@'.", @"unable to rename database message informative message"), [self database], [databaseRenameNameField stringValue]]); + } + [dbActionRename release]; - [self setDatabases: self]; + + // Update DB list + [self setDatabases:self]; } /** diff --git a/Source/SPDatabaseInfo.m b/Source/SPDatabaseInfo.m index 551a1c77..3c70aec9 100644 --- a/Source/SPDatabaseInfo.m +++ b/Source/SPDatabaseInfo.m @@ -27,42 +27,44 @@ @implementation SPDatabaseInfo --(BOOL)databaseExists:(NSString *)databaseName { +-(BOOL)databaseExists:(NSString *)databaseName +{ NSArray *names = [self listDBs]; + return [names containsObject:databaseName]; } -- (NSArray *)listDBs { +- (NSArray *)listDBs +{ return [self listDBsLike:nil]; } - (NSArray *)listDBsLike:(NSString *)dbsName { NSString *listDBStatement = nil; + if ((dbsName == nil) || ([dbsName isEqualToString:@""])) { listDBStatement = [NSString stringWithFormat:@"SHOW DATABASES"]; } else { listDBStatement = [NSString stringWithFormat:@"SHOW DATABASES LIKE %@", [dbsName backtickQuotedString]]; } - DLog(@"running query : %@", listDBStatement); + MCPResult *theResult = [connection queryString:listDBStatement]; - if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Failed to retrieve databases list", @"database list error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to retrieve a list of databases.\n\nMySQL said: %@", - @"database list error informative message"), - [connection getLastErrorMessage]]); - return NO; - } + if ([connection queryErrored]) return NO; NSMutableArray *names = [NSMutableArray array]; NSMutableString *name; + if ([theResult numOfRows] > 1) { - int i; - for ( i = 0 ; i < [theResult numOfRows] ; i++ ) { + + NSUInteger i; + + for (i = 0 ; i < [theResult numOfRows]; i++) + { name = [[theResult fetchRowAsArray] objectAtIndex:0]; + [names addObject:name]; } } diff --git a/Source/SPDatabaseRename.h b/Source/SPDatabaseRename.h index 6273c4dd..e679f476 100644 --- a/Source/SPDatabaseRename.h +++ b/Source/SPDatabaseRename.h @@ -23,7 +23,8 @@ // More info at <http://code.google.com/p/sequel-pro/> #import "SPDBActionCommons.h" -#import "SPDatabaseInfo.h" + +@class SPDatabaseInfo; /** * The SPDatabaseRename class povides functionality to rename a database. @@ -53,7 +54,7 @@ * @param NSString targetDatabaseName the name of the target database * @result BOOL success */ -- (BOOL)renameDatabaseFrom: (NSString *)sourceDatabaseName to: (NSString *)targetDatabaseName; +- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName; /** * This method creates a new database. @@ -61,7 +62,7 @@ * @param NSString newDatabaseName name of the new database to be created * @return BOOL YES on success, otherwise NO */ -- (BOOL) createDatabase: (NSString *)newDatabaseName; +- (BOOL)createDatabase:(NSString *)newDatabaseName; /** * This method drops a database. @@ -69,6 +70,6 @@ * @param NSString databaseName name of the database to drop * @return BOOL YES on success, otherwise NO */ -- (BOOL) dropDatabase: (NSString *)databaseName; +- (BOOL)dropDatabase:(NSString *)databaseName; @end diff --git a/Source/SPDatabaseRename.m b/Source/SPDatabaseRename.m index 33d01724..b565b406 100644 --- a/Source/SPDatabaseRename.m +++ b/Source/SPDatabaseRename.m @@ -30,97 +30,85 @@ @synthesize dbInfo; -- (SPDatabaseInfo *)getDBInfoObject { +- (SPDatabaseInfo *)getDBInfoObject +{ if (dbInfo != nil) { return dbInfo; - } else { + } + else { dbInfo = [[SPDatabaseInfo alloc] init]; + [dbInfo setConnection:[self connection]]; [dbInfo setMessageWindow:messageWindow]; } + return dbInfo; } -- (BOOL)renameDatabaseFrom: (NSString *)sourceDatabaseName to: (NSString *)targetDatabaseName { +- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName +{ SPDatabaseInfo *databaseInfo = [self getDBInfoObject]; - // check, whether the source database exists and the target database doesn't. + // Check, whether the source database exists and the target database doesn't. NSArray *tables = [NSArray array]; + BOOL sourceExists = [databaseInfo databaseExists:sourceDatabaseName]; BOOL targetExists = [databaseInfo databaseExists:targetDatabaseName]; + if (sourceExists && !targetExists) { - // retrieve the list of tables/views/funcs/triggers from the source database + // Retrieve the list of tables/views/funcs/triggers from the source database tables = [connection listTablesFromDB:sourceDatabaseName]; - } else { - SPBeginAlertSheet(NSLocalizedString(@"Cannot create existing database", @"create database exists error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to create the target database.\n\nDatabase %@ already exists.", - @"create database error informative message"), - targetDatabaseName]); + } + else { return NO; } - DLog(@"list of found tables of source db: %@", tables); - + BOOL success = [self createDatabase:targetDatabaseName]; + SPTableCopy *dbActionTableCopy = [[SPTableCopy alloc] init]; + [dbActionTableCopy setConnection:connection]; - for (NSString *currentTable in tables) { - if ([dbActionTableCopy moveTable:currentTable - from:sourceDatabaseName - to:targetDatabaseName]) { - } else { - success = FALSE; - } + for (NSString *currentTable in tables) + { + success = [dbActionTableCopy moveTable:currentTable from:sourceDatabaseName to:targetDatabaseName]; } + tables = [connection listTablesFromDB:sourceDatabaseName]; + if ([tables count] == 0) { [self dropDatabase:sourceDatabaseName]; - } else { - SPBeginAlertSheet(NSLocalizedString(@"Failed to delete database", @"delete database error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"Database %@ not empty, skipping drop database.", - @"delete database not empty error informative message"), - sourceDatabaseName]); - } + } + return success; } -- (BOOL) createDatabase: (NSString *)newDatabaseName { - NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", - [newDatabaseName backtickQuotedString]]; +- (BOOL)createDatabase:(NSString *)newDatabaseName +{ + NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", [newDatabaseName backtickQuotedString]]; + [connection queryString:createStatement]; - if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Failed to create database", @"create database error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to create a database.\n\nMySQL said: %@", - @"create database error informative message"), - [connection getLastErrorMessage]]); - return NO; - } - return YES; + if ([connection queryErrored]) return NO; + + return YES; } -- (BOOL) dropDatabase: (NSString *)databaseName { - NSString *dropStatement = [NSString stringWithFormat:@"DROP DATABASE %@", - [databaseName backtickQuotedString]]; +- (BOOL)dropDatabase:(NSString *)databaseName +{ + NSString *dropStatement = [NSString stringWithFormat:@"DROP DATABASE %@", [databaseName backtickQuotedString]]; + [connection queryString:dropStatement]; - if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Failed to drop database", @"drop database error message"), - NSLocalizedString(@"OK", @"OK button"), nil, nil, messageWindow, self, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to drop a database.\n\nMySQL said: %@", - @"drop database error informative message"), - [connection getLastErrorMessage]]); - return NO; - } - return YES; + if ([connection queryErrored]) return NO; + + return YES; } -- (void)dealloc { - [dbInfo dealloc]; +- (void)dealloc +{ + [dbInfo release], dbInfo = nil; } @end
\ No newline at end of file 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; } |