diff options
author | rowanbeentje <rowan@beent.je> | 2009-04-08 23:15:03 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2009-04-08 23:15:03 +0000 |
commit | 291bc96e1c0b916b2106da936e4c732ec7897bcf (patch) | |
tree | ab97ab91a5e6d5008a4839b6781a783938be506f /Source/TableDump.m | |
parent | ce44079e15c6ad11589b0237fd78ee6b4b364bfa (diff) | |
download | sequelpro-291bc96e1c0b916b2106da936e4c732ec7897bcf.tar.gz sequelpro-291bc96e1c0b916b2106da936e4c732ec7897bcf.tar.bz2 sequelpro-291bc96e1c0b916b2106da936e4c732ec7897bcf.zip |
- Second part of r498: ensure that SQL imports are read in as UTF8, and queries are sent as UTF8, if possible; this correctly preserves encoding data when working with files export from Sequel Pro or via mysqldump, and attempts to fall back to the current connection encodings for other files. This should resolve Issue #116.
Diffstat (limited to 'Source/TableDump.m')
-rw-r--r-- | Source/TableDump.m | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/Source/TableDump.m b/Source/TableDump.m index 8dce751f..3f543d4b 100644 --- a/Source/TableDump.m +++ b/Source/TableDump.m @@ -375,11 +375,27 @@ NSError *errorStr = nil; NSMutableString *errors = [NSMutableString string]; NSString *fileType = [[importFormatPopup selectedItem] title]; + BOOL importSQLAsUTF8 = YES; + + // Load file into string. For SQL imports, try UTF8 file encoding before the current encoding. + if ([fileType isEqualToString:@"SQL"]) { + NSLog(@"Reading as utf8"); + dumpFile = [SPSQLParser stringWithContentsOfFile:filename + encoding:NSUTF8StringEncoding + error:&errorStr]; + NSLog(dumpFile); + if (errorStr) { + importSQLAsUTF8 = NO; + errorStr = nil; + } + } - //load file into string - dumpFile = [SPSQLParser stringWithContentsOfFile:filename - encoding:[CMMCPConnection encodingForMySQLEncoding:[[tableDocumentInstance connectionEncoding] UTF8String]] - error:&errorStr]; + // If the SQL-as-UTF8 read failed, and for CSVs, use the current connection encoding. + if (!importSQLAsUTF8 || [fileType isEqualToString:@"CSV"]) { + dumpFile = [SPSQLParser stringWithContentsOfFile:filename + encoding:[CMMCPConnection encodingForMySQLEncoding:[[tableDocumentInstance connectionEncoding] UTF8String]] + error:&errorStr]; + } if (errorStr) { NSBeginAlertSheet(NSLocalizedString(@"Error", @"Title of error alert"), @@ -435,7 +451,11 @@ if ([[[queries objectAtIndex:i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) continue; - [mySQLConnection queryString:[queries objectAtIndex:i]]; + if (importSQLAsUTF8) { + [mySQLConnection queryString:[queries objectAtIndex:i] usingEncoding:NSUTF8StringEncoding]; + } else { + [mySQLConnection queryString:[queries objectAtIndex:i]]; + } if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""] && ![[mySQLConnection getLastErrorMessage] isEqualToString:@"Query was empty"]) { [errors appendString:[NSString stringWithFormat:NSLocalizedString(@"[ERROR in query %d] %@\n", @"error text when multiple custom query failed"), (i+1),[mySQLConnection getLastErrorMessage]]]; |