1414 *
1515 * The driver requires PDO with the SQLite driver, and the PCRE engine.
1616 */
17- class WP_PDO_MySQL_On_SQLite extends PDO {
17+ class WP_MySQL_On_SQLite extends PDO {
1818 /**
1919 * The path to the MySQL SQL grammar file.
2020 */
@@ -632,8 +632,17 @@ class WP_PDO_MySQL_On_SQLite extends PDO {
632632 *
633633 * Set up an SQLite connection and the MySQL-on-SQLite driver.
634634 *
635- * @param WP_SQLite_Connection $connection A SQLite database connection.
636- * @param string $db_name The database name.
635+ * @param string $dsn MySQL-on-SQLite DSN containing the SQLite path and database name.
636+ * @param string|null $username Optional. Ignored by this driver.
637+ * @param string|null $password Optional. Ignored by this driver.
638+ * @param array $options {
639+ * Optional driver options.
640+ *
641+ * @type int $mysql_version Optional. MySQL version to emulate. Default 80038.
642+ * @type PDO|null $pdo Optional. Existing SQLite PDO connection.
643+ * @type string|null $journal_mode Optional. SQLite journal mode. Default 'WAL'.
644+ * @type string|int|null $synchronous Optional. SQLite synchronous setting.
645+ * }
637646 *
638647 * @throws WP_SQLite_Driver_Exception When the driver initialization fails.
639648 */
@@ -789,11 +798,12 @@ function ( string $sql, array $params ) {
789798 *
790799 * A single MySQL query can be translated into zero or more SQLite queries.
791800 *
792- * @param string $query Full SQL statement string.
793- * @param int $fetch_mode PDO fetch mode. Default is PDO::FETCH_OBJ.
794- * @param array ...$fetch_mode_args Additional fetch mode arguments.
801+ * @param string $query Full SQL statement string.
802+ * @param int|null $fetch_mode Optional. PDO fetch mode. Defaults to the configured
803+ * PDO::ATTR_DEFAULT_FETCH_MODE.
804+ * @param mixed ...$fetch_mode_args Additional fetch mode arguments.
795805 *
796- * @return mixed Return value, depending on the query type .
806+ * @return PDOStatement|false PDO statement, or false when the fetch mode is invalid on PHP < 8.1 .
797807 *
798808 * @throws WP_SQLite_Driver_Exception When the query execution fails.
799809 */
@@ -1013,15 +1023,6 @@ public function beginTransaction(): bool {
10131023 return true ;
10141024 }
10151025
1016- /**
1017- * A temporary alias for back compatibility.
1018- *
1019- * @see self::beginTransaction()
1020- */
1021- public function begin_transaction (): void {
1022- $ this ->beginTransaction ();
1023- }
1024-
10251026 /**
10261027 * PDO API: Commit a transaction.
10271028 *
@@ -4073,10 +4074,10 @@ private function translate_string_literal( WP_Parser_Node $node ): string {
40734074 /*
40744075 * Translate datetime literals.
40754076 *
4076- * Process only strings that could possibly represent a datetime
4077- * literal ("YYYY-MM-DDTHH:MM:SS ", "YYYY-MM-DDTHH:MM:SSZ", etc.).
4077+ * Process only strings that could possibly represent a date or datetime
4078+ * literal ("YYYY-M-D ", "YYYY-MM-DDTHH:MM:SSZ", etc.).
40784079 */
4079- if ( strlen ( $ value ) >= 19 && is_numeric ( $ value [0 ] ) ) {
4080+ if ( strlen ( $ value ) >= 8 && is_numeric ( $ value [0 ] ) ) {
40804081 $ value = $ this ->translate_datetime_literal ( $ value );
40814082 }
40824083
@@ -4211,7 +4212,7 @@ private function translate_query_expression( WP_Parser_Node $node ): string {
42114212 * When the ORDER BY clause is present, we need to disambiguate the item
42124213 * list and make sure they don't cause an "ambiguous column name" error.
42134214 *
4214- * @see WP_SQLite_Driver ::disambiguate_item()
4215+ * @see WP_MySQL_On_SQLite ::disambiguate_item()
42154216 */
42164217 $ disambiguated_order_list = array ();
42174218 $ order_clause = $ node ->get_first_child_node ( 'orderClause ' );
@@ -4279,7 +4280,7 @@ private function translate_query_specification( WP_Parser_Node $node ): string {
42794280 * When the GROUP BY or HAVING clause is present, we need to disambiguate
42804281 * the items to ensure they don't cause an "ambiguous column name" error.
42814282 *
4282- * @see WP_SQLite_Driver ::disambiguate_item()
4283+ * @see WP_MySQL_On_SQLite ::disambiguate_item()
42834284 */
42844285 $ group_by_clause = null ;
42854286 $ having_clause = null ;
@@ -4681,7 +4682,11 @@ private function translate_function_call( WP_Parser_Node $node ): string {
46814682 */
46824683 private function translate_datetime_literal ( string $ value ): string {
46834684 /*
4684- * The code below converts the date format to one preferred by SQLite.
4685+ * Normalize MySQL date and datetime literals for SQLite.
4686+ *
4687+ * MySQL accepts date and time components without zero padding. SQLite
4688+ * stores temporal values as text, so they must be normalized for lexical
4689+ * comparisons and SQLite date functions to work correctly.
46854690 *
46864691 * MySQL accepts ISO 8601 date strings: 'YYYY-MM-DDTHH:MM:SSZ'
46874692 * SQLite prefers a slightly different format: 'YYYY-MM-DD HH:MM:SS'
@@ -4704,8 +4709,25 @@ private function translate_datetime_literal( string $value ): string {
47044709 * which is true in the unit test suite, but there could also be a timezone offset
47054710 * like "+00:00" or "+01:00". We could add support for that later if needed.
47064711 */
4707- if ( 1 === preg_match ( '/^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})Z$/ ' , $ value , $ matches ) ) {
4708- $ value = $ matches [1 ] . ' ' . $ matches [2 ];
4712+ if (
4713+ 1 === preg_match (
4714+ '/^(\d{4})-(\d{1,2})-(\d{1,2})(?:([ T])(\d{1,2}):(\d{1,2}):(\d{1,2})(Z)?)?$/ ' ,
4715+ $ value ,
4716+ $ matches
4717+ )
4718+ ) {
4719+ $ value = sprintf ( '%04d-%02d-%02d ' , $ matches [1 ], $ matches [2 ], $ matches [3 ] );
4720+ if ( isset ( $ matches [4 ] ) && '' !== $ matches [4 ] ) {
4721+ $ is_iso_8601 = 'T ' === $ matches [4 ] && 'Z ' === ( $ matches [8 ] ?? '' );
4722+ $ value .= sprintf (
4723+ '%s%02d:%02d:%02d%s ' ,
4724+ $ is_iso_8601 ? ' ' : $ matches [4 ],
4725+ $ matches [5 ],
4726+ $ matches [6 ],
4727+ $ matches [7 ],
4728+ $ is_iso_8601 ? '' : ( $ matches [8 ] ?? '' )
4729+ );
4730+ }
47094731 }
47104732
47114733 /*
@@ -5767,7 +5789,7 @@ private function unnest_parenthesized_expression( WP_Parser_Node $node ): WP_Par
57675789 * consider column references in forms like "db.table.column".
57685790 *
57695791 * @param array $disambiguation_map The SELECT item disambiguation map (column name => array of select items).
5770- * @see WP_SQLite_Driver ::create_select_item_disambiguation_map()
5792+ * @see WP_MySQL_On_SQLite ::create_select_item_disambiguation_map()
57715793 * @param WP_Parser_Node $expr The expression AST node or subnode.
57725794 * @return string|null The disambiguated and translated expression;
57735795 * null when the expression cannot be disambiguated.
@@ -5806,7 +5828,7 @@ private function disambiguate_item( array $disambiguation_map, WP_Parser_Node $e
58065828 * Create a SELECT item disambiguation map from a SELECT item list for use
58075829 * with the ORDER BY, GROUP BY, and HAVING clause disambiguation algorithm.
58085830 *
5809- * @see WP_SQLite_Driver ::disambiguate_item()
5831+ * @see WP_MySQL_On_SQLite ::disambiguate_item()
58105832 *
58115833 * @param WP_Parser_Node $select_item_list The "selectItemList" AST node.
58125834 * @return array The SELECT item disambiguation map (column name => array of select items).
0 commit comments