diff options
Diffstat (limited to 'TableDocument.m')
-rw-r--r-- | TableDocument.m | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/TableDocument.m b/TableDocument.m index 59aad2e0..2dcb5f1a 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]]; } @@ -260,9 +260,6 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa [portField setStringValue:[self valueForKeyPath:@"selectedFavorite.port"]]; [databaseField setStringValue:[self valueForKeyPath:@"selectedFavorite.database"]]; [passwordField setStringValue:[self selectedFavoritePassword]]; - - [selectedFavorite release]; - selectedFavorite = [[favoritesButton titleOfSelectedItem] retain]; } /** @@ -368,7 +365,7 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa [self willChangeValueForKey:@"favorites"]; // write favorites and password - NSDictionary *newFavorite = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:favoriteName, host, socket, user, port, database, nil] + NSMutableDictionary *newFavorite = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:favoriteName, host, socket, user, port, database, nil] forKeys:[NSArray arrayWithObjects:@"name", @"host", @"socket", @"user", @"port", @"database", nil]]; [favorites addObject:newFavorite]; @@ -378,9 +375,6 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa account:[NSString stringWithFormat:@"%@@%@/%@", user, host, database]]; } - // select new favorite - selectedFavorite = [favoriteName retain]; - [self didChangeValueForKey:@"favorites"]; [favoritesController setSelectedObjects:[NSArray arrayWithObject:newFavorite]]; } @@ -638,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]]; @@ -748,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); @@ -761,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 @@ -775,7 +771,29 @@ 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]; + + [self setEncoding: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 { @@ -1428,7 +1446,6 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa // [tableWindow makeKeyAndOrderFront:self]; prefs = [[NSUserDefaults standardUserDefaults] retain]; - selectedFavorite = [[NSString alloc] initWithString:NSLocalizedString(@"Custom", @"menu item for custom connection")]; //register for notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willPerformQuery:) @@ -1593,6 +1610,10 @@ objectValueForTableColumn:(NSTableColumn *)aTableColumn - (IBAction)terminate:(id)sender { [[NSApp orderedDocuments] makeObjectsPerformSelector:@selector(cancelConnectSheet:) withObject:nil]; + + // Save the favourites - commits any unsaved changes ie favourite renames + [prefs setObject:[self favorites] forKey:@"favorites"]; + [NSApp terminate:sender]; } @@ -1603,7 +1624,6 @@ objectValueForTableColumn:(NSTableColumn *)aTableColumn [favorites release]; [variables release]; [selectedDatabase release]; - [selectedFavorite release]; [mySQLVersion release]; [prefs release]; |