aboutsummaryrefslogtreecommitdiffstats
path: root/Source/TableDump.m
diff options
context:
space:
mode:
authorrowanbeentje <rowan@beent.je>2010-02-05 01:51:17 +0000
committerrowanbeentje <rowan@beent.je>2010-02-05 01:51:17 +0000
commitf880dea7369cb6ce57c552cd359f871bf7e2e414 (patch)
treee7ae9cecd00dd89dc8a3e66f0ce362fa2c023d6d /Source/TableDump.m
parent557d012ed48835423c36bcd0c10754ea3f7e2d3b (diff)
downloadsequelpro-f880dea7369cb6ce57c552cd359f871bf7e2e414.tar.gz
sequelpro-f880dea7369cb6ce57c552cd359f871bf7e2e414.tar.bz2
sequelpro-f880dea7369cb6ce57c552cd359f871bf7e2e414.zip
- Rework SPSQLParser, extending DELIMITER support into all the original functions (off by default). Remove the forked "...Sql..." functions, as they're now duplicates, and switch CustomQuery to using the original methods.
- TableDump imports can now process DELIMITERs correctly as a result. - Alter the TableDump display of tables etc to use TablesList as the source of information, and used cached lists where appropriate for a small speedup. Also means we gain consistent sorting. - Display procedures and functions in the toggleable list when exporting as SQL - Tweak the procedure and function export to only export selected items, and also to respect the "export drop syntax" and "export create syntax" checkboxes - Fix a crash when removing items from the TablesList resulted in an errorneous selection by deselecting all rows before deleting (and preemptively applying the same fix to TableContent)
Diffstat (limited to 'Source/TableDump.m')
-rw-r--r--Source/TableDump.m134
1 files changed, 91 insertions, 43 deletions
diff --git a/Source/TableDump.m b/Source/TableDump.m
index b6f71c2c..c835b526 100644
--- a/Source/TableDump.m
+++ b/Source/TableDump.m
@@ -45,38 +45,42 @@
#pragma mark IBAction methods
/**
- * Get the tables in db
+ * Update the table lists with the list of tables, retrieved from the
+ * tablesList. If the user has pressed the reload button, trigger a reload
+ * from the server; otherwise used the cached lists.
+ * Retrieve only tables for all modes except SQL.
*/
- (IBAction)reloadTables:(id)sender
{
- MCPResult *queryResult;
- NSInteger i;
-
- //get tables
+
+ // Trigger a reload if necessary
+ if (sender != self) [tablesListInstance updateTables:self];
+
+ // Clear all existing tables
[tables removeAllObjects];
- queryResult = (MCPResult *)[mySQLConnection listTables];
-
- if ([queryResult numOfRows]) [queryResult dataSeek:0];
- NSMutableArray *unsortedTables = [NSMutableArray array];
- for ( i = 0 ; i < [queryResult numOfRows] ; i++ ) {
- [unsortedTables addObject:[[queryResult fetchRowAsArray] objectAtIndex:0]];
+
+ // For all modes, retrieve table and view names
+ NSArray *tablesAndViews = [tablesListInstance allTableAndViewNames];
+ for (id itemName in tablesAndViews) {
+ [tables addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:YES], itemName, [NSNumber numberWithInt:SP_TABLETYPE_TABLE], nil]];
}
-
- NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
- NSArray *sortedTables = [unsortedTables sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];
- [desc release];
-
- for ( i = 0 ; i < [sortedTables count]; i++ ) {
- [tables addObject:
- [NSMutableArray arrayWithObjects:
- [NSNumber numberWithBool:YES],
- [sortedTables objectAtIndex:i],
- nil]];
+
+ // For SQL only, add procedures and functions
+ if (exportMode == SPExportingSQL) {
+ NSArray *procedures = [tablesListInstance allProcedureNames];
+ for (id procName in procedures) {
+ [tables addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:YES], procName, [NSNumber numberWithInt:SP_TABLETYPE_PROC], nil]];
+ }
+ NSArray *functions = [tablesListInstance allFunctionNames];
+ for (id funcName in functions) {
+ [tables addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:YES], funcName, [NSNumber numberWithInt:SP_TABLETYPE_FUNC], nil]];
+ }
}
-
- [exportDumpTableView reloadData];
- [exportMultipleCSVTableView reloadData];
- [exportMultipleXMLTableView reloadData];
+
+ // Update interface
+ if (exportMode == SPExportingSQL) [exportDumpTableView reloadData];
+ else if (exportMode == SPExportingCSV) [exportMultipleCSVTableView reloadData];
+ else if (exportMode == SPExportingXML) [exportMultipleXMLTableView reloadData];
}
/**
@@ -160,6 +164,7 @@
switch ( tag ) {
case 5:
// export dump
+ exportMode = SPExportingSQL;
[self reloadTables:self];
file = [NSString stringWithFormat:@"%@_%@.sql", [tableDocumentInstance database], currentDate];
[savePanel setRequiredFileType:@"sql"];
@@ -169,6 +174,7 @@
// Export the full resultset for the currently selected table to a file in CSV format
case 6:
+ exportMode = SPExportingCSV;
file = [NSString stringWithFormat:@"%@.csv", [tableDocumentInstance table]];
[savePanel setAccessoryView:exportCSVView];
[csvFullStreamingSwitch setEnabled:YES];
@@ -177,12 +183,14 @@
// Export the full resultset for the currently selected table to a file in XML format
case 7:
+ exportMode = SPExportingXML;
file = [NSString stringWithFormat:@"%@.xml", [tableDocumentInstance table]];
contextInfo = @"exportTableContentAsXML";
break;
// Export the current "browse" view to a file in CSV format
case 8:
+ exportMode = SPExportingCSV;
file = [NSString stringWithFormat:@"%@ view.csv", [tableDocumentInstance table]];
[savePanel setAccessoryView:exportCSVView];
[csvFullStreamingSwitch setEnabled:NO];
@@ -191,12 +199,14 @@
// Export the current "browse" view to a file in XML format
case 9:
+ exportMode = SPExportingXML;
file = [NSString stringWithFormat:@"%@ view.xml", [tableDocumentInstance table]];
contextInfo = @"exportBrowseViewAsXML";
break;
// Export the current custom query result set to a file in CSV format
case 10:
+ exportMode = SPExportingCSV;
file = @"customresult.csv";
[savePanel setAccessoryView:exportCSVView];
[csvFullStreamingSwitch setEnabled:NO];
@@ -205,12 +215,14 @@
// Export the current custom query result set to a file in XML format
case 11:
+ exportMode = SPExportingXML;
file = @"customresult.xml";
contextInfo = @"exportCustomResultAsXML";
break;
// Export multiple tables to a file in CSV format
case 12:
+ exportMode = SPExportingCSV;
[self reloadTables:self];
file = [NSString stringWithFormat:@"%@.csv", [tableDocumentInstance database]];
[savePanel setAccessoryView:exportMultipleCSVView];
@@ -219,6 +231,7 @@
// Export multiple tables to a file in XML format
case 13:
+ exportMode = SPExportingXML;
[self reloadTables:self];
file = [NSString stringWithFormat:@"%@.xml", [tableDocumentInstance database]];
[savePanel setAccessoryView:exportMultipleXMLView];
@@ -227,6 +240,7 @@
// graphviz dot file
case 14:
+ exportMode = SPExportingDOT;
[self reloadTables:self];
file = [NSString stringWithString:[tableDocumentInstance database]];
[savePanel setRequiredFileType:@"dot"];
@@ -606,6 +620,7 @@
// Read in the file in a loop
sqlParser = [[SPSQLParser alloc] init];
+ [sqlParser setDelimiterSupport:YES];
sqlDataBuffer = [[NSMutableData alloc] init];
importPool = [[NSAutoreleasePool alloc] init];
while (1) {
@@ -1341,6 +1356,8 @@
NSArray *fieldNames;
NSArray *theRow;
NSMutableArray *selectedTables = [NSMutableArray array];
+ NSMutableArray *selectedProcs = [NSMutableArray array];
+ NSMutableArray *selectedFuncs = [NSMutableArray array];
NSMutableDictionary *viewSyntaxes = [NSMutableDictionary dictionary];
NSMutableString *metaString = [NSMutableString string];
NSMutableString *cellValue = [NSMutableString string];
@@ -1373,10 +1390,22 @@
[tableDocumentInstance setQueryMode:SPImportExportQueryMode];
- // Copy over the selected table names into a table in preparation for iteration
+ // Copy over the selected item names into tables in preparation for iteration
+ NSMutableArray *targetArray;
for ( i = 0 ; i < [tables count] ; i++ ) {
if ( [NSArrayObjectAtIndex(NSArrayObjectAtIndex(tables, i), 0) boolValue] ) {
- [selectedTables addObject:[NSString stringWithString:NSArrayObjectAtIndex(NSArrayObjectAtIndex(tables, i), 1)]];
+ switch ([NSArrayObjectAtIndex(NSArrayObjectAtIndex(tables, i), 2) intValue]) {
+ case SP_TABLETYPE_PROC:
+ targetArray = selectedProcs;
+ break;
+ case SP_TABLETYPE_FUNC:
+ targetArray = selectedFuncs;
+ break;
+ default:
+ targetArray = selectedTables;
+ break;
+ }
+ [targetArray addObject:[NSString stringWithString:NSArrayObjectAtIndex(NSArrayObjectAtIndex(tables, i), 1)]];
}
}
@@ -1675,8 +1704,26 @@
// Add an additional separator between tables
[fileHandle writeData:[[NSString stringWithString:@"\n\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
-
- for (NSString *procedureType in [NSArray arrayWithObjects:@"PROCEDURE", @"FUNCTION", nil]) {
+
+ // Process any deferred views, adding commands to delete the placeholder tables and add the actual views
+ viewSyntaxEnumerator = [viewSyntaxes keyEnumerator];
+ while (tableName = [viewSyntaxEnumerator nextObject]) {
+ [metaString setString:@"\n\n"];
+ [metaString appendFormat:@"DROP TABLE %@;\n", [tableName backtickQuotedString]];
+ [metaString appendFormat:@"%@;\n", [viewSyntaxes objectForKey:tableName]];
+ [fileHandle writeData:[metaString dataUsingEncoding:NSUTF8StringEncoding]];
+ }
+
+ // Export procedures and functions
+ for (NSString *procedureType in [NSArray arrayWithObjects:@"PROCEDURE", @"FUNCTION", nil]) {
+
+ // Retrieve the array of selected procedures or functions, and skip export if not selected
+ NSMutableArray *selectedItems;
+ if ([procedureType isEqualToString:@"PROCEDURE"]) selectedItems = selectedProcs;
+ else selectedItems = selectedFuncs;
+ if (![selectedItems count]) continue;
+
+ // Retrieve the definitions
queryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"/*!50003 SHOW %@ STATUS WHERE `Db` = %@ */;",
procedureType,
[[tableDocumentInstance database] tickQuotedString]]];
@@ -1689,14 +1736,24 @@
[[tableDocumentInstance database] tickQuotedString]]];
[metaString appendString:@"--\n"];
[metaString appendString:@"DELIMITER ;;\n"];
-
+
+ // Loop through the definitions, exporting if enabled
for (int t=0; t<[queryResult numOfRows]; t++) {
NSDictionary *proceduresList = [[NSDictionary alloc] initWithDictionary:[queryResult fetchRowAsDictionary]];
NSString *procedureName = [NSString stringWithFormat:@"%@", [proceduresList objectForKey:@"Name"]];
+
+ // Only proceed if the item was selected for export
+ if (![selectedItems containsObject:procedureName]) continue;
+
+ // Add the "drop" command if specified in the export dialog
+ if ([addDropTableSwitch state] == NSOnState) {
+ [metaString appendString:[NSString stringWithFormat:@"/*!50003 DROP %@ IF EXISTS %@ */;;\n",
+ procedureType,
+ [procedureName backtickQuotedString]]];
+ }
- [metaString appendString:[NSString stringWithFormat:@"/*!50003 DROP %@ IF EXISTS %@ */;;\n",
- procedureType,
- [procedureName backtickQuotedString]]];
+ // Only continue if the "create syntax" is specified in the export dialog
+ if ([addCreateTableSwitch state] == NSOffState) continue;
//Definer is user@host but we need to escape it to `user`@`host`
NSArray *procedureDefiner = [[proceduresList objectForKey:@"Definer"] componentsSeparatedByString:@"@"];
@@ -1748,15 +1805,6 @@
}
- // Process any deferred views, adding commands to delete the placeholder tables and add the actual views
- viewSyntaxEnumerator = [viewSyntaxes keyEnumerator];
- while (tableName = [viewSyntaxEnumerator nextObject]) {
- [metaString setString:@"\n\n"];
- [metaString appendFormat:@"DROP TABLE %@;\n", [tableName backtickQuotedString]];
- [metaString appendFormat:@"%@;\n", [viewSyntaxes objectForKey:tableName]];
- [fileHandle writeData:[metaString dataUsingEncoding:NSUTF8StringEncoding]];
- }
-
// Restore unique checks, foreign key checks, and other settings saved at the start
[metaString setString:@"\n\n\n"];
[metaString appendString:@"/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"];