diff options
author | Bibiko <bibiko@eva.mpg.de> | 2010-09-04 14:06:22 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2010-09-04 14:06:22 +0000 |
commit | 7476468dd0b38d6cca82c85f244d16edaa81a520 (patch) | |
tree | da86325743af0f51cfca998d1ba89e82f914e233 /Source | |
parent | 7a5af6632f0c6797466372358327ddd75a4e7f22 (diff) | |
download | sequelpro-7476468dd0b38d6cca82c85f244d16edaa81a520.tar.gz sequelpro-7476468dd0b38d6cca82c85f244d16edaa81a520.tar.bz2 sequelpro-7476468dd0b38d6cca82c85f244d16edaa81a520.zip |
• CSV Import into new TABLE
- added button "New"
- added chance to change encoding and engine of the new table which will be saved in the Prefs
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPConstants.h | 3 | ||||
-rw-r--r-- | Source/SPConstants.m | 2 | ||||
-rw-r--r-- | Source/SPFieldMapperController.h | 10 | ||||
-rw-r--r-- | Source/SPFieldMapperController.m | 194 |
4 files changed, 156 insertions, 53 deletions
diff --git a/Source/SPConstants.h b/Source/SPConstants.h index 1ee44264..9b89deb3 100644 --- a/Source/SPConstants.h +++ b/Source/SPConstants.h @@ -350,6 +350,9 @@ extern NSString *SPQuickLookTypes; extern NSString *SPTableChangedNotification; extern NSString *SPBlobTextEditorSpellCheckingEnabled; extern NSString *SPUniqueSchemaDelimiter; +extern NSString *SPLastImportIntoNewTableEncoding; +extern NSString *SPLastImportIntoNewTableType; + // URLs extern NSString *SPDonationsURL; diff --git a/Source/SPConstants.m b/Source/SPConstants.m index dd6758b1..e3de4b06 100644 --- a/Source/SPConstants.m +++ b/Source/SPConstants.m @@ -174,6 +174,8 @@ NSString *SPQuickLookTypes = @"QuickLookTypes"; NSString *SPTableChangedNotification = @"SPTableSelectionChanged"; NSString *SPBlobTextEditorSpellCheckingEnabled = @"BlobTextEditorSpellCheckingEnabled"; NSString *SPUniqueSchemaDelimiter = @""; // U+FFF8 +NSString *SPLastImportIntoNewTableEncoding = @"LastImportIntoNewTableEncoding"; +NSString *SPLastImportIntoNewTableType = @"LastImportIntoNewTableType"; // URLs NSString *SPDonationsURL = @"http://www.sequelpro.com/donate.html"; diff --git a/Source/SPFieldMapperController.h b/Source/SPFieldMapperController.h index 17b6db9a..484ce53d 100644 --- a/Source/SPFieldMapperController.h +++ b/Source/SPFieldMapperController.h @@ -54,6 +54,10 @@ IBOutlet NSTextField *newTableNameTextField; IBOutlet NSTextField *newTableNameLabel; IBOutlet NSButton *newTableNameInfoButton; + IBOutlet NSButton *newTableButton; + IBOutlet id newTableInfoWindow; + IBOutlet id newTableInfoEncodingPopup; + IBOutlet id newTableInfoEnginePopup; IBOutlet id globalValuesSheet; IBOutlet NSButton *addGlobalValueButton; @@ -85,6 +89,7 @@ id customQueryInstance; id fieldMappingImportArray; id tablesListInstance; + id databaseDataInstance; NSInteger fieldMappingCurrentRow; NSMutableArray *fieldMappingArray; @@ -98,6 +103,8 @@ NSMutableArray *fieldMappingTableDefaultValues; NSMutableArray *defaultFieldTypesForComboBox; + NSString *newTableEncoding; + NSString *newTableEngine; NSNumber *doImport; NSNumber *doNotImport; @@ -170,6 +177,9 @@ - (IBAction)closeGlobalValuesSheet:(id)sender; - (IBAction)advancedCheckboxValidation:(id)sender; +- (IBAction)newTable:(id)sender; +- (IBAction)newTableInfo:(id)sender; +- (IBAction)closeInfoSheet:(id)sender; - (IBAction)addNewColumn:(id)sender; - (IBAction)setAllTypesTo:(id)sender; diff --git a/Source/SPFieldMapperController.m b/Source/SPFieldMapperController.m index c7ba8ee7..c77435cc 100644 --- a/Source/SPFieldMapperController.m +++ b/Source/SPFieldMapperController.m @@ -79,6 +79,12 @@ prefs = [NSUserDefaults standardUserDefaults]; tablesListInstance = [theDelegate valueForKeyPath:@"tablesListInstance"]; + databaseDataInstance = [tablesListInstance valueForKeyPath:@"databaseDataInstance"]; + + if(![prefs objectForKey:SPLastImportIntoNewTableType]) + [prefs setObject:@"Default" forKey:SPLastImportIntoNewTableType]; + if(![prefs objectForKey:SPLastImportIntoNewTableEncoding]) + [prefs setObject:@"Default" forKey:SPLastImportIntoNewTableEncoding]; } @@ -110,6 +116,7 @@ [newTableNameTextField setHidden:YES]; [newTableNameLabel setHidden:YES]; [newTableNameInfoButton setHidden:YES]; + [newTableButton setHidden:NO]; // Init table target popup menu [tableTargetPopup removeAllItems]; @@ -358,6 +365,14 @@ #pragma mark - #pragma mark IBAction methods +- (IBAction)closeInfoSheet:(id)sender +{ + [prefs setObject:[newTableInfoEnginePopup titleOfSelectedItem] forKey:SPLastImportIntoNewTableType]; + [prefs setObject:[newTableInfoEncodingPopup titleOfSelectedItem] forKey:SPLastImportIntoNewTableEncoding]; + [NSApp endSheet:[sender window] returnCode:[sender tag]]; + [[sender window] orderOut:self]; +} + - (IBAction)closeSheet:(id)sender { @@ -375,6 +390,15 @@ if(columnIndex < numberOfColumns-1) [createString appendString:@", \n"]; } [createString appendString:@")"]; + + if(![[prefs objectForKey:SPLastImportIntoNewTableType] isEqualToString:@"Default"]) + [createString appendFormat:@" ENGINE=%@", [prefs objectForKey:SPLastImportIntoNewTableType]]; + if(![[prefs objectForKey:SPLastImportIntoNewTableEncoding] isEqualToString:@"Default"]) { + NSString *encodingName = [[prefs objectForKey:SPLastImportIntoNewTableEncoding] stringByMatching:@"\\((.*)\\)" capture:1L]; + if (!encodingName) encodingName = @"utf8"; + [createString appendFormat:[NSString stringWithFormat:@" DEFAULT CHARACTER SET %@", [encodingName backtickQuotedString]]]; + } + [mySQLConnection queryString:createString]; if ([mySQLConnection queryErrored]) { @@ -437,59 +461,7 @@ // New Table was chosen else if([tableTargetPopup selectedItem] == [tableTargetPopup itemAtIndex:0]) { - - newTableMode = YES; - - [[[fieldMapperTableView menu] itemAtIndex:0] setHidden:NO]; - [[[fieldMapperTableView menu] itemAtIndex:1] setHidden:NO]; - - [importMethodPopup selectItemWithTitle:@"INSERT"]; - [[importMethodPopup itemWithTitle:@"UPDATE"] setEnabled:NO]; - [[importMethodPopup itemWithTitle:@"REPLACE"] setEnabled:NO]; - - [tableTargetPopup setHidden:YES]; - [newTableNameTextField setHidden:NO]; - [newTableNameLabel setHidden:NO]; - [newTableNameInfoButton setHidden:NO]; - [newTableNameTextField selectText:nil]; - - [fieldMappingTableColumnNames removeAllObjects]; - [fieldMappingTableDefaultValues removeAllObjects]; - [fieldMappingTableTypes removeAllObjects]; - if([importFieldNamesHeaderSwitch state] == NSOnState) { - for(id h in NSArrayObjectAtIndex(fieldMappingImportArray, 0)) { - [fieldMappingTableColumnNames addObject:h]; - [fieldMappingTableDefaultValues addObject:@""]; - [fieldMappingTableTypes addObject:@"varchar(255)"]; - } - } else { - i = 0; - for(id h in NSArrayObjectAtIndex(fieldMappingImportArray, 0)) { - [fieldMappingTableColumnNames addObject:[NSString stringWithFormat:@"col_%ld", i++]]; - [fieldMappingTableDefaultValues addObject:@""]; - [fieldMappingTableTypes addObject:@"varchar(255)"]; - } - } - - // Update the table view - NSInteger i; - fieldMappingCurrentRow = 0; - if (fieldMappingArray) [fieldMappingArray release], fieldMappingArray = nil; - [self setupFieldMappingArray]; - [rowDownButton setEnabled:NO]; - [rowUpButton setEnabled:([fieldMappingImportArray count] > 1)]; - [recordCountLabel setStringValue:[NSString stringWithFormat:@"%ld of %@%lu records", (long)(fieldMappingCurrentRow+1), fieldMappingImportArrayIsPreview?@"first ":@"", (unsigned long)[fieldMappingImportArray count]]]; - - [self updateFieldMappingButtonCell]; - [self updateFieldMappingOperatorOptions]; - - // Set all operators to doNotImport - [fieldMappingOperatorArray removeAllObjects]; - for(i=0; i < [fieldMappingTableColumnNames count]; i++) - [fieldMappingOperatorArray addObject:doImport]; - - [fieldMapperTableView reloadData]; - [self validateImportButton]; + [self newTable:nil]; return; } @@ -738,6 +710,63 @@ } } +- (IBAction)newTable:(id)sender +{ + newTableMode = YES; + + [[[fieldMapperTableView menu] itemAtIndex:0] setHidden:NO]; + [[[fieldMapperTableView menu] itemAtIndex:1] setHidden:NO]; + + [importMethodPopup selectItemWithTitle:@"INSERT"]; + [[importMethodPopup itemWithTitle:@"UPDATE"] setEnabled:NO]; + [[importMethodPopup itemWithTitle:@"REPLACE"] setEnabled:NO]; + + [tableTargetPopup setHidden:YES]; + [newTableNameTextField setHidden:NO]; + [newTableNameLabel setHidden:NO]; + [newTableNameInfoButton setHidden:NO]; + [newTableButton setHidden:YES]; + [newTableNameTextField selectText:nil]; + + [fieldMappingTableColumnNames removeAllObjects]; + [fieldMappingTableDefaultValues removeAllObjects]; + [fieldMappingTableTypes removeAllObjects]; + if([importFieldNamesHeaderSwitch state] == NSOnState) { + for(id h in NSArrayObjectAtIndex(fieldMappingImportArray, 0)) { + [fieldMappingTableColumnNames addObject:h]; + [fieldMappingTableDefaultValues addObject:@""]; + [fieldMappingTableTypes addObject:@"varchar(255)"]; + } + } else { + NSInteger i = 0; + for(id h in NSArrayObjectAtIndex(fieldMappingImportArray, 0)) { + [fieldMappingTableColumnNames addObject:[NSString stringWithFormat:@"col_%ld", i++]]; + [fieldMappingTableDefaultValues addObject:@""]; + [fieldMappingTableTypes addObject:@"varchar(255)"]; + } + } + + // Update the table view + NSInteger i; + fieldMappingCurrentRow = 0; + if (fieldMappingArray) [fieldMappingArray release], fieldMappingArray = nil; + [self setupFieldMappingArray]; + [rowDownButton setEnabled:NO]; + [rowUpButton setEnabled:([fieldMappingImportArray count] > 1)]; + [recordCountLabel setStringValue:[NSString stringWithFormat:@"%ld of %@%lu records", (long)(fieldMappingCurrentRow+1), fieldMappingImportArrayIsPreview?@"first ":@"", (unsigned long)[fieldMappingImportArray count]]]; + + [self updateFieldMappingButtonCell]; + [self updateFieldMappingOperatorOptions]; + + // Set all operators to doNotImport + [fieldMappingOperatorArray removeAllObjects]; + for(i=0; i < [fieldMappingTableColumnNames count]; i++) + [fieldMappingOperatorArray addObject:doImport]; + + [fieldMapperTableView reloadData]; + [self validateImportButton]; +} + - (IBAction)addNewColumn:(id)sender { @@ -762,6 +791,65 @@ [type release]; } +- (IBAction)newTableInfo:(id)sender +{ + [[self window] endEditingFor:nil]; + + // Populate the table type (engine) popup button + [newTableInfoEnginePopup removeAllItems]; + + NSArray *engines = [databaseDataInstance getDatabaseStorageEngines]; + + // Add default menu item + [newTableInfoEnginePopup addItemWithTitle:@"Default"]; + [[newTableInfoEnginePopup menu] addItem:[NSMenuItem separatorItem]]; + + for (NSDictionary *engine in engines) + { + [newTableInfoEnginePopup addItemWithTitle:[engine objectForKey:@"Engine"]]; + } + + [newTableInfoEnginePopup selectItemWithTitle:[prefs objectForKey:SPLastImportIntoNewTableType]]; + + // Populate the table encoding popup button with a default menu item + [newTableInfoEncodingPopup removeAllItems]; + [newTableInfoEncodingPopup addItemWithTitle:@"Default"]; + + // Retrieve the server-supported encodings and add them to the menu + NSArray *encodings = [databaseDataInstance getDatabaseCharacterSetEncodings]; + NSString *utf8MenuItemTitle = nil; + if ([encodings count] > 0 + && ([mySQLConnection serverMajorVersion] > 4 + || ([mySQLConnection serverMajorVersion] == 4 && [mySQLConnection serverMinorVersion] >= 1))) + { + [[newTableInfoEncodingPopup menu] addItem:[NSMenuItem separatorItem]]; + for (NSDictionary *encoding in encodings) { + NSString *menuItemTitle = (![encoding objectForKey:@"DESCRIPTION"]) ? [encoding objectForKey:@"CHARACTER_SET_NAME"] : [NSString stringWithFormat:@"%@ (%@)", [encoding objectForKey:@"DESCRIPTION"], [encoding objectForKey:@"CHARACTER_SET_NAME"]]; + [newTableInfoEncodingPopup addItemWithTitle:menuItemTitle]; + + // If the UTF8 entry has been encountered, store the menu title + if ([[encoding objectForKey:@"CHARACTER_SET_NAME"] isEqualToString:@"utf8"]) { + utf8MenuItemTitle = [NSString stringWithString:menuItemTitle]; + } + } + + // If a UTF8 entry was found, promote it to the top of the list + if (utf8MenuItemTitle) { + [[newTableInfoEncodingPopup menu] insertItem:[NSMenuItem separatorItem] atIndex:2]; + [newTableInfoEncodingPopup insertItemWithTitle:utf8MenuItemTitle atIndex:2]; + } + + [newTableInfoEncodingPopup selectItemWithTitle:[prefs objectForKey:SPLastImportIntoNewTableEncoding]]; + + } + + [NSApp beginSheet:newTableInfoWindow + modalForWindow:[self window] + modalDelegate:self + didEndSelector:nil + contextInfo:nil]; +} + #pragma mark - #pragma mark Global Value Sheet |