Skip to content

Commit

Permalink
Make it possible to use empty strings in content filters (#3083)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoagx committed Jun 15, 2019
1 parent 6c52588 commit 1ae3a5e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Source/SPRuleFilterController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ + (void)_writeFilterTree:(NSDictionary *)in toString:(NSMutableString *)out wrap
// SPTableFilterParser will return nil if it doesn't like the arguments and NSMutableString doesn't like nil
if(!sql) {
if(err) *err = [NSError errorWithDomain:SPErrorDomain code:3 userInfo:@{
NSLocalizedDescriptionKey: NSLocalizedString(@"No valid SQL expression could be generated. Make sure that you have filled in all required fields.", @"filter to sql conversion : internal error : SPTableFilterParser failed"),
NSLocalizedDescriptionKey: NSLocalizedString(@"No valid SQL expression could be generated. Perhaps the filter definition is invalid.", @"filter to sql conversion : internal error : SPTableFilterParser failed"),
}];
[parser release];
return;
Expand Down
73 changes: 20 additions & 53 deletions Source/SPTableFilterParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,13 @@ - (void)dealloc

- (NSString *)filterString
{
NSString *filterString;

// argument if Filter requires only one argument
NSMutableString *argument = [[NSMutableString alloc] initWithString:(_argument? _argument : @"")];

// If the filter field is empty and the selected filter does not require
// only one argument, then no filtering is required - return nil.
if (![argument length] && numberOfArguments == 1) {
[argument release];
return nil;
}

// arguments if Filter requires two arguments
NSMutableString *firstBetweenArgument = [[NSMutableString alloc] initWithString:(_firstBetweenArgument? _firstBetweenArgument : @"")];
NSMutableString *secondBetweenArgument = [[NSMutableString alloc] initWithString:(_secondBetweenArgument? _secondBetweenArgument : @"")];

// If filter requires two arguments and either of the argument fields are empty
// return nil.
if (numberOfArguments == 2) {
if (([firstBetweenArgument length] == 0) || ([secondBetweenArgument length] == 0)) {
[argument release];
[firstBetweenArgument release];
[secondBetweenArgument release];
return nil;
}
}

// Retrieve actual WHERE clause
NSMutableString *clause = [[NSMutableString alloc] init];
[clause setString:_clause];
Expand Down Expand Up @@ -135,40 +115,27 @@ - (NSString *)filterString
}

// Construct the filter string according the required number of arguments

if(suppressLeadingTablePlaceholder) {
if (numberOfArguments == 2) {
filterString = [NSString stringWithFormat:clause,
[[self class] escapeFilterArgument:firstBetweenArgument againstClause:clause],
[[self class] escapeFilterArgument:secondBetweenArgument againstClause:clause]];
} else if (numberOfArguments == 1) {
filterString = [NSString stringWithFormat:clause, [[self class] escapeFilterArgument:argument againstClause:clause]];
} else {
filterString = [NSString stringWithString:clause];
if(numberOfArguments > 2) {
SPLog(@"Filter with more than 2 arguments is not yet supported.");
NSBeep();
}
}
NSMutableString *filterString = [NSMutableString string];

if(!suppressLeadingTablePlaceholder) {
[filterString appendFormat:@"%@ ",[_currentField backtickQuotedString]];
}

NSUInteger numArgs = numberOfArguments;
if(numArgs > 2) {
SPLog(@"Filter with more than 2 arguments is not yet supported.");
NSBeep();
numArgs = 2;
}

if (numArgs == 2) {
[filterString appendFormat:clause,
[[self class] escapeFilterArgument:firstBetweenArgument againstClause:clause],
[[self class] escapeFilterArgument:secondBetweenArgument againstClause:clause]];
} else if (numArgs == 1) {
[filterString appendFormat:clause, [[self class] escapeFilterArgument:argument againstClause:clause]];
} else {
if (numberOfArguments == 2) {
filterString = [NSString stringWithFormat:@"%@ %@",
[_currentField backtickQuotedString],
[NSString stringWithFormat:clause,
[[self class] escapeFilterArgument:firstBetweenArgument againstClause:clause],
[[self class] escapeFilterArgument:secondBetweenArgument againstClause:clause]]];
} else if (numberOfArguments == 1) {
filterString = [NSString stringWithFormat:@"%@ %@",
[_currentField backtickQuotedString],
[NSString stringWithFormat:clause, [[self class] escapeFilterArgument:argument againstClause:clause]]];
} else {
filterString = [NSString stringWithFormat:@"%@ %@",
[_currentField backtickQuotedString], clause];
if(numberOfArguments > 2) {
SPLog(@"Filter with more than 2 arguments is not yet supported.");
NSBeep();
}
}
[filterString appendString:clause];
}

[argument release];
Expand Down

0 comments on commit 1ae3a5e

Please sign in to comment.