From 4c4c5c22a47417eaa458739d1599c7bb24f75053 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Fri, 19 Jun 2009 23:02:15 +0000 Subject: =?UTF-8?q?=E2=80=A2=20queryString:=20=20code=20cleaning=20and=20p?= =?UTF-8?q?rocessing=20each=20encoding=20equally=20-=20Since=20we=20are=20?= =?UTF-8?q?using=20mysql=5Freal=5Fquery()=20there=20is=20no=20need=20to=20?= =?UTF-8?q?generate=20a=20\0=20terminated=20cString.=20-=20The=20disadvant?= =?UTF-8?q?age=20of=20using=20UTF8String=20is=20that=20it=20returns=20NULL?= =?UTF-8?q?=20if=20the=20encoding=20fails.=20-=20To=20process=20each=20enc?= =?UTF-8?q?oding=20equally=20we=20can=20now=20use=20[NSString=20dataUsingE?= =?UTF-8?q?ncoding:enc=20allowLossyConversion:lossy].=20In=20oder=20to=20a?= =?UTF-8?q?void=20the=20overhead=20while=20calling=20it=20the=20following?= =?UTF-8?q?=20inline=20function=20is=20introduced:=20NSStringDataUsingLoss?= =?UTF-8?q?yEncoding(aStr,=20enc,=20lossy)=20:=3D=20[aStr=20dataUsingEncod?= =?UTF-8?q?ing:enc=20allowLossyConversion:lossy]=20=E2=80=A2=20import=20of?= =?UTF-8?q?=20CSV:=20code=20cleaned=20and=20optimized=20for=20speed=20a=20?= =?UTF-8?q?little?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/TableDump.m | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'Source/TableDump.m') 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]]]; } } } -- cgit v1.2.3