From 4db3922e4777aac10855d457ba4f2962fcd8fd8c Mon Sep 17 00:00:00 2001 From: dmoagx Date: Sun, 10 Mar 2013 03:54:28 +0000 Subject: * Add support for querying the server default charset and collation * Move a bit of redundant code into a method --- Source/SPDatabaseData.m | 89 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 26 deletions(-) (limited to 'Source/SPDatabaseData.m') 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; @@ -388,6 +410,23 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, #pragma mark - #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; -- cgit v1.2.3