diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPDatabaseData.h | 5 | ||||
-rw-r--r-- | Source/SPDatabaseData.m | 89 | ||||
-rw-r--r-- | Source/SPServerSupport.h | 8 | ||||
-rw-r--r-- | Source/SPServerSupport.m | 8 |
4 files changed, 76 insertions, 34 deletions
diff --git a/Source/SPDatabaseData.h b/Source/SPDatabaseData.h index 9eb4551c..abd3a27b 100644 --- a/Source/SPDatabaseData.h +++ b/Source/SPDatabaseData.h @@ -46,6 +46,8 @@ NSString *characterSetEncoding; NSString *defaultCharacterSetEncoding; NSString *defaultCollation; + NSString *serverDefaultCharacterSetEncoding; + NSString *serverDefaultCollation; NSString *defaultStorageEngine; NSMutableArray *collations; @@ -79,4 +81,7 @@ - (NSString *)getDatabaseDefaultCollation; - (NSString *)getDatabaseDefaultStorageEngine; +- (NSString *)getServerDefaultCharacterSet; +- (NSString *)getServerDefaultCollation; + @end 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; diff --git a/Source/SPServerSupport.h b/Source/SPServerSupport.h index 2f562177..4cb68726 100644 --- a/Source/SPServerSupport.h +++ b/Source/SPServerSupport.h @@ -62,7 +62,7 @@ // Encoding BOOL supportsShowCharacterSet; BOOL supportsShowCollation; - BOOL supportsCharacterSetDatabaseVar; + BOOL supportsCharacterSetAndCollationVars; BOOL supportsPost41CharacterSetHandling; // User account related @@ -150,10 +150,10 @@ @property (readonly) BOOL supportsShowCollation; /** - * @property supportsCharacterSetDatabaseVar Indicates if the server supports the 'character_set_database' - * variable. + * @property supportsCharacterSetAndCollationVars Indicates if the server supports the 'character_set_*' and 'collation_*' + * variables. */ -@property (readonly) BOOL supportsCharacterSetDatabaseVar; +@property (readonly) BOOL supportsCharacterSetAndCollationVars; /** * @property supportsPost41CharacterSetHandling Indicates whether the server supports post 4.1 character set diff --git a/Source/SPServerSupport.m b/Source/SPServerSupport.m index b32a9dc2..78a2b287 100644 --- a/Source/SPServerSupport.m +++ b/Source/SPServerSupport.m @@ -56,7 +56,7 @@ @synthesize supportsSpatialExtensions; @synthesize supportsShowCharacterSet; @synthesize supportsShowCollation; -@synthesize supportsCharacterSetDatabaseVar; +@synthesize supportsCharacterSetAndCollationVars; @synthesize supportsPost41CharacterSetHandling; @synthesize supportsCreateUser; @synthesize supportsRenameUser; @@ -142,8 +142,8 @@ // The SHOW COLLATION statement wasn't added until MySQL 4.1.0 supportsShowCollation = [self isEqualToOrGreaterThanMajorVersion:4 minor:1 release:0]; - // The variable 'character_set_database' wasn't added until MySQL 4.1.1 - supportsCharacterSetDatabaseVar = [self isEqualToOrGreaterThanMajorVersion:4 minor:1 release:1]; + // The variables 'character_set_*' and 'collation_*' weren't added until MySQL 4.1.1 + supportsCharacterSetAndCollationVars = [self isEqualToOrGreaterThanMajorVersion:4 minor:1 release:1]; // As of MySQL 4.1 encoding support was greatly improved supportsPost41CharacterSetHandling = [self isEqualToOrGreaterThanMajorVersion:4 minor:1 release:0]; @@ -263,7 +263,7 @@ supportsSpatialExtensions = NO; supportsShowCharacterSet = NO; supportsShowCollation = NO; - supportsCharacterSetDatabaseVar = NO; + supportsCharacterSetAndCollationVars = NO; supportsPost41CharacterSetHandling = NO; supportsCreateUser = NO; supportsRenameUser = NO; |