Skip to content

Commit bacf013

Browse files
committed
Expand PDO statement result support
Initialize the original MySQL query string and delegate supported result-oriented PDOStatement operations to the wrapped SQLite statement.
1 parent 759b9f2 commit bacf013

3 files changed

Lines changed: 82 additions & 10 deletions

File tree

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* PDO uses camel case naming, enable non-snake case:
99
* phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
1010
* phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
11+
* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
1112
*
1213
* PDO uses $class as a variable name, enable it:
1314
* phpcs:disable Universal.NamingConventions.NoReservedKeywordParameterNames.classFound
@@ -118,7 +119,7 @@ public function fetchAll( $mode = PDO::FETCH_DEFAULT, ...$args ): array {
118119
* - PDO::FETCH_BOUND: bind values to PHP variables, can't be used with fetchAll()
119120
* - PDO::FETCH_FUNC: custom function, only works with fetchAll(), can't be default [1 extra arg]
120121
*/
121-
class WP_MySQL_On_SQLite_Statement extends PDOStatement {
122+
class WP_MySQL_On_SQLite_Statement extends PDOStatement implements IteratorAggregate {
122123
use WP_MySQL_On_SQLite_Statement_PHP_Compat;
123124

124125
/**
@@ -139,13 +140,20 @@ class WP_MySQL_On_SQLite_Statement extends PDOStatement {
139140
* Constructor.
140141
*
141142
* @param PDOStatement $statement The original PDO statement.
142-
* @param int $affected_rows The number of affected rows.
143+
* @param string $query The original MySQL query.
144+
* @param int|null $affected_rows The number of affected rows.
143145
*/
144146
public function __construct(
145147
PDOStatement $statement,
148+
string $query,
146149
?int $affected_rows = null
147150
) {
148-
$this->statement = $statement;
151+
$this->statement = $statement;
152+
153+
// Userland can only initialize PDOStatement::$queryString on PHP 8.1+.
154+
if ( PHP_VERSION_ID >= 80100 ) {
155+
$this->queryString = $query;
156+
}
149157
$this->affected_rows = $affected_rows;
150158
}
151159

@@ -239,7 +247,7 @@ public function getColumnMeta( $column ): array {
239247
* or null if there is no error.
240248
*/
241249
public function errorCode(): ?string {
242-
throw new RuntimeException( 'Not implemented' );
250+
return $this->statement->errorCode();
243251
}
244252

245253
/**
@@ -251,7 +259,11 @@ public function errorCode(): ?string {
251259
* 2: Driver-specific error message.
252260
*/
253261
public function errorInfo(): array {
254-
throw new RuntimeException( 'Not implemented' );
262+
// Normalize successful results. PDO_SQLite may retain stale driver-specific fields on PHP < 8.0.
263+
if ( '00000' === $this->statement->errorCode() ) {
264+
return array( '00000', null, null );
265+
}
266+
return $this->statement->errorInfo();
255267
}
256268

257269
/**
@@ -282,7 +294,7 @@ public function setAttribute( $attribute, $value ): bool {
282294
* @return Iterator The iterator for the result set.
283295
*/
284296
public function getIterator(): Iterator {
285-
throw new RuntimeException( 'Not implemented' );
297+
yield from $this->statement;
286298
}
287299

288300
/**
@@ -300,7 +312,7 @@ public function nextRowset(): bool {
300312
* @return bool True on success, false on failure.
301313
*/
302314
public function closeCursor(): bool {
303-
throw new RuntimeException( 'Not implemented' );
315+
return $this->statement->closeCursor();
304316
}
305317

306318
/**
@@ -313,8 +325,8 @@ public function closeCursor(): bool {
313325
* @param mixed $driverOptions Optional parameters for the driver.
314326
* @return bool True on success, false on failure.
315327
*/
316-
public function bindColumn( $column, &$var, $type = null, $maxLength = null, $driverOptions = null ): bool {
317-
throw new RuntimeException( 'Not implemented' );
328+
public function bindColumn( $column, &$var, $type = PDO::PARAM_STR, $maxLength = 0, $driverOptions = null ): bool {
329+
return $this->statement->bindColumn( $column, $var, $type, $maxLength, $driverOptions );
318330
}
319331

320332
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
10501050
$this->last_result_statement = $this->create_result_statement_from_data( array(), array() );
10511051
}
10521052

1053-
$stmt = new WP_MySQL_On_SQLite_Statement( $this->last_result_statement, $this->last_affected_rows );
1053+
$stmt = new WP_MySQL_On_SQLite_Statement( $this->last_result_statement, $query, $this->last_affected_rows );
10541054
$stmt->setFetchMode( $fetch_mode, ...$fetch_mode_args );
10551055
return $stmt;
10561056
} catch ( Throwable $e ) {

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,66 @@ public function test_query(): void {
161161
}
162162
}
163163

164+
public function test_statement_query_string(): void {
165+
$query = 'SELECT 1 AS value';
166+
$stmt = $this->driver->query( $query );
167+
168+
$this->assertSame( $query, $stmt->queryString );
169+
}
170+
171+
public function test_statement_error_information(): void {
172+
$stmt = $this->driver->query( 'SELECT 1' );
173+
174+
$this->assertSame( '00000', $stmt->errorCode() );
175+
$this->assertSame( array( '00000', null, null ), $stmt->errorInfo() );
176+
}
177+
178+
public function test_statement_error_information_discards_stale_sqlite_error(): void {
179+
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
180+
$pdo = new $pdo_class( 'sqlite::memory:' );
181+
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
182+
$pdo->query( 'SELECT * FROM missing_table' );
183+
184+
$stmt = new WP_MySQL_On_SQLite_Statement(
185+
$pdo->query( 'SELECT 1' ),
186+
'SELECT 1',
187+
function () {
188+
return false;
189+
}
190+
);
191+
192+
$this->assertSame( '00000', $stmt->errorCode() );
193+
$this->assertSame( array( '00000', null, null ), $stmt->errorInfo() );
194+
}
195+
196+
public function test_statement_iteration(): void {
197+
$stmt = $this->driver->query( 'SELECT 1 AS value UNION ALL SELECT 2', PDO::FETCH_ASSOC );
198+
199+
$this->assertSame(
200+
array(
201+
array( 'value' => '1' ),
202+
array( 'value' => '2' ),
203+
),
204+
iterator_to_array( $stmt )
205+
);
206+
}
207+
208+
public function test_statement_close_cursor(): void {
209+
$stmt = $this->driver->query( 'SELECT 1 UNION ALL SELECT 2' );
210+
211+
$this->assertTrue( $stmt->closeCursor() );
212+
$this->assertFalse( $stmt->fetch() );
213+
}
214+
215+
public function test_statement_bind_column(): void {
216+
$stmt = $this->driver->query( 'SELECT 1 AS value' );
217+
$value = null;
218+
219+
$this->assertTrue( $stmt->bindColumn( 'value', $value ) );
220+
$this->assertTrue( $stmt->fetch( PDO::FETCH_BOUND ) );
221+
$this->assertSame( '1', $value );
222+
}
223+
164224
/**
165225
* @dataProvider data_pdo_fetch_methods
166226
*/

0 commit comments

Comments
 (0)