Skip to content

Add PostgreSQL support and bIdent() identifier quoting - #17

Merged
ellisgl merged 1 commit into
mainfrom
postgresql-support
Nov 25, 2026
Merged

ellisgl merged 1 commit into
mainfrom
postgresql-support

Conversation

@ellisgl

@ellisgl ellisgl commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Summary

The docs quote names with MySQL backticks, and sql() only understood MySQL's rules for strings and comments. This PR adds portable identifier quoting and PostgreSQL rules for sql().

Identifier quoting

  • New Statement::bIdent() fills a %% placeholder with quoted names: `users` for MySQL, "users" for PostgreSQL.
  • 'db.users.id' quotes each part, 'users.*' leaves the star unquoted, and an array gives a column list.
  • Quote characters inside a name are doubled. Empty names, empty parts and NUL bytes throw InvalidArgumentException.
  • New IdentifierBindingInterface, with QuotedIdentifierBindings (any quote characters, e.g. [ ] for SQL Server), MySQLIdentifierBindings and PostgreSQLIdentifierBindings.

PostgreSQL dialect

  • New Dialect enum and PostgreSQLBindingFactory. Statement reads the dialect from its Bindings.
  • With Dialect::PostgreSQL, sql():
    • treats a backslash in '...' as a plain character, but as an escape in E'...'
    • treats $$...$$ and $tag$...$tag$ as strings, and "..." as an identifier
    • treats -- as a comment without needing a space after it, and lets /* */ comments nest
    • doesn't treat # as a comment, so #> and #>> work
    • reads ?? as a literal ?, for the jsonb ?, ?| and ?& operators
  • Bindings takes optional $identifier and $dialect arguments, both defaulting to MySQL, so existing code is unchanged.

Bug fix

  • Raw (%%) values containing $1 or \ were read as regex back references when they were put into the SQL. They're now inserted as is.

Known limits from PDO

PDO scans the SQL again before sending it, and its scanner doesn't follow every PostgreSQL rule. Both limits are documented in the README.

  • On PHP 8.3, a ? inside a dollar-quoted string fails with "mixed named and positional parameters". PHP 8.4 and newer handle it.
  • On every PHP version, PDO ends a comment at the first */, so a ? in the outer comment after a nested one closes is read as a placeholder.

Testing

  • New unit tests that need no database: IdentifierBindingsTest and StatementPostgreSQLPlaceholderTest.
  • New GLPDO2PostgreSQLTest runs against a real server and is skipped unless PGHOST is set. The two dollar-quote tests require PHP 8.4.
  • Ran locally against PostgreSQL 17 on PHP 8.5 and PHP 8.3; all pass, with the two 8.4-only tests skipped on 8.3. PHPStan level 8 and PSR-12 are clean.
  • CI now runs a postgres:17 service alongside MySQL. The MySQL integration tests weren't run locally because no MySQL server was available.

🤖 Generated with Claude Code

- Statement::bIdent() quotes table, column and database names for the
  database in use ('db.users.id', 'users.*', arrays for column lists).
- IdentifierBindingInterface with QuotedIdentifierBindings,
  MySQLIdentifierBindings and PostgreSQLIdentifierBindings.
- Dialect enum and PostgreSQLBindingFactory. With the PostgreSQL dialect,
  sql() reads standard '...' strings, E'...' strings, dollar quoting,
  "..." identifiers, "--" comments and nesting block comments; "#" is not
  a comment, and "??" is a literal "?" for the jsonb operators.
- Bindings takes optional identifier and dialect arguments, defaulting to
  MySQL, so existing code is unchanged.
- Fix raw ('%%') values containing '$1' or '\' being read as regex back
  references.
- PostgreSQL integration tests, run in CI against a postgres:17 service.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ellisgl
ellisgl merged commit 3787056 into main Nov 25, 2026
5 checks passed
@ellisgl
ellisgl deleted the postgresql-support branch November 25, 2026 17:32
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.

1 participant