Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Possibility to append expressions to query output
- Loading branch information
1 parent
f4eacb4
commit be43e4b
Showing
6 changed files
with
383 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of sad_spirit/pg_gateway package | ||
* | ||
* (c) Alexey Borzov <avb@php.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace sad_spirit\pg_gateway\fragments\target_list; | ||
|
||
use sad_spirit\pg_gateway\{ | ||
Condition, | ||
TableLocator, | ||
fragments\TargetListManipulator | ||
}; | ||
use sad_spirit\pg_builder\nodes\{ | ||
Identifier, | ||
TargetElement, | ||
lists\TargetList | ||
}; | ||
|
||
/** | ||
* Adds an expression created by Condition to the TargetList | ||
*/ | ||
class ConditionAppender extends TargetListManipulator | ||
{ | ||
private Condition $condition; | ||
private ?string $alias; | ||
|
||
public function __construct(Condition $condition, ?string $alias = null) | ||
{ | ||
$this->condition = $condition; | ||
$this->alias = $alias; | ||
} | ||
|
||
public function modifyTargetList(TargetList $targetList): void | ||
{ | ||
$targetList[] = new TargetElement( | ||
$this->condition->generateExpression(), | ||
null === $this->alias ? null : new Identifier($this->alias) | ||
); | ||
} | ||
|
||
public function getKey(): ?string | ||
{ | ||
$conditionKey = $this->condition->getKey(); | ||
$aliasKey = null === $this->alias ? '' : '.' . TableLocator::hash($this->alias); | ||
|
||
return null === $conditionKey | ||
? null | ||
: 'output.' . $conditionKey . $aliasKey; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of sad_spirit/pg_gateway package | ||
* | ||
* (c) Alexey Borzov <avb@php.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace sad_spirit\pg_gateway\fragments\target_list; | ||
|
||
use sad_spirit\pg_gateway\{ | ||
TableLocator, | ||
exceptions\LogicException, | ||
fragments\TargetListManipulator | ||
}; | ||
use sad_spirit\pg_builder\Parser; | ||
use sad_spirit\pg_builder\nodes\{ | ||
Identifier, | ||
TargetElement, | ||
lists\TargetList | ||
}; | ||
|
||
/** | ||
* Parses the given SQL string and merge()s it to the TargetList | ||
*/ | ||
class SqlStringAppender extends TargetListManipulator | ||
{ | ||
private Parser $parser; | ||
private string $sql; | ||
private ?string $alias; | ||
|
||
public function __construct(Parser $parser, string $sql, ?string $alias = null) | ||
{ | ||
$this->parser = $parser; | ||
$this->sql = $sql; | ||
$this->alias = $alias; | ||
} | ||
|
||
public function modifyTargetList(TargetList $targetList): void | ||
{ | ||
$parsed = $this->parser->parseTargetList($this->sql); | ||
|
||
if (null === $this->alias) { | ||
$targetList->merge($parsed); | ||
} elseif (1 === \count($parsed)) { | ||
$targetList[] = new TargetElement(clone $parsed[0]->expression, new Identifier($this->alias)); | ||
} else { | ||
throw new LogicException("Parsing resulted in multiple expressions, cannot apply alias"); | ||
} | ||
} | ||
|
||
public function getKey(): ?string | ||
{ | ||
return TableLocator::hash([self::class, $this->sql, $this->alias]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of sad_spirit/pg_gateway package | ||
* | ||
* (c) Alexey Borzov <avb@php.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace sad_spirit\pg_gateway\tests\fragments\target_list; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use sad_spirit\pg_gateway\fragments\target_list\ConditionAppender; | ||
use sad_spirit\pg_gateway\tests\NormalizeWhitespace; | ||
use sad_spirit\pg_gateway\tests\assets\ConditionImplementation; | ||
use sad_spirit\pg_builder\Select; | ||
use sad_spirit\pg_builder\StatementFactory; | ||
use sad_spirit\pg_builder\nodes\expressions\KeywordConstant; | ||
|
||
class ConditionAppenderTest extends TestCase | ||
{ | ||
use NormalizeWhitespace; | ||
|
||
public function testKeyIsNullIfConditionKeyIsNull(): void | ||
{ | ||
$manipulator = new ConditionAppender( | ||
new ConditionImplementation(new KeywordConstant(KeywordConstant::FALSE), null) | ||
); | ||
|
||
$this::assertNull($manipulator->getKey()); | ||
} | ||
|
||
public function testKeyDependsOnConditionKey(): void | ||
{ | ||
$manipulator = new ConditionAppender( | ||
new ConditionImplementation(new KeywordConstant(KeywordConstant::FALSE), 'some_key') | ||
); | ||
|
||
$this::assertNotNull($manipulator->getKey()); | ||
$this::assertStringContainsString('some_key', $manipulator->getKey()); | ||
} | ||
|
||
public function testKeyDependsOnAlias(): void | ||
{ | ||
$manipulatorOne = new ConditionAppender( | ||
new ConditionImplementation(new KeywordConstant(KeywordConstant::FALSE), 'some_key'), | ||
'alias_one' | ||
); | ||
$manipulatorTwo = new ConditionAppender( | ||
new ConditionImplementation(new KeywordConstant(KeywordConstant::FALSE), 'some_key'), | ||
'alias_two' | ||
); | ||
$manipulatorThree = new ConditionAppender( | ||
new ConditionImplementation(new KeywordConstant(KeywordConstant::FALSE), 'some_key'), | ||
'alias_two' | ||
); | ||
|
||
$this::assertNotEquals($manipulatorOne->getKey(), $manipulatorTwo->getKey()); | ||
$this::assertEquals($manipulatorTwo->getKey(), $manipulatorThree->getKey()); | ||
} | ||
|
||
public function testModifyTargetList(): void | ||
{ | ||
$factory = new StatementFactory(); | ||
|
||
/** @var Select $select */ | ||
$select = $factory->createFromString('select self.foo as bar, quux.xyzzy'); | ||
(new ConditionAppender( | ||
new ConditionImplementation(new KeywordConstant(KeywordConstant::NULL)), | ||
'baz' | ||
))->modifyTargetList($select->list); | ||
|
||
(new ConditionAppender( | ||
new ConditionImplementation(new KeywordConstant(KeywordConstant::TRUE)), | ||
))->modifyTargetList($select->list); | ||
|
||
$this::assertStringEqualsStringNormalizingWhitespace( | ||
'select self.foo as bar, quux.xyzzy, null as baz, true', | ||
$factory->createFromAST($select)->getSql() | ||
); | ||
} | ||
} |
Oops, something went wrong.