Skip to content

Commit bd01571

Browse files
authored
Improve Expression::getDebugQuery() performance (#1224)
1 parent e2c22d2 commit bd01571

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/Persistence/Sql/Expression.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ abstract class Expression implements Expressionable, \ArrayAccess
2424
{
2525
use DiContainerTrait;
2626

27+
private static ?SqlFormatter $debugFormatter = null;
28+
2729
/** "[]" in template, escape as parameter */
2830
protected const ESCAPE_PARAM = 'param';
2931
/** "{}" in template, escape as identifier */
@@ -434,11 +436,14 @@ function ($matches) use ($params, &$i) {
434436
return $k;
435437
}, $sql);
436438

437-
$sqlFormatter = new SqlFormatter(new NullHighlighter());
438-
$sql = $sqlFormatter->format($sql);
439+
if (self::$debugFormatter === null) {
440+
self::$debugFormatter = new SqlFormatter(new NullHighlighter());
441+
}
442+
443+
$sql = self::$debugFormatter->format($sql);
439444

440445
// fix string literal tokenize 2/2
441-
$sql = str_replace(array_keys($origStringTokens), $origStringTokens, $sqlFormatter->format($sql));
446+
$sql = str_replace(array_keys($origStringTokens), $origStringTokens, $sql);
442447
}
443448

444449
return $sql;

src/Schema/Migrator.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,14 @@ protected function fixTableNameForListMethod(string $tableName): string
426426

427427
public function introspectTableToModel(string $tableName): Model
428428
{
429-
$columns = $this->createSchemaManager()->listTableColumns($this->fixTableNameForListMethod($tableName));
429+
$schemaManager = $this->createSchemaManager();
430+
431+
$columns = $schemaManager->listTableColumns($this->fixTableNameForListMethod($tableName));
430432
if ($columns === []) {
431433
$this->assertTableExists($tableName);
432434
}
433435

434-
$indexes = $this->createSchemaManager()->listTableIndexes($this->fixTableNameForListMethod($tableName));
436+
$indexes = $schemaManager->listTableIndexes($this->fixTableNameForListMethod($tableName));
435437
$primaryIndexes = array_filter($indexes, static fn ($v) => $v->isPrimary() && count($v->getColumns()) === 1);
436438
if (count($primaryIndexes) !== 1) {
437439
throw (new Exception('Table must contain exactly one primary key'))

0 commit comments

Comments
 (0)