diff options
author | Max <post@wickenrode.com> | 2015-10-31 01:16:52 +0100 |
---|---|---|
committer | Max <post@wickenrode.com> | 2015-10-31 01:16:52 +0100 |
commit | d5dd74e8f3b1a73b125fa66db7b08b269933819d (patch) | |
tree | 9d727a7ba9d0a973a4ae1b4363b98138e5d04363 | |
parent | b5be6b8b785ecd25a7366afd541844ee78f4420d (diff) | |
download | sequelpro-d5dd74e8f3b1a73b125fa66db7b08b269933819d.tar.gz sequelpro-d5dd74e8f3b1a73b125fa66db7b08b269933819d.tar.bz2 sequelpro-d5dd74e8f3b1a73b125fa66db7b08b269933819d.zip |
Add method to get charset from collation
-rw-r--r-- | Source/SPDatabaseData.h | 1 | ||||
-rw-r--r-- | Source/SPDatabaseData.m | 45 |
2 files changed, 33 insertions, 13 deletions
diff --git a/Source/SPDatabaseData.h b/Source/SPDatabaseData.h index f2fe08e1..cdc44fa3 100644 --- a/Source/SPDatabaseData.h +++ b/Source/SPDatabaseData.h @@ -74,6 +74,7 @@ - (NSArray *)getDatabaseCollations; - (NSArray *)getDatabaseCollationsForEncoding:(NSString *)encoding; - (NSString *)getDefaultCollationForEncoding:(NSString *)encoding; +- (NSString *)getEncodingFromCollation:(NSString *)collation; - (NSArray *)getDatabaseStorageEngines; - (NSArray *)getDatabaseCharacterSetEncodings; diff --git a/Source/SPDatabaseData.m b/Source/SPDatabaseData.m index 2d1d4f14..0cbb756a 100644 --- a/Source/SPDatabaseData.m +++ b/Source/SPDatabaseData.m @@ -130,7 +130,7 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, ++c; } - while (c[0].nr != 0); + while (c->nr != 0); } } @@ -180,12 +180,14 @@ NSInteger _sortStorageEngineEntry(NSDictionary *itemOne, NSDictionary *itemTwo, NSString *charSet = [NSString stringWithCString:c->name encoding:NSUTF8StringEncoding]; if ([charSet isEqualToString:characterSetEncoding]) { - [characterSetCollations addObject:@{@"COLLATION_NAME" : [NSString stringWithCString:c->collation encoding:NSUTF8StringEncoding]}]; + [characterSetCollations addObject:@{ + @"COLLATION_NAME" : [NSString stringWithCString:c->collation encoding:NSUTF8StringEncoding] + }]; } ++c; } - while (c[0].nr != 0); + while (c->nr != 0); } if ([characterSetCollations count]) { @@ -222,6 +224,23 @@ copy_return: return defaultCollationForCharacterSet; } +/** Get the name of the mysql charset a given collation belongs to. + * @param collation Name of the collation (e.g. "latin1_swedish_ci") + * @return name of the charset (e.g. "latin1") or nil if unknown + * + * According to the MySQL doc every collation can only ever belong to a single charset. + */ +- (NSString *)getEncodingFromCollation:(NSString *)collation { + if([collation length]) { //shortcut for nil and @"" + for(NSDictionary *coll in [self getDatabaseCollations]) { + if([[coll objectForKey:@"COLLATION_NAME"] isEqualToString:collation]) { + return [coll objectForKey:@"CHARACTER_SET_NAME"]; + } + } + } + return nil; +} + /** * Returns all of the database's available storage engines. */ @@ -321,11 +340,11 @@ copy_return: 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]; + [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]; } @@ -336,14 +355,14 @@ copy_return: const SPDatabaseCharSets *c = SPGetDatabaseCharacterSets(); #warning This probably won't work as intended. See my comment in getDatabaseCollationsForEncoding: do { - [characterSetEncodings addObject:[NSDictionary dictionaryWithObjectsAndKeys: - [NSString stringWithCString:c->name encoding:NSUTF8StringEncoding], @"CHARACTER_SET_NAME", - [NSString stringWithCString:c->description encoding:NSUTF8StringEncoding], @"DESCRIPTION", - nil]]; + [characterSetEncodings addObject:@{ + @"CHARACTER_SET_NAME" : [NSString stringWithCString:c->name encoding:NSUTF8StringEncoding], + @"DESCRIPTION" : [NSString stringWithCString:c->description encoding:NSUTF8StringEncoding] + }]; ++c; } - while (c[0].nr != 0); + while (c->nr != 0); } } |