Skip to content

Commit

Permalink
Merge pull request #52 from moufmouf/having_alias_issue
Browse files Browse the repository at this point in the history
Fixing issue with alias and having
  • Loading branch information
moufmouf committed Jul 31, 2019
2 parents e6c267f + 7843b2f commit 902cf9d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/SQLParser/Node/Expression.php
Expand Up @@ -204,6 +204,9 @@ public function toInstanceDescriptor(MoufManager $moufManager)
*/
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true)
{
if (empty($this->subTree)) {
return $this->getBaseExpression();
}
$sql = NodeFactory::toSql($this->subTree, $dbConnection, $parameters, $this->delimiter, false, $indent, $conditionsMode, $extrapolateParameters);

if ($sql === null) {
Expand Down Expand Up @@ -264,6 +267,10 @@ public function walk(VisitorInterface $visitor)
*/
public function canBeBypassed(array $parameters): bool
{
if (empty($this->subTree)) {
// Some expression can (rarely) have no subtree. Don't know why.
return false;
}
if (is_iterable($this->subTree)) {
foreach ($this->subTree as $node) {
if (!$node instanceof BypassableInterface || !$node->canBeBypassed($parameters)) {
Expand Down
1 change: 1 addition & 0 deletions src/SQLParser/Node/NodeFactory.php
Expand Up @@ -389,6 +389,7 @@ public static function toObject(array $desc)
unset($desc['alias']);
unset($desc['direction']);
unset($desc['delim']);
unset($desc['no_quotes']);
if (!empty($desc)) {
error_log('MagicQuery - NodeFactory: Unexpected parameters in exception: '.var_export($desc, true));
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Mouf/Database/MagicQueryTest.php
Expand Up @@ -180,6 +180,9 @@ public function testStandardSelect()

$sql = 'SELECT a FROM users u WHERE status = (CASE WHEN u.id = 1 THEN u.status_1 ELSE u.status_2 END)';
$this->assertEquals('SELECT a FROM users AS u WHERE status = (CASE WHEN u.id = 1 THEN u.status_1 ELSE u.status_2 END)', self::simplifySql($magicQuery->build($sql)));

$sql = 'SELECT 42 as fooalias FROM bar HAVING fooalias = 24';
$this->assertEquals('SELECT 42 AS fooalias FROM bar HAVING fooalias = 24', self::simplifySql($magicQuery->build($sql)));
}

/**
Expand Down

0 comments on commit 902cf9d

Please sign in to comment.