Skip to content

Commit a0cc583

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 e85eaae commit a0cc583

8 files changed

Lines changed: 532 additions & 339 deletions

File tree

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

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ public function fetchAll( $mode = null, $class_name = null, $constructor_args =
6565
}
6666
return $this->fetchAllRows( $mode, $class_name, $constructor_args );
6767
}
68+
69+
/**
70+
* Get metadata for a column in a result set.
71+
*
72+
* @param int $column The index of the column (0-indexed).
73+
* @return array|false The column metadata as an associative array,
74+
* or false if the column does not exist.
75+
*/
76+
#[ReturnTypeWillChange]
77+
public function getColumnMeta( $column ) {
78+
return $this->getColumnMetadata( $column );
79+
}
6880
}
6981
} else {
7082
/**
@@ -95,6 +107,21 @@ public function setFetchMode( $mode, ...$args ): bool {
95107
public function fetchAll( $mode = PDO::FETCH_DEFAULT, ...$args ): array {
96108
return $this->fetchAllRows( $mode, ...$args );
97109
}
110+
111+
/**
112+
* Get metadata for a column in a result set.
113+
*
114+
* @param int $column The index of the column (0-indexed).
115+
* @return array|false The column metadata as an associative array,
116+
* or false if the column does not exist.
117+
*/
118+
#[ReturnTypeWillChange]
119+
public function getColumnMeta( int $column ) {
120+
if ( $column < 0 ) {
121+
throw new ValueError( 'PDOStatement::getColumnMeta(): Argument #1 ($column) must be greater than or equal to 0' );
122+
}
123+
return $this->getColumnMetadata( $column );
124+
}
98125
}
99126
}
100127

@@ -129,6 +156,20 @@ class WP_MySQL_On_SQLite_Statement extends PDOStatement implements IteratorAggre
129156
*/
130157
private $statement;
131158

159+
/**
160+
* Resolve MySQL-compatible metadata by column index.
161+
*
162+
* @var callable
163+
*/
164+
private $column_meta_resolver;
165+
166+
/**
167+
* Resolved MySQL-compatible metadata, keyed by column index.
168+
*
169+
* @var array<int, array|false>
170+
*/
171+
private $resolved_column_meta = array();
172+
132173
/**
133174
* The number of affected rows.
134175
*
@@ -139,13 +180,15 @@ class WP_MySQL_On_SQLite_Statement extends PDOStatement implements IteratorAggre
139180
/**
140181
* Constructor.
141182
*
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.
183+
* @param PDOStatement $statement The original PDO statement.
184+
* @param string $query The original MySQL query.
185+
* @param callable $column_meta_resolver Resolves metadata by column index.
186+
* @param int|null $affected_rows The number of affected rows.
145187
*/
146188
public function __construct(
147189
PDOStatement $statement,
148190
string $query,
191+
callable $column_meta_resolver,
149192
?int $affected_rows = null
150193
) {
151194
$this->statement = $statement;
@@ -154,7 +197,8 @@ public function __construct(
154197
if ( PHP_VERSION_ID >= 80100 ) {
155198
$this->queryString = $query;
156199
}
157-
$this->affected_rows = $affected_rows;
200+
$this->column_meta_resolver = $column_meta_resolver;
201+
$this->affected_rows = $affected_rows;
158202
}
159203

160204
/**
@@ -229,17 +273,6 @@ public function fetchObject( $class = 'stdClass', $constructorArgs = array() ) {
229273
return $this->statement->fetchObject( $class, $constructorArgs );
230274
}
231275

232-
/**
233-
* Get metadata for a column in a result set.
234-
*
235-
* @param int $column The index of the column (0-indexed).
236-
* @return array|false The column metadata as an associative array,
237-
* or false if the column does not exist.
238-
*/
239-
public function getColumnMeta( $column ): array {
240-
throw new RuntimeException( 'Not implemented' );
241-
}
242-
243276
/**
244277
* Fetch the SQLSTATE associated with the last statement operation.
245278
*
@@ -366,6 +399,26 @@ public function debugDumpParams(): ?bool {
366399
throw new RuntimeException( 'Not implemented' );
367400
}
368401

402+
/**
403+
* Get metadata for a column in a result set.
404+
*
405+
* This is used internally by the "WP_MySQL_On_SQLite_Statement_PHP_Compat" trait,
406+
* that is defined conditionally based on the current PHP version.
407+
*
408+
* @param int $column The index of the column (0-indexed).
409+
* @return array|false The column metadata as an associative array,
410+
* or false if the column does not exist.
411+
*/
412+
private function getColumnMetadata( $column ) {
413+
if ( ! array_key_exists( $column, $this->resolved_column_meta ) ) {
414+
$this->resolved_column_meta[ $column ] = call_user_func(
415+
$this->column_meta_resolver,
416+
$column
417+
);
418+
}
419+
return $this->resolved_column_meta[ $column ];
420+
}
421+
369422
/**
370423
* Fetch all remaining rows from the result set.
371424
*

0 commit comments

Comments
 (0)