aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorrowanbeentje <rowan@beent.je>2013-02-28 23:12:40 +0000
committerrowanbeentje <rowan@beent.je>2013-02-28 23:12:40 +0000
commitb7c8b06a8aad371f02139d5fe4ac64bba7f11811 (patch)
treee4f0402bec3f058261a5c3b64279adf06d49e2c7 /Source
parente708cef969bb833a35615f1b5bada42a0d464fcd (diff)
downloadsequelpro-b7c8b06a8aad371f02139d5fe4ac64bba7f11811.tar.gz
sequelpro-b7c8b06a8aad371f02139d5fe4ac64bba7f11811.tar.bz2
sequelpro-b7c8b06a8aad371f02139d5fe4ac64bba7f11811.zip
- Fix escaping of backlsashes in non-LIKE clauses, addressing string matching with the = and RegExp operators - addresses Issue #1563
Diffstat (limited to 'Source')
-rw-r--r--Source/SPTableContent.m33
1 files changed, 21 insertions, 12 deletions
diff --git a/Source/SPTableContent.m b/Source/SPTableContent.m
index 95ccec59..2ca31b70 100644
--- a/Source/SPTableContent.m
+++ b/Source/SPTableContent.m
@@ -1261,7 +1261,9 @@ static NSString *SPTableFilterSetDefaultOperator = @"SPTableFilterSetDefaultOper
}
/**
- * Esacpe argument by looking for used quoting strings in clause
+ * Escape argument by looking for used quoting strings in a clause. Attempt to
+ * be smart - use a single escape for most clauses, doubling up for LIKE clauses.
+ * Also attempt to not escape what look like common escape sequences - \n, \r, \t.
*
* @param argument The to be used filter argument which should be be escaped
*
@@ -1270,26 +1272,33 @@ static NSString *SPTableFilterSetDefaultOperator = @"SPTableFilterSetDefaultOper
*/
- (NSString *)escapeFilterArgument:(NSString *)argument againstClause:(NSString *)clause
{
+ BOOL clauseIsLike = [clause isMatchedByRegex:@"(?i)\\blike\\b.*?%(?!@)"];
+ NSString *recognizedEscapeSequences, *escapeSequence, *regexTerm;
+ NSMutableString *arg = [argument mutableCopy];
+
+ // Determine the character set not to escape slashes before, and the escape depth
+ if (clauseIsLike) {
+ recognizedEscapeSequences = @"nrt_%";
+ escapeSequence = @"\\\\\\\\\\\\\\\\";
+ } else {
+ recognizedEscapeSequences = @"nrt";
+ escapeSequence = @"\\\\\\\\";
+ }
+ regexTerm = [NSString stringWithFormat:@"(\\\\)(?![%@])", recognizedEscapeSequences];
- NSMutableString *arg = [[NSMutableString alloc] init];
- [arg setString:argument];
-
- [arg replaceOccurrencesOfRegex:@"(\\\\)(?![nrt_%])" withString:@"\\\\\\\\\\\\\\\\"];
- [arg flushCachedRegexData];
- [arg replaceOccurrencesOfRegex:@"(\\\\)(?=[nrt])" withString:@"\\\\\\"];
+ // Escape slashes appropriately
+ [arg replaceOccurrencesOfRegex:regexTerm withString:escapeSequence];
[arg flushCachedRegexData];
// Get quote sign for escaping - this should work for 99% of all cases
NSString *quoteSign = [clause stringByMatching:@"([\"'])[^\\1]*?%@[^\\1]*?\\1" capture:1L];
- // Esape argument
+
+ // Escape argument
if(quoteSign != nil && [quoteSign length] == 1) {
[arg replaceOccurrencesOfRegex:[NSString stringWithFormat:@"(%@)", quoteSign] withString:@"\\\\$1"];
[arg flushCachedRegexData];
}
- // if([clause isMatchedByRegex:@"(?i)\\blike\\b.*?%(?!@)"]) {
- // [arg replaceOccurrencesOfRegex:@"([_%])" withString:@"\\\\$1"];
- // [arg flushCachedRegexData];
- // }
+
return [arg autorelease];
}