diff options
author | Bibiko <bibiko@eva.mpg.de> | 2009-06-19 23:02:15 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2009-06-19 23:02:15 +0000 |
commit | 4c4c5c22a47417eaa458739d1599c7bb24f75053 (patch) | |
tree | 6d0f15daa532684fe215b7fdf0a3ed3219bdf7c1 | |
parent | 77d8891b8b3dd3321eac871da0bde6fe1f38ea94 (diff) | |
download | sequelpro-4c4c5c22a47417eaa458739d1599c7bb24f75053.tar.gz sequelpro-4c4c5c22a47417eaa458739d1599c7bb24f75053.tar.bz2 sequelpro-4c4c5c22a47417eaa458739d1599c7bb24f75053.zip |
• queryString: code cleaning and processing each encoding equally
- Since we are using mysql_real_query() there is no need to generate a \0 terminated cString.
- The disadvantage of using UTF8String is that it returns NULL if the encoding fails.
- To process each encoding equally we can now use [NSString dataUsingEncoding:enc allowLossyConversion:lossy]. In oder to avoid the overhead while calling it the following inline function is introduced:
NSStringDataUsingLossyEncoding(aStr, enc, lossy) := [aStr dataUsingEncoding:enc allowLossyConversion:lossy]
• import of CSV: code cleaned and optimized for speed a little
-rw-r--r-- | Source/CMMCPConnection.m | 13 | ||||
-rw-r--r-- | Source/SPStringAdditions.h | 11 | ||||
-rw-r--r-- | Source/TableDump.m | 45 |
3 files changed, 39 insertions, 30 deletions
diff --git a/Source/CMMCPConnection.m b/Source/CMMCPConnection.m index 3101beb9..60b8d2da 100644 --- a/Source/CMMCPConnection.m +++ b/Source/CMMCPConnection.m @@ -757,17 +757,10 @@ static void forcePingTimeout(int signalNumber); (void)(NSString*)(*willQueryStringPtr)(delegate, willQueryStringSEL, query); // Derive the query string in the correct encoding - switch(encoding) { - case NSUTF8StringEncoding: - theCQuery = NSStringUTF8String(query); - break; - default: - theCQuery = (const char*)(NSString*)(int)(*cStringPtr)(self, cStringSEL, query, encoding); - //[self cStringFromString:query usingEncoding:encoding]; - } - + NSData *d = NSStringDataUsingLossyEncoding(query, encoding, 1); + theCQuery = [d bytes]; // Set the length of the current query - theCQueryLength = strlen(theCQuery); + theCQueryLength = [d length]; // Check query length against max_allowed_packet; if it is larger, the // query would error, so if max_allowed_packet is editable for the user diff --git a/Source/SPStringAdditions.h b/Source/SPStringAdditions.h index 3b6805e2..540492c0 100644 --- a/Source/SPStringAdditions.h +++ b/Source/SPStringAdditions.h @@ -38,6 +38,17 @@ static inline const char* NSStringUTF8String(NSString* self) { return to_return; } +/* + * NSStringDataUsingLossyEncoding(aStr, enc, lossy) := [aStr dataUsingEncoding:enc allowLossyConversion:lossy] + */ +static inline NSData* NSStringDataUsingLossyEncoding(NSString* self, int encoding, int lossy) { + typedef NSData* (*SPStringDataUsingLossyEncodingMethodPtr)(NSString*, SEL, int, int); + static SPStringDataUsingLossyEncodingMethodPtr SPNSStringDataUsingLossyEncoding; + if (!SPNSStringDataUsingLossyEncoding) SPNSStringDataUsingLossyEncoding = (SPStringDataUsingLossyEncodingMethodPtr)[self methodForSelector:@selector(dataUsingEncoding:allowLossyConversion:)]; + NSData* to_return = SPNSStringDataUsingLossyEncoding(self, @selector(dataUsingEncoding:allowLossyConversion:), encoding, lossy); + return to_return; +} + @interface NSString (SPStringAdditions) diff --git a/Source/TableDump.m b/Source/TableDump.m index 1490299a..4cbd858c 100644 --- a/Source/TableDump.m +++ b/Source/TableDump.m @@ -451,7 +451,7 @@ //import dump file NSArray *queries; - int i; + int i=0; //open progress sheet [NSApp beginSheet:singleProgressSheet @@ -485,9 +485,9 @@ // Skip blank or whitespace-only queries to avoid errors NSString *q = [NSArrayObjectAtIndex(queries, i) stringByTrimmingCharactersInSet:whitespaceAndNewline]; if (![q length]) continue; - + [mySQLConnection queryString:q usingEncoding:NSUTF8StringEncoding]; - + if ([[mySQLConnection getLastErrorMessage] length] && ![[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]]]; } @@ -640,6 +640,7 @@ NSMutableString *fNames = [NSMutableString string]; //NSMutableArray *fValuesIndexes = [NSMutableArray array]; NSMutableString *fValues = [NSMutableString string]; + NSString *insertFormatString = nil; int i,j; //open progress sheet @@ -663,37 +664,41 @@ //import array long importArrayCount = [importArray count]; + long fieldMappingArrayCount = [fieldMappingArray count]; + insertFormatString = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (%%@)", + [[fieldMappingPopup titleOfSelectedItem] backtickQuotedString], fNames]; + int fieldMappingIntValue; + Class nullClass = [NSNull class]; + for ( i = 0 ; i < importArrayCount ; i++ ) { //show progress bar [singleProgressBar setDoubleValue:((i+1)*100/importArrayCount)]; - // [singleProgressBar displayIfNeeded]; - + if ( !([importFieldNamesSwitch state] && (i == 0)) ) { //put values in string [fValues setString:@""]; - - for ( j = 0 ; j < [fieldMappingArray count] ; j++ ) { - - if ([NSArrayObjectAtIndex(fieldMappingArray,j) intValue] > 0) { + + for ( j = 0 ; j < fieldMappingArrayCount ; j++ ) { + fieldMappingIntValue = [NSArrayObjectAtIndex(fieldMappingArray,j) intValue]; + if ( fieldMappingIntValue > 0 ) { + if ( [fValues length] ) [fValues appendString:@","]; - - if ([[NSArrayObjectAtIndex(importArray, i) objectAtIndex:([NSArrayObjectAtIndex(fieldMappingArray, j) intValue] - 1)] isMemberOfClass:[NSNull class]] ) { - [fValues appendString:@"NULL"]; - } else { - [fValues appendString:[NSString stringWithFormat:@"'%@'",[mySQLConnection prepareString:[NSArrayObjectAtIndex(importArray ,i) objectAtIndex:([NSArrayObjectAtIndex(fieldMappingArray ,j) intValue] - 1)]]]]; - } + + id c = NSArrayObjectAtIndex(NSArrayObjectAtIndex(importArray, i), (fieldMappingIntValue - 1)); + + [fValues appendString: ([c isMemberOfClass:nullClass]) ? + @"NULL" : [NSString stringWithFormat:@"'%@'", [mySQLConnection prepareString:c]]]; } } //perform query - [mySQLConnection queryString:[NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (%@)", - [[fieldMappingPopup titleOfSelectedItem] backtickQuotedString], - fNames, - fValues]]; + [mySQLConnection queryString:[NSString stringWithFormat:insertFormatString, fValues]]; if ( ![[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { - [errors appendString:[NSString stringWithFormat:NSLocalizedString(@"[ERROR in line %d] %@\n", @"error text when reading of csv file gave errors"), (i+1),[mySQLConnection getLastErrorMessage]]]; + [errors appendString:[NSString stringWithFormat: + NSLocalizedString(@"[ERROR in line %d] %@\n", @"error text when reading of csv file gave errors"), + (i+1),[mySQLConnection getLastErrorMessage]]]; } } } |