Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize Database BaseBuilder : use foreach instead of for with count when possible #3282

Merged
merged 4 commits into from
Jul 13, 2020
Merged
Changes from 3 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: 22 additions & 21 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,12 @@ public function join(string $table, string $cond, string $type = '', bool $escap
}

$cond = ' ON ';
for ($i = 0, $c = count($conditions); $i < $c; $i ++)
ksort($conditions);
foreach ($conditions as $i => $condition)
{
$operator = $this->getOperator($conditions[$i]);
$operator = $this->getOperator($condition);
$cond .= $joints[$i];
$cond .= preg_match("/(\(*)?([\[\]\w\.'-]+)" . preg_quote($operator) . '(.*)/i', $conditions[$i], $match) ? $match[1] . $this->db->protectIdentifiers($match[2]) . $operator . $this->db->protectIdentifiers($match[3]) : $conditions[$i];
$cond .= preg_match("/(\(*)?([\[\]\w\.'-]+)" . preg_quote($operator) . '(.*)/i', $condition, $match) ? $match[1] . $this->db->protectIdentifiers($match[2]) . $operator . $this->db->protectIdentifiers($match[3]) : $condition;
}
}

Expand Down Expand Up @@ -3061,28 +3062,28 @@ protected function compileWhereHaving(string $qb_key): string
{
if (! empty($this->$qb_key))
{
for ($i = 0, $c = count($this->$qb_key); $i < $c; $i ++)
foreach ($this->$qb_key as &$qbkey)
{
// Is this condition already compiled?
if (is_string($this->{$qb_key}[$i]))
if (is_string($qbkey))
{
continue;
}
elseif ($this->{$qb_key}[$i]['escape'] === false)
elseif ($qbkey['escape'] === false)
{
$this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
$qbkey = $qbkey['condition'];
continue;
}

// Split multiple conditions
$conditions = preg_split(
'/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i', $this->{$qb_key}[$i]['condition'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
'/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i', $qbkey['condition'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);

for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci ++)
foreach ($conditions as &$condition)
{
if (($op = $this->getOperator($conditions[$ci])) === false
|| ! preg_match('/^(\(?)(.*)(' . preg_quote($op, '/') . ')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches)
if (($op = $this->getOperator($condition)) === false
|| ! preg_match('/^(\(?)(.*)(' . preg_quote($op, '/') . ')\s*(.*(?<!\)))?(\)?)$/i', $condition, $matches)
)
{
continue;
Expand Down Expand Up @@ -3112,11 +3113,11 @@ protected function compileWhereHaving(string $qb_key): string
$matches[4] = ' ' . $matches[4];
}

$conditions[$ci] = $matches[1] . $this->db->protectIdentifiers(trim($matches[2]))
$condition = $matches[1] . $this->db->protectIdentifiers(trim($matches[2]))
. ' ' . trim($matches[3]) . $matches[4] . $matches[5];
}

$this->{$qb_key}[$i] = implode('', $conditions);
$qbkey = implode('', $conditions);
}

return ($qb_key === 'QBHaving' ? "\nHAVING " : "\nWHERE ")
Expand All @@ -3143,16 +3144,16 @@ protected function compileGroupBy(): string
{
if (! empty($this->QBGroupBy))
{
for ($i = 0, $c = count($this->QBGroupBy); $i < $c; $i ++)
foreach ($this->QBGroupBy as &$groupBy)
{
// Is it already compiled?
if (is_string($this->QBGroupBy[$i]))
if (is_string($groupBy))
{
continue;
}

$this->QBGroupBy[$i] = ($this->QBGroupBy[$i]['escape'] === false ||
$this->isLiteral($this->QBGroupBy[$i]['field'])) ? $this->QBGroupBy[$i]['field'] : $this->db->protectIdentifiers($this->QBGroupBy[$i]['field']);
$groupBy = ($groupBy['escape'] === false ||
$this->isLiteral($groupBy['field'])) ? $groupBy['field'] : $this->db->protectIdentifiers($groupBy['field']);
}

return "\nGROUP BY " . implode(', ', $this->QBGroupBy);
Expand All @@ -3178,14 +3179,14 @@ protected function compileOrderBy(): string
{
if (is_array($this->QBOrderBy) && ! empty($this->QBOrderBy))
{
for ($i = 0, $c = count($this->QBOrderBy); $i < $c; $i ++)
foreach ($this->QBOrderBy as &$orderBy)
{
if ($this->QBOrderBy[$i]['escape'] !== false && ! $this->isLiteral($this->QBOrderBy[$i]['field']))
if ($orderBy['escape'] !== false && ! $this->isLiteral($orderBy['field']))
{
$this->QBOrderBy[$i]['field'] = $this->db->protectIdentifiers($this->QBOrderBy[$i]['field']);
$orderBy['field'] = $this->db->protectIdentifiers($orderBy['field']);
}

$this->QBOrderBy[$i] = $this->QBOrderBy[$i]['field'] . $this->QBOrderBy[$i]['direction'];
$orderBy = $orderBy['field'] . $orderBy['direction'];
}

return $this->QBOrderBy = "\nORDER BY " . implode(', ', $this->QBOrderBy);
Expand Down