From 473d19dfad6e8092266cfb082261cdafaf1062aa Mon Sep 17 00:00:00 2001 From: bamse16 Date: Sun, 11 Jul 2010 15:01:13 +0000 Subject: Added support for edit triggers. Doubleclick on a row and the Add trigger pops up with all the fields prepopulated. Edit works as 'DROP' old trigger and then 'CREATE' new trigger (since MySQL doesn't have support for 'ALTER' trigger). In case of error while adding the new trigger, when you add a new trigger, all the info from the original trigger is preserved. (Don't know if this is the best solution). --- Source/SPTableTriggers.h | 4 +++ Source/SPTableTriggers.m | 93 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 5 deletions(-) (limited to 'Source') diff --git a/Source/SPTableTriggers.h b/Source/SPTableTriggers.h index c03b5f2c..b1923830 100644 --- a/Source/SPTableTriggers.h +++ b/Source/SPTableTriggers.h @@ -52,6 +52,10 @@ MCPConnection *connection; NSMutableArray *triggerData; + + BOOL isEdit; + NSString *editTriggerName; + NSString *editTriggerStatement; } @property (readwrite, assign) MCPConnection *connection; diff --git a/Source/SPTableTriggers.m b/Source/SPTableTriggers.m index 29667eaa..ed056347 100644 --- a/Source/SPTableTriggers.m +++ b/Source/SPTableTriggers.m @@ -49,6 +49,7 @@ { if ((self = [super init])) { triggerData = [[NSMutableArray alloc] init]; + isEdit = NO; } return self; @@ -148,6 +149,26 @@ { [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], + [editTriggerName backtickQuotedString]]; + [connection queryString:queryDelete]; + if([connection queryErrored]) + { + 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]]); + } + } + NSString *triggerName = [triggerNameTextField stringValue]; NSString *triggerActionTime = [[triggerActionTimePopUpButton titleOfSelectedItem] uppercaseString]; NSString *triggerEvent = [[triggerEventPopUpButton titleOfSelectedItem] uppercaseString]; @@ -164,21 +185,39 @@ [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"), [connection getLastErrorMessage]]); + [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 { - [self _refreshTriggerDataForcingCacheRefresh:YES]; + [triggerNameTextField setStringValue:@""]; + [triggerStatementTextView setString:@""]; } + + // After Edit, rename button to Add + if(isEdit) + { + isEdit = NO; + [confirmAddTriggerButton setTitle: NSLocalizedString(@"Add", @"Add trigger button")]; + } + + [self _refreshTriggerDataForcingCacheRefresh:YES]; } /** * Displays the add new trigger sheet. */ - (IBAction)addTrigger:(id)sender -{ +{ + [NSApp beginSheet:addTriggerPanel modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self @@ -251,6 +290,51 @@ { 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] + 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] + 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 + contextInfo:nil]; + } + return NO; } @@ -319,7 +403,6 @@ [connection queryString:query]; if ([connection queryErrored]) { - SPBeginAlertSheet(NSLocalizedString(@"Unable to delete trigger", @"error deleting trigger message"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [NSApp mainWindow], nil, nil, nil, -- cgit v1.2.3