Skip to content

Commit

Permalink
Improve performance of quoting column names up to 10% using `array_ma…
Browse files Browse the repository at this point in the history
…p()`
  • Loading branch information
Tigrov committed Oct 4, 2023
1 parent c0ced2f commit b82cb14
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/QueryBuilder/AbstractDMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ public function batchInsert(string $table, array $columns, iterable $rows, array
return '';
}

foreach ($columns as $i => $name) {
$columns[$i] = $this->quoter->quoteColumnName($name);
}
$columns = array_map(
[$this->quoter, 'quoteColumnName'],
$columns,
);

return 'INSERT INTO ' . $this->quoter->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
Expand Down Expand Up @@ -298,9 +299,10 @@ protected function prepareUpsertColumns(
/** @psalm-var array<string, mixed> $insertColumns */
$insertNames = $this->getNormalizeColumnNames(array_keys($insertColumns));

Check warning on line 300 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractDMLQueryBuilder.php#L300

Added line #L300 was not covered by tests

foreach ($insertNames as $i => $name) {
$insertNames[$i] = $this->quoter->quoteColumnName($name);
}
$insertNames = array_map(
[$this->quoter, 'quoteColumnName'],
$insertNames,
);

Check warning on line 305 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractDMLQueryBuilder.php#L302-L305

Added lines #L302 - L305 were not covered by tests
}

/** @psalm-var string[] $uniqueNames */
Expand Down Expand Up @@ -374,13 +376,13 @@ static function (Constraint $constraint): string {
array_filter(
$constraints,
static function (Constraint $constraint) use ($quoter, $columns, &$columnNames): bool {

Check warning on line 378 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractDMLQueryBuilder.php#L378

Added line #L378 was not covered by tests
/** @psalm-var string[] $getColumnNames */
$getColumnNames = (array) $constraint->getColumnNames();
$constraintColumnNames = [];
/** @psalm-var string[] $constraintColumnNames */
$constraintColumnNames = (array) $constraint->getColumnNames();

Check warning on line 380 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractDMLQueryBuilder.php#L380

Added line #L380 was not covered by tests

foreach ($getColumnNames as $columnName) {
$constraintColumnNames[] = $quoter->quoteColumnName($columnName);
}
$constraintColumnNames = array_map(
[$quoter, 'quoteColumnName'],
$constraintColumnNames,
);

Check warning on line 385 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractDMLQueryBuilder.php#L382-L385

Added lines #L382 - L385 were not covered by tests

$result = empty(array_diff($constraintColumnNames, $columns));

Check warning on line 387 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractDMLQueryBuilder.php#L387

Added line #L387 was not covered by tests

Expand All @@ -406,9 +408,9 @@ static function (Constraint $constraint) use ($quoter, $columns, &$columnNames):
*/
protected function normalizeColumnNames(array $columns): array
{
/** @var string[] $columnList */
$columnList = array_keys($columns);
$normalizedNames = $this->getNormalizeColumnNames($columnList);
/** @var string[] $columnNames */
$columnNames = array_keys($columns);
$normalizedNames = $this->getNormalizeColumnNames($columnNames);

return array_combine($normalizedNames, $columns);
}
Expand Down

0 comments on commit b82cb14

Please sign in to comment.