diff options
author | Max <post@wickenrode.com> | 2015-03-12 02:17:31 +0100 |
---|---|---|
committer | Max <post@wickenrode.com> | 2015-03-12 02:17:31 +0100 |
commit | 8cc66aa348870e6cdf086500515848b07ea5aa06 (patch) | |
tree | 4b5c9ff7a7df2f85da06c0342cda7741f6584b32 | |
parent | 8b1ff9c9b8a996ff0c6321e58709c25f0c5763c1 (diff) | |
download | sequelpro-8cc66aa348870e6cdf086500515848b07ea5aa06.tar.gz sequelpro-8cc66aa348870e6cdf086500515848b07ea5aa06.tar.bz2 sequelpro-8cc66aa348870e6cdf086500515848b07ea5aa06.zip |
Fix Sequel Pro forgetting database charset when renaming or copying a database (#2082)
(While we're at it, also removed some duplicate CREATE DATABASE code)
-rw-r--r-- | Source/SPDatabaseAction.h | 27 | ||||
-rw-r--r-- | Source/SPDatabaseAction.m | 45 | ||||
-rw-r--r-- | Source/SPDatabaseCopy.h | 16 | ||||
-rw-r--r-- | Source/SPDatabaseCopy.m | 34 | ||||
-rw-r--r-- | Source/SPDatabaseDocument.h | 2 | ||||
-rw-r--r-- | Source/SPDatabaseDocument.m | 38 | ||||
-rw-r--r-- | Source/SPDatabaseRename.h | 8 | ||||
-rw-r--r-- | Source/SPDatabaseRename.m | 53 |
8 files changed, 132 insertions, 91 deletions
diff --git a/Source/SPDatabaseAction.h b/Source/SPDatabaseAction.h index 00de9065..52af621f 100644 --- a/Source/SPDatabaseAction.h +++ b/Source/SPDatabaseAction.h @@ -31,6 +31,14 @@ @class SPTablesList; @class SPMySQLConnection; +@interface SPCreateDatabaseInfo : NSObject + +@property (readwrite,retain) NSString *databaseName; +@property (readwrite,retain) NSString *defaultEncoding; +@property (readwrite,retain) NSString *defaultCollation; + +@end + @interface SPDatabaseAction : NSObject { NSWindow *messageWindow; @@ -53,4 +61,23 @@ */ @property (readwrite, assign) SPTablesList *tablesList; +/** + * This method creates a new database. + * + * @param dbInfo database name/charset/collation (charset, collation may be nil) + * @return success + * @see createDatabase:withEncoding:collation: + */ +- (BOOL)createDatabase:(SPCreateDatabaseInfo *)dbInfo; + +/** + * This method creates a new database. + * + * @param database name of the new database to be created + * @param encoding charset of the new database (can be nil to skip) + * @param collation sorting collation of the new database (can be nil. Will be ignored if encoding == nil) + * @return YES on success, otherwise NO + */ +- (BOOL)createDatabase:(NSString *)database withEncoding:(NSString *)encoding collation:(NSString *)collation; + @end diff --git a/Source/SPDatabaseAction.m b/Source/SPDatabaseAction.m index 998aff95..f5ed8b60 100644 --- a/Source/SPDatabaseAction.m +++ b/Source/SPDatabaseAction.m @@ -30,10 +30,55 @@ #import "SPDatabaseAction.h" +#import <SPMySQL/SPMySQL.h> + +@implementation SPCreateDatabaseInfo + +@synthesize databaseName; +@synthesize defaultEncoding; +@synthesize defaultCollation; + +- (void)dealloc +{ + [self setDatabaseName:nil]; + [self setDefaultEncoding:nil]; + [self setDefaultCollation:nil]; + [super dealloc]; +} + +@end + +#pragma mark - + @implementation SPDatabaseAction @synthesize connection; @synthesize messageWindow; @synthesize tablesList; +- (BOOL)createDatabase:(SPCreateDatabaseInfo *)dbInfo +{ + return [self createDatabase:[dbInfo databaseName] + withEncoding:[dbInfo defaultEncoding] + collation:[dbInfo defaultCollation]]; +} + +- (BOOL)createDatabase:(NSString *)database withEncoding:(NSString *)encoding collation:(NSString *)collation +{ + NSParameterAssert(database != nil && [database length] > 0); + + NSMutableString *query = [NSMutableString stringWithFormat:@"CREATE DATABASE %@", [database backtickQuotedString]]; + + if([encoding length]) { // [nil length] == 0 + [query appendFormat:@" DEFAULT CHARACTER SET = %@",[encoding backtickQuotedString]]; + if([collation length]) { + [query appendFormat:@" DEFAULT COLLATE = %@",[collation backtickQuotedString]]; + } + } + + [connection queryString:query]; + + return ![connection queryErrored]; +} + @end diff --git a/Source/SPDatabaseCopy.h b/Source/SPDatabaseCopy.h index 081378e2..3adf7fa0 100644 --- a/Source/SPDatabaseCopy.h +++ b/Source/SPDatabaseCopy.h @@ -38,18 +38,10 @@ /** * This method clones an existing database. * - * @param NSString sourceDatabaseName the name of the source database - * @param NSString targetDatabaseName the name of the target database - * @result BOOL success + * @param sourceDatabase information tuple about source database + * @param targetDatabaseName the name of the target database + * @result success */ -- (BOOL)copyDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName withContent:(BOOL)copyWithContent; - -/** - * 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 *)newDatabaseName; +- (BOOL)copyDatabaseFrom:(SPCreateDatabaseInfo *)sourceDatabase to:(NSString *)targetDatabaseName withContent:(BOOL)copyWithContent; @end diff --git a/Source/SPDatabaseCopy.m b/Source/SPDatabaseCopy.m index 5ea0a2d5..94a5e896 100644 --- a/Source/SPDatabaseCopy.m +++ b/Source/SPDatabaseCopy.m @@ -35,46 +35,34 @@ @implementation SPDatabaseCopy -- (BOOL)copyDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName withContent:(BOOL)copyWithContent +- (BOOL)copyDatabaseFrom:(SPCreateDatabaseInfo *)sourceDatabase to:(NSString *)targetDatabaseName withContent:(BOOL)copyWithContent { NSArray *tables = nil; // Check whether the source database exists and the target database doesn't. - BOOL sourceExists = [[connection databases] containsObject:sourceDatabaseName]; + BOOL sourceExists = [[connection databases] containsObject:[sourceDatabase databaseName]]; BOOL targetExists = [[connection databases] containsObject:targetDatabaseName]; - if (sourceExists && !targetExists) { - - // Retrieve the list of tables/views/funcs/triggers from the source database - tables = [connection tablesFromDatabase:sourceDatabaseName]; - } - else { + if (!sourceExists || targetExists) return NO; - } - + + // Retrieve the list of tables/views/funcs/triggers from the source database + tables = [connection tablesFromDatabase:[sourceDatabase databaseName]]; + // Abort if database creation failed - if (![self createDatabase:targetDatabaseName]) return NO; + if (![self createDatabase:targetDatabaseName + withEncoding:[sourceDatabase defaultEncoding] + collation:[sourceDatabase defaultCollation]]) return NO; SPTableCopy *dbActionTableCopy = [[SPTableCopy alloc] init]; [dbActionTableCopy setConnection:connection]; - BOOL success = [dbActionTableCopy copyTables:tables from:sourceDatabaseName to:targetDatabaseName withContent:copyWithContent]; + BOOL success = [dbActionTableCopy copyTables:tables from:[sourceDatabase databaseName] to:targetDatabaseName withContent:copyWithContent]; [dbActionTableCopy release]; return success; } -- (BOOL)createDatabase:(NSString *)newDatabaseName -{ - NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", [newDatabaseName backtickQuotedString]]; - - [connection queryString:createStatement]; - - if ([connection queryErrored]) return NO; - - return YES; -} - @end diff --git a/Source/SPDatabaseDocument.h b/Source/SPDatabaseDocument.h index 0cd485b9..23b22d4a 100644 --- a/Source/SPDatabaseDocument.h +++ b/Source/SPDatabaseDocument.h @@ -56,6 +56,7 @@ @class SPMySQLConnection; @class SPCharsetCollationHelper; @class SPGotoDatabaseController; +@class SPCreateDatabaseInfo; #import "SPDatabaseContentViewDelegate.h" #import "SPConnectionControllerDelegateProtocol.h" @@ -451,6 +452,7 @@ #endif - (NSArray *)allTableNames; - (SPTablesList *)tablesListInstance; +- (SPCreateDatabaseInfo *)createDatabaseInfo; #ifndef SP_CODA /* method decls */ // Notification center methods diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m index 7f531f60..506e78f6 100644 --- a/Source/SPDatabaseDocument.m +++ b/Source/SPDatabaseDocument.m @@ -2769,6 +2769,17 @@ static NSString *SPAlterDatabaseAction = @"SPAlterDatabase"; return tablesListInstance; } +- (SPCreateDatabaseInfo *)createDatabaseInfo +{ + SPCreateDatabaseInfo *dbInfo = [[SPCreateDatabaseInfo alloc] init]; + + [dbInfo setDatabaseName:[self database]]; + [dbInfo setDefaultEncoding:[databaseDataInstance getDatabaseDefaultCharacterSet]]; + [dbInfo setDefaultCollation:[databaseDataInstance getDatabaseDefaultCollation]]; + + return [dbInfo autorelease]; +} + #pragma mark - #pragma mark Notification center methods @@ -5903,7 +5914,7 @@ static NSString *SPAlterDatabaseAction = @"SPAlterDatabase"; BOOL copyWithContent = [copyDatabaseDataButton state] == NSOnState; - if ([dbActionCopy copyDatabaseFrom:[self database] to:[databaseCopyNameField stringValue] withContent:copyWithContent]) { + if ([dbActionCopy copyDatabaseFrom:[self createDatabaseInfo] to:[databaseCopyNameField stringValue] withContent:copyWithContent]) { [self selectDatabase:[databaseCopyNameField stringValue] item:nil]; } else { @@ -5934,7 +5945,7 @@ static NSString *SPAlterDatabaseAction = @"SPAlterDatabase"; [dbActionRename setConnection:[self getConnection]]; [dbActionRename setMessageWindow:parentWindow]; - if ([dbActionRename renameDatabaseFrom:[self database] to:newDatabaseName]) { + if ([dbActionRename renameDatabaseFrom:[self createDatabaseInfo] to:newDatabaseName]) { [self setDatabases:self]; [self selectDatabase:newDatabaseName item:nil]; } @@ -5974,25 +5985,16 @@ static NSString *SPAlterDatabaseAction = @"SPAlterDatabase"; // As we're amending identifiers, ensure UTF8 if (![[mySQLConnection encoding] isEqualToString:@"utf8"]) [mySQLConnection setEncoding:@"utf8"]; - NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", [[databaseNameField stringValue] backtickQuotedString]]; - - // If there is an encoding selected other than the default we must specify it in CREATE DATABASE statement - NSString *encodingName = [addDatabaseCharsetHelper selectedCharset]; - if (encodingName) - createStatement = [NSString stringWithFormat:@"%@ DEFAULT CHARACTER SET %@", createStatement, [encodingName backtickQuotedString]]; + SPDatabaseAction *dbAction = [[SPDatabaseAction alloc] init]; + [dbAction setConnection:mySQLConnection]; + BOOL res = [dbAction createDatabase:[databaseNameField stringValue] + withEncoding:[addDatabaseCharsetHelper selectedCharset] + collation:[addDatabaseCharsetHelper selectedCollation]]; + [dbAction release]; - // If there is a collation selected other than the default we must specify it in the CREATE DATABASE statement - NSString *collationName = [addDatabaseCharsetHelper selectedCollation]; - if (collationName) - createStatement = [NSString stringWithFormat:@"%@ DEFAULT COLLATE %@", createStatement, [collationName backtickQuotedString]]; - - // Create the database - [mySQLConnection queryString:createStatement]; - - if ([mySQLConnection queryErrored]) { + if (!res) { // An error occurred SPBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, parentWindow, self, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"Couldn't create database.\nMySQL said: %@", @"message of panel when creation of db failed"), [mySQLConnection lastErrorMessage]]); - return; } diff --git a/Source/SPDatabaseRename.h b/Source/SPDatabaseRename.h index 2f9cb4db..becf460e 100644 --- a/Source/SPDatabaseRename.h +++ b/Source/SPDatabaseRename.h @@ -38,10 +38,10 @@ /** * This method renames an existing database. * - * @param NSString sourceDatabaseName the name of the source database - * @param NSString targetDatabaseName the name of the target database - * @result BOOL success + * @param sourceDatabase information tuple about the source database + * @param targetDatabase the name of the target database + * @result success */ -- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabase to:(NSString *)targetDatabase; +- (BOOL)renameDatabaseFrom:(SPCreateDatabaseInfo *)sourceDatabase to:(NSString *)targetDatabase; @end diff --git a/Source/SPDatabaseRename.m b/Source/SPDatabaseRename.m index b07f513d..04f71182 100644 --- a/Source/SPDatabaseRename.m +++ b/Source/SPDatabaseRename.m @@ -37,7 +37,6 @@ @interface SPDatabaseRename () -- (BOOL)_createDatabase:(NSString *)database; - (BOOL)_dropDatabase:(NSString *)database; - (void)_moveTables:(NSArray *)tables fromDatabase:(NSString *)sourceDatabase toDatabase:(NSString *)targetDatabase; @@ -47,33 +46,32 @@ @implementation SPDatabaseRename -- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabase to:(NSString *)targetDatabase +- (BOOL)renameDatabaseFrom:(SPCreateDatabaseInfo *)sourceDatabase to:(NSString *)targetDatabase { - NSArray *tables = nil; - NSArray *views = nil; - // Check, whether the source database exists and the target database doesn't - BOOL sourceExists = [[connection databases] containsObject:sourceDatabase]; + BOOL sourceExists = [[connection databases] containsObject:[sourceDatabase databaseName]]; BOOL targetExists = [[connection databases] containsObject:targetDatabase]; - if (sourceExists && !targetExists) { - tables = [tablesList allTableNames]; - views = [tablesList allViewNames]; - } - else { - return NO; - } - - BOOL success = [self _createDatabase:targetDatabase]; + if (!sourceExists || targetExists) return NO; + + NSArray *tables = [tablesList allTableNames]; + NSArray *views = [tablesList allViewNames]; - [self _moveTables:tables fromDatabase:sourceDatabase toDatabase:targetDatabase]; + BOOL success = [self createDatabase:targetDatabase + withEncoding:[sourceDatabase defaultEncoding] + collation:[sourceDatabase defaultCollation]]; - tables = [connection tablesFromDatabase:sourceDatabase]; - + [self _moveTables:tables fromDatabase:[sourceDatabase databaseName] toDatabase:targetDatabase]; + +#warning Section disabled because it might destroy data (views, functions, events, ...) +/* + tables = [connection tablesFromDatabase:[sourceDatabase databaseName]]; + if ([tables count] == 0) { - [self _dropDatabase:sourceDatabase]; - } - + [self _dropDatabase:[sourceDatabase databaseName]]; + } +*/ + return success; } @@ -81,19 +79,6 @@ #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]]]; - - return ![connection queryErrored]; -} - -/** * This method drops a database. * * @param NSString databaseName name of the database to drop |