Skip to content

Commit a10a005

Browse files
committed
Adding support for CASE value WHEN.. THEN... constructs
1 parent 2b293a1 commit a10a005

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

src/SQLParser/Node/NodeFactory.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ private static function buildFromSubtree($subTree)
462462
'||' => 'SQLParser\Node\OrOp',
463463
'OR' => 'SQLParser\Node\OrOp',
464464
'XOR' => 'SQLParser\Node\XorOp',
465-
'WHEN' => 'SQLParser\Node\WhenConditions',
466465
'THEN' => 'SQLParser\Node\Then',
467466
'ELSE' => 'SQLParser\Node\ElseOperation'
468467
);
@@ -567,8 +566,13 @@ public static function simplify($nodes)
567566
$tmpOperators = $selectedOperators;
568567
$nextOperator = array_shift($tmpOperators);
569568

569+
$isSelectedOperatorFirst = null;
570+
570571
foreach ($nodes as $node) {
571572
if ($node === $nextOperator) {
573+
if ($isSelectedOperatorFirst === null) {
574+
$isSelectedOperatorFirst = true;
575+
}
572576
// Let's apply the "simplify" method on the operand before storing it.
573577
//$operands[] = self::simplify($operand);
574578
$simple = self::simplify($operand);
@@ -581,6 +585,9 @@ public static function simplify($nodes)
581585
$operand = array();
582586
$nextOperator = array_shift($tmpOperators);
583587
} else {
588+
if ($isSelectedOperatorFirst === null) {
589+
$isSelectedOperatorFirst = false;
590+
}
584591
$operand[] = $node;
585592
}
586593
}
@@ -611,7 +618,6 @@ public static function simplify($nodes)
611618
array('INTERVAL'),
612619
array('BINARY', 'COLLATE'),
613620
array('!'),
614-
array('CASE', 'WHEN', 'THEN', 'ELSE'),
615621
array('NOT'),
616622
*/
617623

@@ -649,19 +655,17 @@ public static function simplify($nodes)
649655
$instance->setMaxValueOperand($maxOperand);
650656

651657
return $instance;
652-
/*} elseif ($operation === 'WHEN') {
653-
$when = array_shift($operands);
658+
} elseif ($operation === 'WHEN') {
654659

655-
$whenOperands = $when->getOperands();
656-
$condition = array_shift($whenOperands);
657-
$result = array_shift($whenOperands);
660+
$instance = new WhenConditions();
658661

659-
$instance = new WhenThen();
660-
$instance->setCondition($condition);
661-
$instance->setResult($result);
662+
if (!$isSelectedOperatorFirst) {
663+
$value = array_shift($operands);
664+
$instance->setValue($value);
665+
}
666+
$instance->setOperands($operands);
662667

663668
return $instance;
664-
*/
665669
} elseif ($operation === 'CASE') {
666670
$innerOperation = array_shift($operands);
667671

src/SQLParser/Node/WhenConditions.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,32 @@
1010
use SQLParser\Node\Traverser\VisitorInterface;
1111

1212
/**
13-
* This class represents a set of WHEN ... THEN ... construct (inside a CASE).
13+
* This class represents a set of ... WHEN ... THEN ... construct (inside a CASE).
1414
*
1515
* @author David Négrier <[email protected]>
1616
*/
1717
class WhenConditions extends AbstractManyInstancesOperator
1818
{
19+
20+
private $value;
21+
22+
public function getValue()
23+
{
24+
return $this->value;
25+
}
26+
27+
/**
28+
* Sets the value.
29+
*
30+
* @Important
31+
*
32+
* @param NodeInterface|NodeInterface[]|string $value
33+
*/
34+
public function setValue($value)
35+
{
36+
$this->value = $value;
37+
}
38+
1939
/**
2040
* Renders the object as a SQL string.
2141
*
@@ -29,6 +49,11 @@ class WhenConditions extends AbstractManyInstancesOperator
2949
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY)
3050
{
3151
$fullSql = '';
52+
53+
if ($this->value) {
54+
$fullSql = NodeFactory::toSql($this->value, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode);
55+
}
56+
3257
foreach ($this->getOperands() as $operand) {
3358
$sql = NodeFactory::toSql($operand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode);
3459
if ($sql != null) {

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public function testStandardSelect()
9292
// Test CASE WHERE
9393
$sql = "SELECT CASE WHEN status = 'on' THEN '1' WHEN status = 'off' THEN '0' ELSE '-1' END AS my_case FROM users";
9494
$this->assertEquals("SELECT CASE WHEN status = 'on' THEN '1' WHEN status = 'off' THEN '0' ELSE '-1' END AS my_case FROM users", self::simplifySql($magicQuery->build($sql)));
95+
96+
// Test CASE WHERE like SWITCH CASE
97+
$sql = "SELECT CASE status WHEN 'on' THEN '1' WHEN 'off' THEN '0' ELSE '-1' END AS my_case FROM users";
98+
$this->assertEquals("SELECT CASE status WHEN 'on' THEN '1' WHEN 'off' THEN '0' ELSE '-1' END AS my_case FROM users", self::simplifySql($magicQuery->build($sql)));
99+
95100
}
96101

97102
public function testWithCache() {

0 commit comments

Comments
 (0)