Skip to content

Commit 7913c0b

Browse files
committed
Add check for prevention of wrong order errors in using Multiple JOINs
Signed-off-by: Deven Bansod <[email protected]>
1 parent a1fdf68 commit 7913c0b

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/Statement.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,20 +457,48 @@ public function validateClauseOrder($parser, $list)
457457
}
458458

459459
$minIdx = -1;
460+
461+
/**
462+
* For tracking JOIN clauses in a query
463+
* 0 - JOIN not found till now
464+
* 1 - JOIN has been found
465+
* 2 - A Non-JOIN clause has been found
466+
* after a previously found JOIN clause
467+
*
468+
* @var int $joinStart
469+
*/
470+
$joinStart = 0;
471+
472+
$error = 0;
460473
foreach ($clauses as $clauseType => $index) {
461474
$clauseStartIdx = Utils\Query::getClauseStartOffset(
462475
$this,
463476
$list,
464477
$clauseType
465478
);
466479

480+
// Handle ordering of Multiple Joins in a query
481+
if ($clauseStartIdx != -1) {
482+
if ($joinStart == 0 && stripos($clauseType, 'JOIN')) {
483+
$joinStart = 1;
484+
} elseif ($joinStart == 1 && ! stripos($clauseType, 'JOIN')) {
485+
$joinStart = 2;
486+
} elseif ($joinStart == 2 && stripos($clauseType, 'JOIN')) {
487+
$error = 1;
488+
}
489+
}
490+
467491
if ($clauseStartIdx != -1 && $clauseStartIdx < $minIdx) {
468-
$token = $list->tokens[$clauseStartIdx];
469-
$parser->error(
470-
__('Unexpected ordering of clauses.'),
471-
$token
472-
);
473-
return false;
492+
if ($joinStart == 0 || ($joinStart == 2 && $error = 1)) {
493+
$token = $list->tokens[$clauseStartIdx];
494+
$parser->error(
495+
__('Unexpected ordering of clauses.'),
496+
$token
497+
);
498+
return false;
499+
} else {
500+
$minIdx = $clauseStartIdx;
501+
}
474502
} elseif ($clauseStartIdx != -1) {
475503
$minIdx = $clauseStartIdx;
476504
}

0 commit comments

Comments
 (0)