aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
Diffstat (limited to 'Source')
-rw-r--r--Source/CMCopyTable.h2
-rw-r--r--Source/CMCopyTable.m123
-rw-r--r--Source/SPArrayAdditions.h1
-rw-r--r--Source/SPArrayAdditions.m17
-rw-r--r--Source/SPNarrowDownCompletion.h9
-rw-r--r--Source/sequel-pro_Prefix.pch8
6 files changed, 131 insertions, 29 deletions
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