diff --git a/src/Statement.php b/src/Statement.php index 28de6125..d63d86a0 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -9,6 +9,7 @@ use function array_flip; use function array_keys; +use function array_push; use function count; use function in_array; use function stripos; @@ -457,6 +458,28 @@ public function getClauses() return static::$CLAUSES; } + /** + * Gets the clause order of this statement as an array + * with clause as key and index as value. + * + * @return array + */ + public function getClauseOrder(): array + { + $clauses = []; + foreach (array_keys($this->getClauses()) as $key) { + if ($key === '_END_OPTIONS') { + if (static::$END_OPTIONS !== []) { + array_push($clauses, ...array_keys(static::$END_OPTIONS)); + } + } else { + $clauses[] = $key; + } + } + + return array_flip($clauses); + } + /** * Builds the string representation of this statement. * diff --git a/src/Utils/Query.php b/src/Utils/Query.php index f5b5320b..ee4a39e7 100644 --- a/src/Utils/Query.php +++ b/src/Utils/Query.php @@ -641,7 +641,7 @@ public static function getClause($statement, $list, $clause, $type = 0, $skipFir /** * The clauses of this type of statement and their index. */ - $clauses = array_flip(array_keys($statement->getClauses())); + $clauses = $statement->getClauseOrder(); /** * Lexer used for lexing the clause. diff --git a/tests/Utils/QueryTest.php b/tests/Utils/QueryTest.php index 62981ed0..a6250503 100644 --- a/tests/Utils/QueryTest.php +++ b/tests/Utils/QueryTest.php @@ -600,6 +600,16 @@ public function testReplaceClause(): void 'ORDER BY city' ) ); + + $parser = new Parser('SELECT * FROM `t` FOR UPDATE'); + $this->assertEquals( + 'SELECT * FROM `t` LIMIT 0, 25 FOR UPDATE', + Query::replaceClause( + $parser->statements[0], + $parser->list, + 'LIMIT 0, 25' + ) + ); } public function testReplaceClauseOnlyKeyword(): void