Skip to content

Commit 1f557c2

Browse files
authored
Unify emulated MySQL server identity (#479)
## Summary This PR is stacked on #477. This PR makes the configured **MySQL version** the single source of truth for parsing and version reporting. It also makes the driver consistently identify itself as **MySQL on SQLite** instead of exposing SQLite details through MySQL-facing APIs. The changes are: - **Version handling:** Validate `mysql_version`, unify its usage, and fix formatting. - **Server identity:** Report the driver name and consistent server, client, SQL, and WordPress version information. - **System variables:** Report `version` and `version_comment` through both `@@` variables and `SHOW VARIABLES`. ## Why The parser previously used a hard-coded MySQL version, while different public APIs could report conflicting MySQL and SQLite identities. The version and server identity reporting was messy and unfinished. ## Reported values With the default `mysql_version` (`80038`) and current driver version (`3.0.0-rc.8`), the APIs report: The MariaDB reference values were measured with MariaDB 10.11.15 from the official container and PHP 8.5.9 with mysqlnd. Distribution and client suffixes can vary. | Interface | MySQL on SQLite | MariaDB reference | | --- | --- | --- | | `PDO::ATTR_DRIVER_NAME` | `mysql` | `mysql` | | `PDO::ATTR_SERVER_VERSION` | `8.0.38-mysql-on-sqlite-3.0.0-rc.8` | `10.11.15-MariaDB-ubu2204` | | `PDO::ATTR_CLIENT_VERSION` | `mysqlnd 8.0.38-mysql-on-sqlite-3.0.0-rc.8` | `mysqlnd 8.5.9` | | `VERSION()` | `8.0.38-mysql-on-sqlite-3.0.0-rc.8` | `10.11.15-MariaDB-ubu2204` | | `@@version`<br>`@@GLOBAL.version`<br>`SHOW VARIABLES: version` | `8.0.38-mysql-on-sqlite-3.0.0-rc.8` | `10.11.15-MariaDB-ubu2204` | | `@@version_comment`<br>`@@GLOBAL.version_comment`<br>`SHOW VARIABLES: version_comment` | `MySQL on SQLite` | `mariadb.org binary distribution` | | `WP_SQLite_DB::db_version()` | `8.0.38` | `10.11.15` | | `WP_SQLite_DB::db_server_info()` | `8.0.38-mysql-on-sqlite-3.0.0-rc.8` | `10.11.15-MariaDB-ubu2204` | | `WP_MySQL_On_SQLite::$client_info` | `mysqlnd 8.0.38-mysql-on-sqlite-3.0.0-rc.8` | `mysqlnd 8.5.9` | ## Accepted WordPress test failures Reporting MySQL 8.0.38 causes WordPress to apply MySQL 8 behavior when interpreting database metadata. This exposes three existing compatibility gaps: - `Tests_DB_Charset::test_get_column_charset` data sets `#5` and `#6`, because the driver still reports `utf8` instead of the modern `utf8mb3` alias. - `Tests_DB_dbDelta::test_column_type_change`, because the driver still reports integer display widths that MySQL 8.0.19 and later omit. These cases are tracked as expected failures in this PR. Version-dependent metadata reporting will be addressed separately in a follow-up.
1 parent 75664b9 commit 1f557c2

9 files changed

Lines changed: 250 additions & 73 deletions

File tree

‎.github/workflows/wp-tests-phpunit-run.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const expectedFailures = [
2222
'Tests_Comment_CheckComment::test_should_return_false_when_comment_previously_approved_is_enabled_and_author_does_not_have_approved_comment',
2323
'Tests_Comment_CheckComment::test_should_return_true_when_comment_previously_approved_is_enabled_and_author_has_approved_comment',
2424
'Tests_Comment_CheckComment::test_should_return_false_when_comment_previously_approved_is_enabled_and_user_does_not_have_a_previously_approved_comment_with_any_email',
25+
'Tests_DB_Charset::test_get_column_charset with data set #5',
26+
'Tests_DB_Charset::test_get_column_charset with data set #6',
2527
'Tests_DB_Charset::test_get_column_charset_is_mysql_undefined with data set #1',
2628
'Tests_DB_Charset::test_get_column_charset_is_mysql_undefined with data set #2',
2729
'Tests_DB_Charset::test_get_column_charset_is_mysql_undefined with data set #3',
@@ -52,7 +54,7 @@ const expectedFailures = [
5254
'Tests_DB_Charset::test_strip_invalid_text with data set #39',
5355
'Tests_DB_Charset::test_strip_invalid_text with data set #40',
5456
'Tests_DB_Charset::test_strip_invalid_text with data set #41',
55-
'Tests_DB_dbDelta::test_spatial_indices',
57+
'Tests_DB_dbDelta::test_column_type_change',
5658
'Tests_DB::test_mysqli_flush_sync',
5759
'Tests_DB::test_query_value_contains_invalid_chars',
5860
'Tests_DB::test_replace',

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

Lines changed: 108 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ class WP_MySQL_On_SQLite extends PDO {
4545
*/
4646
private const MYSQL_GRAMMAR_PATH = __DIR__ . '/../mysql/mysql-grammar.php';
4747

48+
/**
49+
* The minimum supported MySQL version.
50+
*/
51+
private const MINIMUM_MYSQL_VERSION = 50700;
52+
53+
/**
54+
* The default MySQL version to emulate.
55+
*/
56+
const DEFAULT_MYSQL_VERSION = 80038;
57+
4858
/**
4959
* The minimum required version of SQLite.
5060
*
@@ -488,7 +498,7 @@ class WP_MySQL_On_SQLite extends PDO {
488498
private $mysql_version;
489499

490500
/**
491-
* The SQLite engine version.
501+
* The emulated MySQL client library version.
492502
*
493503
* This is a mysqli-like property that is needed to avoid a PHP warning in
494504
* the WordPress health info. The "WP_Debug_Data::get_wp_database()" method
@@ -775,6 +785,7 @@ class WP_MySQL_On_SQLite extends PDO {
775785
* @type string|int|null $synchronous Optional. SQLite synchronous setting.
776786
* }
777787
*
788+
* @throws InvalidArgumentException When the MySQL version is invalid.
778789
* @throws WP_MySQL_On_SQLite_Exception When the driver initialization fails.
779790
*/
780791
public function __construct(
@@ -824,6 +835,16 @@ public function __construct(
824835
$path = $args['path'] ?? ':memory:';
825836
$db_name = $args['dbname'] ?? 'sqlite_database';
826837

838+
$mysql_version = $options['mysql_version'] ?? self::DEFAULT_MYSQL_VERSION;
839+
if ( ! is_int( $mysql_version ) || $mysql_version < self::MINIMUM_MYSQL_VERSION ) {
840+
throw new InvalidArgumentException(
841+
sprintf(
842+
'The "mysql_version" option must be an integer greater than or equal to %d.',
843+
self::MINIMUM_MYSQL_VERSION
844+
)
845+
);
846+
}
847+
827848
// Create a new SQLite connection.
828849
$pdo_options = array_filter(
829850
$options,
@@ -852,7 +873,7 @@ function ( $key ) {
852873
}
853874
$this->connection = new WP_SQLite_Connection( $connection_options );
854875

855-
$this->mysql_version = $options['mysql_version'] ?? 80038;
876+
$this->mysql_version = $mysql_version;
856877
$this->main_db_name = $db_name;
857878
$this->db_name = $db_name;
858879
$this->set_sql_modes( $this->get_default_sql_modes() );
@@ -909,8 +930,8 @@ function ( $key ) {
909930
}
910931
}
911932

912-
// Load SQLite version to a property used by WordPress health info.
913-
$this->client_info = $sqlite_version;
933+
// Load MySQL client information to a property used by WordPress health info.
934+
$this->client_info = $this->getAttribute( PDO::ATTR_CLIENT_VERSION );
914935

915936
// Enable foreign keys. By default, they are off.
916937
$this->connection->query( 'PRAGMA foreign_keys = ON' );
@@ -1423,11 +1444,16 @@ public function setAttribute( $attribute, $value ): bool {
14231444
*/
14241445
#[ReturnTypeWillChange]
14251446
public function getAttribute( $attribute ) {
1426-
// Return the caller's error mode instead of the exception mode used internally.
1427-
if ( PDO::ATTR_ERRMODE === $attribute ) {
1447+
if ( PDO::ATTR_DRIVER_NAME === $attribute ) {
1448+
return 'mysql';
1449+
} elseif ( PDO::ATTR_CLIENT_VERSION === $attribute ) {
1450+
return 'mysqlnd ' . $this->get_mysql_server_version_string();
1451+
} elseif ( PDO::ATTR_SERVER_VERSION === $attribute ) {
1452+
return $this->get_mysql_server_version_string();
1453+
} elseif ( PDO::ATTR_ERRMODE === $attribute ) {
1454+
// Return the caller's error mode instead of the exception mode used internally.
14281455
return $this->error_mode;
1429-
}
1430-
if ( PDO::ATTR_STRINGIFY_FETCHES === $attribute && PHP_VERSION_ID < 80200 ) {
1456+
} elseif ( PDO::ATTR_STRINGIFY_FETCHES === $attribute && PHP_VERSION_ID < 80200 ) {
14311457
// PDO SQLite cannot report this attribute before PHP 8.2.
14321458
return $this->stringify_fetches;
14331459
}
@@ -1546,7 +1572,7 @@ public function get_last_sqlite_queries(): array {
15461572
public function create_parser( string $query ): WP_MySQL_Parser {
15471573
$lexer = new WP_MySQL_Lexer(
15481574
$query,
1549-
80038,
1575+
$this->mysql_version,
15501576
$this->get_active_sql_mode_names()
15511577
);
15521578
$tokens = $lexer instanceof WP_MySQL_Native_Lexer
@@ -3247,31 +3273,7 @@ private function execute_show_statement( WP_Parser_Node $node ): void {
32473273
$this->execute_show_tables_statement( $node );
32483274
return;
32493275
case WP_MySQL_Lexer::VARIABLES_SYMBOL:
3250-
$this->last_column_meta = array(
3251-
array(
3252-
'native_type' => 'STRING',
3253-
'pdo_type' => PDO::PARAM_STR,
3254-
'flags' => array( 'not_null' ),
3255-
'table' => 'session_variables',
3256-
'name' => 'Variable_name',
3257-
'len' => 256,
3258-
'precision' => 0,
3259-
),
3260-
array(
3261-
'native_type' => 'STRING',
3262-
'pdo_type' => PDO::PARAM_STR,
3263-
'flags' => array(),
3264-
'table' => 'session_variables',
3265-
'name' => 'Value',
3266-
'len' => 4096,
3267-
'precision' => 0,
3268-
),
3269-
);
3270-
$this->last_result_statement = $this->create_result_statement_from_data(
3271-
array_column( $this->last_column_meta, 'name' ),
3272-
array()
3273-
);
3274-
$this->found_rows = 0;
3276+
$this->execute_show_variables_statement( $node );
32753277
return;
32763278
}
32773279

@@ -3284,6 +3286,39 @@ private function execute_show_statement( WP_Parser_Node $node ): void {
32843286
);
32853287
}
32863288

3289+
/**
3290+
* Translate and execute a MySQL SHOW VARIABLES statement in SQLite.
3291+
*
3292+
* @param WP_Parser_Node $node The "showStatement" AST node.
3293+
*/
3294+
private function execute_show_variables_statement( WP_Parser_Node $node ): void {
3295+
$like_or_where = $node->get_first_child_node( 'likeOrWhere' );
3296+
if ( null !== $like_or_where ) {
3297+
$condition = $this->translate_show_like_or_where_condition( $like_or_where, 'Variable_name' );
3298+
}
3299+
3300+
$query = sprintf(
3301+
"SELECT column1 AS `Variable_name`, column2 AS `Value`
3302+
FROM (
3303+
VALUES
3304+
('version', ?),
3305+
('version_comment', ?)
3306+
)
3307+
WHERE TRUE %s
3308+
ORDER BY column1",
3309+
$condition ?? ''
3310+
);
3311+
$params = array(
3312+
$this->get_mysql_server_version_string(),
3313+
$this->get_mysql_server_version_comment(),
3314+
);
3315+
3316+
$stmt = $this->execute_sqlite_query( $query, $params );
3317+
$this->store_last_column_meta_from_statement( $stmt );
3318+
$this->last_result_statement = $stmt;
3319+
$this->found_rows = array( $query, $params );
3320+
}
3321+
32873322
/**
32883323
* Translate and execute a MySQL SHOW COLLATION statement in SQLite.
32893324
*
@@ -4294,15 +4329,9 @@ private function translate( $node ): ?string {
42944329
if ( 'sql_mode' === $name ) {
42954330
$value = implode( ',', $this->get_active_sql_mode_names() );
42964331
} elseif ( 'version' === $name ) {
4297-
$version = (string) $this->mysql_version;
4298-
$value = sprintf(
4299-
'%d.%d.%d',
4300-
$version[0],
4301-
substr( $version, 1, 2 ),
4302-
substr( $version, 3, 2 )
4303-
);
4332+
$value = $this->get_mysql_server_version_string();
43044333
} elseif ( 'version_comment' === $name ) {
4305-
$value = 'MySQL Community Server - GPL';
4334+
$value = $this->get_mysql_server_version_comment();
43064335
} elseif ( WP_MySQL_Lexer::SESSION_SYMBOL === $type ) {
43074336
$value = $this->session_system_variables[ $name ] ?? null;
43084337
} else {
@@ -5077,19 +5106,48 @@ private function translate_function_call( WP_Parser_Node $node ): string {
50775106
return 0;
50785107
}
50795108
case 'VERSION':
5080-
$version = (string) $this->mysql_version;
5081-
$value = sprintf(
5082-
'%d.%d.%d',
5083-
$version[0],
5084-
substr( $version, 1, 2 ),
5085-
substr( $version, 3, 2 )
5086-
);
5087-
return $this->quote_sqlite_value( $value );
5109+
return $this->quote_sqlite_value( $this->get_mysql_server_version_string() );
50885110
default:
50895111
return $this->translate_sequence( $node->get_children() );
50905112
}
50915113
}
50925114

5115+
/**
5116+
* Get the configured MySQL version in dotted notation.
5117+
*
5118+
* @return string The MySQL version.
5119+
*/
5120+
private function get_mysql_version_string(): string {
5121+
return sprintf(
5122+
'%d.%d.%d',
5123+
intdiv( $this->mysql_version, 10000 ),
5124+
intdiv( $this->mysql_version % 10000, 100 ),
5125+
$this->mysql_version % 100
5126+
);
5127+
}
5128+
5129+
/**
5130+
* Get the raw version string of the emulated MySQL server.
5131+
*
5132+
* @return string The MySQL server version.
5133+
*/
5134+
private function get_mysql_server_version_string(): string {
5135+
return sprintf(
5136+
'%s-mysql-on-sqlite-%s',
5137+
$this->get_mysql_version_string(),
5138+
SQLITE_DRIVER_VERSION
5139+
);
5140+
}
5141+
5142+
/**
5143+
* Get the comment identifying the emulated MySQL server implementation.
5144+
*
5145+
* @return string The MySQL server version comment.
5146+
*/
5147+
private function get_mysql_server_version_comment(): string {
5148+
return 'MySQL on SQLite';
5149+
}
5150+
50935151
/**
50945152
* Translate a MySQL datetime literal to SQLite.
50955153
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
class WP_SQLite_Driver {
2727
/**
28-
* The SQLite engine version.
28+
* The emulated MySQL client library version.
2929
*
3030
* This is a mysqli-like property that is needed to avoid a PHP warning in
3131
* the WordPress health info. The "WP_Debug_Data::get_wp_database()" method
@@ -75,7 +75,7 @@ class WP_SQLite_Driver {
7575
public function __construct(
7676
WP_SQLite_Connection $connection,
7777
string $database,
78-
int $mysql_version = 80038
78+
int $mysql_version = WP_MySQL_On_SQLite::DEFAULT_MYSQL_VERSION
7979
) {
8080
$this->mysql_on_sqlite_driver = new WP_MySQL_On_SQLite(
8181
sprintf( 'mysql-on-sqlite:dbname=%s', str_replace( ';', ';;', $database ) ),

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,78 @@ public function test_constructor_applies_pdo_options(): void {
101101
$this->assertSame( PDO::ERRMODE_EXCEPTION, $driver->get_sqlite_pdo()->getAttribute( PDO::ATTR_ERRMODE ) );
102102
}
103103

104+
public function test_reports_mysql_driver_name(): void {
105+
$this->assertSame( 'mysql', $this->driver->getAttribute( PDO::ATTR_DRIVER_NAME ) );
106+
$this->assertSame( 'sqlite', $this->driver->get_sqlite_pdo()->getAttribute( PDO::ATTR_DRIVER_NAME ) );
107+
}
108+
109+
public function test_configured_mysql_version_controls_reporting_and_parsing(): void {
110+
$driver = new WP_MySQL_On_SQLite(
111+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
112+
null,
113+
null,
114+
array( 'mysql_version' => 50744 )
115+
);
116+
$server_version = '5.7.44-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION;
117+
118+
$this->assertSame( $server_version, $driver->getAttribute( PDO::ATTR_SERVER_VERSION ) );
119+
$this->assertSame( 'mysqlnd ' . $server_version, $driver->getAttribute( PDO::ATTR_CLIENT_VERSION ) );
120+
$this->assertSame( 'mysqlnd ' . $server_version, $driver->client_info );
121+
$this->assertSame( $server_version, $driver->query( 'SELECT VERSION()' )->fetchColumn() );
122+
$this->assertSame( $server_version, $driver->query( 'SELECT @@version' )->fetchColumn() );
123+
$this->assertEquals( 1, $driver->query( 'SELECT 1 /*!80000 + 1 */' )->fetchColumn() );
124+
}
125+
126+
public function test_formats_six_digit_mysql_version(): void {
127+
$driver = new WP_MySQL_On_SQLite(
128+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
129+
null,
130+
null,
131+
array( 'mysql_version' => 100000 )
132+
);
133+
$server_version = '10.0.0-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION;
134+
135+
$this->assertSame( $server_version, $driver->getAttribute( PDO::ATTR_SERVER_VERSION ) );
136+
$this->assertSame( 'mysqlnd ' . $server_version, $driver->getAttribute( PDO::ATTR_CLIENT_VERSION ) );
137+
$this->assertSame( $server_version, $driver->query( 'SELECT VERSION()' )->fetchColumn() );
138+
$this->assertSame( $server_version, $driver->query( 'SELECT @@version' )->fetchColumn() );
139+
}
140+
141+
/**
142+
* @dataProvider data_invalid_mysql_versions
143+
*/
144+
public function test_rejects_invalid_mysql_version( $mysql_version ): void {
145+
$this->expectException( InvalidArgumentException::class );
146+
$this->expectExceptionMessage(
147+
'The "mysql_version" option must be an integer greater than or equal to 50700.'
148+
);
149+
150+
new WP_MySQL_On_SQLite(
151+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
152+
null,
153+
null,
154+
array( 'mysql_version' => $mysql_version )
155+
);
156+
}
157+
158+
public function data_invalid_mysql_versions(): array {
159+
return array(
160+
'string' => array( '80038' ),
161+
'float' => array( 80038.0 ),
162+
'boolean' => array( true ),
163+
'array' => array( array( 80038 ) ),
164+
'below minimum' => array( 50699 ),
165+
'zero' => array( 0 ),
166+
'negative' => array( -80038 ),
167+
);
168+
}
169+
170+
public function test_uses_shared_default_mysql_version(): void {
171+
$this->assertSame( 80038, WP_MySQL_On_SQLite::DEFAULT_MYSQL_VERSION );
172+
$this->assertSame( '8.0.38-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $this->driver->getAttribute( PDO::ATTR_SERVER_VERSION ) );
173+
$this->assertSame( 'mysqlnd 8.0.38-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $this->driver->getAttribute( PDO::ATTR_CLIENT_VERSION ) );
174+
}
175+
104176
public function test_constructor_applies_fetch_column_default(): void {
105177
$driver = new WP_MySQL_On_SQLite(
106178
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',

0 commit comments

Comments
 (0)