Skip to content

Fix replace clause of select statement with FOR UPDATE #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, int>
*/
public function getClauseOrder(): array
{
$clauses = [];
foreach (array_keys($this->getClauses()) as $key) {
if ($key === '_END_OPTIONS') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When merging in master, this key changes name

if (static::$END_OPTIONS !== []) {
array_push($clauses, ...array_keys(static::$END_OPTIONS));
}
Comment on lines +472 to +474
Copy link
Contributor Author

@MoonE MoonE Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When merging to master (PHP >= 7.3) the if is no long needed

Suggested change
if (static::$END_OPTIONS !== []) {
array_push($clauses, ...array_keys(static::$END_OPTIONS));
}
array_push($clauses, ...array_keys(static::$END_OPTIONS));

https://www.php.net/manual/en/function.array-push.php

} else {
$clauses[] = $key;
}
}

return array_flip($clauses);
}

/**
* Builds the string representation of this statement.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions tests/Utils/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down