From 37cae99ed1ace10a7d106255efdc104d1af760e9 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Mon, 15 Jun 2009 18:45:58 +0000 Subject: Implementation of enhancement #273: Allow user to truncate table by right clicking. --- Interfaces/English.lproj/DBView.xib | 68 ++++++++++--- Interfaces/English.lproj/Localizable.strings | Bin 51956 -> 58774 bytes Source/TablesList.h | 3 + Source/TablesList.m | 139 ++++++++++++++++++--------- 4 files changed, 151 insertions(+), 59 deletions(-) diff --git a/Interfaces/English.lproj/DBView.xib b/Interfaces/English.lproj/DBView.xib index 3373c38c..0b503445 100644 --- a/Interfaces/English.lproj/DBView.xib +++ b/Interfaces/English.lproj/DBView.xib @@ -2,12 +2,13 @@ 1050 - 9G55 + 9J61 677 - 949.43 + 949.46 353.00 YES + YES @@ -45,7 +46,7 @@ {3.40282e+38, 3.40282e+38} {780, 480} - + 256 YES @@ -386,7 +387,10 @@ 1048576 2147483647 - + + NSImage + button_action + _popUpItemAction: @@ -434,11 +438,20 @@ _popUpItemAction: + + + Truncate Table + + 2147483647 + + + _popUpItemAction: + + Remove Table - 1048576 2147483647 @@ -447,6 +460,7 @@ + 4 YES 1 YES @@ -2950,7 +2964,7 @@ 1048576 2147483647 - + _popUpItemAction: @@ -4388,6 +4402,7 @@ {944, 550} + {{0, 0}, {1440, 878}} {780, 502} @@ -12289,7 +12304,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 NSView - + 265 YES @@ -12310,7 +12325,6 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {{14, 0}, {22, 22}} - YES 130560 @@ -12324,8 +12338,6 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 {50, 22} - - NSView @@ -16109,6 +16121,22 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 6003 + + + truncateTableButton + + + + 6005 + + + + truncateTable: + + + + 6006 + @@ -19045,6 +19073,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 + @@ -22794,6 +22823,11 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 + + 6004 + + + @@ -24158,6 +24192,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 6.ImportedFromIB2 6000.IBPluginDependency 6001.IBPluginDependency + 6004.IBPluginDependency 604.IBEditorWindowLastContentRect 604.IBWindowTemplateEditedContentRect 604.ImportedFromIB2 @@ -25408,7 +25443,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{71, 99}, {175, 93}} + {{476, -4}, {175, 113}} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -25430,8 +25465,8 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{171, 147}, {944, 550}} - {{171, 147}, {944, 550}} + {{467, 84}, {944, 550}} + {{467, 84}, {944, 550}} {{62, 352}, {845, 504}} @@ -26071,6 +26106,7 @@ aGUgYWN0aXZlIHNlbGVjdGlvbiAo4oyl4oyYUik com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin {{67, 340}, {667, 416}} {{67, 340}, {667, 416}} @@ -26323,7 +26359,7 @@ Y2hhbmdlIHRoZSBvcmRlcg - 6003 + 6006 @@ -27530,6 +27566,7 @@ Y2hhbmdlIHRoZSBvcmRlcg copyTable: removeTable: renameTable: + truncateTable: updateTables: @@ -27541,6 +27578,7 @@ Y2hhbmdlIHRoZSBvcmRlcg id id id + id @@ -27575,6 +27613,7 @@ Y2hhbmdlIHRoZSBvcmRlcg tableTypeButton tableWindow tablesListView + truncateTableButton YES @@ -27606,6 +27645,7 @@ Y2hhbmdlIHRoZSBvcmRlcg id id id + id diff --git a/Interfaces/English.lproj/Localizable.strings b/Interfaces/English.lproj/Localizable.strings index 5e8aaa32..50e87373 100644 Binary files a/Interfaces/English.lproj/Localizable.strings and b/Interfaces/English.lproj/Localizable.strings differ diff --git a/Source/TablesList.h b/Source/TablesList.h index 6fd86c56..574f9c18 100644 --- a/Source/TablesList.h +++ b/Source/TablesList.h @@ -64,6 +64,7 @@ enum sp_table_types IBOutlet id tableRenameField; IBOutlet id tableRenameText; IBOutlet id renameTableButton; + IBOutlet id truncateTableButton; IBOutlet NSMenuItem *removeTableMenuItem; IBOutlet NSMenuItem *duplicateTableMenuItem; @@ -85,12 +86,14 @@ enum sp_table_types - (IBAction)removeTable:(id)sender; - (IBAction)copyTable:(id)sender; - (IBAction)renameTable:(id)sender; +- (IBAction)truncateTable:(id)sender; // copyTableSheet methods - (IBAction)closeCopyTableSheet:(id)sender; // Additional methods - (void)removeTable; +- (void)truncateTable; - (void)setConnection:(CMMCPConnection *)theConnection; - (void)doPerformQueryService:(NSString *)query; diff --git a/Source/TablesList.m b/Source/TablesList.m index d90f7972..98b67904 100644 --- a/Source/TablesList.m +++ b/Source/TablesList.m @@ -396,15 +396,9 @@ - (IBAction)copyTable:(id)sender { CMMCPResult *queryResult; -// NSArray *fieldNames; -// NSArray *theRow; -// NSMutableString *rowValue = [NSMutableString string]; -// NSMutableArray *fieldValues; int code; NSString *tableType; int tblType; -// int rowCount, i, j; -// BOOL errors = NO; if ( [tablesListView numberOfSelectedRows] != 1 ) { return; @@ -672,6 +666,32 @@ [tableDocumentInstance name], [tableDocumentInstance database], [tableRenameField stringValue]]]; } +/** + * Truncates the currently selected table(s). + */ +- (IBAction)truncateTable:(id)sender +{ + if (![tablesListView numberOfSelectedRows]) + return; + + [tableWindow endEditingFor:nil]; + + NSAlert *alert = [NSAlert alertWithMessageText:@"" defaultButton:NSLocalizedString(@"Truncate", @"truncate button") alternateButton:NSLocalizedString(@"Cancel", @"cancel button") otherButton:nil informativeTextWithFormat:@""]; + + [alert setAlertStyle:NSCriticalAlertStyle]; + + if ([tablesListView numberOfSelectedRows] == 1) { + [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Truncate table '%@'?", @"truncate table message"), [tables objectAtIndex:[tablesListView selectedRow]]]]; + [alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Are you sure you want to truncate the table '%@'. This operation cannot be undone.", @"truncate table informative message"), [tables objectAtIndex:[tablesListView selectedRow]]]]; + } + else { + [alert setMessageText:NSLocalizedString(@"Truncate selected tables?", @"truncate tables message")]; + [alert setInformativeText:NSLocalizedString(@"Are you sure you want to truncate the selected tables. This operation cannot be undone.", @"truncate tables informative message")]; + } + + [alert beginSheetModalForWindow:tableWindow modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:@"truncateTable"]; +} + #pragma mark Alert sheet methods /** @@ -679,15 +699,23 @@ */ - (void)sheetDidEnd:(NSAlert *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo { - - if ( [contextInfo isEqualToString:@"addRow"] ) { + if ([contextInfo isEqualToString:@"addRow"]) { alertSheetOpened = NO; - } else if ( [contextInfo isEqualToString:@"removeRow"] ) { + } + else if ([contextInfo isEqualToString:@"removeRow"]) { [[sheet window] orderOut:nil]; - if ( returnCode == NSAlertDefaultReturn ) { + + if (returnCode == NSAlertDefaultReturn) { [self removeTable]; } } + else if ([contextInfo isEqualToString:@"truncateTable"]) { + [[sheet window] orderOut:nil]; + + if (returnCode == NSAlertDefaultReturn) { + [self truncateTable]; + } + } } /** @@ -701,7 +729,7 @@ #pragma mark Additional methods /** - * Removes selected table(s) or view(s) from mysql-db and tableView + * Removes the selected table(s) or view(s) from mysql-db and tableView */ - (void)removeTable { @@ -746,11 +774,6 @@ currentIndex = [indexes indexLessThanIndex:currentIndex]; } - //[tablesListView deselectAll:self]; - - //[tableSourceInstance loadTable:nil]; - //[tableContentInstance loadTable:nil]; - //[tableStatusInstance loadTable:nil]; [tablesListView reloadData]; // set window title @@ -760,17 +783,37 @@ if ( error ) { NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"Couldn't remove '%@'.\nMySQL said: %@", @"message of panel when an item cannot be removed"), [tables objectAtIndex:currentIndex], errorText]); - - /* - NSRunAlertPanel(NSLocalizedString(@"Error", @"error"), - [NSString stringWithFormat:NSLocalizedString(@"Couldn't remove table.\nMySQL said: %@", @"message of panel when table cannot be removed"), errorText], - NSLocalizedString(@"OK", @"OK button"), nil, nil, nil ); - */ } [tablesListView deselectAll:self]; } +/** + * Trucates the selected table(s). + */ +- (void)truncateTable +{ + NSIndexSet *indexes = [tablesListView selectedRowIndexes]; + + // Get last index + unsigned currentIndex = [indexes lastIndex]; + + while (currentIndex != NSNotFound) + { + [mySQLConnection queryString:[NSString stringWithFormat: @"TRUNCATE TABLE %@", [[tables objectAtIndex:currentIndex] backtickQuotedString]]]; + + // Couldn't truncate table + if (![[mySQLConnection getLastErrorMessage] isEqualTo:@""]) { + NSBeginAlertSheet(NSLocalizedString(@"Error truncating table", @"error truncating table message"), + NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"An error occurred while trying to truncate the table '%@'.\n\nMySQL said: %@", @"error truncating table informative message"), [tables objectAtIndex:currentIndex], [mySQLConnection getLastErrorMessage]]); + } + + // Get next index (beginning from the end) + currentIndex = [indexes indexLessThanIndex:currentIndex]; + } +} + /** * Sets the connection (received from TableDocument) and makes things that have to be done only once */ @@ -1095,8 +1138,7 @@ * Loads a table in content or source view (if tab selected) */ - (void)tableViewSelectionDidChange:(NSNotification *)aNotification -{ - +{ if ( [tablesListView numberOfSelectedRows] == 1 && [[self tableName] length] ) { // Reset the table information caches @@ -1155,6 +1197,7 @@ // Set gear menu items Remove/Duplicate table/view and mainMenu > Table items // according to the table types NSMenu *tableSubMenu = [[[NSApp mainMenu] itemAtIndex:5] submenu]; + if([[tableTypes objectAtIndex:[tablesListView selectedRow]] intValue] == SP_TABLETYPE_VIEW) { // Change mainMenu > Table > ... according to table type @@ -1172,10 +1215,11 @@ [[tableSubMenu itemAtIndex:9] setHidden:YES]; // checksum [renameTableMenuItem setHidden:NO]; // we don't have to check the mysql version - [renameTableMenuItem setTitle:NSLocalizedString(@"Rename view", @"rename view menu title")]; + [renameTableMenuItem setTitle:NSLocalizedString(@"Rename View", @"rename view menu title")]; [duplicateTableMenuItem setHidden:NO]; - [duplicateTableMenuItem setTitle:NSLocalizedString(@"Duplicate view", @"duplicate view menu title")]; - [removeTableMenuItem setTitle:NSLocalizedString(@"Remove view", @"remove view menu title")]; + [duplicateTableMenuItem setTitle:NSLocalizedString(@"Duplicate View", @"duplicate view menu title")]; + [truncateTableButton setHidden:YES]; + [removeTableMenuItem setTitle:NSLocalizedString(@"Remove View", @"remove view menu title")]; } else if([[tableTypes objectAtIndex:[tablesListView selectedRow]] intValue] == SP_TABLETYPE_TABLE) { [[tableSubMenu itemAtIndex:0] setTitle:NSLocalizedString(@"Copy Create Table Syntax", @"copy create table syntax menu item")]; @@ -1192,10 +1236,12 @@ [[tableSubMenu itemAtIndex:9] setHidden:NO]; [renameTableMenuItem setHidden:NO]; - [renameTableMenuItem setTitle:NSLocalizedString(@"Rename table", @"rename table menu title")]; + [renameTableMenuItem setTitle:NSLocalizedString(@"Rename Table", @"rename table menu title")]; [duplicateTableMenuItem setHidden:NO]; - [duplicateTableMenuItem setTitle:NSLocalizedString(@"Duplicate table", @"duplicate table menu title")]; - [removeTableMenuItem setTitle:NSLocalizedString(@"Remove table", @"remove table menu title")]; + [duplicateTableMenuItem setTitle:NSLocalizedString(@"Duplicate Table", @"duplicate table menu title")]; + [truncateTableButton setHidden:NO]; + [truncateTableButton setTitle:NSLocalizedString(@"Truncate Table", @"truncate table menu title")]; + [removeTableMenuItem setTitle:NSLocalizedString(@"Remove Table", @"remove table menu title")]; } else if([[tableTypes objectAtIndex:[tablesListView selectedRow]] intValue] == SP_TABLETYPE_PROC) { [[tableSubMenu itemAtIndex:0] setTitle:NSLocalizedString(@"Copy Create Procedure Syntax", @"copy create proc syntax menu item")]; @@ -1210,10 +1256,11 @@ [[tableSubMenu itemAtIndex:9] setHidden:YES]; [renameTableMenuItem setHidden:NO]; - [renameTableMenuItem setTitle:NSLocalizedString(@"Rename procedure", @"rename proc menu title")]; + [renameTableMenuItem setTitle:NSLocalizedString(@"Rename Procedure", @"rename proc menu title")]; [duplicateTableMenuItem setHidden:NO]; - [duplicateTableMenuItem setTitle:NSLocalizedString(@"Duplicate procedure", @"duplicate proc menu title")]; - [removeTableMenuItem setTitle:NSLocalizedString(@"Remove procedure", @"remove proc menu title")]; + [duplicateTableMenuItem setTitle:NSLocalizedString(@"Duplicate Procedure", @"duplicate proc menu title")]; + [truncateTableButton setHidden:YES]; + [removeTableMenuItem setTitle:NSLocalizedString(@"Remove Procedure", @"remove proc menu title")]; } else if([[tableTypes objectAtIndex:[tablesListView selectedRow]] intValue] == SP_TABLETYPE_FUNC) { [[tableSubMenu itemAtIndex:0] setTitle:NSLocalizedString(@"Copy Create Function Syntax", @"copy create func syntax menu item")]; @@ -1228,10 +1275,11 @@ [[tableSubMenu itemAtIndex:9] setHidden:YES]; [renameTableMenuItem setHidden:NO]; - [renameTableMenuItem setTitle:NSLocalizedString(@"Rename function", @"rename func menu title")]; + [renameTableMenuItem setTitle:NSLocalizedString(@"Rename Function", @"rename func menu title")]; [duplicateTableMenuItem setHidden:NO]; - [duplicateTableMenuItem setTitle:NSLocalizedString(@"Duplicate function", @"duplicate func menu title")]; - [removeTableMenuItem setTitle:NSLocalizedString(@"Remove function", @"remove func menu title")]; + [duplicateTableMenuItem setTitle:NSLocalizedString(@"Duplicate Function", @"duplicate func menu title")]; + [truncateTableButton setHidden:YES]; + [removeTableMenuItem setTitle:NSLocalizedString(@"Remove Function", @"remove func menu title")]; } // set window title [tableWindow setTitle:[NSString stringWithFormat:@"(MySQL %@) %@/%@/%@", [tableDocumentInstance mySQLVersion], @@ -1270,21 +1318,22 @@ { switch(lastType) { case SP_TABLETYPE_TABLE: - [removeTableMenuItem setTitle:NSLocalizedString(@"Remove tables", @"remove tables menu title")]; + [removeTableMenuItem setTitle:NSLocalizedString(@"Remove Tables", @"remove tables menu title")]; + [truncateTableButton setTitle:NSLocalizedString(@"Truncate Tables", @"truncate tables menu item")]; break; case SP_TABLETYPE_VIEW: - [removeTableMenuItem setTitle:NSLocalizedString(@"Remove views", @"remove views menu title")]; + [removeTableMenuItem setTitle:NSLocalizedString(@"Remove Views", @"remove views menu title")]; break; case SP_TABLETYPE_PROC: - [removeTableMenuItem setTitle:NSLocalizedString(@"Remove procedures", @"remove procedures menu title")]; + [removeTableMenuItem setTitle:NSLocalizedString(@"Remove Procedures", @"remove procedures menu title")]; break; case SP_TABLETYPE_FUNC: - [removeTableMenuItem setTitle:NSLocalizedString(@"Remove functions", @"remove functions menu title")]; + [removeTableMenuItem setTitle:NSLocalizedString(@"Remove Functions", @"remove functions menu title")]; break; } } else { - [removeTableMenuItem setTitle:NSLocalizedString(@"Remove items", @"remove items menu title")]; + [removeTableMenuItem setTitle:NSLocalizedString(@"Remove Items", @"remove items menu title")]; } } [renameTableMenuItem setHidden:YES]; @@ -1393,15 +1442,15 @@ { // popup button below table list if ([menuItem action] == @selector(copyTable:)) { - return ([tablesListView numberOfSelectedRows] == 1) && [[self tableName] length] && [tablesListView numberOfSelectedRows] > 0; + return (([tablesListView numberOfSelectedRows] == 1) && [[self tableName] length] && [tablesListView numberOfSelectedRows] > 0); } - if ([menuItem action] == @selector(removeTable:) ) { - return [tablesListView numberOfSelectedRows] > 0; + if ([menuItem action] == @selector(removeTable:) || [menuItem action] == @selector(truncateTable:)) { + return ([tablesListView numberOfSelectedRows] > 0); } if ([menuItem action] == @selector(renameTable:)) { - return ([tablesListView numberOfSelectedRows] == 1) && [[self tableName] length]; + return (([tablesListView numberOfSelectedRows] == 1) && [[self tableName] length]); } return [super validateMenuItem:menuItem]; -- cgit v1.2.3