1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 01:34:31 +02:00

Add option to force use of LIKE in DatabaseQuerySelectFulltext class, for when operating on columns that lack fulltext indexes

This commit is contained in:
Ryan Cramer
2021-07-23 09:50:35 -04:00
parent be825ce98b
commit 916ac6f5c5
2 changed files with 49 additions and 2 deletions

View File

@@ -745,7 +745,11 @@ abstract class DatabaseQuery extends WireData {
if($exception && $options['throw']) {
if($this->wire()->config->allowExceptions) throw $exception; // throw original
throw new WireDatabaseQueryException($exception->getMessage(), $exception->getCode(), $exception);
$message = (string) $exception->getMessage();
$code = (int) $exception->getCode();
// note: re-throw below complains about wrong arguments if the above two
// lines are called in the line below, so variables are intermediary
throw new WireDatabaseQueryException($message, $code, $exception);
}
return $options['returnQuery'] ? $query : $result;

View File

@@ -20,7 +20,7 @@
* This file is licensed under the MIT license
* https://processwire.com/about/license/mit/
*
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer
* ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com
*
* @property-read $tableField
@@ -135,6 +135,28 @@ class DatabaseQuerySelectFulltext extends Wire {
'matchCommands' => array('#='),
);
/**
* Alternate operators to substitute when LIKE match is forced due to no FULLTEXT index
*
* @var array of operator to replacement operator
*
*/
protected $likeAlternateOperators = array(
'*=' => '%=',
'^=' => '%^=',
'$=' => '%$=',
'~=' => '~%=',
'~|=' => '~|%=',
);
/**
* Force use of LIKE?
*
* @var bool
*
*/
protected $forceLike = false;
/**
* Construct
*
@@ -280,6 +302,10 @@ class DatabaseQuerySelectFulltext extends Wire {
// if allowOrder has not been specifically set, then set value now
if($this->allowOrder === null) $this->allowOrder = $allowOrder;
if($this->forceLike && isset($this->likeAlternateOperators[$operator])) {
$operator = $this->likeAlternateOperators[$operator];
}
$this->operator = $operator;
foreach($this->methodOperators as $name => $operators) {
@@ -1380,4 +1406,21 @@ class DatabaseQuerySelectFulltext extends Wire {
if($word) {}
return '';
}
/**
* Call forceLike(true) to force use of LIKE, or omit argument to get current setting
*
* This forces LIKE only for matching operators that have a LIKE equivalent.
* This includes these operators: `*=`, `^=`, `$=`, `~=`, `~|=`.
*
* @param bool|null $forceLike
* @return bool
* @since 3.0.182
*
*/
public function forceLike($forceLike = null) {
if(is_bool($forceLike)) $this->forceLike = $forceLike;
return $this->forceLike;
}
}