Skip to content

Commit bab7cd3

Browse files
committed
Implement PDO and PDOStatement getAttribute() and setAttribute()
1 parent a60eaf2 commit bab7cd3

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

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
*
@@ -775,7 +785,7 @@ public function query( string $query, ?int $fetch_mode = PDO::FETCH_COLUMN, ...$
775785
$columns = is_array( $this->last_column_meta ) ? $this->last_column_meta : array();
776786
$rows = is_array( $this->last_result ) ? $this->last_result : array();
777787
$affected_rows = is_int( $this->last_return_value ) ? $this->last_return_value : 0;
778-
return new WP_PDO_Synthetic_Statement( $columns, $rows, $affected_rows );
788+
return new WP_PDO_Synthetic_Statement( $this, $columns, $rows, $affected_rows );
779789
} catch ( Throwable $e ) {
780790
try {
781791
$this->rollback_user_transaction();
@@ -872,6 +882,29 @@ public function inTransaction(): bool {
872882
return $this->connection->get_pdo()->inTransaction();
873883
}
874884

885+
/**
886+
* PDO API: Set a PDO attribute.
887+
*
888+
* @param int $attribute The attribute to set.
889+
* @param mixed $value The value of the attribute.
890+
* @return bool True on success, false on failure.
891+
*/
892+
public function setAttribute( $attribute, $value ): bool {
893+
$this->pdo_attributes[ $attribute ] = $value;
894+
return true;
895+
}
896+
897+
/**
898+
* PDO API: Get a PDO attribute.
899+
*
900+
* @param int $attribute The attribute to get.
901+
* @return mixed The value of the attribute.
902+
*/
903+
#[ReturnTypeWillChange]
904+
public function getAttribute( $attribute ) {
905+
return $this->pdo_attributes[ $attribute ] ?? null;
906+
}
907+
875908
/**
876909
* Get the SQLite connection instance.
877910
*

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ class ValueError extends Error {
9595
class WP_PDO_Synthetic_Statement extends PDOStatement {
9696
use WP_PDO_Synthetic_Statement_PHP_Compat;
9797

98+
/**
99+
* The PDO connection.
100+
*
101+
* @var PDO
102+
*/
103+
private $pdo;
104+
98105
/**
99106
* Basic column metadata (containing at least name, table name, and native type).
100107
*
@@ -139,18 +146,28 @@ class WP_PDO_Synthetic_Statement extends PDOStatement {
139146
*/
140147
private $fetch_mode_args = array();
141148

149+
/**
150+
* The PDO attributes set for this statement.
151+
*
152+
* @var array<int, mixed>
153+
*/
154+
private $attributes = array();
155+
142156
/**
143157
* Constructor.
144158
*
159+
* @param PDO $pdo The PDO connection.
145160
* @param array $columns Basic column metadata (containing at least name, table name, and native type).
146161
* @param array $rows Rows of the result set.
147162
* @param int $affected_rows The number of affected rows.
148163
*/
149164
public function __construct(
165+
PDO $pdo,
150166
array $columns,
151167
array $rows,
152168
int $affected_rows
153169
) {
170+
$this->pdo = $pdo;
154171
$this->columns = $columns;
155172
$this->rows = $rows;
156173
$this->affected_rows = $affected_rows;
@@ -337,7 +354,7 @@ public function errorInfo(): array {
337354
*/
338355
#[ReturnTypeWillChange]
339356
public function getAttribute( $attribute ) {
340-
throw new RuntimeException( 'Not implemented' );
357+
return $this->attributes[ $attribute ] ?? $this->pdo->getAttribute( $attribute );
341358
}
342359

343360
/**
@@ -348,7 +365,8 @@ public function getAttribute( $attribute ) {
348365
* @return bool True on success, false on failure.
349366
*/
350367
public function setAttribute( $attribute, $value ): bool {
351-
throw new RuntimeException( 'Not implemented' );
368+
$this->attributes[ $attribute ] = $value;
369+
return true;
352370
}
353371

354372
/**

0 commit comments

Comments
 (0)