Skip to content

Commit 902a032

Browse files
committed
Unify emulated MySQL version
Use the configured MySQL version for parser version gates and report it consistently through PDO, SQL, and wpdb APIs.
1 parent 8def32c commit 902a032

8 files changed

Lines changed: 178 additions & 39 deletions

File tree

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

Lines changed: 59 additions & 21 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
*
@@ -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() );
@@ -1423,11 +1444,14 @@ 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_CLIENT_VERSION === $attribute ) {
1448+
return 'mysqlnd ' . $this->get_mysql_server_version_string();
1449+
} elseif ( PDO::ATTR_SERVER_VERSION === $attribute ) {
1450+
return $this->get_mysql_server_version_string();
1451+
} elseif ( PDO::ATTR_ERRMODE === $attribute ) {
1452+
// Return the caller's error mode instead of the exception mode used internally.
14281453
return $this->error_mode;
1429-
}
1430-
if ( PDO::ATTR_STRINGIFY_FETCHES === $attribute && PHP_VERSION_ID < 80200 ) {
1454+
} elseif ( PDO::ATTR_STRINGIFY_FETCHES === $attribute && PHP_VERSION_ID < 80200 ) {
14311455
// PDO SQLite cannot report this attribute before PHP 8.2.
14321456
return $this->stringify_fetches;
14331457
}
@@ -1546,7 +1570,7 @@ public function get_last_sqlite_queries(): array {
15461570
public function create_parser( string $query ): WP_MySQL_Parser {
15471571
$lexer = new WP_MySQL_Lexer(
15481572
$query,
1549-
80038,
1573+
$this->mysql_version,
15501574
$this->get_active_sql_mode_names()
15511575
);
15521576
$tokens = $lexer instanceof WP_MySQL_Native_Lexer
@@ -4294,13 +4318,7 @@ private function translate( $node ): ?string {
42944318
if ( 'sql_mode' === $name ) {
42954319
$value = implode( ',', $this->get_active_sql_mode_names() );
42964320
} 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-
);
4321+
$value = $this->get_mysql_server_version_string();
43044322
} elseif ( 'version_comment' === $name ) {
43054323
$value = 'MySQL Community Server - GPL';
43064324
} elseif ( WP_MySQL_Lexer::SESSION_SYMBOL === $type ) {
@@ -5077,19 +5095,39 @@ private function translate_function_call( WP_Parser_Node $node ): string {
50775095
return 0;
50785096
}
50795097
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 );
5098+
return $this->quote_sqlite_value( $this->get_mysql_server_version_string() );
50885099
default:
50895100
return $this->translate_sequence( $node->get_children() );
50905101
}
50915102
}
50925103

5104+
/**
5105+
* Get the configured MySQL version in dotted notation.
5106+
*
5107+
* @return string The MySQL version.
5108+
*/
5109+
private function get_mysql_version_string(): string {
5110+
return sprintf(
5111+
'%d.%d.%d',
5112+
intdiv( $this->mysql_version, 10000 ),
5113+
intdiv( $this->mysql_version % 10000, 100 ),
5114+
$this->mysql_version % 100
5115+
);
5116+
}
5117+
5118+
/**
5119+
* Get the raw version string of the emulated MySQL server.
5120+
*
5121+
* @return string The MySQL server version.
5122+
*/
5123+
private function get_mysql_server_version_string(): string {
5124+
return sprintf(
5125+
'%s-mysql-on-sqlite-%s',
5126+
$this->get_mysql_version_string(),
5127+
SQLITE_DRIVER_VERSION
5128+
);
5129+
}
5130+
50935131
/**
50945132
* Translate a MySQL datetime literal to SQLite.
50955133
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,72 @@ 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_configured_mysql_version_controls_reporting_and_parsing(): void {
105+
$driver = new WP_MySQL_On_SQLite(
106+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
107+
null,
108+
null,
109+
array( 'mysql_version' => 50744 )
110+
);
111+
$server_version = '5.7.44-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION;
112+
113+
$this->assertSame( $server_version, $driver->getAttribute( PDO::ATTR_SERVER_VERSION ) );
114+
$this->assertSame( 'mysqlnd ' . $server_version, $driver->getAttribute( PDO::ATTR_CLIENT_VERSION ) );
115+
$this->assertSame( $server_version, $driver->query( 'SELECT VERSION()' )->fetchColumn() );
116+
$this->assertSame( $server_version, $driver->query( 'SELECT @@version' )->fetchColumn() );
117+
$this->assertEquals( 1, $driver->query( 'SELECT 1 /*!80000 + 1 */' )->fetchColumn() );
118+
}
119+
120+
public function test_formats_six_digit_mysql_version(): void {
121+
$driver = new WP_MySQL_On_SQLite(
122+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
123+
null,
124+
null,
125+
array( 'mysql_version' => 100000 )
126+
);
127+
$server_version = '10.0.0-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION;
128+
129+
$this->assertSame( $server_version, $driver->getAttribute( PDO::ATTR_SERVER_VERSION ) );
130+
$this->assertSame( 'mysqlnd ' . $server_version, $driver->getAttribute( PDO::ATTR_CLIENT_VERSION ) );
131+
$this->assertSame( $server_version, $driver->query( 'SELECT VERSION()' )->fetchColumn() );
132+
$this->assertSame( $server_version, $driver->query( 'SELECT @@version' )->fetchColumn() );
133+
}
134+
135+
/**
136+
* @dataProvider data_invalid_mysql_versions
137+
*/
138+
public function test_rejects_invalid_mysql_version( $mysql_version ): void {
139+
$this->expectException( InvalidArgumentException::class );
140+
$this->expectExceptionMessage(
141+
'The "mysql_version" option must be an integer greater than or equal to 50700.'
142+
);
143+
144+
new WP_MySQL_On_SQLite(
145+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
146+
null,
147+
null,
148+
array( 'mysql_version' => $mysql_version )
149+
);
150+
}
151+
152+
public function data_invalid_mysql_versions(): array {
153+
return array(
154+
'string' => array( '80038' ),
155+
'float' => array( 80038.0 ),
156+
'boolean' => array( true ),
157+
'array' => array( array( 80038 ) ),
158+
'below minimum' => array( 50699 ),
159+
'zero' => array( 0 ),
160+
'negative' => array( -80038 ),
161+
);
162+
}
163+
164+
public function test_uses_shared_default_mysql_version(): void {
165+
$this->assertSame( 80038, WP_MySQL_On_SQLite::DEFAULT_MYSQL_VERSION );
166+
$this->assertSame( '8.0.38-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $this->driver->getAttribute( PDO::ATTR_SERVER_VERSION ) );
167+
$this->assertSame( 'mysqlnd 8.0.38-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $this->driver->getAttribute( PDO::ATTR_CLIENT_VERSION ) );
168+
}
169+
104170
public function test_constructor_applies_fetch_column_default(): void {
105171
$driver = new WP_MySQL_On_SQLite(
106172
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7363,7 +7363,7 @@ public function testSetStatement(): void {
73637363

73647364
public function testBuiltInSystemVariables(): void {
73657365
$result = $this->assertQuery( 'SELECT @@version' );
7366-
$this->assertSame( '8.0.38', $result[0]->{'@@version'} );
7366+
$this->assertSame( '8.0.38-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $result[0]->{'@@version'} );
73677367

73687368
$result = $this->assertQuery( 'SELECT @@version_comment' );
73697369
$this->assertSame( 'MySQL Community Server - GPL', $result[0]->{'@@version_comment'} );
@@ -12681,7 +12681,7 @@ public function testUpdateErrorsInNonStrictMode(): void {
1268112681

1268212682
public function testVersionFunction(): void {
1268312683
$result = $this->query( 'SELECT VERSION()' );
12684-
$this->assertSame( '8.0.38', $result[0]->{'VERSION()'} );
12684+
$this->assertSame( '8.0.38-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $result[0]->{'VERSION()'} );
1268512685
}
1268612686

1268712687
public function testFromBase64Function(): void {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@ public function __construct( WP_MySQL_On_SQLite $driver ) {
3131
$this->assertSame( $this->driver, $wpdb->get_driver() );
3232
}
3333

34+
public function test_reports_emulated_mysql_version(): void {
35+
$driver = new WP_MySQL_On_SQLite(
36+
'mysql-on-sqlite:dbname=wp',
37+
null,
38+
null,
39+
array(
40+
'pdo' => $this->driver->get_sqlite_pdo(),
41+
'mysql_version' => 50744,
42+
)
43+
);
44+
$wpdb = new class( $driver ) extends WP_SQLite_DB {
45+
public function __construct( WP_MySQL_On_SQLite $driver ) {
46+
$this->dbh = $driver;
47+
}
48+
};
49+
50+
$this->assertSame( '5.7.44', $wpdb->db_version() );
51+
$this->assertSame( '5.7.44-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $wpdb->db_server_info() );
52+
}
53+
54+
public function test_reports_empty_version_without_database_connection(): void {
55+
$wpdb = new class() extends WP_SQLite_DB {
56+
public function __construct() {
57+
$this->dbh = null;
58+
}
59+
};
60+
61+
$this->assertSame( '', $wpdb->db_version() );
62+
$this->assertSame( '', $wpdb->db_server_info() );
63+
}
64+
3465
public function test_rejects_driver_access_without_database_connection(): void {
3566
$wpdb = new class() extends WP_SQLite_DB {
3667
public function __construct() {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ public function test_delegates_diagnostics_and_native_queries(): void {
6969
$this->assertSame( '42', $this->driver->execute_sqlite_query( 'SELECT 42' )->fetchColumn() );
7070
}
7171

72+
public function test_preserves_configured_mysql_version(): void {
73+
$driver = new WP_SQLite_Driver(
74+
new WP_SQLite_Connection( array( 'pdo' => $this->sqlite ) ),
75+
'wp',
76+
50744
77+
);
78+
79+
$this->assertSame( '5.7.44-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $driver->query( 'SELECT VERSION()' )[0]->{'VERSION()'} );
80+
$this->assertSame( '1', $driver->query( 'SELECT 1 /*!80000 + 1 */' )[0]->{'1'} );
81+
}
82+
7283
public function test_preserves_transaction_method_aliases(): void {
7384
$this->driver->begin_transaction();
7485
$this->driver->query( "INSERT INTO t (value) VALUES ('rolled back')" );

‎packages/mysql-on-sqlite/tests/fixtures/class-wpdb-stub.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ class WPDB_Stub {
44
public function add_placeholder_escape( $query ) {
55
return $query;
66
}
7+
8+
public function db_version() {
9+
return preg_replace( '/[^0-9.].*/', '', $this->db_server_info() );
10+
}
711
}
812

913
class_alias( WPDB_Stub::class, 'wpdb' );

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -746,29 +746,18 @@ public function has_cap( $db_cap ) {
746746
}
747747

748748
/**
749-
* Method to return database version number.
749+
* Returns the raw version string of the emulated MySQL server.
750750
*
751-
* This overrides wpdb::db_version() to avoid using MySQL function.
752-
* It returns mysql version number, but it means nothing for SQLite.
753-
* So it return the newest mysql version.
751+
* @see wpdb::db_server_info()
754752
*
755-
* @see wpdb::db_version()
756-
*/
757-
public function db_version() {
758-
return '8.0';
759-
}
760-
761-
/**
762-
* Returns the version of the SQLite engine.
763-
*
764-
* @return string SQLite engine version, or an empty string while disconnected.
753+
* @return string Emulated MySQL server version, or an empty string while disconnected.
765754
*/
766755
public function db_server_info() {
767756
if ( ! $this->dbh ) {
768757
return '';
769758
}
770759

771-
return $this->dbh->get_sqlite_version();
760+
return $this->dbh->getAttribute( PDO::ATTR_SERVER_VERSION ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
772761
}
773762

774763
/**

0 commit comments

Comments
 (0)