Skip to content

ext/pdo_sqlite: add ATTR_SQL and ATTR_EXPANDED_SQL statement attributes#21456

Open
iliaal wants to merge 2 commits intophp:masterfrom
iliaal:fix/gh-21322-pdo-sqlite-attr-sql
Open

ext/pdo_sqlite: add ATTR_SQL and ATTR_EXPANDED_SQL statement attributes#21456
iliaal wants to merge 2 commits intophp:masterfrom
iliaal:fix/gh-21322-pdo-sqlite-attr-sql

Conversation

@iliaal
Copy link
Contributor

@iliaal iliaal commented Mar 15, 2026

Summary

Implements #21322 -- adds support for retrieving the SQL text of a prepared PDO SQLite statement via PDOStatement::getAttribute(), mirroring SQLite3Stmt::getSQL().

Two new statement attributes on Pdo\Sqlite:

  • ATTR_SQL -- returns the original SQL text (sqlite3_sql())
  • ATTR_EXPANDED_SQL -- returns SQL with bound parameters expanded (sqlite3_expanded_sql(), guarded by configure check)
$stmt = $db->prepare('SELECT :name AS greeting');
$stmt->bindValue(':name', 'hello');
$stmt->execute();

$stmt->getAttribute(Pdo\Sqlite::ATTR_SQL);          // "SELECT :name AS greeting"
$stmt->getAttribute(Pdo\Sqlite::ATTR_EXPANDED_SQL); // "SELECT 'hello' AS greeting"

Changes

  • php_pdo_sqlite_int.h -- new enum values PDO_SQLITE_ATTR_SQL, PDO_SQLITE_ATTR_EXPANDED_SQL
  • sqlite_statement.c -- handle new attributes in pdo_sqlite_stmt_get_attribute()
  • config.m4 / config.w32 -- configure check for sqlite3_expanded_sql
  • pdo_sqlite.stub.php + pdo_sqlite_arginfo.h -- PHP-visible constants
  • New test pdo_sqlite_getattr_sql.phpt, updated pdo_sqlite_constants.phpt

Feature addition is fairly small, I don't think it needs an RFC.

Implements feature request #21322

iliaal added 2 commits March 14, 2026 09:46
…ment::setFetchMode

When setFetchMode(PDO::FETCH_DEFAULT) is called, mode=0
(PDO_FETCH_USE_DEFAULT) gets stored as the statement's default fetch
type. Later, do_fetch() tries to resolve PDO_FETCH_USE_DEFAULT by
reading stmt->default_fetch_type, which is also 0 — circular
reference that on 8.4 silently fell through to FETCH_BOTH and on
master throws a ValueError.

Resolve PDO_FETCH_USE_DEFAULT to the connection-level default early in
pdo_stmt_setup_fetch_mode(), before flags extraction and the mode
switch, so the rest of the function processes the actual fetch mode.

Closes phpGH-20214
Add support for retrieving the SQL text of a prepared statement via
PDOStatement::getAttribute(), mirroring SQLite3Stmt::getSQL().

  Pdo\Sqlite::ATTR_SQL          - original SQL text (sqlite3_sql)
  Pdo\Sqlite::ATTR_EXPANDED_SQL - SQL with bound parameters expanded
                                   (sqlite3_expanded_sql)

Closes phpGH-21322
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant