Repository navigation
Quote ORDER BY names as identifiers where fragment quoting breaks them - #243
Merged
Merged
Conversation
Select::processOrder() fragment-quoted each order name, so names such as x-y, my col, a"b or café rendered as several quoted pieces. Such names are now quoted whole as an identifier chain split only on '.'. Names whose parts between '*' and '.' already render as one identifier each, such as t.col and price*qty, render byte-for-byte as before.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Replace quoteOrderIdentifier() with IdentifierQuoter::quote(), so ORDER BY names follow the same rule as columns: "t"."as" rather than the invalid "t".as, and arithmetic mixed with a hyphenated name, such as price*x-y, now throws instead of becoming one identifier. The tests use the shared TrustingHyphenWordPlatform, replacing HyphenWordSql92Platform.
This was referenced Nov 4, 2026
tyrsson
previously approved these changes
Nov 17, 2026
simon-mundy
dismissed
tyrsson’s stale review
November 17, 2026 05:16
The merge-base changed after approval.
tyrsson
approved these changes
Nov 17, 2026
simon-mundy
added a commit
to simon-mundy/phpdb
that referenced
this pull request
Nov 17, 2026
The IdentifierQuoter, column-name and ORDER BY tests from php-db#241 and php-db#243 pinned the old SQL-92 escaping, which turned an embedded " into '. With this PR's fix the quote is doubled, so a"b now renders "a""b".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Select::order()renders some order names as broken SQL. On the SQL-92 platform:'x-y DESC'"x""-""y" DESC"x-y" DESC't-1.x-y DESC'"t""-""1"."x""-""y" DESC"t-1"."x-y" DESC['my col' => 'DESC']"my" "col" DESC"my col" DESC'a"b DESC'"a""'""b" DESC"a'b" DESC'café DESC'"café" DESC['t.as' => 'DESC']"t".as DESC"t"."as" DESCThe same happens in comma lists (
'x-y DESC, b') and in list arrays (['x-y DESC', 'b']).Cause
processOrder()passes each parsed order name toAbstractPlatform::quoteIdentifierInFragment(). That splits the name on every non-word character and quotes each piece, leaving only the safe words (*, space,.,as) bare. Select columns (#241), predicate names (#244) and Update SET columns (#238) have the same cause.Fix
processOrder()now quotes each name withIdentifierQuoter::quote(), the shared rule added in #241:price*qty, keep fragment quoting.price*x-yorCOUNT(id), throwsInvalidArgumentException, as does an empty name or qualifier part.The check asks the platform itself. phpdb-mysql treats
-as a word character, so`x-y`and`price`*`x-y`render as before there.The order-string parsing is unchanged: comma splitting, the first-space split into name and direction, ASC/DESC detection and
Expressionentries work as before.What stays the same
Every order whose output was valid renders byte-for-byte as before. The tests pin these forms:
a,t.col DESC;a DESC, b;price*qty DESC,['price * qty' => 'DESC'],['t.price*t.qty' => 'DESC'];'my col DESC', which still parses as the namemy(see the follow-up in Quote Select, join and GROUP BY column names as identifiers #241);Expressionentries, alone and in arrays;Tests
test/unit/Sql/SelectOrderNameTest.php(#[Group('unit')]) has four data-provider tests:x-y.*andt.as;TrustingHyphenWordPlatform(backtick quoting,-as a word character) and with identifier quoting off;price*x-y,COUNT(id), andx+yon the hyphen platform.The duplicate
HyphenWordSql92Platformtest asset is removed in favour of #241'sTrustingHyphenWordPlatform.mago format --check,mago lintandmago analyzeexit 0 on PHP 8.3, 8.4 and 8.5, and so doesmago guard.