diff options
author | dmoagx <post@wickenrode.com> | 2013-03-10 03:54:28 +0000 |
---|---|---|
committer | dmoagx <post@wickenrode.com> | 2013-03-10 03:54:28 +0000 |
commit | 4db3922e4777aac10855d457ba4f2962fcd8fd8c (patch) | |
tree | 80b3bc537aecc922888f4c30e2536a6d1e999e91 /Source/SPDatabaseData.m | |
parent | f551b70055c726fc2c96d93acfe3dfd1ac6c1f31 (diff) | |
download | sequelpro-4db3922e4777aac10855d457ba4f2962fcd8fd8c.tar.gz sequelpro-4db3922e4777aac10855d457ba4f2962fcd8fd8c.tar.bz2 sequelpro-4db3922e4777aac10855d457ba4f2962fcd8fd8c.zip |
* Add support for querying the server default charset and collation
* Move a bit of redundant code into a method
Diffstat (limited to 'Source/SPDatabaseData.m')
-rw-r--r-- | Source/SPDatabaseData.m | 89 |
1 files changed, 63 insertions, 26 deletions
diff --git a/Source/SPDatabaseData.m b/Source/SPDatabaseData.m index 835c7328..787f5e1e 100644 --- a/Source/SPDatabaseData.m +++ b/Source/SPDatabaseData.m @@ -38,6 +38,7 @@ @interface SPDatabaseData () +- (NSString *)_getSingleVariableValue:(NSString *)variable; - (NSArray *)_getDatabaseDataForQuery:(NSString *)query; + (NSArray *)_relabelCollationResult:(NSArray *)data; @@ -61,6 +62,8 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, characterSetEncoding = nil; defaultCollation = nil; defaultCharacterSetEncoding = nil; + serverDefaultCollation = nil; + serverDefaultCharacterSetEncoding = nil; collations = [[NSMutableArray alloc] init]; characterSetCollations = [[NSMutableArray alloc] init]; @@ -84,6 +87,8 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, if (characterSetEncoding != nil) [characterSetEncoding release], characterSetEncoding = nil; if (defaultCollation != nil) [defaultCollation release], defaultCollation = nil; if (defaultCharacterSetEncoding != nil) [defaultCharacterSetEncoding release], defaultCharacterSetEncoding = nil; + if (serverDefaultCharacterSetEncoding) [serverDefaultCharacterSetEncoding release], serverDefaultCharacterSetEncoding = nil; + if (serverDefaultCollation) [serverDefaultCollation release], serverDefaultCollation = nil; [collations removeAllObjects]; [characterSetCollations removeAllObjects]; @@ -191,15 +196,10 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, [storageEngines addObject:[NSDictionary dictionaryWithObject:@"MyISAM" forKey:@"Engine"]]; // Check if InnoDB support is enabled - SPMySQLResult *result = [connection queryString:@"SHOW VARIABLES LIKE 'have_innodb'"]; + NSString *result = [self _getSingleVariableValue:@"have_innodb"]; - [result setReturnDataAsStrings:YES]; - - if ([result numberOfRows] == 1) { - if ([[[result getRowAsDictionary] objectForKey:@"Value"] isEqualToString:@"YES"]) { - [storageEngines addObject:[NSDictionary dictionaryWithObject:@"InnoDB" forKey:@"Engine"]]; - } - } + if(result && [result isEqualToString:@"YES"]) + [storageEngines addObject:[NSDictionary dictionaryWithObject:@"InnoDB" forKey:@"Engine"]]; // Before MySQL 4.1 the MEMORY engine was known as HEAP and the ISAM engine was included if ([serverSupport supportsPre41StorageEngines]) { @@ -319,13 +319,9 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, if (!defaultCharacterSetEncoding) { [defaultCharacterSetEncoding release]; - NSString *variable = [serverSupport supportsCharacterSetDatabaseVar] ? @"character_set_database" : @"character_set"; - - SPMySQLResult *result = [connection queryString:[NSString stringWithFormat:@"SHOW VARIABLES LIKE %@", [variable tickQuotedString]]]; - - [result setReturnDataAsStrings:YES]; + NSString *variable = [serverSupport supportsCharacterSetAndCollationVars] ? @"character_set_database" : @"character_set"; - defaultCharacterSetEncoding = [[[result getRowAsDictionary] objectForKey:@"Value"] retain]; + defaultCharacterSetEncoding = [[self _getSingleVariableValue:variable] retain]; } return defaultCharacterSetEncoding; @@ -341,14 +337,44 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, if (!defaultCollation) { [defaultCollation release]; - SPMySQLResult *result = [connection queryString:@"SHOW VARIABLES LIKE 'collation_database'"]; + defaultCollation = [[self _getSingleVariableValue:@"collation_database"] retain]; + } + + return defaultCollation; +} + +/** + * Returns the server's default character set encoding. + * + * @return The default encoding as a string + */ +- (NSString *)getServerDefaultCharacterSet +{ + if (!serverDefaultCharacterSetEncoding) { + [serverDefaultCharacterSetEncoding release]; - [result setReturnDataAsStrings:YES]; + NSString *variable = [serverSupport supportsCharacterSetAndCollationVars] ? @"character_set_server" : @"character_set"; - defaultCollation = [[[result getRowAsDictionary] objectForKey:@"Value"] retain]; + serverDefaultCharacterSetEncoding = [[self _getSingleVariableValue:variable] retain]; } + + return serverDefaultCharacterSetEncoding; +} + +/** + * Returns the server's default collation. + * + * @return The default collation as a string (nil on MySQL 3 databases) + */ +- (NSString *)getServerDefaultCollation +{ + if (!serverDefaultCollation) { + [serverDefaultCollation release]; - return defaultCollation; + serverDefaultCollation = [[self _getSingleVariableValue:@"collation_server"] retain]; + } + + return serverDefaultCollation; } /** @@ -375,11 +401,7 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, } // Retrieve the corresponding value for the determined key, ensuring return as a string - SPMySQLResult *result = [connection queryString:[NSString stringWithFormat:@"SHOW VARIABLES LIKE %@", [storageEngineKey tickQuotedString]]];; - - [result setReturnDataAsStrings:YES]; - - defaultStorageEngine = [[[result getRowAsDictionary] objectForKey:@"Value"] retain]; + defaultStorageEngine = [[self _getSingleVariableValue:storageEngineKey] retain]; } return defaultStorageEngine; @@ -389,6 +411,23 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, #pragma mark Private API /** + * Look up the value of a single server variable + * @param variable The name of a server variable. Must not contain wildcards + * @return The value as string or nil if no such variable exists or the result is ambigious + */ +- (NSString *)_getSingleVariableValue:(NSString *)variable +{ + SPMySQLResult *result = [connection queryString:[NSString stringWithFormat:@"SHOW VARIABLES LIKE %@", [variable tickQuotedString]]];; + + [result setReturnDataAsStrings:YES]; + + if ([result numberOfRows] != 1) + return nil; + + return [[result getRowAsDictionary] objectForKey:@"Value"]; +} + +/** * Executes the supplied query against the current connection and returns the result as an array of * NSDictionarys, one for each row. */ @@ -455,9 +494,7 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, - (void)dealloc { - if (characterSetEncoding) [characterSetEncoding release], characterSetEncoding = nil; - if (defaultCharacterSetEncoding) [defaultCharacterSetEncoding release], defaultCharacterSetEncoding = nil; - if (defaultCollation) [defaultCollation release], defaultCollation = nil; + [self resetAllData]; [collations release], collations = nil; [characterSetCollations release], characterSetCollations = nil; |