From 88f89987a4329b394ee30a2a77705d3afbb35195 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Fri, 12 Mar 2010 01:02:52 +0000 Subject: Various printing support enhancements, including: - Splitting out all printing methods to SPPrintController which is category of TableDocument. - The ability to print table relations. - If present the inclusion of table indexes when printing a table's source. - If the user has use monospaced fonts enables, then the print out's tabular data will be in a monospaced font. - Lots of other style enhancements, including page headings and sections headings. --- Source/CustomQuery.m | 4 +- Source/SPConstants.h | 4 + Source/SPConstants.m | 4 + Source/SPPrintController.h | 33 ++++++ Source/SPPrintController.m | 256 +++++++++++++++++++++++++++++++++++++++++++++ Source/SPTableRelations.h | 4 + Source/SPTableRelations.m | 44 +++++++- Source/TableDocument.h | 27 +++-- Source/TableDocument.m | 168 +---------------------------- Source/TableSource.h | 2 +- Source/TableSource.m | 56 +++++++--- 11 files changed, 401 insertions(+), 201 deletions(-) create mode 100644 Source/SPPrintController.h create mode 100644 Source/SPPrintController.m (limited to 'Source') diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 33285910..370db2bc 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -3077,13 +3077,13 @@ NSError *error; helpHTMLTemplate = [[NSString alloc] - initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sequel-pro-mysql-help-template" ofType:@"html"] + initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:SPHTMLHelpTemplate ofType:@"html"] encoding:NSUTF8StringEncoding error:&error]; // an error occurred while reading if (helpHTMLTemplate == nil) { - NSLog(@"%@", [NSString stringWithFormat:@"Error reading “sequel-pro-mysql-help-template.html”!
%@", [error localizedFailureReason]]); + NSLog(@"%@", [NSString stringWithFormat:@"Error reading “%@.html”!
%@", SPHTMLHelpTemplate, [error localizedFailureReason]]); NSBeep(); } diff --git a/Source/SPConstants.h b/Source/SPConstants.h index fd20509b..c8342a5f 100644 --- a/Source/SPConstants.h +++ b/Source/SPConstants.h @@ -71,6 +71,10 @@ extern NSString *SPQueryFavortiesPasteboardDragType; extern NSString *SPFileExtensionDefault; extern NSString *SPFileExtensionSQL; +// Filenames +extern NSString *SPHTMLPrintTemplate; +extern NSString *SPHTMLHelpTemplate; + // Preference key constants // General Prefpane diff --git a/Source/SPConstants.m b/Source/SPConstants.m index 23f1278f..a395e11a 100644 --- a/Source/SPConstants.m +++ b/Source/SPConstants.m @@ -40,6 +40,10 @@ NSString *SPContentFilterPasteboardDragType = @"SPContentFilterPasteboard"; NSString *SPFileExtensionDefault = @"spf"; NSString *SPFileExtensionSQL = @"sql"; +// Filenames +NSString *SPHTMLPrintTemplate = @"sequel-pro-print-template"; +NSString *SPHTMLHelpTemplate = @"sequel-pro-mysql-help-template"; + // Preference key constants // General Prefpane NSString *SPDefaultFavorite = @"DefaultFavorite"; diff --git a/Source/SPPrintController.h b/Source/SPPrintController.h new file mode 100644 index 00000000..8fd1acf8 --- /dev/null +++ b/Source/SPPrintController.h @@ -0,0 +1,33 @@ +// +// $Id$ +// +// SPPrintController.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on March 11, 2010 +// Copyright (c) 2010 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +#import "TableDocument.h" + +@interface TableDocument (SPPrintController) + +- (NSString *)generateHTMLforPrinting; +- (NSArray *)columnNames; + +@end diff --git a/Source/SPPrintController.m b/Source/SPPrintController.m new file mode 100644 index 00000000..718a203b --- /dev/null +++ b/Source/SPPrintController.m @@ -0,0 +1,256 @@ +// +// $Id$ +// +// SPPrintController.m +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on March 11, 2010 +// Copyright (c) 2010 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +#import "SPPrintController.h" +#import "TableContent.h" +#import "TableSource.h" +#import "CustomQuery.h" +#import "SPConstants.h" +#import "SPTableRelations.h" +#import "SPPrintAccessory.h" +#import "MGTemplateEngine.h" +#import "ICUTemplateMatcher.h" +#import "SPConnectionController.h" + +@implementation TableDocument (SPPrintController) + +/** + * WebView delegate method. + */ +- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame +{ + // Because we need the webFrame loaded (for preview), we've moved the actual printing here + NSPrintInfo *printInfo = [self printInfo]; + + [printInfo setHorizontalPagination:NSFitPagination]; + [printInfo setVerticalPagination:NSAutoPagination]; + [printInfo setVerticallyCentered:NO]; + [printInfo setTopMargin:30]; + [printInfo setBottomMargin:30]; + [printInfo setLeftMargin:10]; + [printInfo setRightMargin:10]; + + NSPrintOperation *op = [NSPrintOperation printOperationWithView:[[[printWebView mainFrame] frameView] documentView] printInfo:printInfo]; + + // Add the ability to select the orientation to print panel + NSPrintPanel *printPanel = [op printPanel]; + + [printPanel setOptions:[printPanel options] + NSPrintPanelShowsOrientation + NSPrintPanelShowsScaling + NSPrintPanelShowsPaperSize]; + + SPPrintAccessory *printAccessory = [[SPPrintAccessory alloc] initWithNibName:@"PrintAccessory" bundle:nil]; + + [printAccessory setPrintView:printWebView]; + [printPanel addAccessoryController:printAccessory]; + + [[NSPageLayout pageLayout] addAccessoryController:printAccessory]; + [printAccessory release]; + + [op setPrintPanel:printPanel]; + + [op runOperationModalForWindow:tableWindow + delegate:self + didRunSelector:nil + contextInfo:nil]; + +} + +/** + * Loads the print document interface. The actual printing is done in the doneLoading delegate. + */ +- (IBAction)printDocument:(id)sender +{ + [[printWebView mainFrame] loadHTMLString:[self generateHTMLforPrinting] baseURL:nil]; +} + + +/** + * Generates the HTML for the current view that is being printed. + */ +- (NSString *)generateHTMLforPrinting +{ + // Set up template engine with your chosen matcher + MGTemplateEngine *engine = [MGTemplateEngine templateEngine]; + + [engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]]; + + NSString *versionForPrint = [NSString stringWithFormat:@"%@ %@ (build %@)", + [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"], + [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"], + [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] + ]; + + NSMutableDictionary *connection = [[NSMutableDictionary alloc] init]; + + if ([[self user] length]) { + [connection setValue:[self user] forKey:@"username"]; + } + + if ([[self table] length]) { + [connection setValue:[self table] forKey:@"table"]; + } + + + if ([connectionController port] && [[connectionController port] length]) { + [connection setValue:[connectionController port] forKey:@"port"]; + } + + [connection setValue:[self host] forKey:@"hostname"]; + [connection setValue:selectedDatabase forKey:@"database"]; + [connection setValue:versionForPrint forKey:@"version"]; + + NSString *title = @""; + NSArray *rows, *indexes, *indexColumns = nil; + NSArray *columns = [self columnNames]; + + NSMutableDictionary *printData = [NSMutableDictionary dictionary]; + + // Table source view + if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 0) { + + NSDictionary *tableSource = [tableSourceInstance tableSourceForPrinting]; + + if ([[tableSource objectForKey:@"structure"] count] > 1) { + + title = @"Table Structure"; + + rows = [[NSArray alloc] initWithArray: + [[tableSource objectForKey:@"structure"] objectsAtIndexes: + [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[tableSource objectForKey:@"structure"] count] - 1)] + ] + ]; + + indexes = [[NSArray alloc] initWithArray: + [[tableSource objectForKey:@"indexes"] objectsAtIndexes: + [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[tableSource objectForKey:@"indexes"] count] - 1)] + ] + ]; + + indexColumns = [[tableSource objectForKey:@"indexes"] objectAtIndex:0]; + + [printData setObject:indexes forKey:@"indexes"]; + [printData setObject:indexColumns forKey:@"indexColumns"]; + } + } + // Table content view + else if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 1) { + if ([[tableContentInstance currentResult] count] > 1) { + + title = @"Table Content"; + + rows = [[NSArray alloc] initWithArray: + [[tableContentInstance currentDataResult] objectsAtIndexes: + [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[tableContentInstance currentResult] count] - 1)] + ] + ]; + + [connection setValue:[tableContentInstance usedQuery] forKey:@"query"]; + } + } + // Custom query view + else if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 2) { + if ([[customQueryInstance currentResult] count] > 1) { + + title = @"Query Result"; + + rows = [[NSArray alloc] initWithArray: + [[customQueryInstance currentResult] objectsAtIndexes: + [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[customQueryInstance currentResult] count] - 1)] + ] + ]; + + [connection setValue:[customQueryInstance usedQuery] forKey:@"query"]; + } + } + // Table relations view + else if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 4) { + if ([[tableRelationsInstance relationDataForPrinting] count] > 1) { + + title = @"Table Relations"; + + NSArray *data = [tableRelationsInstance relationDataForPrinting]; + + rows = [[NSArray alloc] initWithArray: + [data objectsAtIndexes: + [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, ([data count] - 1))] + ] + ]; + } + } + + [engine setObject:connection forKey:@"c"]; + + [printData setObject:title forKey:@"title"]; + [printData setObject:columns forKey:@"columns"]; + [printData setObject:rows forKey:@"rows"]; + [printData setObject:([prefs boolForKey:SPUseMonospacedFonts]) ? SPDefaultMonospacedFontName : @"Lucida Grande" forKey:@"font"]; + + [connection release]; + + if (rows) [rows release]; + + // Process the template and display the results. + NSString *result = [engine processTemplateInFileAtPath:[[NSBundle mainBundle] pathForResource:SPHTMLPrintTemplate ofType:@"html"] withVariables:printData]; + + return result; +} + +/** + * Returns an array of columns for whichever view is being printed. + */ +- (NSArray *)columnNames +{ + NSArray *columns = nil; + + // Table source view + if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 0 + && [[tableSourceInstance tableSourceForPrinting] count] > 0 ) { + + columns = [[NSArray alloc] initWithArray:[[[tableSourceInstance tableSourceForPrinting] objectForKey:@"structure"] objectAtIndex:0] copyItems:YES]; + } + // Table content view + else if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 1 + && [[tableContentInstance currentResult] count] > 0 ) { + + columns = [[NSArray alloc] initWithArray:[[tableContentInstance currentResult] objectAtIndex:0] copyItems:YES]; + } + // Custom query view + else if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 2 + && [[customQueryInstance currentResult] count] > 0 ) { + + columns = [[NSArray alloc] initWithArray:[[customQueryInstance currentResult] objectAtIndex:0] copyItems:YES]; + } + // Table relations view + else if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 4 + && [[tableRelationsInstance relationDataForPrinting] count] > 0 ) { + + columns = [[NSArray alloc] initWithArray:[[tableRelationsInstance relationDataForPrinting] objectAtIndex:0] copyItems:YES]; + } + + if (columns) [columns autorelease]; + + return columns; +} + +@end diff --git a/Source/SPTableRelations.h b/Source/SPTableRelations.h index fb32fd12..5b1db7dd 100644 --- a/Source/SPTableRelations.h +++ b/Source/SPTableRelations.h @@ -55,6 +55,7 @@ NSMutableArray *relationData; } +@property (readonly) NSMutableArray *relationData; @property (readwrite, assign) MCPConnection *connection; // IB action methods @@ -70,4 +71,7 @@ - (void)startDocumentTaskForTab:(NSNotification *)aNotification; - (void)endDocumentTaskForTab:(NSNotification *)aNotification; +// Other +- (NSArray *)relationDataForPrinting; + @end diff --git a/Source/SPTableRelations.m b/Source/SPTableRelations.m index 10780fd1..a88da5e7 100644 --- a/Source/SPTableRelations.m +++ b/Source/SPTableRelations.m @@ -41,6 +41,7 @@ @implementation SPTableRelations @synthesize connection; +@synthesize relationData; /** * init @@ -316,8 +317,7 @@ { // Only proceed if this view is selected. - if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableRelations]) - return; + if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableRelations]) return; [addRelationButton setEnabled:NO]; [refreshRelationsButton setEnabled:NO]; @@ -331,19 +331,55 @@ { // Only proceed if this view is selected. - if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableRelations]) - return; + if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableRelations]) return; if ([relationsTableView isEnabled]) { [addRelationButton setEnabled:YES]; [refreshRelationsButton setEnabled:YES]; } + [removeRelationButton setEnabled:([relationsTableView numberOfSelectedRows] > 0)]; } #pragma mark - #pragma mark Other +/** + * Returns an array of relation data to be used for printing purposes. The first element in the array is always + * an array of the columns and each subsequent element is an array of relation data. + */ +- (NSArray *)relationDataForPrinting +{ + NSMutableArray *headings = [NSMutableArray array]; + NSMutableArray *tempData = [NSMutableArray array]; + NSMutableArray *data = [NSMutableArray array]; + + // Get the relations table view's columns + for (NSTableColumn *column in [relationsTableView tableColumns]) + { + [headings addObject:[[column headerCell] stringValue]]; + } + + [data addObject:headings]; + + // Get the relation data + for (NSDictionary *relation in relationData) + { + NSMutableArray *temp = [NSMutableArray array]; + + [temp addObject:[relation objectForKey:@"name"]]; + [temp addObject:[relation objectForKey:@"columns"]]; + [temp addObject:[relation objectForKey:@"fk_table"]]; + [temp addObject:[relation objectForKey:@"fk_columns"]]; + [temp addObject:([relation objectForKey:@"on_update"]) ? [relation objectForKey:@"on_update"] : @""]; + [temp addObject:([relation objectForKey:@"on_delete"]) ? [relation objectForKey:@"on_delete"] : @""]; + + [data addObject:temp]; + } + + return data; +} + /** * NSAlert didEnd method. */ diff --git a/Source/TableDocument.h b/Source/TableDocument.h index 4be47ae1..41bd1186 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -165,8 +165,6 @@ id statusValues; } -- (NSString *)getHTMLforPrint; - - (BOOL)isUntitled; - (void)initQueryEditorWithString:(NSString *)query; @@ -191,18 +189,18 @@ - (NSArray *)allSystemDatabaseNames; // Task progress and notification methods -- (void) startTaskWithDescription:(NSString *)description; -- (void) showTaskProgressWindow:(NSTimer *)theTimer; -- (void) setTaskDescription:(NSString *)description; -- (void) setTaskPercentage:(CGFloat)taskPercentage; -- (void) setTaskProgressToIndeterminateAfterDelay:(BOOL)afterDelay; -- (void) endTask; -- (void) enableTaskCancellationWithTitle:(NSString *)buttonTitle callbackObject:(id)callbackObject callbackFunction:(SEL)callbackFunction; -- (void) disableTaskCancellation; -- (IBAction) cancelTask:(id)sender; -- (BOOL) isWorking; -- (void) setDatabaseListIsSelectable:(BOOL)isSelectable; -- (void) centerTaskWindow; +- (void)startTaskWithDescription:(NSString *)description; +- (void)showTaskProgressWindow:(NSTimer *)theTimer; +- (void)setTaskDescription:(NSString *)description; +- (void)setTaskPercentage:(CGFloat)taskPercentage; +- (void)setTaskProgressToIndeterminateAfterDelay:(BOOL)afterDelay; +- (void)endTask; +- (void)enableTaskCancellationWithTitle:(NSString *)buttonTitle callbackObject:(id)callbackObject callbackFunction:(SEL)callbackFunction; +- (void)disableTaskCancellation; +- (IBAction)cancelTask:(id)sender; +- (BOOL)isWorking; +- (void)setDatabaseListIsSelectable:(BOOL)isSelectable; +- (void)centerTaskWindow; // Encoding methods - (void)setConnectionEncoding:(NSString *)mysqlEncoding reloadingViews:(BOOL)reloadViews; @@ -259,7 +257,6 @@ - (NSString *)user; - (NSString *)displaySPName; - (NSString *)keyChainID; -- (NSArray *)columnNames; // Notification center methods - (void)willPerformQuery:(NSNotification *)notification; diff --git a/Source/TableDocument.m b/Source/TableDocument.m index 249faee9..6d082a59 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -46,7 +46,6 @@ #import "SPConnectionController.h" #import "SPHistoryController.h" #import "SPPreferenceController.h" -#import "SPPrintAccessory.h" #import "SPUserManager.h" #import "SPEncodingPopupAccessory.h" #import "SPConstants.h" @@ -55,10 +54,6 @@ #import "SPServerVariablesController.h" #import "SPAlertSheets.h" -// Printing -#import "MGTemplateEngine.h" -#import "ICUTemplateMatcher.h" - @interface TableDocument (PrivateAPI) - (void)_addDatabase; @@ -774,136 +769,6 @@ keyChainID = [[NSString stringWithString:theID] retain]; } -#pragma mark - -#pragma mark Printing - -- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame -{ - // Because I need the webFrame loaded (for preview), I've moved the actuall printing here. - NSPrintInfo *printInfo = [self printInfo]; - [printInfo setHorizontalPagination:NSFitPagination]; - [printInfo setVerticalPagination:NSAutoPagination]; - [printInfo setVerticallyCentered:NO]; - [printInfo setTopMargin:30]; - [printInfo setBottomMargin:30]; - [printInfo setLeftMargin:10]; - [printInfo setRightMargin:10]; - - NSPrintOperation *op = [NSPrintOperation - printOperationWithView:[[[printWebView mainFrame] frameView] documentView] - printInfo:printInfo]; - - //add ability to select orientation to print panel - NSPrintPanel *printPanel = [op printPanel]; - [printPanel setOptions:[printPanel options] + NSPrintPanelShowsOrientation + NSPrintPanelShowsScaling + NSPrintPanelShowsPaperSize]; - - SPPrintAccessory *printAccessory = [[SPPrintAccessory alloc] initWithNibName:@"PrintAccessory" bundle:nil]; - [printAccessory setPrintView:printWebView]; - [printPanel addAccessoryController:printAccessory]; - - NSPageLayout *pageLayout = [NSPageLayout pageLayout]; - [pageLayout addAccessoryController:printAccessory]; - [printAccessory release]; - - [op setPrintPanel:printPanel]; - - [op runOperationModalForWindow:tableWindow - delegate:self - didRunSelector: - @selector(printOperationDidRun:success:contextInfo:) - contextInfo:NULL]; - -} - -- (IBAction)printDocument:(id)sender -{ - // Here load the printing document. The actual printing is done in the doneLoading delegate. - [[printWebView mainFrame] loadHTMLString:[self getHTMLforPrint] baseURL:nil]; -} - -- (void)printOperationDidRun:(NSPrintOperation *)printOperation success:(BOOL)success contextInfo:(void *)info -{ - // Selector for print... maybe we can get rid of this? -} - -- (NSString *)getHTMLforPrint -{ - // Set up template engine with your chosen matcher. - MGTemplateEngine *engine = [MGTemplateEngine templateEngine]; - - [engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]]; - - NSString *versionForPrint = [NSString stringWithFormat:@"%@ %@ (build %@)", - [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"], - [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"], - [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] - ]; - - NSMutableDictionary *connection = [[NSMutableDictionary alloc] init]; - - if ([[self user] length]) { - [connection setValue:[self user] forKey:@"username"]; - } - - if ([[self table] length]) { - [connection setValue:[self table] forKey:@"table"]; - } - - [connection setValue:[self host] forKey:@"hostname"]; - - if ([connectionController port] && [[connectionController port] length]) { - [connection setValue:[connectionController port] forKey:@"port"]; - } - - [connection setValue:selectedDatabase forKey:@"database"]; - [connection setValue:versionForPrint forKey:@"version"]; - - NSArray *rows = nil; - NSArray *columns = [self columnNames]; - - if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 0) { - if ([[tableSourceInstance tableStructureForPrint] count] > 1) - rows = [[NSArray alloc] initWithArray: - [[tableSourceInstance tableStructureForPrint] objectsAtIndexes: - [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[tableSourceInstance tableStructureForPrint] count] - 1)] - ] - ]; - } - else if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 1) { - if ([[tableContentInstance currentResult] count] > 1) - rows = [[NSArray alloc] initWithArray: - [[tableContentInstance currentDataResult] objectsAtIndexes: - [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[tableContentInstance currentResult] count] - 1)] - ] - ]; - - [connection setValue:[tableContentInstance usedQuery] forKey:@"query"]; - } - else if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 2) { - if ([[customQueryInstance currentResult] count] > 1) - rows = [[NSArray alloc] initWithArray: - [[customQueryInstance currentResult] objectsAtIndexes: - [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[customQueryInstance currentResult] count] - 1)] - ] - ]; - - [connection setValue:[customQueryInstance usedQuery] forKey:@"query"]; - } - - [engine setObject:connection forKey:@"c"]; - - NSDictionary *printData = [NSDictionary dictionaryWithObjectsAndKeys:columns, @"columns", rows, @"rows", nil]; - - [connection release]; - - if (rows) [rows release]; - - // Process the template and display the results. - NSString *result = [engine processTemplateInFileAtPath:[[NSBundle mainBundle] pathForResource:@"sequel-pro-print-template" ofType:@"html"] withVariables:printData]; - - return result; -} - #pragma mark - #pragma mark Database methods @@ -1845,28 +1710,6 @@ notificationName:@"Syntax Copied"]; } -- (NSArray *)columnNames -{ - NSArray *columns = nil; - if ( [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 0 - && [[tableSourceInstance tableStructureForPrint] count] > 0 ){ - columns = [[NSArray alloc] initWithArray:[[tableSourceInstance tableStructureForPrint] objectAtIndex:0] copyItems:YES]; - } - else if ( [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 1 - && [[tableContentInstance currentResult] count] > 0 ){ - columns = [[NSArray alloc] initWithArray:[[tableContentInstance currentResult] objectAtIndex:0] copyItems:YES]; - } - else if ( [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 2 - && [[customQueryInstance currentResult] count] > 0 ){ - columns = [[NSArray alloc] initWithArray:[[customQueryInstance currentResult] objectAtIndex:0] copyItems:YES]; - } - - if(columns) { - [columns autorelease]; - } - return columns; -} - /** * Performs a MySQL check table on the selected table and presents the result to the user via an alert sheet. */ @@ -3149,13 +2992,10 @@ return ([self database] != nil && [self table] != nil); } - if ([menuItem action] == @selector(printDocument:)) { - return ( - ( - [self database] != nil - && [[tablesListInstance valueForKeyPath:@"tablesListView"] numberOfSelectedRows] == 1 - ) - || [tableWindow firstResponder] == customQueryInstance); + if ([menuItem action] == @selector(printDocument:)) { + return (((([self database] != nil) && + ([[tablesListInstance valueForKeyPath:@"tablesListView"] numberOfSelectedRows] == 1)) || + ([tableWindow firstResponder] == customQueryInstance))); } if ([menuItem action] == @selector(chooseEncoding:)) { diff --git a/Source/TableSource.h b/Source/TableSource.h index 3650ec14..54a0c492 100644 --- a/Source/TableSource.h +++ b/Source/TableSource.h @@ -100,7 +100,7 @@ - (NSString *)defaultValueForField:(NSString *)field; - (NSArray *)fieldNames; - (NSDictionary *)enumFields; -- (NSArray *)tableStructureForPrint; +- (NSDictionary *)tableSourceForPrinting; // Task interaction - (void)startDocumentTaskForTab:(NSNotification *)aNotification; diff --git a/Source/TableSource.m b/Source/TableSource.m index ed624445..05eb0c53 100644 --- a/Source/TableSource.m +++ b/Source/TableSource.m @@ -1115,22 +1115,50 @@ returns a dictionary containing enum/set field names as key and possible values return [NSDictionary dictionaryWithDictionary:enumFields]; } -- (NSArray *)tableStructureForPrint -{ - MCPResult *queryResult; - NSMutableArray *tempResult = [NSMutableArray array]; +/** + * Returns a dictionary describing the source of the table to be used for printing purposes. The object accessible + * via the key 'structure' is an array of the tables fields, where the first element is always the field names + * and each subsequent element is the field data. This is also true for the table's indexes, which are accessible + * via the key 'indexes'. + */ +- (NSDictionary *)tableSourceForPrinting +{ NSInteger i; + NSMutableArray *tempResult = [NSMutableArray array]; + NSMutableArray *tempResult2 = [NSMutableArray array]; + + MCPResult *structureQueryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW COLUMNS FROM %@", [selectedTable backtickQuotedString]]]; + MCPResult *indexesQueryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW INDEXES FROM %@", [selectedTable backtickQuotedString]]]; + + [structureQueryResult setReturnDataAsStrings:YES]; + [indexesQueryResult setReturnDataAsStrings:YES]; + + if ([structureQueryResult numOfRows]) [structureQueryResult dataSeek:0]; + if ([indexesQueryResult numOfRows]) [indexesQueryResult dataSeek:0]; + + [tempResult addObject:[structureQueryResult fetchFieldNames]]; + + NSMutableArray *temp = [[[indexesQueryResult fetchFieldNames] mutableCopy] autorelease]; - queryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW COLUMNS FROM %@", [selectedTable backtickQuotedString]]]; - [queryResult setReturnDataAsStrings:YES]; + // Remove the 'table' column + [temp removeObjectAtIndex:0]; - if ([queryResult numOfRows]) [queryResult dataSeek:0]; - [tempResult addObject:[queryResult fetchFieldNames]]; - for ( i = 0 ; i < [queryResult numOfRows] ; i++ ) { - [tempResult addObject:[queryResult fetchRowAsArray]]; + [tempResult2 addObject:temp]; + + for (i = 0 ; i < [structureQueryResult numOfRows]; i++) { + [tempResult addObject:[structureQueryResult fetchRowAsArray]]; } - return tempResult; + for (i = 0 ; i < [indexesQueryResult numOfRows]; i++) { + NSMutableArray *index = [[[indexesQueryResult fetchRowAsArray] mutableCopy] autorelease]; + + // Remove the 'table' column values + [index removeObjectAtIndex:0]; + + [tempResult2 addObject:index]; + } + + return [NSDictionary dictionaryWithObjectsAndKeys:tempResult, @"structure", tempResult2, @"indexes", nil]; } #pragma mark - @@ -1143,8 +1171,7 @@ returns a dictionary containing enum/set field names as key and possible values { // Only proceed if this view is selected. - if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableStructure]) - return; + if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableStructure]) return; [tableSourceView setEnabled:NO]; [addFieldButton setEnabled:NO]; @@ -1166,8 +1193,7 @@ returns a dictionary containing enum/set field names as key and possible values { // Only re-enable elements if the current tab is the structure view - if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableStructure]) - return; + if (![[tableDocumentInstance selectedToolbarItemIdentifier] isEqualToString:SPMainToolbarTableStructure]) return; BOOL editingEnabled = ([tablesListInstance tableType] == SP_TABLETYPE_TABLE); [tableSourceView setEnabled:YES]; -- cgit v1.2.3