From 33db54fee22d960cf51478fcb79649b19d131d01 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Wed, 1 Sep 2010 10:53:11 +0000 Subject: =?UTF-8?q?=E2=80=A2=20Show/Copy=20Create=20Syntax=20now=20come=20?= =?UTF-8?q?up=20with=20all=20create=20syntaxes=20of=20selected=20items=20?= =?UTF-8?q?=E2=80=A2=20if=20Create=20Syntax=20is=20a=20PROC=20wrap=20the?= =?UTF-8?q?=20output=20by=20DELIMITER=20;;=20etc.=20to=20simplify=20a=20co?= =?UTF-8?q?py&paste=20=E2=80=A2=20merged=20code=20for=20copyCreateTableSyn?= =?UTF-8?q?tax=20into=20showCreateTableSyntax=20distinguished=20by=20the?= =?UTF-8?q?=20sender=20(if=20sender=20=3D=3D=20self)=20then=20copy=20other?= =?UTF-8?q?wise=20show?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPDatabaseDocument.m | 209 ++++++++++++++++++++++---------------------- Source/SPTablesList.m | 12 ++- 2 files changed, 111 insertions(+), 110 deletions(-) (limited to 'Source') diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m index 00831c85..7632d50a 100644 --- a/Source/SPDatabaseDocument.m +++ b/Source/SPDatabaseDocument.m @@ -1734,66 +1734,123 @@ #pragma mark Table Methods /** - * Displays the CREATE TABLE syntax of the selected table to the user via a HUD panel. + * Copies if sender == self or displays or the CREATE TABLE syntax of the selected table(s) to the user . */ - (IBAction)showCreateTableSyntax:(id)sender { - //Create the query and get results + NSInteger colOffs = 1; NSString *query = nil; NSString *typeString = @""; + NSString *header = @""; + NSMutableString *createSyntax = [NSMutableString string]; - if( [tablesListInstance tableType] == SPTableTypeTable ) { - query = [NSString stringWithFormat:@"SHOW CREATE TABLE %@", [[self table] backtickQuotedString]]; - typeString = @"table"; - } - else if( [tablesListInstance tableType] == SPTableTypeView ) { - query = [NSString stringWithFormat:@"SHOW CREATE VIEW %@", [[self table] backtickQuotedString]]; - typeString = @"view"; - } - else if( [tablesListInstance tableType] == SPTableTypeProc ) { - query = [NSString stringWithFormat:@"SHOW CREATE PROCEDURE %@", [[self table] backtickQuotedString]]; - typeString = @"procedure"; - colOffs = 2; - } - else if( [tablesListInstance tableType] == SPTableTypeFunc ) { - query = [NSString stringWithFormat:@"SHOW CREATE FUNCTION %@", [[self table] backtickQuotedString]]; - typeString = @"function"; - colOffs = 2; - } + NSIndexSet *indexes = [[tablesListInstance valueForKeyPath:@"tablesListView"] selectedRowIndexes]; - if (query == nil) return; + NSUInteger currentIndex = [indexes firstIndex]; + NSInteger counter = 0; + NSInteger type; - MCPResult *theResult = [mySQLConnection queryString:query]; - [theResult setReturnDataAsStrings:YES]; + NSArray *types = [tablesListInstance selectedTableTypes]; + NSArray *items = [tablesListInstance selectedTableItems]; - // Check for errors, only displaying if the connection hasn't been terminated - if ([mySQLConnection queryErrored]) { - if ([mySQLConnection isConnected]) { - NSRunAlertPanel(@"Error", [NSString stringWithFormat:NSLocalizedString(@"An error occured while creating table syntax.\n\n: %@", @"Error shown when unable to show create table syntax"),[mySQLConnection getLastErrorMessage]], @"OK", nil, nil); + while (currentIndex != NSNotFound) + { + + type = [[types objectAtIndex:counter] intValue]; + query = nil; + + if( type == SPTableTypeTable ) { + query = [NSString stringWithFormat:@"SHOW CREATE TABLE %@", [[items objectAtIndex:counter] backtickQuotedString]]; + typeString = @"TABLE"; + } + else if( type == SPTableTypeView ) { + query = [NSString stringWithFormat:@"SHOW CREATE VIEW %@", [[items objectAtIndex:counter] backtickQuotedString]]; + typeString = @"VIEW"; + } + else if( type == SPTableTypeProc ) { + query = [NSString stringWithFormat:@"SHOW CREATE PROCEDURE %@", [[items objectAtIndex:counter] backtickQuotedString]]; + typeString = @"PROCEDURE"; + colOffs = 2; + } + else if( type == SPTableTypeFunc ) { + query = [NSString stringWithFormat:@"SHOW CREATE FUNCTION %@", [[items objectAtIndex:counter] backtickQuotedString]]; + typeString = @"FUNCTION"; + colOffs = 2; } - return; - } + if (query == nil) { + NSLog(@"Unknown type for selected item while getting the create syntax for '%@'", [items objectAtIndex:counter]); + NSBeep(); + return; + } - NSString *tableSyntax = [[theResult fetchRowAsArray] objectAtIndex:colOffs]; + MCPResult *theResult = [mySQLConnection queryString:query]; + [theResult setReturnDataAsStrings:YES]; + + // Check for errors, only displaying if the connection hasn't been terminated + if ([mySQLConnection queryErrored]) { + if ([mySQLConnection isConnected]) { + NSRunAlertPanel(@"Error", [NSString stringWithFormat:NSLocalizedString(@"An error occured while creating table syntax.\n\n: %@", @"Error shown when unable to show create table syntax"),[mySQLConnection getLastErrorMessage]], @"OK", nil, nil); + } + + return; + } + + NSString *tableSyntax; + if( type == SPTableTypeProc ) + tableSyntax = [NSString stringWithFormat:@"DELIMITER ;;\n%@;;\nDELIMITER ", [[theResult fetchRowAsArray] objectAtIndex:colOffs]]; + else + tableSyntax = [[theResult fetchRowAsArray] objectAtIndex:colOffs]; + + // A NULL value indicates that the user does not have permission to view the syntax + if ([tableSyntax isNSNull]) { + [[NSAlert alertWithMessageText:NSLocalizedString(@"Permission Denied", @"Permission Denied") + defaultButton:NSLocalizedString(@"OK", @"OK button") + alternateButton:nil otherButton:nil + informativeTextWithFormat:NSLocalizedString(@"The creation syntax could not be retrieved due to a permissions error.\n\nPlease check your user permissions with an administrator.", @"Create syntax permission denied detail")] + beginSheetModalForWindow:parentWindow + modalDelegate:self didEndSelector:NULL contextInfo:NULL]; + return; + } + + if([indexes count] > 1) + header = [NSString stringWithFormat:@"# Create syntax for %@ '%@'\n", typeString, [items objectAtIndex:counter]]; + + [createSyntax appendFormat:@"%@%@;%@", header, (type == SPTableTypeView) ? [tableSyntax createViewSyntaxPrettifier] : tableSyntax, (counter < [indexes count]-1) ? @"\n\n" : @""]; + + counter++; + + // Get next index (beginning from the end) + currentIndex = [indexes indexGreaterThanIndex:currentIndex]; - // A NULL value indicates that the user does not have permission to view the syntax - if ([tableSyntax isNSNull]) { - [[NSAlert alertWithMessageText:NSLocalizedString(@"Permission Denied", @"Permission Denied") - defaultButton:NSLocalizedString(@"OK", @"OK button") - alternateButton:nil otherButton:nil - informativeTextWithFormat:NSLocalizedString(@"The creation syntax could not be retrieved due to a permissions error.\n\nPlease check your user permissions with an administrator.", @"Create syntax permission denied detail")] - beginSheetModalForWindow:parentWindow - modalDelegate:self didEndSelector:NULL contextInfo:NULL]; - return; } + + // copy to the clipboard if sender was self, otherwise + // show syntax(es) in sheet + if(sender == self) { + NSPasteboard *pb = [NSPasteboard generalPasteboard]; + [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self]; + [pb setString:createSyntax forType:NSStringPboardType]; - [createTableSyntaxTextField setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Create syntax for %@ '%@'", @"Create syntax label"), typeString, [self table]]]; + // Table syntax copied Growl notification + [[SPGrowlController sharedGrowlController] notifyWithTitle:@"Syntax Copied" + description:[NSString stringWithFormat:NSLocalizedString(@"Syntax for %@ table copied",@"description for table syntax copied growl notification"), [self table]] + document:self + notificationName:@"Syntax Copied"]; + return; + + } + + if([indexes count] == 1) + [createTableSyntaxTextField setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Create syntax for %@ '%@'", @"Create syntax label"), typeString, [self table]]]; + else + [createTableSyntaxTextField setStringValue:NSLocalizedString(@"Create syntaxes for selected items", @"Create syntaxes for selected items label")]; + [createTableSyntaxTextView setEditable:YES]; [createTableSyntaxTextView setString:@""]; - [createTableSyntaxTextView insertText:([tablesListInstance tableType] == SPTableTypeView) ? [[tableSyntax createViewSyntaxPrettifier] stringByAppendingString:@";"] : [tableSyntax stringByAppendingString:@";"]]; + [createTableSyntaxTextView insertText:createSyntax]; [createTableSyntaxTextView setEditable:NO]; [createTableSyntaxWindow makeFirstResponder:createTableSyntaxTextField]; @@ -1812,65 +1869,10 @@ */ - (IBAction)copyCreateTableSyntax:(id)sender { - // Create the query and get results - NSString *query = nil; - NSInteger colOffs = 1; - - if( [tablesListInstance tableType] == SPTableTypeTable ) { - query = [NSString stringWithFormat:@"SHOW CREATE TABLE %@", [[self table] backtickQuotedString]]; - } - else if( [tablesListInstance tableType] == SPTableTypeView ) { - query = [NSString stringWithFormat:@"SHOW CREATE VIEW %@", [[self table] backtickQuotedString]]; - } - else if( [tablesListInstance tableType] == SPTableTypeProc ) { - query = [NSString stringWithFormat:@"SHOW CREATE PROCEDURE %@", [[self table] backtickQuotedString]]; - colOffs = 2; - } - else if( [tablesListInstance tableType] == SPTableTypeFunc ) { - query = [NSString stringWithFormat:@"SHOW CREATE FUNCTION %@", [[self table] backtickQuotedString]]; - colOffs = 2; - } - - if( query == nil ) - return; - - MCPResult *theResult = [mySQLConnection queryString:query]; - [theResult setReturnDataAsStrings:YES]; - - // Check for errors, only displaying if the connection hasn't been terminated - if ([mySQLConnection queryErrored]) { - if ([mySQLConnection isConnected]) { - NSRunAlertPanel(@"Error", [NSString stringWithFormat:NSLocalizedString(@"An error occured while creating table syntax.\n\n: %@", @"Error shown when unable to show create table syntax"),[mySQLConnection getLastErrorMessage]], @"OK", nil, nil); - } - return; - } - NSString *tableSyntax = [[theResult fetchRowAsArray] objectAtIndex:colOffs]; + [self showCreateTableSyntax:self]; + return; - // A NULL value indicates that the user does not have permission to view the syntax - if ([tableSyntax isNSNull]) { - [[NSAlert alertWithMessageText:NSLocalizedString(@"Permission Denied", @"Permission Denied") - defaultButton:NSLocalizedString(@"OK", @"OK button") - alternateButton:nil otherButton:nil - informativeTextWithFormat:NSLocalizedString(@"The creation syntax could not be retrieved due to a permissions error.\n\nPlease check your user permissions with an administrator.", @"Create syntax permission denied detail")] - beginSheetModalForWindow:parentWindow - modalDelegate:self didEndSelector:NULL contextInfo:NULL]; - return; - } - - // copy to the clipboard - NSPasteboard *pb = [NSPasteboard generalPasteboard]; - [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self]; - if([tablesListInstance tableType] == SPTableTypeView) - [pb setString:[tableSyntax createViewSyntaxPrettifier] forType:NSStringPboardType]; - else - [pb setString:tableSyntax forType:NSStringPboardType]; - - // Table syntax copied Growl notification - [[SPGrowlController sharedGrowlController] notifyWithTitle:@"Syntax Copied" - description:[NSString stringWithFormat:NSLocalizedString(@"Syntax for %@ table copied",@"description for table syntax copied growl notification"), [self table]] - document:self - notificationName:@"Syntax Copied"]; } /** @@ -3576,19 +3578,14 @@ return [self supportsEncoding]; } - // table menu items - if ([menuItem action] == @selector(showCreateTableSyntax:) || - [menuItem action] == @selector(copyCreateTableSyntax:)) - { - return ([self table] != nil && [[self table] isNotEqualTo:@""]); - } - if ([menuItem action] == @selector(analyzeTable:) || [menuItem action] == @selector(optimizeTable:) || [menuItem action] == @selector(repairTable:) || [menuItem action] == @selector(flushTable:) || [menuItem action] == @selector(checkTable:) || - [menuItem action] == @selector(checksumTable:)) + [menuItem action] == @selector(checksumTable:) || + [menuItem action] == @selector(showCreateTableSyntax:) || + [menuItem action] == @selector(copyCreateTableSyntax:)) { return ([[[tablesListInstance valueForKeyPath:@"tablesListView"] selectedRowIndexes] count]) ? YES:NO; } diff --git a/Source/SPTablesList.m b/Source/SPTablesList.m index b0e4594e..e3a5cdd3 100644 --- a/Source/SPTablesList.m +++ b/Source/SPTablesList.m @@ -886,18 +886,22 @@ [renameTableContextMenuItem setHidden:YES]; [duplicateTableContextMenuItem setHidden:YES]; [separatorTableContextMenuItem setHidden:YES]; - [separatorTableContextMenuItem2 setHidden:YES]; - [showCreateSyntaxContextMenuItem setHidden:YES]; + [separatorTableContextMenuItem2 setHidden:NO]; + [showCreateSyntaxContextMenuItem setTitle:NSLocalizedString(@"Show Create Syntaxes...", @"show create syntaxes menu item")]; + [showCreateSyntaxContextMenuItem setHidden:NO]; // 'Gear' menu [renameTableMenuItem setHidden:YES]; [duplicateTableMenuItem setHidden:YES]; [separatorTableMenuItem setHidden:YES]; - [separatorTableMenuItem2 setHidden:YES]; - [showCreateSyntaxMenuItem setHidden:YES]; + [separatorTableMenuItem2 setHidden:NO]; + [showCreateSyntaxMenuItem setTitle:NSLocalizedString(@"Show Create Syntaxes...", @"show create syntaxes menu item")]; + [showCreateSyntaxMenuItem setHidden:NO]; // Get main menu "Table"'s submenu NSMenu *tableSubMenu = [[[NSApp mainMenu] itemWithTag:SPMainMenuTable] submenu]; + [[tableSubMenu itemAtIndex:3] setTitle:NSLocalizedString(@"Copy Create Syntaxes", @"copy create syntaxes menu item")]; + [[tableSubMenu itemAtIndex:4] setTitle:NSLocalizedString(@"Show Create Syntaxes...", @"show create syntaxes menu item")]; [[tableSubMenu itemAtIndex:6] setTitle:NSLocalizedString(@"Check Selected Items", @"check selected items menu item")]; [[tableSubMenu itemAtIndex:7] setTitle:NSLocalizedString(@"Repair Selected Items", @"repair selected items menu item")]; -- cgit v1.2.3