@@ -451,16 +451,26 @@ public function validateClauseOrder($parser, $list)
451
451
452
452
/**
453
453
* For tracking JOIN clauses in a query
454
- * 0 - JOIN not found till now
455
- * 1 - JOIN has been found
456
- * 2 - A Non-JOIN clause has been found
457
- * after a previously found JOIN clause.
454
+ * = 0 - JOIN not found till now
455
+ * > 0 - Index of first JOIN clause in the statement
458
456
*
459
457
* @var int
460
458
*/
461
- $ joinStart = 0 ;
459
+ $ minJoin = 0 ;
460
+
461
+ /**
462
+ * For tracking JOIN clauses in a query
463
+ * = 0 - JOIN not found till now
464
+ * > 0 - Index of last JOIN clause
465
+ * (which appears together with other JOINs)
466
+ * in the statement
467
+ *
468
+ * @var int
469
+ */
470
+ $ maxJoin = 0 ;
462
471
463
472
$ error = 0 ;
473
+ $ lastIdx = 0 ;
464
474
foreach ($ clauses as $ clauseType => $ index ) {
465
475
$ clauseStartIdx = Utils \Query::getClauseStartOffset (
466
476
$ this ,
@@ -470,17 +480,19 @@ public function validateClauseOrder($parser, $list)
470
480
471
481
// Handle ordering of Multiple Joins in a query
472
482
if ($ clauseStartIdx != -1 ) {
473
- if ($ joinStart == 0 && stripos ($ clauseType , 'JOIN ' ) !== false ) {
474
- $ joinStart = 1 ;
475
- } elseif ($ joinStart == 1 && stripos ($ clauseType , 'JOIN ' ) === false ) {
476
- $ joinStart = 2 ;
477
- } elseif ($ joinStart == 2 && stripos ($ clauseType , 'JOIN ' ) !== false ) {
483
+ if ($ minJoin === 0 && stripos ($ clauseType , 'JOIN ' )) {
484
+ // First JOIN clause is detected
485
+ $ minJoin = $ maxJoin = $ clauseStartIdx ;
486
+ } elseif ($ minJoin !== 0 && ! stripos ($ clauseType , 'JOIN ' )) {
487
+ // After a previous JOIN clause, a non-JOIN clause has been detected
488
+ $ maxJoin = $ lastIdx ;
489
+ } elseif ($ maxJoin < $ clauseStartIdx && stripos ($ clauseType , 'JOIN ' )) {
478
490
$ error = 1 ;
479
491
}
480
492
}
481
493
482
494
if ($ clauseStartIdx != -1 && $ clauseStartIdx < $ minIdx ) {
483
- if ($ joinStart == 0 || ( $ joinStart == 2 && $ error = 1 ) ) {
495
+ if ($ minJoin === 0 || $ error === 1 ) {
484
496
$ token = $ list ->tokens [$ clauseStartIdx ];
485
497
$ parser ->error (
486
498
'Unexpected ordering of clauses. ' ,
@@ -493,6 +505,8 @@ public function validateClauseOrder($parser, $list)
493
505
} elseif ($ clauseStartIdx != -1 ) {
494
506
$ minIdx = $ clauseStartIdx ;
495
507
}
508
+
509
+ $ lastIdx = ($ clauseStartIdx !== -1 ) ? $ clauseStartIdx : $ lastIdx ;
496
510
}
497
511
498
512
return true ;
0 commit comments