diff options
Diffstat (limited to 'TableDocument.m')
-rw-r--r-- | TableDocument.m | 45 |
1 files changed, 38 insertions, 7 deletions
diff --git a/TableDocument.m b/TableDocument.m index e88a62ad..8ca7b0ae 100644 --- a/TableDocument.m +++ b/TableDocument.m @@ -182,7 +182,7 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa // set encoding NSString *encodingName = [prefs objectForKey:@"encoding"]; if ( [encodingName isEqualToString:@"Autodetect"] ) { - [self detectEncoding]; + [self detectDatabaseEncoding]; } else { [self setEncoding:[self mysqlEncodingFromDisplayEncoding:encodingName]]; } @@ -632,14 +632,15 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa { // set encoding of connection and client [mySQLConnection queryString:[NSString stringWithFormat:@"SET NAMES '%@'", mysqlEncoding]]; + if ( [[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { [mySQLConnection setEncoding:[CMMCPConnection encodingForMySQLEncoding:[mysqlEncoding UTF8String]]]; [_encoding autorelease]; _encoding = [mysqlEncoding retain]; } else { - [self detectEncoding]; + [self detectDatabaseEncoding]; } - + // update the selected menu item [self updateEncodingMenuWithSelectedEncoding:[self encodingNameFromMySQLEncoding:mysqlEncoding]]; @@ -742,9 +743,9 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa /** * Autodetect the connection encoding and select the relevant encoding menu item in Database -> Database Encoding */ -- (void)detectEncoding +- (void)detectDatabaseEncoding { - // mysql > 4.0 + // MySQL > 4.0 id mysqlEncoding = [[[mySQLConnection queryString:@"SHOW VARIABLES LIKE 'character_set_connection'"] fetchRowAsDictionary] objectForKey:@"Value"]; _supportsEncoding = (mysqlEncoding != nil); @@ -755,9 +756,10 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa mysqlEncoding = [[[mySQLConnection queryString:@"SHOW VARIABLES LIKE 'character_set'"] fetchRowAsDictionary] objectForKey:@"Value"]; } if ( !mysqlEncoding ) { // older version? -> set encoding to mysql default encoding latin1 - NSLog(@"error: no character encoding found, mysql version is %@", [self mySQLVersion]); + NSLog(@"Error: no character encoding found, mysql version is %@", [self mySQLVersion]); mysqlEncoding = @"latin1"; } + [mySQLConnection setEncoding:[CMMCPConnection encodingForMySQLEncoding:[mysqlEncoding cString]]]; // save the encoding @@ -769,7 +771,36 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa } /** - * when sent by an NSMenuItem, will set the encoding based on the title of the menu item + * Detects and sets the character set encoding of the supplied table name. + */ +- (void)detectTableEncodingForTable:(NSString *)table; +{ + NSString *tableCollation = [[[mySQLConnection queryString:[NSString stringWithFormat:@"SHOW TABLE STATUS WHERE NAME = '%@'", table]] fetchRowAsDictionary] objectForKey:@"Collation"]; + + if (tableCollation != nil) { + // Split up the collation string so we can get the encoding + NSArray *encodingComponents = [tableCollation componentsSeparatedByString:@"_"]; + + if ([encodingComponents count] > 0) { + NSString *tableEncoding = [encodingComponents objectAtIndex:0]; + + [mySQLConnection setEncoding:[CMMCPConnection encodingForMySQLEncoding:[tableEncoding UTF8String]]]; + + // Save the encoding + [_encoding autorelease]; + _encoding = [tableEncoding retain]; + + // update the selected menu item + [self updateEncodingMenuWithSelectedEncoding:[self encodingNameFromMySQLEncoding:tableEncoding]]; + } + } + else { + NSLog(@"Error: unable to determine table collation and thus character encoding for table '%@'", table); + } +} + +/** + * When sent by an NSMenuItem, will set the encoding based on the title of the menu item */ - (IBAction)chooseEncoding:(id)sender { |