diff options
author | avenjamin <avenjamin@gmail.com> | 2009-02-27 08:20:12 +0000 |
---|---|---|
committer | avenjamin <avenjamin@gmail.com> | 2009-02-27 08:20:12 +0000 |
commit | 3e07d763113142a857dc8f1243571a1ae2360c58 (patch) | |
tree | 5cb3ef71439fe942f003c6fe7aa352b913fafae0 | |
parent | 60d029980c919e8299304231f9a3c0847a35f9f1 (diff) | |
download | sequelpro-3e07d763113142a857dc8f1243571a1ae2360c58.tar.gz sequelpro-3e07d763113142a857dc8f1243571a1ae2360c58.tar.bz2 sequelpro-3e07d763113142a857dc8f1243571a1ae2360c58.zip |
Patch submitted by Marius Ursache.
- Fixes crash when you import a csv file into an empty table.
- Added auto refresh of tables list when you import an sql file.
-rw-r--r-- | Source/TableDump.m | 163 |
1 files changed, 86 insertions, 77 deletions
diff --git a/Source/TableDump.m b/Source/TableDump.m index c5b28b68..6bb61975 100644 --- a/Source/TableDump.m +++ b/Source/TableDump.m @@ -438,6 +438,9 @@ [errorsSheet orderOut:nil]; } + //update tables list + [tablesListInstance updateTables:self]; + //////////////// // IMPORT CSV // //////////////// @@ -496,103 +499,109 @@ [fieldMappingPopup selectItemAtIndex:0]; } - [tableListView selectRowIndexes:[NSIndexSet indexSetWithIndex:[[tablesListInstance tables] indexOfObject:[fieldMappingPopup titleOfSelectedItem]]] byExtendingSelection:NO]; - - //set up tableView - currentRow = 0; - fieldMappingArray = nil; - [self setupFieldMappingArray]; - [rowDownButton setEnabled:NO]; - [rowUpButton setEnabled:([importArray count] > 1)]; - [recordCountLabel setStringValue:[NSString stringWithFormat:@"%i of %i records", currentRow+1, [importArray count]]]; - - //set up tableView buttons - [buttonCell setControlSize:NSSmallControlSize]; - [buttonCell setFont:[NSFont labelFontOfSize:[NSFont smallSystemFontSize]]]; - [buttonCell setBordered:NO]; - [[fieldMappingTableView tableColumnWithIdentifier:@"value"] setDataCell:buttonCell]; - [self updateFieldMappingButtonCell]; - [fieldMappingTableView reloadData]; - - // show fieldMapping sheet - [NSApp beginSheet:fieldMappingSheet - modalForWindow:tableWindow - modalDelegate:self - didEndSelector:nil - contextInfo:nil]; - - code = [NSApp runModalForWindow:fieldMappingSheet]; - - [NSApp endSheet:fieldMappingSheet]; - [fieldMappingSheet orderOut:nil]; + int indexOfFirstTable = [[tablesListInstance tables] indexOfObject:[fieldMappingPopup titleOfSelectedItem]]; - if ( code ) { - //import array into db - NSMutableString *fNames = [NSMutableString string]; - //NSMutableArray *fValuesIndexes = [NSMutableArray array]; - NSMutableString *fValues = [NSMutableString string]; - int i,j; + if( indexOfFirstTable == NSNotFound ){ + [errors appendString:[NSString stringWithFormat:NSLocalizedString(@"[ERROR] %@\n", @"error text when trying to import csv data, but we have no tables in the db"), @"Can't import CSV data into a database without any tables!"]]; + } else { + [tableListView selectRowIndexes:[NSIndexSet indexSetWithIndex:indexOfFirstTable] byExtendingSelection:NO]; + + //set up tableView + currentRow = 0; + fieldMappingArray = nil; + [self setupFieldMappingArray]; + [rowDownButton setEnabled:NO]; + [rowUpButton setEnabled:([importArray count] > 1)]; + [recordCountLabel setStringValue:[NSString stringWithFormat:@"%i of %i records", currentRow+1, [importArray count]]]; - //open progress sheet - [NSApp beginSheet:singleProgressSheet + //set up tableView buttons + [buttonCell setControlSize:NSSmallControlSize]; + [buttonCell setFont:[NSFont labelFontOfSize:[NSFont smallSystemFontSize]]]; + [buttonCell setBordered:NO]; + [[fieldMappingTableView tableColumnWithIdentifier:@"value"] setDataCell:buttonCell]; + [self updateFieldMappingButtonCell]; + [fieldMappingTableView reloadData]; + + // show fieldMapping sheet + [NSApp beginSheet:fieldMappingSheet modalForWindow:tableWindow modalDelegate:self didEndSelector:nil contextInfo:nil]; - // get fields to be imported - for (i = 0; i < [fieldMappingArray count] ; i++ ) { - if ([[fieldMappingArray objectAtIndex:i] intValue] > 0) { - if ( [fNames length] ) - [fNames appendString:@","]; - - [fNames appendString:[NSString stringWithFormat:@"`%@`", [[tableSourceInstance fieldNames] objectAtIndex:i]]]; - } - } + code = [NSApp runModalForWindow:fieldMappingSheet]; - //import array - for ( i = 0 ; i < [importArray count] ; i++ ) { - //show progress bar - [singleProgressBar setDoubleValue:((i+1)*100/[importArray count])]; - [singleProgressBar displayIfNeeded]; + [NSApp endSheet:fieldMappingSheet]; + [fieldMappingSheet orderOut:nil]; + + if ( code ) { + //import array into db + NSMutableString *fNames = [NSMutableString string]; + //NSMutableArray *fValuesIndexes = [NSMutableArray array]; + NSMutableString *fValues = [NSMutableString string]; + int i,j; + + //open progress sheet + [NSApp beginSheet:singleProgressSheet + modalForWindow:tableWindow + modalDelegate:self + didEndSelector:nil + contextInfo:nil]; - if ( !([importFieldNamesSwitch state] && (i == 0)) ) { - //put values in string - [fValues setString:@""]; + // get fields to be imported + for (i = 0; i < [fieldMappingArray count] ; i++ ) { + if ([[fieldMappingArray objectAtIndex:i] intValue] > 0) { + if ( [fNames length] ) + [fNames appendString:@","]; + + [fNames appendString:[NSString stringWithFormat:@"`%@`", [[tableSourceInstance fieldNames] objectAtIndex:i]]]; + } + } + + //import array + for ( i = 0 ; i < [importArray count] ; i++ ) { + //show progress bar + [singleProgressBar setDoubleValue:((i+1)*100/[importArray count])]; + [singleProgressBar displayIfNeeded]; - for ( j = 0 ; j < [fieldMappingArray count] ; j++ ) { + if ( !([importFieldNamesSwitch state] && (i == 0)) ) { + //put values in string + [fValues setString:@""]; - if ([[fieldMappingArray objectAtIndex:j] intValue] > 0) { - if ( [fValues length] ) - [fValues appendString:@","]; + for ( j = 0 ; j < [fieldMappingArray count] ; j++ ) { - if ([[[importArray objectAtIndex:i] objectAtIndex:([[fieldMappingArray objectAtIndex:j] intValue] - 1)] isMemberOfClass:[NSNull class]] ) { - [fValues appendString:@"NULL"]; - } else { - [fValues appendString:[NSString stringWithFormat:@"'%@'",[mySQLConnection prepareString:[[importArray objectAtIndex:i] objectAtIndex:([[fieldMappingArray objectAtIndex:j] intValue] - 1)]]]]; + if ([[fieldMappingArray objectAtIndex:j] intValue] > 0) { + if ( [fValues length] ) + [fValues appendString:@","]; + + if ([[[importArray objectAtIndex:i] objectAtIndex:([[fieldMappingArray objectAtIndex:j] intValue] - 1)] isMemberOfClass:[NSNull class]] ) { + [fValues appendString:@"NULL"]; + } else { + [fValues appendString:[NSString stringWithFormat:@"'%@'",[mySQLConnection prepareString:[[importArray objectAtIndex:i] objectAtIndex:([[fieldMappingArray objectAtIndex:j] intValue] - 1)]]]]; + } } } - } - - //perform query - [mySQLConnection queryString:[NSString stringWithFormat:@"INSERT INTO `%@` (%@) VALUES (%@)", - [fieldMappingPopup titleOfSelectedItem], - fNames, - 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]]]; + + //perform query + [mySQLConnection queryString:[NSString stringWithFormat:@"INSERT INTO `%@` (%@) VALUES (%@)", + [fieldMappingPopup titleOfSelectedItem], + fNames, + 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]]]; + } } } + + //close progress sheet + [NSApp endSheet:singleProgressSheet]; + [singleProgressSheet orderOut:nil]; } - //close progress sheet - [NSApp endSheet:singleProgressSheet]; - [singleProgressSheet orderOut:nil]; + [tableContentInstance reloadTableValues:self]; } - [tableContentInstance reloadTableValues:self]; - //display errors if ( [errors length] ) { [errorsView setString:errors]; |