Skip to content

Quote predicate names as identifiers where fragment quoting renders broken SQL - #244

Merged
simon-mundy merged 9 commits into
php-db:0.6.xfrom
simon-mundy:fix/predicate-identifier
Nov 17, 2026
Merged

simon-mundy merged 9 commits into
php-db:0.6.xfrom
simon-mundy:fix/predicate-identifier

Conversation

@simon-mundy

@simon-mundy simon-mundy commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Merge order: builds on #241's shared IdentifierQuoter, so merge #241 first. Until then this diff also shows #241's changes.

Problem

A predicate's left-hand name is rendered through AbstractPlatform::quoteIdentifierInFragment(). It splits the name on every character outside the platform's word class and quotes each piece, except spaces, dots, * and AS. A name that holds any other delimiter therefore renders as a different, invalid identifier:

Call Today (SQL-92) After
where->equalTo('x-y', 1) "x""-""y" = '1' "x-y" = '1'
where(['t-1.x-y' => 1]) "t""-""1"."x""-""y" = '1' "t-1"."x-y" = '1'
where->like('a"b', …) "a""'""b" LIKE … "a'b" LIKE …
where->isNull('nâme') "n""\xC3""\xA2""me" IS NULL (split bytes) "nâme" IS NULL
having->greaterThan('my col', 1) "my" "col" > '1' "my col" > '1'

This affects these predicates in WHERE and HAVING on Select, and in WHERE on Update and Delete:

  • equalTo, notEqualTo
  • lessThan, lessThanOrEqualTo, greaterThan, greaterThanOrEqualTo
  • like, notLike
  • in, notIn
  • between, notBetween
  • isNull, isNotNull
  • where-array keys

Cause

AbstractSql::processExpression() renders every Argument\Identifier through quoteIdentifierInFragment(), and so does processIdentifiersArgument() for Argument\Identifiers. Fragment quoting is meant for loose SQL, not for a single name. Select columns (#241), ORDER BY names (#243) and Update SET columns (#238) have the same cause.

Fix

Both now quote with IdentifierQuoter::quote(), the shared rule added in #241:

  • Plain and dotted names take a fast path and render exactly as before.
  • Names that fragment quoting renders as written, such as price*qty, a * b and x AS y, keep fragment quoting.
  • A name that fragment quoting would split is quoted whole per qualifier part.
  • Expression syntax that no identifier quoting renders as written throws InvalidArgumentException. Examples are a - b, COUNT(id), and a+b on a platform where - is a word character. An empty name or qualifier part throws too.

The check asks the platform itself, so phpdb-mysql, whose fragment pattern treats - as a word character, keeps `a-b` * `c` unchanged.

The same rule also applies to the other Identifier arguments rendered through processExpression(): identifier-typed Expression parameters and DDL names. DDL names should always be quoted whole; that is listed as a follow-up in #241.

What stays the same

  • Plain names, dotted names, t.*, price*qty, a * b, t.a * t.b, x as y / x AS y and a,b render exactly as before.
  • Expression, Literal and string predicates are untouched, and so are values and string join ON clauses, which call quoteIdentifierInFragment() directly with extra safe words.
  • No public or protected API changes. The new exceptions are named constructors on Sql\Exception\InvalidArgumentException.

Behaviour change

Output changes only for names that rendered invalid SQL before. Those that now throw (expression syntax and empty names) used to render broken SQL that only failed at the database. That includes the empty name, which rendered as nothing, e.g. = '1'.

Tests

test/unit/Sql/PredicateIdentifierNameTest.php (#[Group('unit')]) uses data providers to cover:

  • broken forms now quoted as one identifier: hyphen, space, quote character, non-ASCII, tab, dotted hyphen, dotted space and hyphenated qualified star. Each runs across every predicate type above, plus where-array keys (with a value and with null), HAVING, Update WHERE and Delete WHERE;
  • valid forms proven byte-identical across the same paths;
  • an Argument::identifiers() list;
  • the shared TrustingHyphenWordPlatform;
  • rejected names: COUNT(id), a - b, x-y * 2, a+b on the hyphen platform, '' and a..b;
  • Expression, Literal and string predicates left unchanged.

Checks:

  • The full suite passes.
  • mago format --check, mago lint and mago analyze exit 0 on PHP 8.3, 8.4 and 8.5, and so does mago guard.

…roken SQL

Predicate identifiers render through quoteIdentifierInFragment(), which
quotes every piece split out on a non-word character other than a space,
dot, * or AS. A name such as x-y, a"b, nâme or my col therefore renders
as adjacent quoted pieces or a bare sequence of identifiers.

Such names are now quoted whole as an identifier chain, split only on the
qualifier dot. Every name whose fragment quoting renders valid SQL keeps
its current rendering, judged against the platform's own fragment pattern.
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Replace quoteIdentifierArgument() with the shared IdentifierQuoter, so
identifier arguments follow the same rule as columns and ORDER BY names.
Expression syntax that no identifier quoting renders as written, such as
a+b or COUNT(id), and an empty name now throw InvalidArgumentException
instead of rendering broken SQL.
tyrsson
tyrsson previously approved these changes Nov 17, 2026
@simon-mundy
simon-mundy dismissed tyrsson’s stale review November 17, 2026 05:24

The merge-base changed after approval.

tyrsson
tyrsson previously approved these changes Nov 17, 2026
@simon-mundy
simon-mundy dismissed tyrsson’s stale review November 17, 2026 05:29

The merge-base changed after approval.

@simon-mundy simon-mundy added this to the 0.6.0 milestone Nov 17, 2026
@simon-mundy
simon-mundy merged commit 8b240cd into php-db:0.6.x Nov 17, 2026
15 checks passed
simon-mundy added a commit to simon-mundy/phpdb that referenced this pull request Nov 17, 2026
PredicateIdentifierNameTest from php-db#244 pinned the old SQL-92 escaping of
an embedded " as '. With this PR the quote is doubled, so a"b renders
"a""b" in every predicate slot.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants