Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for UNION ALL and UNION DISTINCT #89

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading