From 3f6aa841dd77af2cf8f2818c91cd186aec2ca5e0 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Mon, 6 Sep 2010 12:24:46 +0000 Subject: =?UTF-8?q?=E2=80=A2=20CSV=20Import=20into=20new=20table:=20-=20do?= =?UTF-8?q?=20not=20create=20a=20field=20in=20beforehand=20if=20user=20set?= =?UTF-8?q?=20it=20to=20"Do=20Not=20Import"=20-=20added=20ROUGH=20field=20?= =?UTF-8?q?type=20guessing=20by=20parsing=20the=20up=20to=20100=20first=20?= =?UTF-8?q?rows=20distinguishing=20text=20versa=20integer=20and=20size:=20?= =?UTF-8?q?--=20for=20integer=20distinguishes=20between=20INT=20and=20BIGI?= =?UTF-8?q?NT=20due=20to=20number=20length=20--=20for=20text=20distinguish?= =?UTF-8?q?es=20between=20VARCHAR(255),=20VARCHAR(32767)=20[for=20MySQL=20?= =?UTF-8?q?>=3D=205=20only],=20and=20TEXT=20due=20to=20maximum=20string=20?= =?UTF-8?q?length?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPFieldMapperController.m | 83 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/Source/SPFieldMapperController.m b/Source/SPFieldMapperController.m index 7df1ea81..fe60c5d3 100644 --- a/Source/SPFieldMapperController.m +++ b/Source/SPFieldMapperController.m @@ -431,8 +431,11 @@ NSInteger columnIndex = 0; NSInteger numberOfColumns = [fieldMappingTableColumnNames count]; for(columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) { - [createString appendFormat:@"\t%@ %@", [[fieldMappingTableColumnNames objectAtIndex:columnIndex] backtickQuotedString], [fieldMappingTableTypes objectAtIndex:columnIndex]]; - if(columnIndex < numberOfColumns-1) [createString appendString:@", \n"]; + // add to the new table only those fields which are markes as "Do Import" + if([fieldMappingOperatorArray objectAtIndex:columnIndex] == doImport) { + [createString appendFormat:@"\t%@ %@", [[fieldMappingTableColumnNames objectAtIndex:columnIndex] backtickQuotedString], [fieldMappingTableTypes objectAtIndex:columnIndex]]; + if(columnIndex < numberOfColumns-1) [createString appendString:@", \n"]; + } } [createString appendString:@")"]; @@ -780,21 +783,93 @@ [newTableButton setHidden:YES]; [newTableNameTextField selectText:nil]; + // Check length and type of fieldMappingImportArray 65,535 + NSInteger maxLengthOfSourceColumns [numberOfImportColumns]; + NSInteger typeOfSourceColumns [numberOfImportColumns]; // 0=text 1=integer + NSInteger columnCounter; + + for(columnCounter = 0; columnCounter < numberOfImportColumns; columnCounter++) { + maxLengthOfSourceColumns[columnCounter] = 0; + typeOfSourceColumns[columnCounter] = 1; + } + + BOOL skipFirstRow = importFieldNamesHeader; + + for(NSArray* row in fieldMappingImportArray) { + if(skipFirstRow) { + skipFirstRow = NO; + continue; + } + columnCounter = 0; + for(NSString* col in row) { + if(maxLengthOfSourceColumns[columnCounter] < [col length]) { + maxLengthOfSourceColumns[columnCounter] = [col length]; + } + if(typeOfSourceColumns[columnCounter] == 1) { + if(![[[NSNumber numberWithLongLong:[col longLongValue]] stringValue] isEqualToString:col]) + typeOfSourceColumns[columnCounter] = 0; + } + columnCounter++; + } + } + + columnCounter = 0; [fieldMappingTableColumnNames removeAllObjects]; [fieldMappingTableDefaultValues removeAllObjects]; [fieldMappingTableTypes removeAllObjects]; + BOOL serverGreaterThanVersion4 = ([mySQLConnection serverMajorVersion] >= 5) ? YES : NO; if([importFieldNamesHeaderSwitch state] == NSOnState) { for(id h in NSArrayObjectAtIndex(fieldMappingImportArray, 0)) { [fieldMappingTableColumnNames addObject:h]; [fieldMappingTableDefaultValues addObject:@""]; - [fieldMappingTableTypes addObject:@"VARCHAR(255)"]; + if(typeOfSourceColumns[columnCounter] == 1) { // integer type + if(maxLengthOfSourceColumns[columnCounter] < 9) + [fieldMappingTableTypes addObject:@"INT(11)"]; + else + [fieldMappingTableTypes addObject:@"BIGINT(11)"]; + } else { + if(serverGreaterThanVersion4) { + if(maxLengthOfSourceColumns[columnCounter] < 256) + [fieldMappingTableTypes addObject:@"VARCHAR(255)"]; + else if(maxLengthOfSourceColumns[columnCounter] < 32768) + [fieldMappingTableTypes addObject:@"VARCHAR(32767)"]; + else + [fieldMappingTableTypes addObject:@"TEXT"]; + } else { + if(maxLengthOfSourceColumns[columnCounter] < 256) + [fieldMappingTableTypes addObject:@"VARCHAR(255)"]; + else + [fieldMappingTableTypes addObject:@"TEXT"]; + } + } + columnCounter++; } } else { NSInteger i = 0; for(id h in NSArrayObjectAtIndex(fieldMappingImportArray, 0)) { [fieldMappingTableColumnNames addObject:[NSString stringWithFormat:@"col_%ld", i++]]; [fieldMappingTableDefaultValues addObject:@""]; - [fieldMappingTableTypes addObject:@"VARCHAR(255)"]; + if(typeOfSourceColumns[columnCounter] == 1) { // integer type + if(maxLengthOfSourceColumns[columnCounter] < 9) + [fieldMappingTableTypes addObject:@"INT(11)"]; + else + [fieldMappingTableTypes addObject:@"BIGINT(11)"]; + } else { + if(serverGreaterThanVersion4) { + if(maxLengthOfSourceColumns[columnCounter] < 256) + [fieldMappingTableTypes addObject:@"VARCHAR(255)"]; + else if(maxLengthOfSourceColumns[columnCounter] < 32768) + [fieldMappingTableTypes addObject:@"VARCHAR(32767)"]; + else + [fieldMappingTableTypes addObject:@"TEXT"]; + } else { + if(maxLengthOfSourceColumns[columnCounter] < 256) + [fieldMappingTableTypes addObject:@"VARCHAR(255)"]; + else + [fieldMappingTableTypes addObject:@"TEXT"]; + } + } + columnCounter++; } } -- cgit v1.2.3