Skip to content

Commit 665ecdb

Browse files
committed
Merge branch 'cbalda-1.1' into 1.1
2 parents f7428c2 + 50fbd93 commit 665ecdb

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/SQLParser/Node/Expression.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,21 @@ public function walk(VisitorInterface $visitor)
208208
$node = $result;
209209
}
210210
if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) {
211-
foreach ($this->subTree as $key => $operand) {
212-
$result2 = $operand->walk($visitor);
211+
if (is_array($this->subTree)) {
212+
foreach ($this->subTree as $key => $operand) {
213+
$result2 = $operand->walk($visitor);
214+
if ($result2 === NodeTraverser::REMOVE_NODE) {
215+
unset($this->subTree[$key]);
216+
} elseif ($result2 instanceof NodeInterface) {
217+
$this->subTree[$key] = $result2;
218+
}
219+
}
220+
} else {
221+
$result2 = $this->subTree->walk($visitor);
213222
if ($result2 === NodeTraverser::REMOVE_NODE) {
214-
unset($this->subTree[$key]);
223+
$this->subTree = [];
215224
} elseif ($result2 instanceof NodeInterface) {
216-
$this->subTree[$key] = $result2;
225+
$this->subTree = $result2;
217226
}
218227
}
219228
}

src/SQLParser/Node/NodeFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
namespace SQLParser\Node;
3434

3535
use Mouf\Database\MagicQueryException;
36+
use Mouf\Database\MagicQueryParserException;
3637
use SQLParser\SqlRenderInterface;
3738
use Doctrine\DBAL\Connection;
3839
use Mouf\MoufManager;
@@ -631,6 +632,12 @@ public static function simplify($nodes)
631632
$instance->setLeftOperand($leftOperand);
632633
$instance->setRightOperand($rightOperand);
633634

635+
$leftOperand = array_shift($operands);
636+
$rightOperand = array_shift($operands);
637+
$instance = new self::$OPERATOR_TO_CLASS[$operation]();
638+
$instance->setLeftOperand($leftOperand);
639+
$instance->setRightOperand($rightOperand);
640+
634641
return $instance;
635642
} elseif (isset(self::$OPERATOR_TO_CLASS[$operation]) && is_subclass_of(self::$OPERATOR_TO_CLASS[$operation], 'SQLParser\Node\AbstractManyInstancesOperator')) {
636643
$instance = new self::$OPERATOR_TO_CLASS[$operation]();

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function testParseError()
158158
$magicQuery->build($sql);
159159
}
160160

161-
public function testMagicJoin()
161+
private function getSchema()
162162
{
163163
$schema = new Schema();
164164
$role = $schema->createTable('role');
@@ -175,6 +175,12 @@ public function testMagicJoin()
175175
$role_right->addForeignKeyConstraint($schema->getTable('role'), array('role_id'), array('id'), array('onUpdate' => 'CASCADE'));
176176
$role_right->addForeignKeyConstraint($schema->getTable('right'), array('right_id'), array('id'), array('onUpdate' => 'CASCADE'));
177177
$role_right->setPrimaryKey(['role_id', 'right_id']);
178+
return $schema;
179+
}
180+
181+
public function testMagicJoin()
182+
{
183+
$schema = $this->getSchema();
178184

179185
$schemaAnalyzer = new SchemaAnalyzer(new StubSchemaManager($schema));
180186

@@ -185,6 +191,19 @@ public function testMagicJoin()
185191
$this->assertEquals($expectedSql, self::simplifySql($magicQuery->build($sql)));
186192
}
187193

194+
public function testMagicJoinNodeWalker()
195+
{
196+
$schema = $this->getSchema();
197+
198+
$schemaAnalyzer = new SchemaAnalyzer(new StubSchemaManager($schema));
199+
200+
$magicQuery = new MagicQuery(null, null, $schemaAnalyzer);
201+
202+
$sql = "SELECT role.* FROM magicjoin(role) WHERE role.label = 'my_role' AND (right.label LIKE '%test%' OR right.label LIKE '%testBis%')";
203+
$expectedSql = "SELECT role.* FROM role LEFT JOIN role_right ON (role_right.role_id = role.id) LEFT JOIN right ON (role_right.right_id = right.id) WHERE (role.label = 'my_role') AND (((right.label LIKE '%test%') OR (right.label LIKE '%testBis%')))";
204+
$this->assertEquals($expectedSql, self::simplifySql($magicQuery->build($sql)));
205+
}
206+
188207
public function testMagicJoin2()
189208
{
190209
$schema = new Schema();
@@ -300,4 +319,4 @@ private static function simplifySql($sql)
300319

301320
return $sql;
302321
}
303-
}
322+
}

0 commit comments

Comments
 (0)