From 36f15a52b419beb60f5a8bf271c2b334770c1f9a Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 11 Dec 2012 01:43:37 +0100 Subject: [PATCH] BUG Using GLOB for case sensitive matches in SQLite3 As opposed to LIKE, the GLOB operator is case sensitive by default in SQlite3. It uses "*" instead of "%" for wildcards, which necessitated a new SearchFilter->getWildcard() method. SQlite3 doesn't support per-term modifiers, COLLATE BINARY LIKE is case insensitive by default unless the field collation is set up accordingly. There's connection-level modifiers (PRAGMA case_sensitive_like = true), but that would affect all comparisators in the executed query. --- code/SQLite3Database.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/code/SQLite3Database.php b/code/SQLite3Database.php index 3e13efb..6d530d8 100644 --- a/code/SQLite3Database.php +++ b/code/SQLite3Database.php @@ -1076,6 +1076,37 @@ class SQLite3Database extends SS_Database { return true; } + /** + * Generate a WHERE clause for text matching. + * + * @param String $field Quoted field name + * @param String $value Escaped search. Can include percentage wildcards. + * @param boolean $exact Exact matches or wildcard support. + * @param boolean $negate Negate the clause. + * @param boolean $caseSensitive Enforce case sensitivity if TRUE or FALSE. + * Stick with default collation if set to NULL. + * @return String SQL + */ + public function comparisonClause($field, $value, $exact = false, $negate = false, $caseSensitive = null) { + if($exact && !$caseSensitive) { + $comp = ($negate) ? '!=' : '='; + } else { + if($caseSensitive) { + // GLOB uses asterisks as wildcards. + // Replace them in search string, without replacing escaped percetage signs. + $comp = 'GLOB'; + $value = preg_replace('/^%([^\\\\])/', '*$1', $value); + $value = preg_replace('/([^\\\\])%$/', '$1*', $value); + $value = preg_replace('/([^\\\\])%/', '$1*', $value); + } else { + $comp = 'LIKE'; + } + if($negate) $comp = 'NOT ' . $comp; + } + + return sprintf("%s %s '%s'", $field, $comp, $value); + } + /** * Function to return an SQL datetime expression that can be used with SQLite3 * used for querying a datetime in a certain format