From ecbc556ad0ed6e6c66c76145085a524bf8d6035d Mon Sep 17 00:00:00 2001 From: Bibiko Date: Sat, 6 Mar 2010 14:42:51 +0000 Subject: =?UTF-8?q?=E2=80=A2=20CSV=20Import=20Field=20Mapper=20-=20added?= =?UTF-8?q?=20to=20INSERT=20advanced=20option=20"Skip=20existing=20rows"?= =?UTF-8?q?=20This=20option=20allows=20to=20insert=20only=20those=20source?= =?UTF-8?q?=20file=20rows=20which=20primary=20keys=20do=20not=20exist.=20I?= =?UTF-8?q?t=20executes=20INSERT=20INTO=20=E2=80=A6=20ON=20DUPLICATE=20KEY?= =?UTF-8?q?=20UPDATE=20`pri=5Fkey=5Fname`=20=3D=20`pri=5Fkey=5Fname`.=20Th?= =?UTF-8?q?is=20statement=20suppresses=20all=20warnings/errors=20regarding?= =?UTF-8?q?=20to=20duplicates=20pri=20keys=20etc.=20BUT=20it=20gives=20an?= =?UTF-8?q?=20error=20if=20an=20INSERT=E2=80=A6=20statement=20of=20a=20rem?= =?UTF-8?q?aining=20row=20was=20erroneous.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPFieldMapperController.h | 3 +++ Source/SPFieldMapperController.m | 44 +++++++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 7 deletions(-) (limited to 'Source') diff --git a/Source/SPFieldMapperController.h b/Source/SPFieldMapperController.h index 7db0f5c9..26491bf0 100644 --- a/Source/SPFieldMapperController.h +++ b/Source/SPFieldMapperController.h @@ -62,6 +62,7 @@ IBOutlet id lowPriorityReplaceCheckBox; IBOutlet id lowPriorityUpdateCheckBox; IBOutlet id highPriorityCheckBox; + IBOutlet id skipexistingRowsCheckBox; IBOutlet CMTextView *onupdateTextView; IBOutlet id advancedButton; @@ -96,6 +97,8 @@ BOOL fieldMappingImportArrayIsPreview; BOOL importFieldNamesHeader; BOOL showAdvancedView; + BOOL targetTableHasPrimaryKey; + NSString *primaryKeyField; NSNumber *lastDisabledCSVFieldcolumn; MCPConnection *mySQLConnection; diff --git a/Source/SPFieldMapperController.m b/Source/SPFieldMapperController.m index 7224c38a..a33d6300 100644 --- a/Source/SPFieldMapperController.m +++ b/Source/SPFieldMapperController.m @@ -112,11 +112,14 @@ [lowPriorityReplaceCheckBox setState:NO]; [lowPriorityUpdateCheckBox setState:NO]; [highPriorityCheckBox setState:NO]; - - [advancedButton setState:NSOffState]; + [skipexistingRowsCheckBox setState:NO]; + [skipexistingRowsCheckBox setEnabled:NO]; + [advancedButton setState:NO]; [advancedBox setHidden:YES]; showAdvancedView = NO; + targetTableHasPrimaryKey = NO; + primaryKeyField = nil; heightOffset = 0; [advancedReplaceView setHidden:YES]; [advancedUpdateView setHidden:YES]; @@ -283,6 +286,7 @@ SPTableData *selectedTableData = [[SPTableData alloc] init]; [selectedTableData setConnection:mySQLConnection]; NSDictionary *tableDetails = [selectedTableData informationForTable:[tableTargetPopup titleOfSelectedItem]]; + targetTableHasPrimaryKey = NO; BOOL isReplacePossible = NO; // NSLog(@"d %@", tableDetails); if (tableDetails) { @@ -303,7 +307,8 @@ } else { [fieldMappingTableDefaultValues addObject:@"0"]; } - isReplacePossible = YES; + targetTableHasPrimaryKey = YES; + primaryKeyField = [tableDetails objectForKey:@"primarykeyfield"]; } else { if([column objectForKey:@"unique"]) { [type appendFormat:@",%@",@"UNIQUE"]; @@ -324,7 +329,8 @@ [selectedTableData release]; [[importMethodPopup menu] setAutoenablesItems:NO]; - [[importMethodPopup itemWithTitle:@"REPLACE"] setEnabled:isReplacePossible]; + [[importMethodPopup itemWithTitle:@"REPLACE"] setEnabled:(targetTableHasPrimaryKey|isReplacePossible)]; + [skipexistingRowsCheckBox setEnabled:targetTableHasPrimaryKey]; // Update the table view fieldMappingCurrentRow = 0; @@ -604,16 +610,40 @@ [lowPriorityReplaceCheckBox setState:NO]; return; } + if(sender == skipexistingRowsCheckBox) { + if([skipexistingRowsCheckBox state] == NSOnState) { + [delayedCheckBox setState:NO]; + [delayedCheckBox setEnabled:NO]; + [onupdateCheckBox setState:YES]; + [onupdateCheckBox setEnabled:NO]; + [onupdateTextView setEditable:YES]; + [onupdateTextView setSelectedRange:NSMakeRange(0,[[onupdateTextView string] length])]; + [onupdateTextView insertText:[NSString stringWithFormat:@"%@ = %@", [primaryKeyField backtickQuotedString], [primaryKeyField backtickQuotedString]]]; + [onupdateTextView setBackgroundColor:[NSColor lightGrayColor]]; + [onupdateTextView setEditable:NO]; + } else { + [delayedCheckBox setEnabled:YES]; + [onupdateCheckBox setState:NO]; + [onupdateCheckBox setEnabled:YES]; + BOOL oldEditableState = [onupdateTextView isEditable]; + [onupdateTextView setEditable:YES]; + [onupdateTextView setSelectedRange:NSMakeRange(0,[[onupdateTextView string] length])]; + [onupdateTextView insertText:@""]; + [onupdateTextView setEditable:oldEditableState]; + } + } if(sender == lowPriorityCheckBox && [lowPriorityCheckBox state] == NSOnState) { [highPriorityCheckBox setState:NO]; [delayedCheckBox setState:NO]; - [onupdateCheckBox setEnabled:YES]; + if([skipexistingRowsCheckBox state] == NSOffState) + [onupdateCheckBox setEnabled:YES]; } if(sender == highPriorityCheckBox && [highPriorityCheckBox state] == NSOnState) { [lowPriorityCheckBox setState:NO]; [delayedCheckBox setState:NO]; - [onupdateCheckBox setEnabled:YES]; + if([skipexistingRowsCheckBox state] == NSOffState) + [onupdateCheckBox setEnabled:YES]; } if(sender == delayedCheckBox) { if([delayedCheckBox state] == NSOnState) { @@ -631,7 +661,7 @@ [onupdateTextView setEditable:YES]; [[self window] makeFirstResponder:onupdateTextView]; } - if([onupdateCheckBox state] == NSOffState) { + if([onupdateCheckBox state] == NSOffState && [skipexistingRowsCheckBox state] == NSOffState) { [onupdateTextView setBackgroundColor:[NSColor lightGrayColor]]; [onupdateTextView setEditable:NO]; } -- cgit v1.2.3