Skip to content

Commit

Permalink
WIP add parsed colum/select
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 23, 2024
1 parent b5eeca9 commit 07c8457
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Persistence/Sql/Optimizer/ParsedColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Atk4\Data\Persistence\Sql\Optimizer;

use Atk4\Data\Persistence\Sql\Expression;

class ParsedColumn
{
/** @var Expression|string */
public $expr;
/** @var string|null not-null iff expr is a string */
public $exprTableAlias;
/** @var string */
public $columnAlias;

public function __construct(Expression $expr, string $columnAlias)
{
$exprIdentifier = Util::tryParseIdentifier($expr);
if ($exprIdentifier !== false) {
$this->exprTableAlias = $exprIdentifier[0];
$this->expr = $exprIdentifier[1];
} else {
$this->expr = $expr;
}

$this->columnAlias = Util::parseSingleIdentifier($columnAlias);
}

public function getDsqlExpression(): Expression
{
if ($this->exprTableAlias !== null) {
return new Expression('{}.{} {}', [$this->expr, $this->exprTableAlias, $this->columnAlias]);

Check failure on line 34 in src/Persistence/Sql/Optimizer/ParsedColumn.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

Instantiated class Atk4\Data\Persistence\Sql\Expression is abstract.

Check failure on line 34 in src/Persistence/Sql/Optimizer/ParsedColumn.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

Instantiated class Atk4\Data\Persistence\Sql\Expression is abstract.
}

return new Expression('{} {}', [$this->expr, $this->columnAlias]);

Check failure on line 37 in src/Persistence/Sql/Optimizer/ParsedColumn.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

Instantiated class Atk4\Data\Persistence\Sql\Expression is abstract.

Check failure on line 37 in src/Persistence/Sql/Optimizer/ParsedColumn.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

Instantiated class Atk4\Data\Persistence\Sql\Expression is abstract.
}
}
43 changes: 43 additions & 0 deletions src/Persistence/Sql/Optimizer/ParsedSelect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Atk4\Data\Persistence\Sql\Optimizer;

use Atk4\Data\Persistence\Sql\Expression;
use Atk4\Data\Persistence\Sql\Query;

class ParsedSelect
{
/** @var string */
public const TOP_QUERY_ALIAS = '__atk4_top_query__';

/** @var Query|string */
public $expr;
/** @var string */
public $tableAlias;

/**
* @param Query|string $expr
*/
public function __construct($expr, string $tableAlias)
{
$exprIdentifier = Util::tryParseIdentifier($expr);
if ($exprIdentifier !== false) {
$this->expr = Util::parseSingleIdentifier($expr);
} else {
$this->expr = $expr;
}

$this->tableAlias = Util::parseSingleIdentifier($tableAlias);
}

// public function getDsqlExpression(): Expression
// {
// if ($this->tableAlias === self::TOP_QUERY_ALIAS) {
// return new Expression('{}', [$this->expr]);
// }
//
// return new Expression('{} {}', [$this->expr, $this->tableAlias]);
// }
}
28 changes: 28 additions & 0 deletions src/Persistence/Sql/Optimizer/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,32 @@ public static function parseSingleIdentifier($expr): string

return $v[1];
}

public static function parseSelectQuery(Query $query, string $tableAlias): ParsedSelect
{
$query->args['is_select_parsed'] = [true];
$select = new ParsedSelect($query, $tableAlias);
if (is_string($select->expr)) {
return $select;
}

// traverse $query and parse everything into ParsedSelect/ParsedColumn
foreach ($query->args as $argK => $argV) {

}

return $select;
}

public static function isSelectQueryParsed(Query $query): bool
{
return ($query->args['is_select_parsed'] ?? [])[0] ?? false;
}

public static function parseColumn(Expression $expr, string $columnAlias): ParsedColumn
{
$column = new ParsedColumn($expr, $columnAlias);

return $column;
}
}
19 changes: 19 additions & 0 deletions src/Persistence/Sql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,12 +954,31 @@ public function __debugInfo(): array

// {{{ Miscelanious

protected function toParsedSelect(): Optimizer\ParsedSelect
{
return Optimizer\Util::parseSelectQuery($this, Optimizer\ParsedSelect::TOP_QUERY_ALIAS);
}

/**
* Deduplicate same subselects, field rereads to produce optimized select query
* using CTE/WITH.
*/
protected function transformSelectUsingCte(): Optimizer\ParsedSelect
{
throw new Exception('Not implemented yet');
}

/**
* @return array{string, array<string, mixed>}
*/
private function callParentRender(): array
{
$firstRender = parent::render();
if ($this->mode === 'select' && !Optimizer\Util::isSelectQueryParsed($this)) {
$parsedSelect = $this->toParsedSelect();
$firstRender = $parsedSelect->expr->render();
}

if (($this->args['first_render'] ?? null) === null) {
$this->args['first_render'] = $firstRender;
$secondRender = $this->render();
Expand Down

0 comments on commit 07c8457

Please sign in to comment.