From 2f5c4b9bab18b54f2d37f4b4a05afa116cfd0c44 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Sat, 6 Jun 2009 17:52:08 +0000 Subject: =?UTF-8?q?=E2=80=A2=20moved=20macros=20enumerate()=20and=20sizeOf?= =?UTF-8?q?()=20to=20sequel-pro=5FPrefix.pch=20=E2=80=A2=20added=20to=20SP?= =?UTF-8?q?ArrayAdditions.m=20-=20(NSArray=20*)subarrayWithIndexes:(NSInde?= =?UTF-8?q?xSet=20*)indexes=20=E2=80=A2=20added=20"Copy=20as=20SQL=20INSER?= =?UTF-8?q?T"=20to=20MainMenu=20(not=20yet=20activated)=20=E2=80=A2=20adde?= =?UTF-8?q?d=20to=20CMCopyTable=20-=20(NSString=20*)selectedRowsAsSqlInser?= =?UTF-8?q?ts=20for=20copying=20selectedRows=20as=20=20INSERT=20INTO=20...?= =?UTF-8?q?=20string=20(under=20constructions,=20up=20to=20now=20it=20work?= =?UTF-8?q?s=20for=20strings)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Interfaces/English.lproj/MainMenu.xib | 47 +++++++++++-- Source/CMCopyTable.h | 2 + Source/CMCopyTable.m | 123 ++++++++++++++++++++++++++++------ Source/SPArrayAdditions.h | 1 + Source/SPArrayAdditions.m | 17 +++++ Source/SPNarrowDownCompletion.h | 9 --- Source/sequel-pro_Prefix.pch | 8 +++ 7 files changed, 174 insertions(+), 33 deletions(-) diff --git a/Interfaces/English.lproj/MainMenu.xib b/Interfaces/English.lproj/MainMenu.xib index 0c2f5891..aacfd904 100644 --- a/Interfaces/English.lproj/MainMenu.xib +++ b/Interfaces/English.lproj/MainMenu.xib @@ -8,7 +8,7 @@ 353.00 YES - + YES @@ -592,7 +592,18 @@ 2147483647 - 2001 + 2002 + + + + YES + Copy as SQL INSERT + c + 1835008 + 2147483647 + + + 2002 @@ -2424,6 +2435,14 @@ 933 + + + copy: + + + + 936 + @@ -3235,6 +3254,7 @@ + @@ -3611,6 +3631,11 @@ + + 935 + + + @@ -3947,6 +3972,7 @@ 928.IBPluginDependency 930.IBPluginDependency 932.IBPluginDependency + 935.IBPluginDependency YES @@ -4007,7 +4033,7 @@ com.apple.InterfaceBuilder.CocoaPlugin - {{312, 486}, {268, 233}} + {{312, 466}, {282, 253}} com.apple.InterfaceBuilder.CocoaPlugin {{654, 613}, {157, 223}} @@ -4323,6 +4349,7 @@ w6gg4oaSIGZhY2FkZV0 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin @@ -4345,7 +4372,7 @@ w6gg4oaSIGZhY2FkZV0 - 933 + 936 @@ -4784,6 +4811,18 @@ w6gg4oaSIGZhY2FkZV0 YES + + CMCopyTable + NSTableView + + copy: + id + + + IBDocumentRelativeSource + ../../Source/CMCopyTable.h + + NSObject diff --git a/Source/CMCopyTable.h b/Source/CMCopyTable.h index 38d3d472..e45e2f8c 100644 --- a/Source/CMCopyTable.h +++ b/Source/CMCopyTable.h @@ -87,6 +87,8 @@ */ - (NSString *)draggedRowsAsTabString:(NSArray *)rows; +- (NSString *)selectedRowsAsSqlInserts; + @end extern int MENU_EDIT_COPY_WITH_COLUMN; \ No newline at end of file diff --git a/Source/CMCopyTable.m b/Source/CMCopyTable.m index 3a227bd2..e3183cb3 100644 --- a/Source/CMCopyTable.m +++ b/Source/CMCopyTable.m @@ -23,8 +23,10 @@ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #import "CMCopyTable.h" +#import "SPArrayAdditions.h" int MENU_EDIT_COPY_WITH_COLUMN = 2001; +int MENU_EDIT_COPY_AS_SQL = 2002; @implementation CMCopyTable @@ -32,22 +34,30 @@ int MENU_EDIT_COPY_WITH_COLUMN = 2001; { NSString *tmp = nil; - BOOL withHeaders = NO; - if([sender tag] == MENU_EDIT_COPY_WITH_COLUMN) { - withHeaders = YES; - } - - tmp = [self selectedRowsAsTabStringWithHeaders:withHeaders]; - if ( nil != tmp ) - { - NSPasteboard *pb = [NSPasteboard generalPasteboard]; + if([sender tag] == MENU_EDIT_COPY_AS_SQL) { + tmp = [self selectedRowsAsSqlInserts]; + if ( nil != tmp ) + { + NSPasteboard *pb = [NSPasteboard generalPasteboard]; + + [pb declareTypes:[NSArray arrayWithObjects: NSStringPboardType, nil] + owner:nil]; - [pb declareTypes:[NSArray arrayWithObjects: NSTabularTextPboardType, - NSStringPboardType, nil] - owner:nil]; + [pb setString:tmp forType:NSStringPboardType]; + } + } else { + tmp = [self selectedRowsAsTabStringWithHeaders:([sender tag] == MENU_EDIT_COPY_WITH_COLUMN)]; + if ( nil != tmp ) + { + NSPasteboard *pb = [NSPasteboard generalPasteboard]; - [pb setString:tmp forType:NSStringPboardType]; - [pb setString:tmp forType:NSTabularTextPboardType]; + [pb declareTypes:[NSArray arrayWithObjects: NSTabularTextPboardType, + NSStringPboardType, nil] + owner:nil]; + + [pb setString:tmp forType:NSStringPboardType]; + [pb setString:tmp forType:NSTabularTextPboardType]; + } } } @@ -60,13 +70,11 @@ int MENU_EDIT_COPY_WITH_COLUMN = 2001; //only have the copy menu item enabled when row(s) are selected - (BOOL)validateMenuItem:(NSMenuItem*)anItem { - int row = [self selectedRow]; - if ( [[anItem title] isEqualToString:@"Copy"] || [anItem tag] == MENU_EDIT_COPY_WITH_COLUMN ) + if ( [[anItem title] isEqualToString:@"Copy"] + || [anItem tag] == MENU_EDIT_COPY_WITH_COLUMN + || [anItem tag] == MENU_EDIT_COPY_AS_SQL ) { - if (row < 0 ) - { - return NO; - } + return ([self selectedRow] > -1); } return YES; } @@ -138,6 +146,81 @@ int MENU_EDIT_COPY_WITH_COLUMN = 2001; } } +//get selected rows as SQL INSERT INTO foo VALUES format +//the value in each field is from the objects description method +- (NSString *)selectedRowsAsSqlInserts +{ + if ( [self numberOfSelectedRows] > 0 ) + { + NSArray *columns = [self tableColumns]; + 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]]; + + [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("]; + rowData = nil; + for ( c = 0; c < numColumns; c++) + { + col = [columns objectAtIndex:c]; + rowData = [dataSource tableView:self + objectValueForTableColumn:col + row:row ]; + + if ( nil != rowData ) + { + [result appendString:[NSString stringWithFormat:@"'%@',", [[rowData description] stringByReplacingOccurrencesOfString:@"'" withString:@"\'"] ] ]; + } + else + { + [result appendString:@"'',"]; + } + } //end for each column + + if ( [result length] ) + { + [result deleteCharactersInRange:NSMakeRange([result length]-1, 1)]; + } + [result appendString: [ NSString stringWithFormat:@"),\n"]]; + + row = [rowIndexes indexGreaterThanIndex: row]; + + } //end for each row + + if ( [result length] > 3 ) + { + [result deleteCharactersInRange:NSMakeRange([result length]-2, 2)]; + } + + [result appendString:@";\n"]; + + return result; + } + else + { + return nil; + } +} + //get dragged rows a string of newline separated lines of tab separated fields //the value in each field is from the objects description method - (NSString *)draggedRowsAsTabString:(NSArray *)rows diff --git a/Source/SPArrayAdditions.h b/Source/SPArrayAdditions.h index e12215c0..7a672dc0 100644 --- a/Source/SPArrayAdditions.h +++ b/Source/SPArrayAdditions.h @@ -27,5 +27,6 @@ @interface NSArray (SPArrayAdditions) - (NSString *)componentsJoinedAndBacktickQuoted; +- (NSArray *)subarrayWithIndexes:(NSIndexSet *)indexes; @end diff --git a/Source/SPArrayAdditions.m b/Source/SPArrayAdditions.m index 66473d81..853421e0 100644 --- a/Source/SPArrayAdditions.m +++ b/Source/SPArrayAdditions.m @@ -42,4 +42,21 @@ return result; } +- (NSArray *)subarrayWithIndexes:(NSIndexSet *)indexes +{ + NSMutableArray *subArray = [NSMutableArray arrayWithCapacity:[indexes count]]; + unsigned count = [self count]; + + unsigned index = [indexes firstIndex]; + while ( index != NSNotFound ) + { + if ( index < count ) + [subArray addObject: [self objectAtIndex: index]]; + + index = [indexes indexGreaterThanIndex: index]; + } + + return subArray; +} + @end diff --git a/Source/SPNarrowDownCompletion.h b/Source/SPNarrowDownCompletion.h index 22657d15..6d69b549 100644 --- a/Source/SPNarrowDownCompletion.h +++ b/Source/SPNarrowDownCompletion.h @@ -30,15 +30,6 @@ #define SP_NARROWDOWNLIST_MAX_ROWS 15 -#ifndef enumerate -#define enumerate(container,var) for(NSEnumerator* _enumerator = [container objectEnumerator]; var = [_enumerator nextObject]; ) -#endif - -#ifndef sizeofA -#define sizeofA(a) (sizeof(a)/sizeof(a[0])) -#endif - - @interface SPNarrowDownCompletion : NSWindow { NSArray* suggestions; diff --git a/Source/sequel-pro_Prefix.pch b/Source/sequel-pro_Prefix.pch index 864e88ea..75f66099 100644 --- a/Source/sequel-pro_Prefix.pch +++ b/Source/sequel-pro_Prefix.pch @@ -13,3 +13,11 @@ #endif #define ALog(...) NSLog(__VA_ARGS__) + +#ifndef enumerate +#define enumerate(container,var) for(NSEnumerator* _enumerator = [container objectEnumerator]; var = [_enumerator nextObject]; ) +#endif + +#ifndef sizeofA +#define sizeofA(a) (sizeof(a)/sizeof(a[0])) +#endif -- cgit v1.2.3