Skip to content

Commit 74f336d

Browse files
committed
fixup! Implement PDO connection status methods
1 parent 07cff07 commit 74f336d

6 files changed

Lines changed: 48 additions & 21 deletions

File tree

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
* The SQLite driver uses PDO. Enable PDO function calls:
55
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
66
*
7+
* PDO uses camel case naming, enable non-snake case:
8+
* phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
9+
*
10+
* PDO uses $string as a parameter name, enable it:
11+
* phpcs:disable Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
12+
*
713
* We conditionally define a trait for PHP-version-specific PDO methods:
814
* phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
915
*/
@@ -1130,7 +1136,6 @@ public function exec( $query ) {
11301136
* @return string|false The last insert ID, or false on failure.
11311137
*/
11321138
#[ReturnTypeWillChange]
1133-
// phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
11341139
public function lastInsertId( $name = null ) {
11351140
return $this->last_insert_id;
11361141
}
@@ -1140,7 +1145,6 @@ public function lastInsertId( $name = null ) {
11401145
*
11411146
* @return string|null The SQLSTATE error code, or null when unavailable.
11421147
*/
1143-
// phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
11441148
public function errorCode(): ?string {
11451149
return $this->error_code;
11461150
}
@@ -1150,7 +1154,6 @@ public function errorCode(): ?string {
11501154
*
11511155
* @return array Error information from the last PDO operation.
11521156
*/
1153-
// phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
11541157
public function errorInfo(): array {
11551158
return $this->error_info;
11561159
}
@@ -1163,7 +1166,6 @@ public function errorInfo(): array {
11631166
* @return string The quoted string.
11641167
*/
11651168
#[ReturnTypeWillChange]
1166-
// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
11671169
public function quote( $string, $type = PDO::PARAM_STR ) {
11681170
// Mirror PDO\MySQL::quote() value validation.
11691171
if (
@@ -1229,7 +1231,6 @@ public function quote( $string, $type = PDO::PARAM_STR ) {
12291231
*
12301232
* @return bool True on success, false on failure.
12311233
*/
1232-
// phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
12331234
public function beginTransaction(): bool {
12341235
if ( $this->inTransaction() ) {
12351236
throw $this->new_driver_exception( 'There is already an active transaction' );
@@ -1256,7 +1257,6 @@ public function commit(): bool {
12561257
*
12571258
* @return bool True on success, false on failure.
12581259
*/
1259-
// phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
12601260
public function rollBack(): bool {
12611261
if ( ! $this->inTransaction() ) {
12621262
throw $this->new_driver_exception( 'There is no active transaction' );
@@ -1270,7 +1270,6 @@ public function rollBack(): bool {
12701270
*
12711271
* @return bool True if a transaction is active, false otherwise.
12721272
*/
1273-
// phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
12741273
public function inTransaction(): bool {
12751274
if ( PHP_VERSION_ID < 80400 ) {
12761275
/*

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/*
44
* The SQLite driver uses PDO. Enable PDO function calls:
55
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
6+
*
7+
* PDO uses camel case naming, enable non-snake case:
8+
* phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
69
*/
710

811
/**
@@ -239,7 +242,7 @@ public function execute_sqlite_query( string $sql, array $params = array() ): PD
239242
/**
240243
* Begin a new transaction or nested transaction.
241244
*/
242-
public function beginTransaction(): void { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
245+
public function beginTransaction(): void {
243246
$this->mysql_on_sqlite_driver->beginTransaction();
244247
}
245248

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
/*
4+
* The SQLite driver uses PDO. Enable PDO function calls:
5+
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
6+
*/
7+
38
/**
49
* SQLite information schema builder for MySQL.
510
*
@@ -394,7 +399,7 @@ public function temporary_table_exists( string $table_name ): bool {
394399
* database. Tables that are missing will be created.
395400
*/
396401
public function ensure_information_schema_tables(): void {
397-
$sqlite_version = $this->connection->get_pdo()->getAttribute( PDO::ATTR_SERVER_VERSION ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
402+
$sqlite_version = $this->connection->get_pdo()->getAttribute( PDO::ATTR_SERVER_VERSION );
398403
$supports_strict_tables = version_compare( $sqlite_version, '3.37.0', '>=' );
399404
foreach ( self::INFORMATION_SCHEMA_TABLE_DEFINITIONS as $table_name => $table_body ) {
400405
$this->connection->query(
@@ -462,7 +467,7 @@ public function get_computed_information_schema_table_definition( string $table_
462467
* the SQLite database. Tables that are missing will be created.
463468
*/
464469
public function ensure_temporary_information_schema_tables(): void {
465-
$sqlite_version = $this->connection->get_pdo()->getAttribute( PDO::ATTR_SERVER_VERSION ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
470+
$sqlite_version = $this->connection->get_pdo()->getAttribute( PDO::ATTR_SERVER_VERSION );
466471
$supports_strict_tables = version_compare( $sqlite_version, '3.37.0', '>=' );
467472
foreach ( self::INFORMATION_SCHEMA_TABLE_DEFINITIONS as $table_name => $table_body ) {
468473
// Skip the "schemata" table; MySQL doesn't support temporary databases.
@@ -1345,7 +1350,7 @@ private function record_drop_constraint(
13451350
$name,
13461351
)
13471352
)->fetchAll(
1348-
PDO::FETCH_COLUMN // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
1353+
PDO::FETCH_COLUMN
13491354
);
13501355

13511356
if ( 0 === count( $constraint_types ) ) {
@@ -1638,7 +1643,7 @@ private function extract_index_statistics_data(
16381643
',
16391644
array_merge( array( self::SAVED_DATABASE_NAME, $table_name ), $column_names )
16401645
)->fetchAll(
1641-
PDO::FETCH_ASSOC // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
1646+
PDO::FETCH_ASSOC
16421647
);
16431648
} else {
16441649
$column_info = array();
@@ -1789,7 +1794,7 @@ private function extract_referential_constraint_data( WP_Parser_Node $node, stri
17891794
",
17901795
array( self::SAVED_DATABASE_NAME, $referenced_table_name )
17911796
)->fetchAll(
1792-
PDO::FETCH_ASSOC // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
1797+
PDO::FETCH_ASSOC
17931798
);
17941799

17951800
// Group index columns to a map.
@@ -2719,7 +2724,7 @@ public function get_table_constraint_name( WP_Parser_Node $node, string $table_n
27192724
str_replace( array( '_', '%' ), array( '\\_', '\\%' ), $table_name ) . "\\_{$type}\\_%",
27202725
)
27212726
)->fetchAll(
2722-
PDO::FETCH_COLUMN // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
2727+
PDO::FETCH_COLUMN
27232728
);
27242729

27252730
$last_name_index = 0;
@@ -2818,7 +2823,7 @@ private function get_index_name( WP_Parser_Node $node, string $table_name ): str
28182823
str_replace( array( '_', '%' ), array( '\\_', '\\%' ), $name ) . '\\_%',
28192824
)
28202825
)->fetchAll(
2821-
PDO::FETCH_COLUMN // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
2826+
PDO::FETCH_COLUMN
28222827
);
28232828

28242829
// The name is not used - we can use it as-is.

‎packages/mysql-proxy/src/Adapter/class-sqlite-adapter.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php declare( strict_types = 1 );
22

3+
/*
4+
* PDO uses camel case naming, enable non-snake case:
5+
* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
6+
*/
7+
38
namespace WP_MySQL_Proxy\Adapter;
49

510
use PDO;
@@ -45,7 +50,7 @@ public function handle_query( string $query ): MySQL_Result {
4550
}
4651
return MySQL_Result::from_data( $affected_rows, $last_insert_id, $columns, $rows ?? array() );
4752
} catch ( Throwable $e ) {
48-
$error_info = $e->errorInfo ?? null; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
53+
$error_info = $e->errorInfo ?? null;
4954
if ( $e instanceof PDOException && $error_info ) {
5055
return MySQL_Result::from_error( $error_info[0], $error_info[1], $error_info[2] );
5156
}

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
<?php
2+
3+
/*
4+
* The SQLite driver uses PDO. Enable PDO function calls:
5+
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
6+
*/
7+
28
/**
39
* Extend and replace the wpdb class.
410
*
@@ -153,7 +159,7 @@ public function get_col_length( $table, $column ) {
153159
*/
154160
public function set_sql_mode( $modes = array() ) {
155161
if ( empty( $modes ) ) {
156-
$result = $this->dbh->query( 'SELECT @@SESSION.sql_mode' )->fetchAll( PDO::FETCH_OBJ ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
162+
$result = $this->dbh->query( 'SELECT @@SESSION.sql_mode' )->fetchAll( PDO::FETCH_OBJ );
157163
if ( ! isset( $result[0] ) ) {
158164
return;
159165
}
@@ -480,7 +486,7 @@ public function db_connect( $allow_bail = true ) {
480486
null,
481487
$options
482488
);
483-
$dbh->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
489+
$dbh->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
484490
$this->dbh = $dbh;
485491

486492
/**
@@ -678,7 +684,7 @@ private function _do_query( $query ) {
678684
}
679685

680686
try {
681-
$this->result = $this->dbh->query( $query, PDO::FETCH_OBJ ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
687+
$this->result = $this->dbh->query( $query, PDO::FETCH_OBJ );
682688
} catch ( Throwable $e ) {
683689
$this->last_error = $this->format_error_message( $e );
684690
}

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/install-functions.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
<?php
2+
3+
/*
4+
* The SQLite driver uses PDO. Enable PDO function calls:
5+
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
6+
*
7+
* PDO uses camel case naming, enable non-snake case:
8+
* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
9+
*/
10+
211
/**
312
* Main integration file.
413
*
@@ -37,9 +46,9 @@ function sqlite_make_db_sqlite() {
3746
'journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
3847
)
3948
);
40-
$translator->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
49+
$translator->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
4150
} catch ( PDOException $err ) {
42-
$err_data = $err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
51+
$err_data = $err->errorInfo;
4352
$message = 'Database connection error!<br />';
4453
$message .= sprintf( 'Error message is: %s', $err_data[2] ?? $err->getMessage() );
4554
wp_die( $message, 'Database Error!' );

0 commit comments

Comments
 (0)