Skip to content

Commit 45ce309

Browse files
committed
Fix join with a subquery
fixes thecodingmachine#74
1 parent 7efa677 commit 45ce309

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

src/SQLParser/Node/NodeFactory.php

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -150,38 +150,7 @@ public static function toObject(array $desc)
150150
$expr->setTable($desc['table']);
151151
}
152152

153-
154-
155-
$expr->setTable(str_replace('`', '', $desc['table']));
156-
switch ($desc['join_type']) {
157-
case 'CROSS':
158-
$joinType = 'CROSS JOIN';
159-
break;
160-
case 'JOIN':
161-
$joinType = 'JOIN';
162-
break;
163-
case 'LEFT':
164-
$joinType = 'LEFT JOIN';
165-
break;
166-
case 'RIGHT':
167-
$joinType = 'RIGHT JOIN';
168-
break;
169-
case 'INNER':
170-
$joinType = 'INNER JOIN';
171-
break;
172-
case 'OUTER':
173-
$joinType = 'OUTER JOIN';
174-
break;
175-
case 'NATURAL':
176-
$joinType = 'NATURAL JOIN';
177-
break;
178-
case ',':
179-
$joinType = ',';
180-
break;
181-
default:
182-
throw new \Exception("Unexpected join type: '".$desc['join_type']."'");
183-
}
184-
$expr->setJoinType($joinType);
153+
$expr->setJoinType(self::mapJoinType($desc['join_type']));
185154

186155
if (isset($desc['alias']['name'])) {
187156
$expr->setAlias($desc['alias']['name']);
@@ -222,7 +191,7 @@ public static function toObject(array $desc)
222191
$expr->setSubQuery(self::buildFromSubtree($desc['sub_tree']));
223192

224193
if (isset($desc['join_type'])) {
225-
$expr->setJoinType($desc['join_type']);
194+
$expr->setJoinType(self::mapJoinType($desc['join_type']));
226195
}
227196

228197
if (isset($desc['alias']['name'])) {
@@ -908,4 +877,28 @@ public static function toSql($nodes, AbstractPlatform $platform, array $paramete
908877

909878
return $sql;
910879
}
880+
881+
private static function mapJoinType(string $originalJoinType): string
882+
{
883+
switch ($originalJoinType) {
884+
case 'CROSS':
885+
return 'CROSS JOIN';
886+
case 'JOIN':
887+
return 'JOIN';
888+
case 'LEFT':
889+
return 'LEFT JOIN';
890+
case 'RIGHT':
891+
return 'RIGHT JOIN';
892+
case 'INNER':
893+
return 'INNER JOIN';
894+
case 'OUTER':
895+
return 'OUTER JOIN';
896+
case 'NATURAL':
897+
return 'NATURAL JOIN';
898+
case ',':
899+
return ',';
900+
default:
901+
throw new \Exception("Unexpected join type: '".$originalJoinType."'");
902+
}
903+
}
911904
}

0 commit comments

Comments
 (0)