Skip to content

Commit d86c9f3

Browse files
committed
perf: optimize _processForeignKeys()
Refactors _processForeignKeys() to: - calculate invariant SQL fragments outside the loop - cache escaped table identifier - build SQL string locally before concatenating to the array, saving intermediate string allocations
1 parent 19cae6e commit d86c9f3

1 file changed

Lines changed: 30 additions & 25 deletions

File tree

‎system/Database/Forge.php‎

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,8 @@ protected function _attributeUnique(array &$attributes, array &$field)
10721072
*/
10731073
protected function _attributeAutoIncrement(array &$attributes, array &$field)
10741074
{
1075-
if (! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true
1075+
if (
1076+
! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === true
10761077
&& str_contains(strtolower($field['type']), 'int')
10771078
) {
10781079
$field['auto_increment'] = ' AUTO_INCREMENT';
@@ -1105,7 +1106,7 @@ protected function _processPrimaryKeys(string $table, bool $asQuery = false): st
11051106
$sql .= 'CONSTRAINT ' . $this->db->escapeIdentifiers(($this->primaryKeys['keyName'] === '' ?
11061107
'pk_' . $table :
11071108
$this->primaryKeys['keyName']))
1108-
. ' PRIMARY KEY(' . implode(', ', $this->db->escapeIdentifiers($this->primaryKeys['fields'])) . ')';
1109+
. ' PRIMARY KEY(' . implode(', ', $this->db->escapeIdentifiers($this->primaryKeys['fields'])) . ')';
11091110
}
11101111

11111112
return $sql;
@@ -1225,40 +1226,44 @@ protected function _processForeignKeys(string $table, bool $asQuery = false): ar
12251226
throw new DatabaseException(lang('Database.fieldNotExists', $errorNames));
12261227
}
12271228

1228-
$sqls = [''];
1229+
$sqls = [''];
1230+
$isOci8 = $this->db->DBDriver === 'OCI8';
1231+
$dbPrefix = $this->db->DBPrefix;
1232+
$fkSuffix = $isOci8 ? '_fk' : '_foreign';
12291233

1230-
foreach ($this->foreignKeys as $index => $fkey) {
1231-
if ($asQuery === false) {
1232-
$index = 0;
1233-
} else {
1234-
$sqls[$index] = '';
1235-
}
1234+
// Niezmienne fragmenty SQL przygotowane przed pętlą
1235+
$prefixSql = $asQuery
1236+
? 'ALTER TABLE ' . $this->db->escapeIdentifiers($dbPrefix . $table) . ' ADD '
1237+
: ",\n\t";
12361238

1237-
$nameIndex = $fkey['fkName'] !== '' ?
1238-
$fkey['fkName'] :
1239-
$table . '_' . implode('_', $fkey['field']) . ($this->db->DBDriver === 'OCI8' ? '_fk' : '_foreign');
1240-
1241-
$nameIndexFilled = $this->db->escapeIdentifiers($nameIndex);
1242-
$foreignKeyFilled = implode(', ', $this->db->escapeIdentifiers($fkey['field']));
1243-
$referenceTableFilled = $this->db->escapeIdentifiers($this->db->DBPrefix . $fkey['referenceTable']);
1244-
$referenceFieldFilled = implode(', ', $this->db->escapeIdentifiers($fkey['referenceField']));
1239+
foreach ($this->foreignKeys as $index => $fkey) {
1240+
$idx = $asQuery ? $index : 0;
12451241

12461242
if ($asQuery) {
1247-
$sqls[$index] .= 'ALTER TABLE ' . $this->db->escapeIdentifiers($this->db->DBPrefix . $table) . ' ADD ';
1248-
} else {
1249-
$sqls[$index] .= ",\n\t";
1243+
$sqls[$idx] = '';
12501244
}
12511245

1252-
$formatSql = 'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)';
1253-
$sqls[$index] .= sprintf($formatSql, $nameIndexFilled, $foreignKeyFilled, $referenceTableFilled, $referenceFieldFilled);
1246+
$nameIndex = $fkey['fkName'] !== ''
1247+
? $fkey['fkName']
1248+
: $table . '_' . implode('_', $fkey['field']) . $fkSuffix;
1249+
1250+
$sql = $prefixSql . sprintf(
1251+
'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)',
1252+
$this->db->escapeIdentifiers($nameIndex),
1253+
implode(', ', $this->db->escapeIdentifiers($fkey['field'])),
1254+
$this->db->escapeIdentifiers($dbPrefix . $fkey['referenceTable']),
1255+
implode(', ', $this->db->escapeIdentifiers($fkey['referenceField'])),
1256+
);
12541257

12551258
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $this->fkAllowActions, true)) {
1256-
$sqls[$index] .= ' ON DELETE ' . $fkey['onDelete'];
1259+
$sql .= ' ON DELETE ' . $fkey['onDelete'];
12571260
}
12581261

1259-
if ($this->db->DBDriver !== 'OCI8' && $fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $this->fkAllowActions, true)) {
1260-
$sqls[$index] .= ' ON UPDATE ' . $fkey['onUpdate'];
1262+
if (! $isOci8 && $fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $this->fkAllowActions, true)) {
1263+
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
12611264
}
1265+
1266+
$sqls[$idx] .= $sql;
12621267
}
12631268

12641269
return $sqls;

0 commit comments

Comments
 (0)