Skip to content

Commit 6ac705c

Browse files
committed
Now, MagicQuery will assume that we are asking for the main table if no table name is passed.
1 parent 58d3dab commit 6ac705c

File tree

4 files changed

+22
-36
lines changed

4 files changed

+22
-36
lines changed

src/Mouf/Database/MagicQuery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ private function magicJoin(NodeInterface $select) {
154154
private function magicJoinOnOneQuery(MagicJoinSelect $magicJoinSelect) {
155155
$tableSearchNodeTraverser = new NodeTraverser();
156156
$detectTableVisitor = new DetectTablesVisitor();
157+
$detectTableVisitor->setDefaultTable($magicJoinSelect->getMainTable());
157158
$tableSearchNodeTraverser->addVisitor($detectTableVisitor);
158159

159160
$select = $magicJoinSelect->getSelect();

src/SQLParser/Node/Traverser/DetectTablesVisitor.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/**
1010
* This visitor is in charge of detecting references to tables in columns.
11-
* It will throw an exception if a column does not specify a table name.
11+
* Also, it will modify all references to tables that have no table specified by adding the $defaultTable
1212
*/
1313
class DetectTablesVisitor implements VisitorInterface
1414
{
@@ -17,6 +17,8 @@ class DetectTablesVisitor implements VisitorInterface
1717

1818
private $tables = array();
1919

20+
private $defaultTable;
21+
2022
/**
2123
* Removes all detected magic join selects.
2224
* Useful for reusing the visitor instance on another node traversal.
@@ -26,6 +28,18 @@ public function resetVisitor() {
2628
$this->isSelectVisited = false;
2729
}
2830

31+
/**
32+
* Sets the default table that will be used if no table is specified.
33+
*
34+
* @param string $defaultTable
35+
*/
36+
public function setDefaultTable($defaultTable)
37+
{
38+
$this->defaultTable = $defaultTable;
39+
}
40+
41+
42+
2943
/**
3044
* Return the list of tables referenced in the Select.
3145
* @return string[] The key and the value are the table name.
@@ -54,9 +68,7 @@ public function enterNode(NodeInterface $node)
5468
}
5569
} elseif ($node instanceof ColRef) {
5670
if (empty($node->getTable())) {
57-
$e = new MissingTableRefException("All column references should be in the form 'table.column'. Table part is missing for column '".$node->getColumn()."'");
58-
$e->setMissingTableColRef($node);
59-
throw $e;
71+
$node->setTable($this->defaultTable);
6072
}
6173
$this->tables[$node->getTable()] = $node->getTable();
6274
}

src/SQLParser/Node/Traverser/MissingTableRefException.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/SQLParser/Node/Traverser/DetectTableVisitorTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ public function testWrappedSelect()
5656
$visitor->resetVisitor();
5757
}
5858

59-
/**
60-
* @expectedException \SQLParser\Node\Traverser\MissingTableRefException
61-
*/
62-
public function testMissingTableException()
59+
60+
public function testMissingRefTable()
6361
{
6462
$visitor = new DetectTablesVisitor();
63+
$visitor->setDefaultTable('yop');
6564
$nodeTraverser = new NodeTraverser();
6665
$nodeTraverser->addVisitor($visitor);
6766

@@ -71,5 +70,7 @@ public function testMissingTableException()
7170
$parsed = $parser->parse($sql);
7271
$select = StatementFactory::toObject($parsed);
7372
$nodeTraverser->walk($select);
73+
74+
// TODO check that tata is scoped in yop
7475
}
7576
}

0 commit comments

Comments
 (0)