Skip to content

Commit

Permalink
Merge pull request #947 from Sequel-Ace/query-sort-943
Browse files Browse the repository at this point in the history
#fixed custom query result sorting
  • Loading branch information
jamesstout committed Mar 6, 2021
2 parents 1dd4b0f + 7b82ef7 commit 588d1c4
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions Source/Controllers/MainViewControllers/SPCustomQuery.m
Expand Up @@ -107,13 +107,16 @@ - (void)queryFavoritesHaveBeenUpdated:(NSNotification *)notification;
- (void)historyItemsHaveBeenUpdated:(NSNotification *)notification;
- (void)helpWindowClosedByUser:(NSNotification *)notification;

@property (readwrite, strong) NSMutableDictionary<NSNumber*,NSNumber*> *sortCount;

@end

@implementation SPCustomQuery

@synthesize runAllButton;
@synthesize textViewWasChanged;
@synthesize bracketHighlighter;
@synthesize sortCount;

#pragma mark IBAction methods

Expand Down Expand Up @@ -171,8 +174,8 @@ - (IBAction)runAllQueries:(id)sender
// Re-init sort order
isDesc = NO;
sortColumn = nil;

[sortCount removeAllObjects];

// Retrieve the custom query string and split it into separate SQL queries
queryParser = [[SPSQLParser alloc] initWithString:[textView string]];
[queryParser setDelimiterSupport:YES];
Expand Down Expand Up @@ -211,6 +214,7 @@ - (IBAction)runSelectedQueries:(id)sender
// Re-init sort order
isDesc = NO;
sortColumn = nil;
[sortCount removeAllObjects];


// If the current selection is a single caret position, run the current query.
Expand Down Expand Up @@ -2255,16 +2259,33 @@ - (void)tableView:(NSTableView*)tableView didClickTableColumn:(NSTableColumn *)t

// Sets column order as tri-state descending, ascending, no sort, descending, ascending etc. order if the same
// header is clicked several times
// JCS - i dont think this is tri-state - i don't see a no sort option.
// so re-wrote this as desc or asc.
if (sortField && [[tableColumn identifier] integerValue] == [sortField integerValue]) {
BOOL invert = NO;
if (modifierFlags & NSEventModifierFlagShift) {
invert = YES;
}

// this is the same as saying (isDesc && !invert) || (!isDesc && invert)
if (isDesc == invert) {
isDesc = !isDesc;

NSNumber *count = [sortCount objectForKey:sortField];

if(count == nil){
count = @(0);
}

count = @([count intValue] + 1);

[sortCount setObject:count forKey:sortField];

if(((count.integerValue % 2) != 0)){
isDesc = (invert == NO) ? YES : NO;
SPLog(@"it's odd, set to desc, unless modifier key pressed. isDesc = %hhd", isDesc);
}
else{
isDesc = (invert == NO) ? NO : YES;
SPLog(@"it's even, set to asc, unless modifier key pressed. isDesc = %hhd", isDesc);
}

} else {
// When the column is not sorted, allow to sort in reverse order using Shift+click
if (modifierFlags & NSEventModifierFlagShift) {
Expand Down Expand Up @@ -3265,6 +3286,8 @@ - (instancetype)init
tableRowsSelectable = YES;
selectionIndexToRestore = nil;
selectionViewportToRestore = NSZeroRect;

sortCount = [[NSMutableDictionary alloc] init];

// init tableView's data source
resultData = [[SPDataStorage alloc] init];
Expand Down

0 comments on commit 588d1c4

Please sign in to comment.