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

database performance improvement : use foreach() when possible #1530

Merged
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
4 changes: 2 additions & 2 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,10 +852,10 @@ protected function _alterTable($alter_type, $table, $field)
$sql .= ($alter_type === 'ADD') ? 'ADD ' : $alter_type . ' COLUMN ';

$sqls = [];
for ($i = 0, $c = count($field); $i < $c; $i++)
foreach ($field as $data)
{
$sqls[] = $sql
. ($field[$i]['_literal'] !== false ? $field[$i]['_literal'] : $this->_processColumn($field[$i]));
. ($data['_literal'] !== false ? $data['_literal'] : $this->_processColumn($data));
}

return $sqls;
Expand Down
8 changes: 4 additions & 4 deletions system/Database/MySQLi/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ protected function _alterTable($alter_type, $table, $field)
}

$sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($table);
for ($i = 0, $c = count($field); $i < $c; $i ++)
foreach ($field as $i => $data)
{
if ($field[$i]['_literal'] !== false)
if ($data['_literal'] !== false)
{
$field[$i] = ($alter_type === 'ADD') ? "\n\tADD " . $field[$i]['_literal'] : "\n\tMODIFY " . $field[$i]['_literal'];
$field[$i] = ($alter_type === 'ADD') ? "\n\tADD " . $data['_literal'] : "\n\tMODIFY " . $data['_literal'];
}
else
{
Expand All @@ -182,7 +182,7 @@ protected function _alterTable($alter_type, $table, $field)
}
else
{
$field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
$field[$i]['_literal'] = empty($data['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
}

$field[$i] = $field[$i]['_literal'] . $this->_processColumn($field[$i]);
Expand Down
12 changes: 6 additions & 6 deletions system/Database/MySQLi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public function getFieldData(): array
$retval = [];
$fieldData = $this->resultID->fetch_fields();

for ($i = 0, $c = count($fieldData); $i < $c; $i ++)
foreach ($fieldData as $i => $data)
{
$retval[$i] = new \stdClass();
$retval[$i]->name = $fieldData[$i]->name;
$retval[$i]->type = $fieldData[$i]->type;
$retval[$i]->max_length = $fieldData[$i]->max_length;
$retval[$i]->primary_key = (int) ($fieldData[$i]->flags & 2);
$retval[$i]->default = $fieldData[$i]->def;
$retval[$i]->name = $data->name;
$retval[$i]->type = $data->type;
$retval[$i]->max_length = $data->max_length;
$retval[$i]->primary_key = (int) ($data->flags & 2);
$retval[$i]->default = $data->def;
}

return $retval;
Expand Down
34 changes: 17 additions & 17 deletions system/Database/Postgre/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,42 +106,42 @@ protected function _alterTable($alter_type, $table, $field)

$sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($table);
$sqls = [];
for ($i = 0, $c = count($field); $i < $c; $i ++)
foreach ($field as $data)
{
if ($field[$i]['_literal'] !== false)
if ($data['_literal'] !== false)
{
return false;
}

if (version_compare($this->db->getVersion(), '8', '>=') && isset($field[$i]['type']))
if (version_compare($this->db->getVersion(), '8', '>=') && isset($data['type']))
{
$sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name'])
. " TYPE {$field[$i]['type']}{$field[$i]['length']}";
$sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($data['name'])
. " TYPE {$data['type']}{$data['length']}";
}

if (! empty($field[$i]['default']))
if (! empty($data['default']))
{
$sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name'])
. " SET DEFAULT {$field[$i]['default']}";
$sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($data['name'])
. " SET DEFAULT {$data['default']}";
}

if (isset($field[$i]['null']))
if (isset($data['null']))
{
$sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name'])
. ($field[$i]['null'] === true ? ' DROP' : ' SET') . ' NOT NULL';
$sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($data['name'])
. ($data['null'] === true ? ' DROP' : ' SET') . ' NOT NULL';
}

if (! empty($field[$i]['new_name']))
if (! empty($data['new_name']))
{
$sqls[] = $sql . ' RENAME COLUMN ' . $this->db->escapeIdentifiers($field[$i]['name'])
. ' TO ' . $this->db->escapeIdentifiers($field[$i]['new_name']);
$sqls[] = $sql . ' RENAME COLUMN ' . $this->db->escapeIdentifiers($data['name'])
. ' TO ' . $this->db->escapeIdentifiers($data['new_name']);
}

if (! empty($field[$i]['comment']))
if (! empty($data['comment']))
{
$sqls[] = 'COMMENT ON COLUMN' . $this->db->escapeIdentifiers($table)
. '.' . $this->db->escapeIdentifiers($field[$i]['name'])
. " IS {$field[$i]['comment']}";
. '.' . $this->db->escapeIdentifiers($data['name'])
. " IS {$data['comment']}";
}
}

Expand Down