aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPDatabaseRename.m
diff options
context:
space:
mode:
Diffstat (limited to 'Source/SPDatabaseRename.m')
-rw-r--r--Source/SPDatabaseRename.m123
1 files changed, 68 insertions, 55 deletions
diff --git a/Source/SPDatabaseRename.m b/Source/SPDatabaseRename.m
index 61186678..85763c7f 100644
--- a/Source/SPDatabaseRename.m
+++ b/Source/SPDatabaseRename.m
@@ -25,95 +25,108 @@
#import "SPDBActionCommons.h"
#import "SPDatabaseRename.h"
#import "SPTableCopy.h"
-#import "SPDatabaseInfo.h"
+#import "SPViewCopy.h"
+#import "SPTablesList.h"
+
#import <SPMySQL/SPMySQL.h>
-@implementation SPDatabaseRename
+@interface SPDatabaseRename ()
-@synthesize dbInfo;
+- (BOOL)_createDatabase:(NSString *)database;
+- (BOOL)_dropDatabase:(NSString *)database;
-- (SPDatabaseInfo *)getDBInfoObject
-{
- if (dbInfo != nil) {
- return dbInfo;
- }
- else {
- dbInfo = [[SPDatabaseInfo alloc] init];
-
- [dbInfo setConnection:[self connection]];
- [dbInfo setMessageWindow:messageWindow];
- }
-
- return dbInfo;
-}
+- (void)_moveTables:(NSArray *)tables fromDatabase:(NSString *)sourceDatabase toDatabase:(NSString *)targetDatabase;
+- (void)_moveViews:(NSArray *)views fromDatabase:(NSString *)sourceDatabase toDatabase:(NSString *)targetDatabase;
-- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName
-{
- SPDatabaseInfo *databaseInfo = [self getDBInfoObject];
+@end
+
+@implementation SPDatabaseRename
- // Check, whether the source database exists and the target database doesn't.
- NSArray *tables = nil;
+- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabase to:(NSString *)targetDatabase
+{
+ NSArray *tables = nil;
+ NSArray *views = nil;
- BOOL sourceExists = [databaseInfo databaseExists:sourceDatabaseName];
- BOOL targetExists = [databaseInfo databaseExists:targetDatabaseName];
+ // Check, whether the source database exists and the target database doesn't
+ BOOL sourceExists = [[connection databases] containsObject:sourceDatabase];
+ BOOL targetExists = [[connection databases] containsObject:targetDatabase];
if (sourceExists && !targetExists) {
-
- // Retrieve the list of tables/views/funcs/triggers from the source database
- tables = [connection tablesFromDatabase:sourceDatabaseName];
+ tables = [tablesList allTableNames];
+ views = [tablesList allViewNames];
}
else {
return NO;
}
- BOOL success = [self createDatabase:targetDatabaseName];
-
- SPTableCopy *dbActionTableCopy = [[SPTableCopy alloc] init];
-
- [dbActionTableCopy setConnection:connection];
+ BOOL success = [self _createDatabase:targetDatabase];
- for (NSString *currentTable in tables)
- {
- success = [dbActionTableCopy moveTable:currentTable from:sourceDatabaseName to:targetDatabaseName];
- }
+ [self _moveTables:tables fromDatabase:sourceDatabase toDatabase:targetDatabase];
- [dbActionTableCopy release];
-
- tables = [connection tablesFromDatabase:sourceDatabaseName];
+ tables = [connection tablesFromDatabase:sourceDatabase];
if ([tables count] == 0) {
- [self dropDatabase:sourceDatabaseName];
+ [self _dropDatabase:sourceDatabase];
}
return success;
}
-- (BOOL)createDatabase:(NSString *)newDatabaseName
-{
- NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", [newDatabaseName backtickQuotedString]];
-
- [connection queryString:createStatement];
+#pragma mark -
+#pragma mark Private API
+
+/**
+ * This method creates a new database.
+ *
+ * @param NSString newDatabaseName name of the new database to be created
+ * @return BOOL YES on success, otherwise NO
+ */
+- (BOOL)_createDatabase:(NSString *)database
+{
+ [connection queryString:[NSString stringWithFormat:@"CREATE DATABASE %@", [database backtickQuotedString]]];
- if ([connection queryErrored]) return NO;
+ return ![connection queryErrored];
+}
+
+/**
+ * This method drops a database.
+ *
+ * @param NSString databaseName name of the database to drop
+ * @return BOOL YES on success, otherwise NO
+ */
+- (BOOL)_dropDatabase:(NSString *)database
+{
+ [connection queryString:[NSString stringWithFormat:@"DROP DATABASE %@", [database backtickQuotedString]]];
- return YES;
+ return ![connection queryErrored];
}
-- (BOOL)dropDatabase:(NSString *)databaseName
+- (void)_moveTables:(NSArray *)tables fromDatabase:(NSString *)sourceDatabase toDatabase:(NSString *)targetDatabase
{
- NSString *dropStatement = [NSString stringWithFormat:@"DROP DATABASE %@", [databaseName backtickQuotedString]];
+ SPTableCopy *dbActionTableCopy = [[SPTableCopy alloc] init];
- [connection queryString:dropStatement];
+ [dbActionTableCopy setConnection:connection];
- if ([connection queryErrored]) return NO;
+ for (NSString *table in tables)
+ {
+ [dbActionTableCopy moveTable:table from:sourceDatabase to:targetDatabase];
+ }
- return YES;
+ [dbActionTableCopy release];
}
-- (void)dealloc
+- (void)_moveViews:(NSArray *)views fromDatabase:(NSString *)sourceDatabase toDatabase:(NSString *)targetDatabase
{
- [dbInfo release], dbInfo = nil;
- [super dealloc];
+ SPViewCopy *dbActionViewCopy = [[SPViewCopy alloc] init];
+
+ [dbActionViewCopy setConnection:connection];
+
+ for (NSString *view in views)
+ {
+ [dbActionViewCopy moveView:view from:sourceDatabase to:targetDatabase];
+ }
+
+ [dbActionViewCopy release];
}
@end