Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sequelpro/sequelpro
Browse files Browse the repository at this point in the history
  • Loading branch information
stuconnolly committed Mar 28, 2017
2 parents ed23600 + db424ed commit 93239c0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
11 changes: 11 additions & 0 deletions Source/SPQueryConsoleDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,15 @@ - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColum
#endif
}

- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard
{
NSString *string = [self sqlStringForRowIndexes:rowIndexes];
if([string length]) {
[pboard declareTypes:@[NSStringPboardType] owner:self];
return [pboard setString:string forType:NSStringPboardType];
}

return NO;
}

@end
17 changes: 17 additions & 0 deletions Source/SPQueryController.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ extern NSString *SPTableViewDatabaseColumnID;

+ (SPQueryController *)sharedQueryController;

/**
* Calls -sqlStringForForRowIndexes: with the current selection and
* puts the output into the general Pasteboard (only if non-empty)
*/
- (IBAction)copy:(id)sender;
- (IBAction)clearConsole:(id)sender;
- (IBAction)saveConsoleAs:(id)sender;
Expand All @@ -103,4 +107,17 @@ extern NSString *SPTableViewDatabaseColumnID;

- (NSUInteger)consoleMessageCount;

/**
* Returns the console messages specified by indexes as a string, each message separated by "\n".
* @param indexes The indexes of rows to be returned.
* Invalid indexes will be skipped silently.
* nil is treated as an empty set.
*
* If no (valid) indexes are given, @"" will be returned.
* The output may include other info like timestamp, host, etc. if shown in the table view, as part of a comment.
*
* THIS METHOD IS NOT THREAD-SAFE!
*/
- (NSString *)sqlStringForRowIndexes:(NSIndexSet *)indexes;

@end
85 changes: 46 additions & 39 deletions Source/SPQueryController.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,57 +150,64 @@ - (oneway void)release { }
*/
- (void)copy:(id)sender
{
#ifndef SP_CODA
NSResponder *firstResponder = [[self window] firstResponder];

if ((firstResponder == consoleTableView) && ([consoleTableView numberOfSelectedRows] > 0)) {

NSMutableString *string = [NSMutableString string];

NSIndexSet *rows = [consoleTableView selectedRowIndexes];

BOOL includeTimestamps = ![[consoleTableView tableColumnWithIdentifier:SPTableViewDateColumnID] isHidden];
BOOL includeConnections = ![[consoleTableView tableColumnWithIdentifier:SPTableViewConnectionColumnID] isHidden];
BOOL includeDatabases = ![[consoleTableView tableColumnWithIdentifier:SPTableViewDatabaseColumnID] isHidden];

[string setString:@""];

[rows enumerateIndexesUsingBlock:^(NSUInteger i, BOOL * _Nonnull stop) {
if (i < [messagesVisibleSet count]) {
SPConsoleMessage *message = NSArrayObjectAtIndex(messagesVisibleSet, i);

if (includeTimestamps || includeConnections || includeDatabases) [string appendString:@"/* "];

NSDate *date = [message messageDate];
if (includeTimestamps && date) {
[string appendString:[dateFormatter stringFromDate:date]];
[string appendString:@" "];
}

NSString *connection = [message messageConnection];
if (includeConnections && connection) {
[string appendString:connection];
[string appendString:@" "];
}

NSString *database = [message messageDatabase];
if (includeDatabases && database) {
[string appendString:database];
[string appendString:@" "];
}

if (includeTimestamps || includeConnections || includeDatabases) [string appendString:@"*/ "];

[string appendFormat:@"%@\n", [message message]];
}
}];
NSString *string = [self sqlStringForRowIndexes:rows];

NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];

// Copy the string to the pasteboard
[pasteBoard declareTypes:@[NSStringPboardType] owner:nil];
[pasteBoard setString:string forType:NSStringPboardType];
}
#endif
}

- (NSString *)sqlStringForRowIndexes:(NSIndexSet *)rows
{
if(![rows count]) return @"";

NSMutableString *string = [[NSMutableString alloc] init];

BOOL includeTimestamps = ![[consoleTableView tableColumnWithIdentifier:SPTableViewDateColumnID] isHidden];
BOOL includeConnections = ![[consoleTableView tableColumnWithIdentifier:SPTableViewConnectionColumnID] isHidden];
BOOL includeDatabases = ![[consoleTableView tableColumnWithIdentifier:SPTableViewDatabaseColumnID] isHidden];

[rows enumerateIndexesUsingBlock:^(NSUInteger i, BOOL * _Nonnull stop) {
if (i < [messagesVisibleSet count]) {
SPConsoleMessage *message = NSArrayObjectAtIndex(messagesVisibleSet, i);

if (includeTimestamps || includeConnections || includeDatabases) [string appendString:@"/* "];

NSDate *date = [message messageDate];
if (includeTimestamps && date) {
[string appendString:[dateFormatter stringFromDate:date]];
[string appendString:@" "];
}

NSString *connection = [message messageConnection];
if (includeConnections && connection) {
[string appendString:connection];
[string appendString:@" "];
}

NSString *database = [message messageDatabase];
if (includeDatabases && database) {
[string appendString:database];
[string appendString:@" "];
}

if (includeTimestamps || includeConnections || includeDatabases) [string appendString:@"*/ "];

[string appendString:[message message]];
[string appendString:@"\n"];
}
}];

return [string autorelease];
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Source/SPQueryControllerInitializer.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ - (void)awakeFromNib
{
[[column dataCell] setFont:(useMonospacedFont) ? [NSFont fontWithName:SPDefaultMonospacedFontName size:monospacedFontSize] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
}

//allow drag-out copying of selected rows
[consoleTableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
#endif
}

Expand Down

0 comments on commit 93239c0

Please sign in to comment.