diff options
-rw-r--r-- | Source/SPTableTriggers.m | 215 |
1 files changed, 110 insertions, 105 deletions
diff --git a/Source/SPTableTriggers.m b/Source/SPTableTriggers.m index 8ae46400..dfded203 100644 --- a/Source/SPTableTriggers.m +++ b/Source/SPTableTriggers.m @@ -51,7 +51,7 @@ triggerData = [[NSMutableArray alloc] init]; isEdit = NO; } - + return self; } @@ -62,21 +62,21 @@ { // Set the table triggers view's vertical gridlines if required [triggersTableView setGridStyleMask:([[NSUserDefaults standardUserDefaults] boolForKey:SPDisplayTableViewVerticalGridlines]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone]; - + // Set the strutcture and index view's font BOOL useMonospacedFont = [[NSUserDefaults standardUserDefaults] boolForKey:SPUseMonospacedFonts]; - + for (NSTableColumn *column in [triggersTableView tableColumns]) { [[column dataCell] setFont:(useMonospacedFont) ? [NSFont fontWithName:SPDefaultMonospacedFontName size:[NSFont smallSystemFontSize]] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; } - + // Register as an observer for the when the UseMonospacedFonts preference changes [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:SPUseMonospacedFonts options:NSKeyValueObservingOptionNew context:NULL]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(triggerStatementTextDidChange:) - name:NSTextStorageDidProcessEditingNotification + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(triggerStatementTextDidChange:) + name:NSTextStorageDidProcessEditingNotification object:[triggerStatementTextView textStorage]]; // Add observers for document task activity @@ -146,82 +146,87 @@ * Add a new trigger using the selected values. */ - (IBAction)confirmAddTrigger:(id)sender -{ +{ [self closeTriggerSheet:self]; - + // MySQL doesn't have ALTER TRIGGER, so we delete the old one and add a new one. // In case of error, all the old trigger info is kept in buffer if(isEdit && [editTriggerName length]>0) { - NSString *queryDelete = [NSString stringWithFormat:@"DROP TRIGGER %@.%@", - [[tableDocumentInstance database] backtickQuotedString], + NSString *queryDelete = [NSString stringWithFormat:@"DROP TRIGGER %@.%@", + [[tableDocumentInstance database] backtickQuotedString], [editTriggerName backtickQuotedString]]; [connection queryString:queryDelete]; if([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Unable to delete trigger", - @"error deleting trigger message"), + SPBeginAlertSheet(NSLocalizedString(@"Unable to delete trigger", + @"error deleting trigger message"), NSLocalizedString(@"OK", @"OK button"), - nil, nil, [NSApp mainWindow], nil, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"The selected trigger couldn't be deleted.\n\nMySQL said: %@", - @"error deleting trigger informative message"), + nil, nil, [NSApp mainWindow], nil, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"The selected trigger couldn't be deleted.\n\nMySQL said: %@", + @"error deleting trigger informative message"), [connection getLastErrorMessage]]); } } - + NSString *triggerName = [triggerNameTextField stringValue]; - NSString *triggerActionTime = [[triggerActionTimePopUpButton titleOfSelectedItem] uppercaseString]; - NSString *triggerEvent = [[triggerEventPopUpButton titleOfSelectedItem] uppercaseString]; + NSString *triggerActionTime = ([triggerActionTimePopUpButton indexOfSelectedItem]) ? @"AFTER" : @"BEFORE"; + NSString *triggerEvent = @""; + switch([triggerEventPopUpButton indexOfSelectedItem]) { + case 0: triggerEvent = @"INSERT"; + case 1: triggerEvent = @"UPDATE"; + case 2: triggerEvent = @"DELETE"; + } NSString *triggerStatement = [triggerStatementTextView string]; - - NSString *query = [NSString stringWithFormat:@"CREATE TRIGGER %@ %@ %@ ON %@ FOR EACH ROW %@", + + NSString *query = [NSString stringWithFormat:@"CREATE TRIGGER %@ %@ %@ ON %@ FOR EACH ROW %@", [triggerName backtickQuotedString], triggerActionTime, triggerEvent, [[tablesListInstance tableName] backtickQuotedString], triggerStatement]; - + // Execute query [connection queryString:query]; - + if (([connection queryErrored])) { - SPBeginAlertSheet(NSLocalizedString(@"Error creating trigger", - @"error creating trigger message"), + SPBeginAlertSheet(NSLocalizedString(@"Error creating trigger", + @"error creating trigger message"), NSLocalizedString(@"OK", @"OK button"), - nil, nil, [NSApp mainWindow], nil, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"The specified trigger was unable to be created.\n\nMySQL said: %@", - @"error creating trigger informative message"), + nil, nil, [NSApp mainWindow], nil, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"The specified trigger was unable to be created.\n\nMySQL said: %@", + @"error creating trigger informative message"), [connection getLastErrorMessage]]); // In case of error, restore the original trigger statement if(isEdit) { [triggerStatementTextView setString:editTriggerStatement]; } - } + } else { [triggerNameTextField setStringValue:@""]; [triggerStatementTextView setString:@""]; } - + // After Edit, rename button to Add - if(isEdit) + if(isEdit) { isEdit = NO; [confirmAddTriggerButton setTitle: NSLocalizedString(@"Add", @"Add trigger button")]; } - - [self _refreshTriggerDataForcingCacheRefresh:YES]; + + [self _refreshTriggerDataForcingCacheRefresh:YES]; } /** - * Displays the add new trigger sheet. + * Displays the add new trigger sheet. */ - (IBAction)addTrigger:(id)sender { - + [NSApp beginSheet:addTriggerPanel modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self - didEndSelector:nil + didEndSelector:nil contextInfo:nil]; } @@ -231,22 +236,22 @@ - (IBAction)removeTrigger:(id)sender { if ([triggersTableView numberOfSelectedRows] > 0) { - - NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Delete trigger", @"delete trigger message") - defaultButton:NSLocalizedString(@"Delete", @"delete button") + + NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Delete trigger", @"delete trigger message") + defaultButton:NSLocalizedString(@"Delete", @"delete button") alternateButton:NSLocalizedString(@"Cancel", @"cancel button") - otherButton:nil + otherButton:nil informativeTextWithFormat:NSLocalizedString(@"Are you sure you want to delete the selected triggers? This action cannot be undone.", @"delete selected trigger informative message")]; - + [alert setAlertStyle:NSCriticalAlertStyle]; - + NSArray *buttons = [alert buttons]; - + // Change the alert's cancel button to have the key equivalent of return [[buttons objectAtIndex:0] setKeyEquivalent:@"d"]; [[buttons objectAtIndex:0] setKeyEquivalentModifierMask:NSCommandKeyMask]; [[buttons objectAtIndex:1] setKeyEquivalent:@"\r"]; - + [alert beginSheetModalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:@"removeTrigger"]; } } @@ -283,58 +288,58 @@ [removeTriggerButton setEnabled:([triggersTableView numberOfSelectedRows] > 0)]; } -/* +/** * Double-click action on table cells - for the time being, return NO to disable editing. */ - (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { if ([tableDocumentInstance isWorking]) return NO; - + // Start Edit panel if([triggerData count] > rowIndex && [triggerData objectAtIndex:rowIndex] != NSNotFound) { NSDictionary *trigger = [triggerData objectAtIndex:rowIndex]; - + // Temporary save original name and statement (we need them later) editTriggerName = [trigger objectForKey:@"trigger"]; editTriggerStatement = [trigger objectForKey:@"statement"]; [triggerNameTextField setStringValue:editTriggerName]; [triggerStatementTextView setString:editTriggerStatement]; - + // Timin title is different then what we have saved in the database (case difference) for(int i=0;i<[[triggerActionTimePopUpButton itemArray] count]; i++) { - if([[[triggerActionTimePopUpButton itemTitleAtIndex:i] uppercaseString] + if([[[triggerActionTimePopUpButton itemTitleAtIndex:i] uppercaseString] isEqualToString:[[trigger objectForKey:@"timing"] uppercaseString]]) { [triggerActionTimePopUpButton selectItemAtIndex:i]; break; } } - + // Event title is different then what we have saved in the database (case difference) for(int i=0;i<[[triggerEventPopUpButton itemArray] count]; i++) { - if([[[triggerEventPopUpButton itemTitleAtIndex:i] uppercaseString] + if([[[triggerEventPopUpButton itemTitleAtIndex:i] uppercaseString] isEqualToString:[[trigger objectForKey:@"event"] uppercaseString]]) { [triggerEventPopUpButton selectItemAtIndex:i]; break; } } - + // Change button label from Add to Edit [confirmAddTriggerButton setTitle:NSLocalizedString(@"Edit", @"Edit trigger button")]; isEdit = YES; - + [NSApp beginSheet:addTriggerPanel modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self - didEndSelector:nil + didEndSelector:nil contextInfo:nil]; } - + return NO; } @@ -353,10 +358,10 @@ * Disable all content interactive elements during an ongoing task. */ - (void)startDocumentTaskForTab:(NSNotification *)notification -{ +{ // Only proceed if this view is selected. if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableTriggers]) return; - + [addTriggerButton setEnabled:NO]; [refreshTriggersButton setEnabled:NO]; [removeTriggerButton setEnabled:NO]; @@ -366,15 +371,15 @@ * Enable all content interactive elements after an ongoing task. */ - (void)endDocumentTaskForTab:(NSNotification *)notification -{ +{ // Only proceed if this view is selected. if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableTriggers]) return; - + if ([triggersTableView isEnabled]) { [addTriggerButton setEnabled:YES]; [refreshTriggersButton setEnabled:YES]; } - + [removeTriggerButton setEnabled:([triggersTableView numberOfSelectedRows] > 0)]; } @@ -387,58 +392,58 @@ - (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo { if ([contextInfo isEqualToString:@"removeTrigger"]) { - + if (returnCode == NSAlertDefaultReturn) { - + NSString *database = [tableDocumentInstance database]; NSIndexSet *selectedSet = [triggersTableView selectedRowIndexes]; - + NSUInteger row = [selectedSet lastIndex]; - - while (row != NSNotFound) + + while (row != NSNotFound) { NSString *triggerName = [[triggerData objectAtIndex:row] objectForKey:@"trigger"]; NSString *query = [NSString stringWithFormat:@"DROP TRIGGER %@.%@", [database backtickQuotedString], [triggerName backtickQuotedString]]; - + [connection queryString:query]; - + if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Unable to delete trigger", @"error deleting trigger message"), + SPBeginAlertSheet(NSLocalizedString(@"Unable to delete trigger", @"error deleting trigger message"), NSLocalizedString(@"OK", @"OK button"), - nil, nil, [NSApp mainWindow], nil, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"The selected trigger couldn't be deleted.\n\nMySQL said: %@", @"error deleting trigger informative message"), [connection getLastErrorMessage]]); - + nil, nil, [NSApp mainWindow], nil, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"The selected trigger couldn't be deleted.\n\nMySQL said: %@", @"error deleting trigger informative message"), [connection getLastErrorMessage]]); + // Abort loop break; - } - + } + row = [selectedSet indexLessThanIndex:row]; } - + [self _refreshTriggerDataForcingCacheRefresh:YES]; } - } + } } /** * This method is called as part of Key Value Observing which is used to watch for prefernce changes which effect the interface. */ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context -{ +{ // Display table veiew vertical gridlines preference changed if ([keyPath isEqualToString:SPDisplayTableViewVerticalGridlines]) { [triggersTableView setGridStyleMask:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone]; } // Use monospaced fonts preference changed else if ([keyPath isEqualToString:SPUseMonospacedFonts]) { - + BOOL useMonospacedFont = [[change objectForKey:NSKeyValueChangeNewKey] boolValue]; - + for (NSTableColumn *column in [triggersTableView tableColumns]) { [[column dataCell] setFont:(useMonospacedFont) ? [NSFont fontWithName:SPDefaultMonospacedFontName size:[NSFont smallSystemFontSize]] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; } - + [triggersTableView reloadData]; } } @@ -451,10 +456,10 @@ // Remove row if ([menuItem action] == @selector(removeTrigger:)) { [menuItem setTitle:([triggersTableView numberOfSelectedRows] > 1) ? NSLocalizedString(@"Delete Triggers", @"delete triggers menu item") : NSLocalizedString(@"Delete Trigger", @"delete trigger menu item")]; - + return ([triggersTableView numberOfSelectedRows] > 0); } - + return YES; } @@ -462,7 +467,7 @@ * Toggles the enabled state of confirm add trigger button based on the editing of the trigger's name. */ - (void)controlTextDidChange:(NSNotification *)notification -{ +{ [self _toggleConfirmAddTriggerButtonEnabled]; } @@ -482,25 +487,25 @@ { NSMutableArray *headings = [[NSMutableArray alloc] init]; NSMutableArray *data = [NSMutableArray array]; - + // Get the relations table view's columns for (NSTableColumn *column in [triggersTableView tableColumns]) { [headings addObject:[[column headerCell] stringValue]]; } - + // Get rid of the 'Table' column [headings removeObjectAtIndex:0]; - + [data addObject:headings]; - + [headings release]; - + // Get the relation data for (NSDictionary *trigger in triggerData) { NSMutableArray *temp = [[NSMutableArray alloc] init]; - + [temp addObject:[trigger objectForKey:@"trigger"]]; [temp addObject:[trigger objectForKey:@"event"]]; [temp addObject:[trigger objectForKey:@"timing"]]; @@ -508,27 +513,27 @@ [temp addObject:[trigger objectForKey:@"definer"]]; [temp addObject:([trigger objectForKey:@"created"]) ? [trigger objectForKey:@"created"] : @""]; [temp addObject:[trigger objectForKey:@"sql_mode"]]; - + [data addObject:temp]; - + [temp release]; } - - return data; + + return data; } #pragma mark - -/* +/** * Dealloc. */ - (void)dealloc -{ +{ [triggerData release], triggerData = nil; - + [[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:SPUseMonospacedFonts]; - + [super dealloc]; } @@ -551,19 +556,19 @@ - (void)_refreshTriggerDataForcingCacheRefresh:(BOOL)clearAllCaches { [triggerData removeAllObjects]; - + if ([tablesListInstance tableType] == SPTableTypeTable) { - + if (clearAllCaches) { [tableDataInstance resetAllData]; [tableDataInstance updateTriggersForCurrentTable]; } - + NSArray *triggers = nil; if ([connection serverMajorVersion] >= 5 && [connection serverMinorVersion] >= 0) triggers = [tableDataInstance triggers]; - - for (NSDictionary *trigger in triggers) + + for (NSDictionary *trigger in triggers) { [triggerData addObject:[NSDictionary dictionaryWithObjectsAndKeys: [trigger objectForKey:@"Table"], @"table", @@ -575,10 +580,10 @@ [trigger objectForKey:@"Created"], @"created", [trigger objectForKey:@"sql_mode"], @"sql_mode", nil]]; - - } - } - + + } + } + [triggersTableView reloadData]; } |