diff options
author | stuconnolly <stuart02@gmail.com> | 2010-10-07 18:56:33 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2010-10-07 18:56:33 +0000 |
commit | 44a5f9e552b3d5e1f9ef1c6d11f34e893d67e85b (patch) | |
tree | c833ba970d8cae5f756a31bc274e365c0a44b3bf /Source/SPDatabaseData.m | |
parent | 95d2e4acc393e91aa70ed4c71daa1f776454a936 (diff) | |
download | sequelpro-44a5f9e552b3d5e1f9ef1c6d11f34e893d67e85b.tar.gz sequelpro-44a5f9e552b3d5e1f9ef1c6d11f34e893d67e85b.tar.bz2 sequelpro-44a5f9e552b3d5e1f9ef1c6d11f34e893d67e85b.zip |
Various improvements to server capability/version checking, including:
- Add a new ServerSupport class, for which an instance is created upon each new connection and is then subsequently accessible via SPDatabaseDocument.
- Replace the majority of manual version checking with calls to properties in the above new class.
- Improve the user manager's compatibility with MySQL 3 and 4 servers. Fixes issue #811
Other changes include:
- Disable the encoding popup button when adding a new table or database to servers running pre MySQL 4.1 as it only contains one option, 'Default'.
- Fix various potential memory leaks discovered during static analysis.
- General tidy up and comments.
Diffstat (limited to 'Source/SPDatabaseData.m')
-rw-r--r-- | Source/SPDatabaseData.m | 95 |
1 files changed, 51 insertions, 44 deletions
diff --git a/Source/SPDatabaseData.m b/Source/SPDatabaseData.m index bec1264c..b67ee4da 100644 --- a/Source/SPDatabaseData.m +++ b/Source/SPDatabaseData.m @@ -25,6 +25,7 @@ #import "SPDatabaseData.h" #import "SPStringAdditions.h" +#import "SPServerSupport.h" @interface SPDatabaseData (PrivateAPI) @@ -194,6 +195,10 @@ const SPDatabaseCharSets charsets[] = @implementation SPDatabaseData @synthesize connection; +@synthesize serverSupport; + +#pragma mark - +#pragma mark Initialization /** * Initialize cache arrays. @@ -207,32 +212,22 @@ const SPDatabaseCharSets charsets[] = characterSetCollations = [[NSMutableArray alloc] init]; storageEngines = [[NSMutableArray alloc] init]; characterSetEncodings = [[NSMutableArray alloc] init]; + cachedCollationsByEncoding = [[NSMutableDictionary alloc] init]; } return self; } -/** - * Set the current connection the supplied connection instance. - */ -- (void)setConnection:(MCPConnection *)dbConnection -{ - connection = dbConnection; - - serverMajorVersion = [connection serverMajorVersion]; - serverMinorVersion = [connection serverMinorVersion]; - serverReleaseVersion = [connection serverReleaseVersion]; -} +#pragma mark - +#pragma mark Public API /** * Reset all the cached values. */ - (void)resetAllData { - if (characterSetEncoding != nil) { - [characterSetEncoding release], characterSetEncoding = nil; - } + if (characterSetEncoding != nil) [characterSetEncoding release], characterSetEncoding = nil; [collations removeAllObjects]; [characterSetCollations removeAllObjects]; @@ -248,9 +243,10 @@ const SPDatabaseCharSets charsets[] = if ([collations count] == 0) { // Try to retrieve the available collations from the database - if (serverMajorVersion >= 5) - [collations addObjectsFromArray:[self _getDatabaseDataForQuery:@"SELECT * FROM `information_schema.collations` ORDER BY `collation_name` ASC"]]; - + if ([serverSupport supportsInformationSchema]) { + [collations addObjectsFromArray:[self _getDatabaseDataForQuery:@"SELECT * FROM `information_schema`.`collations` ORDER BY `collation_name` ASC"]]; + } + // If that failed, get the list of collations from the hard-coded list if (![collations count]) { const SPDatabaseCharSets *c = charsets; @@ -284,8 +280,9 @@ const SPDatabaseCharSets charsets[] = return [cachedCollationsByEncoding objectForKey:characterSetEncoding]; // Try to retrieve the available collations for the supplied encoding from the database - if (serverMajorVersion >= 5) + if ([serverSupport supportsInformationSchema]) { [characterSetCollations addObjectsFromArray:[self _getDatabaseDataForQuery:[NSString stringWithFormat:@"SELECT * FROM `information_schema`.`collations` WHERE character_set_name = '%@' ORDER BY `collation_name` ASC", characterSetEncoding]]]; + } // If that failed, get the list of collations matching the supplied encoding from the hard-coded list if (![characterSetCollations count]) { @@ -303,8 +300,9 @@ const SPDatabaseCharSets charsets[] = while (c[0].nr != 0); } - if(characterSetCollations && [characterSetCollations count]) + if (characterSetCollations && [characterSetCollations count]) { [cachedCollationsByEncoding setObject:[NSArray arrayWithArray:characterSetCollations] forKey:characterSetEncoding]; + } } @@ -317,11 +315,12 @@ const SPDatabaseCharSets charsets[] = - (NSArray *)getDatabaseStorageEngines { if ([storageEngines count] == 0) { - if (serverMajorVersion < 5) { + if ([serverSupport isMySQL3] || [serverSupport isMySQL4]) { [storageEngines addObject:[NSDictionary dictionaryWithObject:@"MyISAM" forKey:@"Engine"]]; // Check if InnoDB support is enabled MCPResult *result = [connection queryString:@"SHOW VARIABLES LIKE 'have_innodb'"]; + [result setReturnDataAsStrings:YES]; if ([result numOfRows] == 1) { @@ -331,7 +330,7 @@ const SPDatabaseCharSets charsets[] = } // Before MySQL 4.1 the MEMORY engine was known as HEAP and the ISAM engine was included - if ((serverMajorVersion <= 4) && (serverMinorVersion < 100)) { + if ([serverSupport supportsPre41StorageEngines]) { [storageEngines addObject:[NSDictionary dictionaryWithObject:@"HEAP" forKey:@"Engine"]]; [storageEngines addObject:[NSDictionary dictionaryWithObject:@"ISAM" forKey:@"Engine"]]; } @@ -340,35 +339,32 @@ const SPDatabaseCharSets charsets[] = } // BLACKHOLE storage engine was added in MySQL 4.1.11 - if ((serverMajorVersion >= 4) && - (serverMinorVersion >= 1) && - (serverReleaseVersion >= 11)) - { + if ([serverSupport supportsBlackholeStorageEngine]) { [storageEngines addObject:[NSDictionary dictionaryWithObject:@"BLACKHOLE" forKey:@"Engine"]]; + } - // ARCHIVE storage engine was added in MySQL 4.1.3 - if (serverReleaseVersion >= 3) { - [storageEngines addObject:[NSDictionary dictionaryWithObject:@"ARCHIVE" forKey:@"Engine"]]; - } - - // CSV storage engine was added in MySQL 4.1.4 - if (serverReleaseVersion >= 4) { - [storageEngines addObject:[NSDictionary dictionaryWithObject:@"CSV" forKey:@"Engine"]]; - } - } + // ARCHIVE storage engine was added in MySQL 4.1.3 + if ([serverSupport supportsArchiveStorageEngine]) { + [storageEngines addObject:[NSDictionary dictionaryWithObject:@"ARCHIVE" forKey:@"Engine"]]; + } + + // CSV storage engine was added in MySQL 4.1.4 + if ([serverSupport supportsCSVStorageEngine]) { + [storageEngines addObject:[NSDictionary dictionaryWithObject:@"CSV" forKey:@"Engine"]]; + } } // The table information_schema.engines didn't exist until MySQL 5.1.5 else { - if ((serverMajorVersion >= 5) && - (serverMinorVersion >= 1) && - (serverReleaseVersion >= 5)) + if ([serverSupport supportsInformationSchemaEngines]) { // Check the information_schema.engines table is accessible MCPResult *result = [connection queryString:@"SHOW TABLES IN information_schema LIKE 'ENGINES'"]; if ([result numOfRows] == 1) { + // Table is accessible so get available storage engines - [storageEngines addObjectsFromArray:[self _getDatabaseDataForQuery:@"SELECT Engine, Support FROM information_schema.engines WHERE support IN ('DEFAULT', 'YES');"]]; + // Note, that the case of the column names specified in this query are important. + [storageEngines addObjectsFromArray:[self _getDatabaseDataForQuery:@"SELECT Engine, Support FROM `information_schema`.`engines` WHERE SUPPORT IN ('DEFAULT', 'YES')"]]; } } else { @@ -396,23 +392,28 @@ const SPDatabaseCharSets charsets[] = * information_schema.character_sets. */ - (NSArray *)getDatabaseCharacterSetEncodings -{ +{ if ([characterSetEncodings count] == 0) { // Try to retrieve the available character set encodings from the database // Check the information_schema.character_sets table is accessible - if (serverMajorVersion >= 5) { + if ([serverSupport supportsInformationSchema]) { [characterSetEncodings addObjectsFromArray:[self _getDatabaseDataForQuery:@"SELECT * FROM `information_schema`.`character_sets` ORDER BY `character_set_name` ASC"]]; - } else if (serverMajorVersion == 4 && serverMinorVersion >= 1) { + } + else if ([serverSupport supportsShowCharacterSet]) { NSArray *supportedEncodings = [self _getDatabaseDataForQuery:@"SHOW CHARACTER SET"]; + supportedEncodings = [supportedEncodings sortedArrayUsingFunction:_sortMySQL4CharsetEntry context:nil]; - for (NSDictionary *anEncoding in supportedEncodings) { + + for (NSDictionary *anEncoding in supportedEncodings) + { NSDictionary *convertedEncoding = [NSDictionary dictionaryWithObjectsAndKeys: [anEncoding objectForKey:@"Charset"], @"CHARACTER_SET_NAME", [anEncoding objectForKey:@"Description"], @"DESCRIPTION", [anEncoding objectForKey:@"Default collation"], @"DEFAULT_COLLATE_NAME", [anEncoding objectForKey:@"Maxlen"], @"MAXLEN", nil]; + [characterSetEncodings addObject:convertedEncoding]; } } @@ -432,10 +433,13 @@ const SPDatabaseCharSets charsets[] = while (c[0].nr != 0); } } - + return characterSetEncodings; } +#pragma mark - +#pragma mark Other + /** * Deallocate ivars. */ @@ -454,6 +458,9 @@ const SPDatabaseCharSets charsets[] = [super dealloc]; } +#pragma mark - +#pragma mark Private API + /** * Executes the supplied query against the current connection and returns the result as an array of * NSDictionarys, one for each row. |