aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPDataImport.m
diff options
context:
space:
mode:
authorrowanbeentje <rowan@beent.je>2010-07-29 00:57:11 +0000
committerrowanbeentje <rowan@beent.je>2010-07-29 00:57:11 +0000
commit67bebbe2f9e6222c0a33be77c2d04495026f2437 (patch)
tree5ce673e3c9307a78686195d054f80205ac6c02a9 /Source/SPDataImport.m
parent550c112216171aa81487b32c41d7aa64adc2ec7f (diff)
downloadsequelpro-67bebbe2f9e6222c0a33be77c2d04495026f2437.tar.gz
sequelpro-67bebbe2f9e6222c0a33be77c2d04495026f2437.tar.bz2
sequelpro-67bebbe2f9e6222c0a33be77c2d04495026f2437.zip
- Add support for automatically converting \r and \r\n linebreaks in query syntax to \n. This addresses Issue #652.
- Clean up SPSQLParser, and use a few more CFString methods to avoid obj-c messaging in loops - When importing SQL using autodetect, perform a manual SET NAMES if possible to cope with poorly-exported files.
Diffstat (limited to 'Source/SPDataImport.m')
-rw-r--r--Source/SPDataImport.m24
1 files changed, 22 insertions, 2 deletions
diff --git a/Source/SPDataImport.m b/Source/SPDataImport.m
index 6b20787a..e697ec18 100644
--- a/Source/SPDataImport.m
+++ b/Source/SPDataImport.m
@@ -358,6 +358,7 @@
BOOL importSQLAsUTF8 = YES;
BOOL allDataRead = NO;
NSStringEncoding sqlEncoding = NSUTF8StringEncoding;
+ NSString *connectionEncodingToRestore = nil;
NSCharacterSet *whitespaceAndNewlineCharset = [NSCharacterSet whitespaceAndNewlineCharacterSet];
// Start the notification timer to allow notifications to be shown, even if frontmost, for long queries
@@ -404,6 +405,10 @@
[fileEncodingDetector analyzeData:[detectorFileHandle readDataOfLength:2500000]];
sqlEncoding = [fileEncodingDetector encoding];
[fileEncodingDetector release];
+ if ([MCPConnection mySQLEncodingForStringEncoding:sqlEncoding]) {
+ connectionEncodingToRestore = [tableDocumentInstance connectionEncoding];
+ [mySQLConnection queryString:[NSString stringWithFormat:@"SET NAMES '%@'", [MCPConnection mySQLEncodingForStringEncoding:sqlEncoding]]];
+ }
}
// Otherwise, get the encoding to use from the menu
@@ -425,6 +430,9 @@
// Report file read errors, and bail
@catch (NSException *exception) {
+ if (connectionEncodingToRestore) {
+ [mySQLConnection queryString:[NSString stringWithFormat:@"SET NAMES '%@'", connectionEncodingToRestore]];
+ }
[self closeAndStopProgressSheet];
SPBeginAlertSheet(NSLocalizedString(@"File read error", @"SQL read error title"),
NSLocalizedString(@"OK", @"OK button"),
@@ -466,6 +474,9 @@
sqlString = [[NSString alloc] initWithData:[sqlDataBuffer subdataWithRange:NSMakeRange(dataBufferLastQueryEndPosition, dataBufferPosition - dataBufferLastQueryEndPosition)]
encoding:sqlEncoding];
if (!sqlString) {
+ if (connectionEncodingToRestore) {
+ [mySQLConnection queryString:[NSString stringWithFormat:@"SET NAMES '%@'", connectionEncodingToRestore]];
+ }
[self closeAndStopProgressSheet];
NSString *displayEncoding;
if (![importEncodingPopup indexOfSelectedItem]) {
@@ -522,9 +533,15 @@
while (query = [sqlParser trimAndReturnStringToCharacter:';' trimmingInclusively:YES returningInclusively:NO]) {
if (progressCancelled) break;
fileProcessedLength += [query lengthOfBytesUsingEncoding:sqlEncoding] + 1;
-
+
+ // Ensure whitespace is removed from both ends, and normalise if necessary.
+ if ([sqlParser containsCarriageReturns]) {
+ query = [SPSQLParser normaliseQueryForExecution:query];
+ } else {
+ query = [query stringByTrimmingCharactersInSet:whitespaceAndNewlineCharset];
+ }
+
// Skip blank or whitespace-only queries to avoid errors
- query = [query stringByTrimmingCharactersInSet:whitespaceAndNewlineCharset];
if (![query length]) continue;
// Run the query
@@ -575,6 +592,9 @@
}
// Clean up
+ if (connectionEncodingToRestore) {
+ [mySQLConnection queryString:[NSString stringWithFormat:@"SET NAMES '%@'", connectionEncodingToRestore]];
+ }
[sqlParser release];
[sqlDataBuffer release];
[importPool drain];