Skip to content

Commit

Permalink
Improve Expression::getDebugQuery() performance (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored May 31, 2024
1 parent e2c22d2 commit bd01571
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/Persistence/Sql/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ abstract class Expression implements Expressionable, \ArrayAccess
{
use DiContainerTrait;

private static ?SqlFormatter $debugFormatter = null;

/** "[]" in template, escape as parameter */
protected const ESCAPE_PARAM = 'param';
/** "{}" in template, escape as identifier */
Expand Down Expand Up @@ -434,11 +436,14 @@ function ($matches) use ($params, &$i) {
return $k;
}, $sql);

$sqlFormatter = new SqlFormatter(new NullHighlighter());
$sql = $sqlFormatter->format($sql);
if (self::$debugFormatter === null) {
self::$debugFormatter = new SqlFormatter(new NullHighlighter());
}

$sql = self::$debugFormatter->format($sql);

// fix string literal tokenize 2/2
$sql = str_replace(array_keys($origStringTokens), $origStringTokens, $sqlFormatter->format($sql));
$sql = str_replace(array_keys($origStringTokens), $origStringTokens, $sql);
}

return $sql;
Expand Down
6 changes: 4 additions & 2 deletions src/Schema/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,14 @@ protected function fixTableNameForListMethod(string $tableName): string

public function introspectTableToModel(string $tableName): Model
{
$columns = $this->createSchemaManager()->listTableColumns($this->fixTableNameForListMethod($tableName));
$schemaManager = $this->createSchemaManager();

$columns = $schemaManager->listTableColumns($this->fixTableNameForListMethod($tableName));
if ($columns === []) {
$this->assertTableExists($tableName);
}

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

0 comments on commit bd01571

Please sign in to comment.