diff options
-rw-r--r-- | Interfaces/English.lproj/MainMenu.xib | 47 | ||||
-rw-r--r-- | Source/CMCopyTable.h | 2 | ||||
-rw-r--r-- | Source/CMCopyTable.m | 123 | ||||
-rw-r--r-- | Source/SPArrayAdditions.h | 1 | ||||
-rw-r--r-- | Source/SPArrayAdditions.m | 17 | ||||
-rw-r--r-- | Source/SPNarrowDownCompletion.h | 9 | ||||
-rw-r--r-- | 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 @@ <string key="IBDocument.HIToolboxVersion">353.00</string> <object class="NSMutableArray" key="IBDocument.EditedObjectIDs"> <bool key="EncodedWithXMLCoder">YES</bool> - <integer value="849"/> + <integer value="169"/> </object> <object class="NSArray" key="IBDocument.PluginDependencies"> <bool key="EncodedWithXMLCoder">YES</bool> @@ -592,7 +592,18 @@ <int key="NSMnemonicLoc">2147483647</int> <reference key="NSOnImage" ref="625762401"/> <reference key="NSMixedImage" ref="315854375"/> - <int key="NSTag">2001</int> + <int key="NSTag">2002</int> + </object> + <object class="NSMenuItem" id="121139500"> + <reference key="NSMenu" ref="71086556"/> + <bool key="NSIsHidden">YES</bool> + <string key="NSTitle">Copy as SQL INSERT</string> + <string key="NSKeyEquiv">c</string> + <int key="NSKeyEquivModMask">1835008</int> + <int key="NSMnemonicLoc">2147483647</int> + <reference key="NSOnImage" ref="625762401"/> + <reference key="NSMixedImage" ref="315854375"/> + <int key="NSTag">2002</int> </object> <object class="NSMenuItem" id="892596559"> <reference key="NSMenu" ref="71086556"/> @@ -2424,6 +2435,14 @@ </object> <int key="connectionID">933</int> </object> + <object class="IBConnectionRecord"> + <object class="IBActionConnection" key="connection"> + <string key="label">copy:</string> + <reference key="source" ref="63651044"/> + <reference key="destination" ref="121139500"/> + </object> + <int key="connectionID">936</int> + </object> </object> <object class="IBMutableOrderedSet" key="objectRecords"> <object class="NSArray" key="orderedObjects"> @@ -3235,6 +3254,7 @@ <reference ref="465877627"/> <reference ref="1946"/> <reference ref="609933882"/> + <reference ref="121139500"/> </object> <reference key="parent" ref="119995921"/> </object> @@ -3611,6 +3631,11 @@ <reference key="object" ref="854155133"/> <reference key="parent" ref="1792"/> </object> + <object class="IBObjectRecord"> + <int key="objectID">935</int> + <reference key="object" ref="121139500"/> + <reference key="parent" ref="71086556"/> + </object> </object> </object> <object class="NSMutableDictionary" key="flattenedProperties"> @@ -3947,6 +3972,7 @@ <string>928.IBPluginDependency</string> <string>930.IBPluginDependency</string> <string>932.IBPluginDependency</string> + <string>935.IBPluginDependency</string> </object> <object class="NSMutableArray" key="dict.values"> <bool key="EncodedWithXMLCoder">YES</bool> @@ -4007,7 +4033,7 @@ <reference ref="9"/> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <reference ref="9"/> - <string>{{312, 486}, {268, 233}}</string> + <string>{{312, 466}, {282, 253}}</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <reference ref="9"/> <string>{{654, 613}, {157, 223}}</string> @@ -4323,6 +4349,7 @@ w6gg4oaSIGZhY2FkZV0</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> <string>com.apple.InterfaceBuilder.CocoaPlugin</string> + <string>com.apple.InterfaceBuilder.CocoaPlugin</string> </object> </object> <object class="NSMutableDictionary" key="unlocalizedProperties"> @@ -4345,7 +4372,7 @@ w6gg4oaSIGZhY2FkZV0</string> </object> </object> <nil key="sourceID"/> - <int key="maxID">933</int> + <int key="maxID">936</int> </object> <object class="IBClassDescriber" key="IBDocument.Classes"> <object class="NSMutableArray" key="referencedPartialClassDescriptions"> @@ -4785,6 +4812,18 @@ w6gg4oaSIGZhY2FkZV0</string> <object class="NSMutableArray" key="referencedPartialClassDescriptionsV3.1+"> <bool key="EncodedWithXMLCoder">YES</bool> <object class="IBPartialClassDescription"> + <string key="className">CMCopyTable</string> + <string key="superclassName">NSTableView</string> + <object class="NSMutableDictionary" key="actions"> + <string key="NS.key.0">copy:</string> + <string key="NS.object.0">id</string> + </object> + <object class="IBClassDescriptionSource" key="sourceIdentifier"> + <string key="majorKey">IBDocumentRelativeSource</string> + <string key="minorKey">../../Source/CMCopyTable.h</string> + </object> + </object> + <object class="IBPartialClassDescription"> <string key="className">NSObject</string> <object class="IBClassDescriptionSource" key="sourceIdentifier" id="939419192"> <string key="majorKey">IBDocumentRelativeSource</string> 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", + @"<table>", [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 |