diff options
author | rowanbeentje <rowan@beent.je> | 2008-12-10 01:50:52 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2008-12-10 01:50:52 +0000 |
commit | feede753b44f9253b58f042e2281dcb1e6bb0caa (patch) | |
tree | a78d5db85b4777bc015f69a6c5d0cfbbc388cd57 /TableDocument.m | |
parent | 5a57ab32d28a9c2b3538a73f2fd965558a39abe1 (diff) | |
download | sequelpro-788c9df42dd43144916a81eb892c2d158d99efbd.tar.gz sequelpro-788c9df42dd43144916a81eb892c2d158d99efbd.tar.bz2 sequelpro-788c9df42dd43144916a81eb892c2d158d99efbd.zip |
- Merge in trunk fixes and improvements up to r254release-0.9.3
- Update Info.plist revision numbers to 254
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]; |