Skip to content

Commit 3bb9bb3

Browse files
committed
Implement PDO and PDOStatement getAttribute() and setAttribute()
1 parent 3523415 commit 3bb9bb3

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
@@ -96,6 +96,13 @@ class ValueError extends Error {
9696
class WP_PDO_Synthetic_Statement extends PDOStatement {
9797
use WP_PDO_Synthetic_Statement_PHP_Compat;
9898

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

150+
/**
151+
* The PDO attributes set for this statement.
152+
*
153+
* @var array<int, mixed>
154+
*/
155+
private $attributes = array();
156+
143157
/**
144158
* Constructor.
145159
*
160+
* @param PDO $pdo The PDO connection.
146161
* @param array $columns Basic column metadata (containing at least name, table name, and native type).
147162
* @param array $rows Rows of the result set.
148163
* @param int $affected_rows The number of affected rows.
149164
*/
150165
public function __construct(
166+
PDO $pdo,
151167
array $columns,
152168
array $rows,
153169
int $affected_rows
154170
) {
171+
$this->pdo = $pdo;
155172
$this->columns = $columns;
156173
$this->rows = $rows;
157174
$this->affected_rows = $affected_rows;
@@ -338,7 +355,7 @@ public function errorInfo(): array {
338355
*/
339356
#[ReturnTypeWillChange]
340357
public function getAttribute( $attribute ) {
341-
throw new RuntimeException( 'Not implemented' );
358+
return $this->attributes[ $attribute ] ?? $this->pdo->getAttribute( $attribute );
342359
}
343360

344361
/**
@@ -349,7 +366,8 @@ public function getAttribute( $attribute ) {
349366
* @return bool True on success, false on failure.
350367
*/
351368
public function setAttribute( $attribute, $value ): bool {
352-
throw new RuntimeException( 'Not implemented' );
369+
$this->attributes[ $attribute ] = $value;
370+
return true;
353371
}
354372

355373
/**

0 commit comments

Comments
 (0)