Skip to content

Commit 07cff07

Browse files
committed
Report prepared statements as unsupported
Return the standard IM001 PDO diagnostic according to the configured error mode instead of invoking uninitialized parent PDO state.
1 parent c3233e9 commit 07cff07

2 files changed

Lines changed: 70 additions & 10 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,31 @@ function ( string $sql, array $params ) {
870870
}
871871
}
872872

873+
/**
874+
* PDO API: Prepare a MySQL statement for execution.
875+
*
876+
* Prepared statements are not implemented yet. Report the standard PDO
877+
* unsupported-function diagnostic instead of using uninitialized parent
878+
* PDO state.
879+
*
880+
* @param string $query The MySQL statement to prepare.
881+
* @param array $options Optional statement options.
882+
* @return PDOStatement|false False when exceptions are disabled.
883+
*
884+
* @throws WP_MySQL_On_SQLite_Exception When exception mode is enabled.
885+
*/
886+
#[ReturnTypeWillChange]
887+
public function prepare( string $query, array $options = array() ) {
888+
$driver_message = 'driver does not support prepared statements';
889+
$exception = $this->new_driver_exception(
890+
'SQLSTATE[IM001]: Driver does not support this function: ' . $driver_message,
891+
'IM001',
892+
null,
893+
array( 'IM001', 0, $driver_message )
894+
);
895+
return $this->handle_pdo_error( $exception );
896+
}
897+
873898
/**
874899
* PDO API: Translate and execute a MySQL query in SQLite.
875900
*
@@ -1075,16 +1100,7 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
10751100
$e = $this->new_driver_exception( $e->getMessage(), $e->getCode(), $e );
10761101
}
10771102

1078-
$this->error_info = $e->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
1079-
$this->error_code = $this->error_info[0];
1080-
1081-
if ( PDO::ERRMODE_EXCEPTION === $this->error_mode ) {
1082-
throw $e;
1083-
}
1084-
if ( PDO::ERRMODE_WARNING === $this->error_mode ) {
1085-
trigger_error( $e->getMessage(), E_USER_WARNING );
1086-
}
1087-
return false;
1103+
return $this->handle_pdo_error( $e );
10881104
} finally {
10891105
// A query that doesn't return any rows or fails sets found rows to 0.
10901106
if ( ! $this->is_readonly || isset( $e ) ) {
@@ -1652,6 +1668,26 @@ private function snapshot_column_meta(): array {
16521668
}
16531669
}
16541670

1671+
/**
1672+
* Record and report a PDO operation error according to the configured mode.
1673+
*
1674+
* @param WP_MySQL_On_SQLite_Exception $exception The operation error.
1675+
* @return false Always false when the error is not thrown.
1676+
* @throws WP_MySQL_On_SQLite_Exception When exception mode is enabled.
1677+
*/
1678+
private function handle_pdo_error( WP_MySQL_On_SQLite_Exception $exception ): bool {
1679+
$this->error_info = $exception->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
1680+
$this->error_code = $this->error_info[0];
1681+
1682+
if ( PDO::ERRMODE_EXCEPTION === $this->error_mode ) {
1683+
throw $exception;
1684+
}
1685+
if ( PDO::ERRMODE_WARNING === $this->error_mode ) {
1686+
trigger_error( $exception->getMessage(), E_USER_WARNING );
1687+
}
1688+
return false;
1689+
}
1690+
16551691
/**
16561692
* Translate and execute a MySQL query in SQLite.
16571693
*

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,30 @@ public function test_query(): void {
238238
}
239239
}
240240

241+
public function test_prepare_reports_unsupported_operation(): void {
242+
try {
243+
$this->driver->prepare( 'SELECT ?' );
244+
$this->fail( 'Expected prepare() to throw an exception.' );
245+
} catch ( WP_MySQL_On_SQLite_Exception $exception ) {
246+
$this->assertSame( 'IM001', $exception->getCode() );
247+
$this->assertSame(
248+
array( 'IM001', 0, 'driver does not support prepared statements' ),
249+
$exception->errorInfo
250+
);
251+
}
252+
}
253+
254+
public function test_prepare_respects_silent_error_mode(): void {
255+
$this->driver->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
256+
257+
$this->assertFalse( $this->driver->prepare( 'SELECT ?' ) );
258+
$this->assertSame( 'IM001', $this->driver->errorCode() );
259+
$this->assertSame(
260+
array( 'IM001', 0, 'driver does not support prepared statements' ),
261+
$this->driver->errorInfo()
262+
);
263+
}
264+
241265
public function test_statement_query_string(): void {
242266
$query = 'SELECT 1 AS value';
243267
$stmt = $this->driver->query( $query );

0 commit comments

Comments
 (0)