Skip to content

Commit 0604b25

Browse files
committed
Snapshot result column metadata
Snapshot raw result metadata into each PDO statement, resolve and cache MySQL-compatible metadata lazily, and migrate consumers away from mutable driver-wide last-query metadata. Preserve the legacy metadata getters only on the deprecated WP_SQLite_Driver facade.
1 parent b832938 commit 0604b25

8 files changed

Lines changed: 473 additions & 333 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite-statement.php‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ class WP_MySQL_On_SQLite_Statement extends PDOStatement implements IteratorAggre
129129
*/
130130
private $statement;
131131

132+
/**
133+
* Resolve MySQL-compatible metadata by column index.
134+
*
135+
* @var callable
136+
*/
137+
private $column_meta_resolver;
138+
139+
/**
140+
* Resolved MySQL-compatible metadata, keyed by column index.
141+
*
142+
* @var array<int, array|false>
143+
*/
144+
private $resolved_column_meta = array();
145+
132146
/**
133147
* The number of affected rows.
134148
*
@@ -139,18 +153,24 @@ class WP_MySQL_On_SQLite_Statement extends PDOStatement implements IteratorAggre
139153
/**
140154
* Constructor.
141155
*
142-
* @param PDOStatement $statement The original PDO statement.
143-
* @param string $query The original MySQL query.
144-
* @param int|null $affected_rows The number of affected rows.
156+
* @param PDOStatement $statement The original PDO statement.
157+
* @param string $query The original MySQL query.
158+
* @param callable $column_meta_resolver Resolves metadata by column index.
159+
* @param int|null $affected_rows The number of affected rows.
145160
*/
146161
public function __construct(
147162
PDOStatement $statement,
148163
string $query,
164+
callable $column_meta_resolver,
149165
?int $affected_rows = null
150166
) {
151-
$this->statement = $statement;
152-
$this->affected_rows = $affected_rows;
153-
$this->queryString = $query;
167+
$this->statement = $statement;
168+
// Userland can only initialize PDOStatement::$queryString on PHP 8.1+.
169+
if ( PHP_VERSION_ID >= 80100 ) {
170+
$this->queryString = $query;
171+
}
172+
$this->column_meta_resolver = $column_meta_resolver;
173+
$this->affected_rows = $affected_rows;
154174
}
155175

156176
/**
@@ -232,8 +252,15 @@ public function fetchObject( $class = 'stdClass', $constructorArgs = array() ) {
232252
* @return array|false The column metadata as an associative array,
233253
* or false if the column does not exist.
234254
*/
235-
public function getColumnMeta( $column ): array {
236-
throw new RuntimeException( 'Not implemented' );
255+
#[ReturnTypeWillChange]
256+
public function getColumnMeta( $column ) {
257+
if ( ! array_key_exists( $column, $this->resolved_column_meta ) ) {
258+
$this->resolved_column_meta[ $column ] = call_user_func(
259+
$this->column_meta_resolver,
260+
$column
261+
);
262+
}
263+
return $this->resolved_column_meta[ $column ];
237264
}
238265

239266
/**

0 commit comments

Comments
 (0)