Skip to content

Commit a5e45e7

Browse files
committed
Add support for PDO::ATTR_DEFAULT_FETCH_MODE, simplify attribute handling
1 parent 6cb102d commit a5e45e7

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

tests/WP_PDO_MySQL_On_SQLite_PDO_API_Tests.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,26 @@ public function test_fetch( $query, $mode, $expected ): void {
313313
}
314314
}
315315

316+
public function test_attr_default_fetch_mode(): void {
317+
$this->driver->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NUM );
318+
$result = $this->driver->query( "SELECT 'a', 'b', 'c'" );
319+
$this->assertSame(
320+
array( 'a', 'b', 'c' ),
321+
$result->fetch()
322+
);
323+
324+
$this->driver->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC );
325+
$result = $this->driver->query( "SELECT 'a', 'b', 'c'" );
326+
$this->assertSame(
327+
array(
328+
'a' => 'a',
329+
'b' => 'b',
330+
'c' => 'c',
331+
),
332+
$result->fetch()
333+
);
334+
}
335+
316336
public function data_pdo_fetch_methods(): Generator {
317337
// PDO::FETCH_BOTH
318338
yield 'PDO::FETCH_BOTH' => array(

wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -443,18 +443,6 @@ class WP_PDO_MySQL_On_SQLite extends PDO {
443443
*/
444444
private $information_schema_builder;
445445

446-
/**
447-
* PDO API: The PDO attributes of the connection.
448-
*
449-
* TODO: Add PDO default attribute values.
450-
*
451-
* @var array<int, mixed>
452-
*/
453-
private $pdo_attributes = array(
454-
// On PHP < 8.1, PDO::ATTR_STRINGIFY_FETCHES is enabled by default.
455-
PDO::ATTR_STRINGIFY_FETCHES => PHP_VERSION_ID < 80100 ? true : false,
456-
);
457-
458446
/**
459447
* Last executed MySQL query.
460448
*
@@ -787,7 +775,7 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
787775

788776
// When the default FETCH_BOTH is not set explicitly, additional
789777
// arguments are ignored, and the argument count is not validated.
790-
$fetch_mode = PDO::FETCH_BOTH;
778+
$fetch_mode = $this->connection->get_pdo()->getAttribute( PDO::ATTR_DEFAULT_FETCH_MODE );
791779
$fetch_mode_args = array();
792780
} elseif ( PDO::FETCH_COLUMN === $fetch_mode ) {
793781
if ( 3 !== $arg_count ) {
@@ -1014,24 +1002,31 @@ public function inTransaction(): bool {
10141002
/**
10151003
* PDO API: Set a PDO attribute.
10161004
*
1005+
* TODO: Evaluate whether we should pass all PDO attributes to the PDO SQLite
1006+
* instance, or whether some of them require special handling.
1007+
* See: https://github.com/php/php-src/blob/b391c28f903536e3bc6a0021ae0976ddbc2745f8/ext/pdo/php_pdo_driver.h#L103
1008+
*
10171009
* @param int $attribute The attribute to set.
10181010
* @param mixed $value The value of the attribute.
10191011
* @return bool True on success, false on failure.
10201012
*/
10211013
public function setAttribute( $attribute, $value ): bool {
1022-
$this->pdo_attributes[ $attribute ] = $value;
1023-
return true;
1014+
return $this->connection->get_pdo()->setAttribute( $attribute, $value );
10241015
}
10251016

10261017
/**
10271018
* PDO API: Get a PDO attribute.
10281019
*
1020+
* TODO: Evaluate whether we should get all PDO attributes from the PDO SQLite
1021+
* instance, or whether some of them require special handling.
1022+
* See: https://github.com/php/php-src/blob/b391c28f903536e3bc6a0021ae0976ddbc2745f8/ext/pdo/php_pdo_driver.h#L103
1023+
*
10291024
* @param int $attribute The attribute to get.
10301025
* @return mixed The value of the attribute.
10311026
*/
10321027
#[ReturnTypeWillChange]
10331028
public function getAttribute( $attribute ) {
1034-
return $this->pdo_attributes[ $attribute ] ?? null;
1029+
return $this->connection->get_pdo()->getAttribute( $attribute );
10351030
}
10361031

10371032
/**

0 commit comments

Comments
 (0)