@@ -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 *
0 commit comments