Skip to content

Commit 6eadd3d

Browse files
committed
Implement PDO and PDOStatement getAttribute() and setAttribute()
1 parent e56b913 commit 6eadd3d

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,16 @@ 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+
);
455+
446456
/**
447457
* Last executed MySQL query.
448458
*
@@ -783,7 +793,7 @@ public function query( string $query, ?int $fetch_mode = PDO::FETCH_COLUMN, ...$
783793
$columns = is_array( $this->last_column_meta ) ? $this->last_column_meta : array();
784794
$rows = is_array( $this->last_result ) ? $this->last_result : array();
785795
$affected_rows = is_int( $this->last_return_value ) ? $this->last_return_value : 0;
786-
return new WP_PDO_Synthetic_Statement( $columns, $rows, $affected_rows );
796+
return new WP_PDO_Synthetic_Statement( $this, $columns, $rows, $affected_rows );
787797
} catch ( Throwable $e ) {
788798
try {
789799
$this->rollback_user_transaction();
@@ -880,6 +890,29 @@ public function inTransaction(): bool {
880890
return $this->connection->get_pdo()->inTransaction();
881891
}
882892

893+
/**
894+
* PDO API: Set a PDO attribute.
895+
*
896+
* @param int $attribute The attribute to set.
897+
* @param mixed $value The value of the attribute.
898+
* @return bool True on success, false on failure.
899+
*/
900+
public function setAttribute( $attribute, $value ): bool {
901+
$this->pdo_attributes[ $attribute ] = $value;
902+
return true;
903+
}
904+
905+
/**
906+
* PDO API: Get a PDO attribute.
907+
*
908+
* @param int $attribute The attribute to get.
909+
* @return mixed The value of the attribute.
910+
*/
911+
#[ReturnTypeWillChange]
912+
public function getAttribute( $attribute ) {
913+
return $this->pdo_attributes[ $attribute ] ?? null;
914+
}
915+
883916
/**
884917
* Get the SQLite connection instance.
885918
*

wp-includes/sqlite-ast/class-wp-pdo-synthetic-statement.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public function fetchAll( $mode = PDO::FETCH_DEFAULT, ...$args ): array {
8787
class WP_PDO_Synthetic_Statement extends PDOStatement {
8888
use WP_PDO_Synthetic_Statement_PHP_Compat;
8989

90+
/**
91+
* The PDO connection.
92+
*
93+
* @var PDO
94+
*/
95+
private $pdo;
96+
9097
/**
9198
* Basic column metadata (containing at least name, table name, and native type).
9299
*
@@ -131,18 +138,28 @@ class WP_PDO_Synthetic_Statement extends PDOStatement {
131138
*/
132139
private $fetch_mode_args = array();
133140

141+
/**
142+
* The PDO attributes set for this statement.
143+
*
144+
* @var array<int, mixed>
145+
*/
146+
private $attributes = array();
147+
134148
/**
135149
* Constructor.
136150
*
151+
* @param PDO $pdo The PDO connection.
137152
* @param array $columns Basic column metadata (containing at least name, table name, and native type).
138153
* @param array $rows Rows of the result set.
139154
* @param int $affected_rows The number of affected rows.
140155
*/
141156
public function __construct(
157+
PDO $pdo,
142158
array $columns,
143159
array $rows,
144160
int $affected_rows
145161
) {
162+
$this->pdo = $pdo;
146163
$this->columns = $columns;
147164
$this->rows = $rows;
148165
$this->affected_rows = $affected_rows;
@@ -329,7 +346,7 @@ public function errorInfo(): array {
329346
*/
330347
#[ReturnTypeWillChange]
331348
public function getAttribute( $attribute ) {
332-
throw new RuntimeException( 'Not implemented' );
349+
return $this->attributes[ $attribute ] ?? $this->pdo->getAttribute( $attribute );
333350
}
334351

335352
/**
@@ -340,7 +357,8 @@ public function getAttribute( $attribute ) {
340357
* @return bool True on success, false on failure.
341358
*/
342359
public function setAttribute( $attribute, $value ): bool {
343-
throw new RuntimeException( 'Not implemented' );
360+
$this->attributes[ $attribute ] = $value;
361+
return true;
344362
}
345363

346364
/**

0 commit comments

Comments
 (0)