Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ class BaseBuilder
'RIGHT OUTER',
];

/**
* Strings that determine if a string represents a literal value or a field name
*
* @var string[]
*/
protected $isLiteralStr = [];

/**
* RegExp used to get operators
*
* @var string[]
*/
protected $pregOperators = [];

/**
* Constructor
*
Expand Down Expand Up @@ -2515,13 +2529,11 @@ protected function isLiteral(string $str): bool
return true;
}

static $_str;

if (empty($_str)) {
$_str = ($this->db->escapeChar !== '"') ? ['"', "'"] : ["'"];
if ($this->isLiteralStr === []) {
$this->isLiteralStr = $this->db->escapeChar !== '"' ? ['"', "'"] : ["'"];
}

return in_array($str[0], $_str, true);
return in_array($str[0], $this->isLiteralStr, true);
}

/**
Expand Down Expand Up @@ -2600,7 +2612,10 @@ protected function resetWrite()
*/
protected function hasOperator(string $str): bool
{
return (bool) preg_match('/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sEXISTS|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i', trim($str));
return preg_match(
'/(<|>|!|=|\sIS NULL|\sIS NOT NULL|\sEXISTS|\sBETWEEN|\sLIKE|\sIN\s*\(|\s)/i',
trim($str)
) === 1;
}

/**
Expand All @@ -2610,11 +2625,11 @@ protected function hasOperator(string $str): bool
*/
protected function getOperator(string $str, bool $list = false)
{
static $_operators;

if (empty($_operators)) {
$_les = ($this->db->likeEscapeStr !== '') ? '\s+' . preg_quote(trim(sprintf($this->db->likeEscapeStr, $this->db->likeEscapeChar)), '/') : '';
$_operators = [
if ($this->pregOperators === []) {
$_les = $this->db->likeEscapeStr !== ''
? '\s+' . preg_quote(trim(sprintf($this->db->likeEscapeStr, $this->db->likeEscapeChar)), '/')
: '';
$this->pregOperators = [
'\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
'\s*<>?\s*', // <, <>
'\s*>\s*', // >
Expand All @@ -2630,7 +2645,11 @@ protected function getOperator(string $str, bool $list = false)
];
}

return preg_match_all('/' . implode('|', $_operators) . '/i', $str, $match) ? ($list ? $match[0] : $match[0][0]) : false;
return preg_match_all(
'/' . implode('|', $this->pregOperators) . '/i',
$str,
$match
) ? ($list ? $match[0] : $match[0][0]) : false;
}

/**
Expand Down
29 changes: 21 additions & 8 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ abstract class BaseConnection implements ConnectionInterface
*/
public $likeEscapeChar = '!';

/**
* RegExp used to escape identifiers
*
* @var array
*/
protected $pregEscapeChar = [];

/**
* Holds previously looked up data
* for performance reasons.
Expand Down Expand Up @@ -1119,29 +1126,35 @@ public function escapeIdentifiers($item)
return $item;
}

static $pregEc = [];

if (empty($pregEc)) {
if ($this->pregEscapeChar === []) {
if (is_array($this->escapeChar)) {
$pregEc = [
$this->pregEscapeChar = [
preg_quote($this->escapeChar[0], '/'),
preg_quote($this->escapeChar[1], '/'),
$this->escapeChar[0],
$this->escapeChar[1],
];
} else {
$pregEc[0] = $pregEc[1] = preg_quote($this->escapeChar, '/');
$pregEc[2] = $pregEc[3] = $this->escapeChar;
$this->pregEscapeChar[0] = $this->pregEscapeChar[1] = preg_quote($this->escapeChar, '/');
$this->pregEscapeChar[2] = $this->pregEscapeChar[3] = $this->escapeChar;
}
}

foreach ($this->reservedIdentifiers as $id) {
if (strpos($item, '.' . $id) !== false) {
return preg_replace('/' . $pregEc[0] . '?([^' . $pregEc[1] . '\.]+)' . $pregEc[1] . '?\./i', $pregEc[2] . '$1' . $pregEc[3] . '.', $item);
return preg_replace(
'/' . $this->pregEscapeChar[0] . '?([^' . $this->pregEscapeChar[1] . '\.]+)' . $this->pregEscapeChar[1] . '?\./i',
$this->pregEscapeChar[2] . '$1' . $this->pregEscapeChar[3] . '.',
$item
);
}
}

return preg_replace('/' . $pregEc[0] . '?([^' . $pregEc[1] . '\.]+)' . $pregEc[1] . '?(\.)?/i', $pregEc[2] . '$1' . $pregEc[3] . '$2', $item);
return preg_replace(
'/' . $this->pregEscapeChar[0] . '?([^' . $this->pregEscapeChar[1] . '\.]+)' . $this->pregEscapeChar[1] . '?(\.)?/i',
$this->pregEscapeChar[2] . '$1' . $this->pregEscapeChar[3] . '$2',
$item
);
}

/**
Expand Down