diff options
author | stuconnolly <stuart02@gmail.com> | 2009-05-17 23:35:25 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2009-05-17 23:35:25 +0000 |
commit | e80c61444c0446dcb052bd26d2eef207b97cea4e (patch) | |
tree | e1658f4365ee0d7d9c54d7127192cd456f2929e6 /Source/TablesList.m | |
parent | 769f2adb4e72667e855b2a63d298d711d16e0185 (diff) | |
download | sequelpro-e80c61444c0446dcb052bd26d2eef207b97cea4e.tar.gz sequelpro-e80c61444c0446dcb052bd26d2eef207b97cea4e.tar.bz2 sequelpro-e80c61444c0446dcb052bd26d2eef207b97cea4e.zip |
Add the ability to rename tables. This could potentially be enhanced to allowing renaming views, which is supported as of MySQL version 5.0.14, but requires some version detection to take place.
Diffstat (limited to 'Source/TablesList.m')
-rw-r--r-- | Source/TablesList.m | 78 |
1 files changed, 69 insertions, 9 deletions
diff --git a/Source/TablesList.m b/Source/TablesList.m index ce635c9b..18fd1d9c 100644 --- a/Source/TablesList.m +++ b/Source/TablesList.m @@ -287,9 +287,9 @@ } /** - * Closes the add table sheet and stops the modal session + * Closes the current sheet and stops the modal session */ -- (IBAction)closeTableSheet:(id)sender +- (IBAction)closeSheet:(id)sender { [NSApp stopModalWithCode:[sender tag]]; } @@ -500,6 +500,52 @@ } } +/** + * Renames the currently selected table. + */ +- (IBAction)renameTable:(id)sender +{ + if ((![tableSourceInstance saveRowOnDeselect]) || (![tableContentInstance saveRowOnDeselect]) || (![tableDocumentInstance database])) { + return; + } + + [tableWindow endEditingFor:nil]; + + [tableRenameText setStringValue:[NSString stringWithFormat:@"Rename table %@ to:", [self tableName]]]; + + [NSApp beginSheet:tableRenameSheet + modalForWindow:tableWindow + modalDelegate:self + didEndSelector:nil + contextInfo:nil]; + + NSInteger returnCode = [NSApp runModalForWindow:tableRenameSheet]; + + [NSApp endSheet:tableRenameSheet]; + [tableRenameSheet orderOut:nil]; + + if (!returnCode) { + // Clear table name + [tableRenameField setStringValue:@""]; + + return; + } + + [mySQLConnection queryString:[NSString stringWithFormat:@"RENAME TABLE %@ TO %@", [[self tableName] backtickQuotedString], [[tableRenameField stringValue] backtickQuotedString]]]; + + if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) { + NSBeginAlertSheet(NSLocalizedString(@"Unable to rename table", @"rename table error message"), + NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"The table '%@' was unable to be renamed because an error occurred.\n\nMySQL said: %@", @"rename table error informative message"), [self tableName], [mySQLConnection getLastErrorMessage]]); + } + else { + // If there was no error, rename the table in our list and reload the table view's data + [tables replaceObjectAtIndex:[tablesListView selectedRow] withObject:[tableRenameField stringValue]]; + + [tablesListView reloadData]; + } +} + #pragma mark Alert sheet methods /** @@ -622,9 +668,15 @@ */ - (void)controlTextDidChange:(NSNotification *)notification { - if ([notification object] == tableNameField) { + id object = [notification object]; + + if (object == tableNameField) { [addTableButton setEnabled:([[tableNameField stringValue] length] > 0)]; } + + if (object == tableRenameField) { + [renameTableButton setEnabled:([[tableRenameField stringValue] length] > 0)]; + } } #pragma mark Getter methods @@ -1108,17 +1160,25 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem { // popup button below table list - if ([menuItem action] == @selector(copyTable:)) - { - if( [self tableType] == SP_TABLETYPE_FUNC || [self tableType] == SP_TABLETYPE_PROC ) + if ([menuItem action] == @selector(copyTable:)) { + if ([self tableType] == SP_TABLETYPE_FUNC || [self tableType] == SP_TABLETYPE_PROC) return NO; - 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:)) - { + + if ([menuItem action] == @selector(removeTable:) ) { return [tablesListView numberOfSelectedRows] > 0; } + if ([menuItem action] == @selector(renameTable:)) { + if ([self tableType] == SP_TABLETYPE_VIEW) { + return NO; + } + + return ([tablesListView numberOfSelectedRows] == 1) && [[self tableName] length]; + } + return [super validateMenuItem:menuItem]; } |