| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* The SQLite driver uses PDO. Enable PDO function calls: |
| 5 |
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO |
| 6 |
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDOStatement |
| 7 |
* |
| 8 |
* PDO uses camel case naming, enable non-snake case: |
| 9 |
* phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
| 10 |
* phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase |
| 11 |
* |
| 12 |
* PDO uses $class as a variable name, enable it: |
| 13 |
* phpcs:disable Universal.NamingConventions.NoReservedKeywordParameterNames.classFound |
| 14 |
* |
| 15 |
* Some PDOStatement methods use $var as a variable name, enable it: |
| 16 |
* phpcs:disable Universal.NamingConventions.NoReservedKeywordParameterNames.varFound |
| 17 |
* |
| 18 |
* We use traits to support different PHP versions with incompatible PDO statement |
| 19 |
* method signatures. For that, enable multiple object structures in one file: |
| 20 |
* phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* Some PDOStatement methods are not compatible across different PHP versions. |
| 25 |
* To address "Declaration of ... should be compatible with ..." PHP warnings, |
| 26 |
* we conditionally define traits with different APIs based on the PHP version. |
| 27 |
*/ |
| 28 |
if ( PHP_VERSION_ID < 80000 ) { |
| 29 |
trait WP_PDO_Proxy_Statement_PHP_Compat { |
| 30 |
/** |
| 31 |
* Set the default fetch mode for this statement. |
| 32 |
* |
| 33 |
* @param int $mode The fetch mode to set as the default. |
| 34 |
* @param mixed $params Additional parameters for the default fetch mode. |
| 35 |
* @return bool True on success, false on failure. |
| 36 |
*/ |
| 37 |
public function setFetchMode( $mode, $params = null ): bool { |
| 38 |
// Do not pass additional arguments when they are NULL to prevent |
| 39 |
// "fetch mode doesn't allow any extra arguments" error. |
| 40 |
if ( null === $params ) { |
| 41 |
return $this->setDefaultFetchMode( $mode ); |
| 42 |
} |
| 43 |
return $this->setDefaultFetchMode( $mode, $params ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Fetch all remaining rows from the result set. |
| 48 |
* |
| 49 |
* @param int $mode The fetch mode to use. |
| 50 |
* @param mixed $class_name With PDO::FETCH_CLASS, the name of the class to instantiate. |
| 51 |
* @param mixed $constructor_args With PDO::FETCH_CLASS, the parameters to pass to the class constructor. |
| 52 |
* @return array The result set as an array of rows. |
| 53 |
*/ |
| 54 |
public function fetchAll( $mode = null, $class_name = null, $constructor_args = null ): array { |
| 55 |
// Do not pass additional arguments when they are NULL to prevent |
| 56 |
// "Extraneous additional parameters" error. |
| 57 |
if ( null === $class_name && null === $constructor_args ) { |
| 58 |
return $this->fetchAllRows( $mode ); |
| 59 |
} |
| 60 |
return $this->fetchAllRows( $mode, $class_name, $constructor_args ); |
| 61 |
} |
| 62 |
} |
| 63 |
} else { |
| 64 |
trait WP_PDO_Proxy_Statement_PHP_Compat { |
| 65 |
/** |
| 66 |
* Set the default fetch mode for this statement. |
| 67 |
* |
| 68 |
* @param int $mode The fetch mode to set as the default. |
| 69 |
* @param mixed $args Additional parameters for the default fetch mode. |
| 70 |
* @return bool True on success, false on failure. |
| 71 |
*/ |
| 72 |
#[ReturnTypeWillChange] |
| 73 |
public function setFetchMode( $mode, ...$args ): bool { |
| 74 |
return $this->setDefaultFetchMode( $mode, ...$args ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Fetch all remaining rows from the result set. |
| 79 |
* |
| 80 |
* @param int $mode The fetch mode to use. |
| 81 |
* @param mixed $args Additional parameters for the fetch mode. |
| 82 |
* @return array The result set as an array of rows. |
| 83 |
*/ |
| 84 |
public function fetchAll( $mode = PDO::FETCH_DEFAULT, ...$args ): array { |
| 85 |
return $this->fetchAllRows( $mode, ...$args ); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* PDOStatement implementation that operates on in-memory data. |
| 92 |
* |
| 93 |
* This class implements a complete PDOStatement interface on top of PHP arrays. |
| 94 |
* It is used for result sets that are composed or transformed in the PHP layer. |
| 95 |
* |
| 96 |
* PDO supports the following fetch modes: |
| 97 |
* - PDO::FETCH_DEFAULT: current default fetch mode (available from PHP 8.0) |
| 98 |
* - PDO::FETCH_BOTH: default |
| 99 |
* - PDO::FETCH_NUM: numeric array |
| 100 |
* - PDO::FETCH_ASSOC: associative array |
| 101 |
* - PDO::FETCH_NAMED: associative array retaining duplicate columns |
| 102 |
* - PDO::FETCH_COLUMN: single column value [1 extra arg] |
| 103 |
* - PDO::FETCH_KEY_PAIR: key-value pair |
| 104 |
* - PDO::FETCH_OBJ: object (stdClass) |
| 105 |
* - PDO::FETCH_CLASS: object (custom class) [1-2 extra args] |
| 106 |
* - PDO::FETCH_INTO: update an exisisting object, can't be used with fetchAll() [1 extra arg] |
| 107 |
* - PDO::FETCH_LAZY: lazy fetch via PDORow, can't be used with fetchAll() |
| 108 |
* - PDO::FETCH_BOUND: bind values to PHP variables, can't be used with fetchAll() |
| 109 |
* - PDO::FETCH_FUNC: custom function, only works with fetchAll(), can't be default [1 extra arg] |
| 110 |
*/ |
| 111 |
class WP_PDO_Proxy_Statement extends PDOStatement { |
| 112 |
use WP_PDO_Proxy_Statement_PHP_Compat; |
| 113 |
|
| 114 |
/** |
| 115 |
* The original PDO statement. |
| 116 |
* |
| 117 |
* @var PDOStatement |
| 118 |
*/ |
| 119 |
private $statement; |
| 120 |
|
| 121 |
/** |
| 122 |
* The number of affected rows. |
| 123 |
* |
| 124 |
* @var int|null |
| 125 |
*/ |
| 126 |
private $affected_rows; |
| 127 |
|
| 128 |
/** |
| 129 |
* Constructor. |
| 130 |
* |
| 131 |
* @param PDOStatement $statement The original PDO statement. |
| 132 |
* @param int $affected_rows The number of affected rows. |
| 133 |
*/ |
| 134 |
public function __construct( |
| 135 |
PDOStatement $statement, |
| 136 |
?int $affected_rows = null |
| 137 |
) { |
| 138 |
$this->statement = $statement; |
| 139 |
$this->affected_rows = $affected_rows; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Execute a prepared statement. |
| 144 |
* |
| 145 |
* @param mixed $params The values to bind to the parameters of the prepared statement. |
| 146 |
* @return bool True on success, false on failure. |
| 147 |
*/ |
| 148 |
public function execute( $params = null ): bool { |
| 149 |
return $this->statement->execute( $params ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get the number of columns in the result set. |
| 154 |
* |
| 155 |
* @return int The number of columns in the result set. |
| 156 |
*/ |
| 157 |
public function columnCount(): int { |
| 158 |
return $this->statement->columnCount(); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get the number of rows affected by the statement. |
| 163 |
* |
| 164 |
* @return int The number of rows affected by the statement. |
| 165 |
*/ |
| 166 |
public function rowCount(): int { |
| 167 |
return $this->affected_rows ?? $this->statement->rowCount(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Fetch the next row from the result set. |
| 172 |
* |
| 173 |
* @param int|null $mode The fetch mode. Controls how the row is returned. |
| 174 |
* Default: PDO::FETCH_DEFAULT (null for PHP < 8.0) |
| 175 |
* @param int|null $cursorOrientation The cursor orientation. Controls which row is returned. |
| 176 |
* Default: PDO::FETCH_ORI_NEXT (null for PHP < 8.0) |
| 177 |
* @param int|null $cursorOffset The cursor offset. Controls which row is returned. |
| 178 |
* Default: 0 (null for PHP < 8.0) |
| 179 |
* @return mixed The row data formatted according to the fetch mode; |
| 180 |
* false if there are no more rows or a failure occurs. |
| 181 |
*/ |
| 182 |
#[ReturnTypeWillChange] |
| 183 |
public function fetch( |
| 184 |
$mode = 0, // PDO::FETCH_DEFAULT (available from PHP 8.0) |
| 185 |
$cursorOrientation = 0, |
| 186 |
$cursorOffset = 0 |
| 187 |
) { |
| 188 |
return $this->statement->fetch( $mode, $cursorOrientation, $cursorOffset ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Fetch a single column from the next row of a result set. |
| 193 |
* |
| 194 |
* @param int $column The index of the column to fetch (0-indexed). |
| 195 |
* @return mixed The value of the column; false if there are no more rows. |
| 196 |
*/ |
| 197 |
#[ReturnTypeWillChange] |
| 198 |
public function fetchColumn( $column = 0 ) { |
| 199 |
return $this->statement->fetchColumn( $column ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Fetch the next row as an object. |
| 204 |
* |
| 205 |
* @param string $class The name of the class to instantiate. |
| 206 |
* @param array $constructorArgs The parameters to pass to the class constructor. |
| 207 |
* @return object The next row as an object. |
| 208 |
*/ |
| 209 |
#[ReturnTypeWillChange] |
| 210 |
public function fetchObject( $class = 'stdClass', $constructorArgs = array() ) { |
| 211 |
return $this->statement->fetchObject( $class, $constructorArgs ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Get metadata for a column in a result set. |
| 216 |
* |
| 217 |
* @param int $column The index of the column (0-indexed). |
| 218 |
* @return array|false The column metadata as an associative array, |
| 219 |
* or false if the column does not exist. |
| 220 |
*/ |
| 221 |
public function getColumnMeta( $column ): array { |
| 222 |
throw new RuntimeException( 'Not implemented' ); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Fetch the SQLSTATE associated with the last statement operation. |
| 227 |
* |
| 228 |
* @return string|null The SQLSTATE error code (as defined by the ANSI SQL standard), |
| 229 |
* or null if there is no error. |
| 230 |
*/ |
| 231 |
public function errorCode(): ?string { |
| 232 |
throw new RuntimeException( 'Not implemented' ); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Fetch error information associated with the last statement operation. |
| 237 |
* |
| 238 |
* @return array The array consists of at least the following fields: |
| 239 |
* 0: SQLSTATE error code (as defined by the ANSI SQL standard). |
| 240 |
* 1: Driver-specific error code. |
| 241 |
* 2: Driver-specific error message. |
| 242 |
*/ |
| 243 |
public function errorInfo(): array { |
| 244 |
throw new RuntimeException( 'Not implemented' ); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Get a statement attribute. |
| 249 |
* |
| 250 |
* @param int $attribute The attribute to get. |
| 251 |
* @return mixed The value of the attribute. |
| 252 |
*/ |
| 253 |
#[ReturnTypeWillChange] |
| 254 |
public function getAttribute( $attribute ) { |
| 255 |
return $this->statement->getAttribute( $attribute ); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Set a statement attribute. |
| 260 |
* |
| 261 |
* @param int $attribute The attribute to set. |
| 262 |
* @param mixed $value The value of the attribute. |
| 263 |
* @return bool True on success, false on failure. |
| 264 |
*/ |
| 265 |
public function setAttribute( $attribute, $value ): bool { |
| 266 |
return $this->statement->setAttribute( $attribute, $value ); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Get result set as iterator. |
| 271 |
* |
| 272 |
* @return Iterator The iterator for the result set. |
| 273 |
*/ |
| 274 |
public function getIterator(): Iterator { |
| 275 |
throw new RuntimeException( 'Not implemented' ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Advances to the next rowset in a multi-rowset statement handle. |
| 280 |
* |
| 281 |
* @return bool True on success, false on failure. |
| 282 |
*/ |
| 283 |
public function nextRowset(): bool { |
| 284 |
throw new RuntimeException( 'Not implemented' ); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Closes the cursor, enabling the statement to be executed again. |
| 289 |
* |
| 290 |
* @return bool True on success, false on failure. |
| 291 |
*/ |
| 292 |
public function closeCursor(): bool { |
| 293 |
throw new RuntimeException( 'Not implemented' ); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Bind a column to a PHP variable. |
| 298 |
* |
| 299 |
* @param int|string $column Number of the column (1-indexed) or name of the column in the result set. |
| 300 |
* @param mixed $var PHP variable to which the column will be bound. |
| 301 |
* @param int $type Data type of the parameter, specified by the PDO::PARAM_* constants. |
| 302 |
* @param int $maxLength A hint for pre-allocation. |
| 303 |
* @param mixed $driverOptions Optional parameters for the driver. |
| 304 |
* @return bool True on success, false on failure. |
| 305 |
*/ |
| 306 |
public function bindColumn( $column, &$var, $type = null, $maxLength = null, $driverOptions = null ): bool { |
| 307 |
throw new RuntimeException( 'Not implemented' ); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Bind a parameter to a PHP variable. |
| 312 |
* |
| 313 |
* @param int|string $param Parameter identifier. Either a 1-indexed position of the parameter or a named parameter. |
| 314 |
* @param mixed $var PHP variable to which the parameter will be bound. |
| 315 |
* @param int $type Data type of the parameter, specified by the PDO::PARAM_* constants. |
| 316 |
* @param int $maxLength Length of the data type. |
| 317 |
* @param mixed $driverOptions Optional parameters for the driver. |
| 318 |
* @return bool True on success, false on failure. |
| 319 |
*/ |
| 320 |
public function bindParam( $param, &$var, $type = PDO::PARAM_STR, $maxLength = 0, $driverOptions = null ): bool { |
| 321 |
throw new RuntimeException( 'Not implemented' ); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Bind a value to a parameter. |
| 326 |
* |
| 327 |
* @param int|string $param Parameter identifier. Either a 1-indexed position of the parameter or a named parameter. |
| 328 |
* @param mixed $value The value to bind to the parameter. |
| 329 |
* @param int $type Data type of the parameter, specified by the PDO::PARAM_* constants. |
| 330 |
* @return bool True on success, false on failure. |
| 331 |
*/ |
| 332 |
public function bindValue( $param, $value, $type = PDO::PARAM_STR ): bool { |
| 333 |
throw new RuntimeException( 'Not implemented' ); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Dump information about the statement. |
| 338 |
* |
| 339 |
* Dupms the SQL query and parameters information. |
| 340 |
* |
| 341 |
* @return bool|null Returns null, or false on failure. |
| 342 |
*/ |
| 343 |
public function debugDumpParams(): ?bool { |
| 344 |
throw new RuntimeException( 'Not implemented' ); |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Fetch all remaining rows from the result set. |
| 349 |
* |
| 350 |
* This is used internally by the "WP_PDO_Proxy_Statement_PHP_Compat" trait, |
| 351 |
* that is defined conditionally based on the current PHP version. |
| 352 |
* |
| 353 |
* @param int $mode The fetch mode to use. |
| 354 |
* @param mixed $args Additional parameters for the fetch mode. |
| 355 |
* @return array The result set as an array of rows. |
| 356 |
*/ |
| 357 |
private function fetchAllRows( $mode = null, ...$args ): array { |
| 358 |
return $this->statement->fetchAll( $mode, ...$args ); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Set the default fetch mode for this statement. |
| 363 |
* |
| 364 |
* This is used internally by the "WP_PDO_Proxy_Statement_PHP_Compat" trait, |
| 365 |
* that is defined conditionally based on the current PHP version. |
| 366 |
* |
| 367 |
* @param int $mode The fetch mode to set as the default. |
| 368 |
* @param mixed $args Additional parameters for the default fetch mode. |
| 369 |
* @return bool True on success, false on failure. |
| 370 |
*/ |
| 371 |
private function setDefaultFetchMode( $mode, ...$args ): bool { |
| 372 |
return $this->statement->setFetchMode( $mode, ...$args ); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Polyfill ValueError for PHP < 8.0. |
| 378 |
*/ |
| 379 |
if ( PHP_VERSION_ID < 80000 && ! class_exists( ValueError::class ) ) { |
| 380 |
class ValueError extends Error { |
| 381 |
} |
| 382 |
} |
| 383 |
|