From 7ce154492f0e307644b66a3d2410a255345d86db Mon Sep 17 00:00:00 2001 From: Bibiko Date: Sun, 7 Jun 2009 11:41:12 +0000 Subject: =?UTF-8?q?=E2=80=A2=20improved=20"Copy=20as=20SQL=20INSERT"=20(st?= =?UTF-8?q?ill=20hidden=20-=20can=20be=20tested=20if=20the=20menu=20item?= =?UTF-8?q?=20is=20set=20to=20'not=20hidden'=20in=20the=20MainMenu)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CMCopyTable.h | 7 +++++- Source/CMCopyTable.m | 65 +++++++++++++++++++++++++++++++++++++++------------ Source/TableContent.m | 4 +++- 3 files changed, 59 insertions(+), 17 deletions(-) (limited to 'Source') diff --git a/Source/CMCopyTable.h b/Source/CMCopyTable.h index e45e2f8c..f31e3575 100644 --- a/Source/CMCopyTable.h +++ b/Source/CMCopyTable.h @@ -35,6 +35,8 @@ @interface CMCopyTable : NSTableView { + NSArray* columnDefinitions; + } /*! @@ -89,6 +91,9 @@ - (NSString *)selectedRowsAsSqlInserts; +- (void)setColumnDefinitions:(NSArray *)columnDefs; + @end -extern int MENU_EDIT_COPY_WITH_COLUMN; \ No newline at end of file +extern int MENU_EDIT_COPY_WITH_COLUMN; +extern int MENU_EDIT_COPY_AS_SQL; \ No newline at end of file diff --git a/Source/CMCopyTable.m b/Source/CMCopyTable.m index e3183cb3..ed1e5439 100644 --- a/Source/CMCopyTable.m +++ b/Source/CMCopyTable.m @@ -71,11 +71,14 @@ int MENU_EDIT_COPY_AS_SQL = 2002; - (BOOL)validateMenuItem:(NSMenuItem*)anItem { if ( [[anItem title] isEqualToString:@"Copy"] - || [anItem tag] == MENU_EDIT_COPY_WITH_COLUMN - || [anItem tag] == MENU_EDIT_COPY_AS_SQL ) + || [anItem tag] == MENU_EDIT_COPY_WITH_COLUMN ) { return ([self selectedRow] > -1); } + if ( [anItem tag] == MENU_EDIT_COPY_AS_SQL ) + { + return (columnDefinitions != NULL && [self selectedRow] > -1); + } return YES; } @@ -146,8 +149,10 @@ int MENU_EDIT_COPY_AS_SQL = 2002; } } -//get selected rows as SQL INSERT INTO foo VALUES format -//the value in each field is from the objects description method +/* + * Return selected rows as SQL INSERT INTO foo VALUES baz + * string. + */ - (NSString *)selectedRowsAsSqlInserts { if ( [self numberOfSelectedRows] > 0 ) @@ -156,25 +161,42 @@ int MENU_EDIT_COPY_AS_SQL = 2002; int numColumns = [columns count]; id dataSource = [self dataSource]; id enumObj; - + NSMutableString *result = [NSMutableString stringWithCapacity:numColumns]; // Create an array of table column names NSMutableArray *tbHeader = [NSMutableArray arrayWithCapacity:numColumns]; enumerate(columns, enumObj) [tbHeader addObject:[[enumObj headerCell] stringValue]]; + // Create an hash of header name and typegrouping + NSMutableDictionary *headerType = [NSMutableDictionary dictionaryWithCapacity:numColumns]; + enumerate(columnDefinitions, enumObj) + [headerType setObject:[enumObj objectForKey:@"typegrouping"] forKey:[enumObj objectForKey:@"name"]]; + + // Create array of types according to the column order + NSMutableArray *types = [NSMutableArray arrayWithCapacity:numColumns]; + enumerate(tbHeader, enumObj) + { + NSString *t = [headerType objectForKey:enumObj]; + + if([t isEqualToString:@"bit"] || [t isEqualToString:@"integer"] || [t isEqualToString:@"float"]) + [types addObject:[NSNumber numberWithInt:0]]; + else if([t isEqualToString:@"blobdata"]) + [types addObject:[NSNumber numberWithInt:2]]; + else + [types addObject:[NSNumber numberWithInt:1]]; + + } + [result appendString:[NSString stringWithFormat:@"INSERT INTO `%@` (%@)\nVALUES\n", @"", [tbHeader componentsJoinedAndBacktickQuoted]]]; - //this is really deprecated in 10.3, but the new method is really weird - // NSEnumerator *enumerator = [self selectedRowEnumerator]; - int c; id rowData = nil; NSTableColumn *col = nil; NSIndexSet *rowIndexes = [self selectedRowIndexes]; unsigned row = [rowIndexes firstIndex]; - // while (row = [enumerator nextObject]) + while ( row != NSNotFound ) { [result appendString:@"\t("]; @@ -185,15 +207,23 @@ int MENU_EDIT_COPY_AS_SQL = 2002; rowData = [dataSource tableView:self objectValueForTableColumn:col row:row ]; - - if ( nil != rowData ) - { - [result appendString:[NSString stringWithFormat:@"'%@',", [[rowData description] stringByReplacingOccurrencesOfString:@"'" withString:@"\'"] ] ]; + + if ( nil != rowData ) { + + switch([[types objectAtIndex:c] intValue]) { + case 0: // numeric + [result appendString:[NSString stringWithFormat:@"%@,", [rowData description]]]; + break; + case 1: // string + [result appendString:[NSString stringWithFormat:@"'%@',", [[rowData description] stringByReplacingOccurrencesOfString:@"'" withString:@"\'"] ] ]; + break; + case 2: //blob + [result appendString:[NSString stringWithFormat:@"X'%@',", @""]]; + } } else - { [result appendString:@"'',"]; - } + } //end for each column if ( [result length] ) @@ -280,4 +310,9 @@ int MENU_EDIT_COPY_AS_SQL = 2002; } } +- (void)setColumnDefinitions:(NSArray *)columnDefs +{ + columnDefinitions = [[NSArray arrayWithArray:columnDefs] retain]; +} + @end diff --git a/Source/TableContent.m b/Source/TableContent.m index c93f4f00..a5e17db4 100644 --- a/Source/TableContent.m +++ b/Source/TableContent.m @@ -153,7 +153,9 @@ // of the fieldListForQuery method, and also to decide whether or not to preserve the current filter/sort settings. theColumns = [tableDataInstance columns]; columnNames = [tableDataInstance columnNames]; - + + [tableContentView setColumnDefinitions:theColumns]; + // Retrieve the total number of rows of the current table // to adjustify "Limit From:" maxNumRowsOfCurrentTable = [[[tableDataInstance statusValues] objectForKey:@"Rows"] intValue]; -- cgit v1.2.3