Skip to content

Commit

Permalink
Add support for UNION ALL and UNION DISTINCT
Browse files Browse the repository at this point in the history
The code now supports UNION ALL and UNION DISTINCT statements in addition to the standard UNION. The MagicQueryTest.php file has been updated with added test cases to reflect these changes. Union.php and StatementFactory.php were modified to handle the additional logic required for the new union types.
  • Loading branch information
Zheness committed Mar 1, 2024
1 parent 5107ebf commit 68ad7c3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/SQLParser/Query/StatementFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ public static function toObject(array $desc)
}

return $select;
} elseif (isset($desc['UNION'])) {
}
// UNION and UNION DISTINCT have similar behavior
if (isset($desc['UNION']) || isset($desc['UNION ALL']) || isset($desc['UNION DISTINCT'])) {
$isUnionAll = isset($desc['UNION ALL']);
$unionStatement = $desc['UNION'] ?? ($desc['UNION ALL'] ?? $desc['UNION DISTINCT']);

/** @var Select[] $selects */
$selects = array_map([self::class, 'toObject'], $desc['UNION']);
$selects = array_map([self::class, 'toObject'], $unionStatement);

$union = new Union($selects);
$union = new Union($selects, $isUnionAll);

if (isset($desc['0']) && isset($desc['0']['ORDER'])) {
$order = NodeFactory::mapArrayToNodeObjectList($desc['0']['ORDER']);
Expand All @@ -105,9 +110,9 @@ public static function toObject(array $desc)
}

return $union;
} else {
throw new \BadMethodCallException('Unknown query');
}

throw new \BadMethodCallException('Unknown query');
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/SQLParser/Query/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ class Union implements StatementInterface, NodeInterface
*/
private $selects;

/**
* @var bool
*/
private $isUnionAll;

/**
* Union constructor.
* @param Select[] $selects
*/
public function __construct(array $selects)
public function __construct(array $selects, bool $isUnionAll)
{
$this->selects = $selects;
$this->isUnionAll = $isUnionAll;
}

/** @var NodeInterface[]|NodeInterface */
Expand Down Expand Up @@ -105,7 +111,9 @@ public function toSql(array $parameters, AbstractPlatform $platform, int $indent
return $select->toSql($parameters, $platform, $indent, $conditionsMode, $extrapolateParameters);
}, $this->selects);

$sql = '(' . implode(') UNION (', $selectsSql) . ')';
$unionStatement = $this->isUnionAll ? 'UNION ALL' : 'UNION';

$sql = '(' . implode(') ' . $unionStatement . ' (', $selectsSql) . ')';

if (!empty($this->order)) {
$order = NodeFactory::toSql($this->order, $platform, $parameters, ',', false, $indent + 2, $conditionsMode, $extrapolateParameters);
Expand Down
6 changes: 6 additions & 0 deletions tests/Mouf/Database/MagicQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ public function testStandardSelect()
$sql = 'SELECT a FROM users UNION SELECT a FROM users';
$this->assertEquals('(SELECT a FROM users) UNION (SELECT a FROM users)', self::simplifySql($magicQuery->build($sql)));

$sql = 'SELECT a FROM users UNION ALL SELECT a FROM users';
$this->assertEquals('(SELECT a FROM users) UNION ALL (SELECT a FROM users)', self::simplifySql($magicQuery->build($sql)));

$sql = 'SELECT a FROM users UNION DISTINCT SELECT a FROM users';
$this->assertEquals('(SELECT a FROM users) UNION (SELECT a FROM users)', self::simplifySql($magicQuery->build($sql)));

$sql = 'SELECT a FROM users u, users u2';
$this->assertEquals('SELECT a FROM users u CROSS JOIN users u2', self::simplifySql($magicQuery->build($sql)));

Expand Down

0 comments on commit 68ad7c3

Please sign in to comment.