@@ -605,6 +605,42 @@ class WP_MySQL_On_SQLite extends PDO {
605605 */
606606 private $ last_insert_id = '0 ' ;
607607
608+ /**
609+ * Rows changed by the previous row-changing statement, for ROW_COUNT().
610+ *
611+ * MySQL's ROW_COUNT() reports the rows changed by the preceding INSERT,
612+ * REPLACE, UPDATE, or DELETE statement; -1 when the preceding statement
613+ * was a SELECT or SHOW; 0 for any other statement; -1 before any
614+ * statement ran.
615+ *
616+ * This is deliberately not derived from SQLite's CHANGES(): a single
617+ * MySQL statement can translate to multiple SQLite writes (e.g. a
618+ * multi-table DELETE reports the sum of N DELETEs, while CHANGES()
619+ * would only see the last one).
620+ *
621+ * @var int
622+ */
623+ private $ row_count_value = -1 ;
624+
625+ /**
626+ * Session value set by the most recent LAST_INSERT_ID(expr) call.
627+ *
628+ * MySQL scopes this to the session: it survives across statements until
629+ * the next LAST_INSERT_ID(expr) call or an INSERT/REPLACE generating a
630+ * new AUTO_INCREMENT value. Mirrored into the user-defined functions
631+ * before every query and read back afterwards (see query()).
632+ *
633+ * @var mixed
634+ */
635+ private $ last_insert_id_session_value = null ;
636+
637+ /**
638+ * Whether an explicit LAST_INSERT_ID(expr) session value is stored.
639+ *
640+ * @var bool
641+ */
642+ private $ last_insert_id_session_is_set = false ;
643+
608644 /**
609645 * User-defined functions registered on the SQLite connection.
610646 *
@@ -955,6 +991,7 @@ function ( $key ) {
955991
956992 // Register SQLite functions.
957993 $ this ->user_defined_functions = WP_SQLite_PDO_User_Defined_Functions ::register_for ( $ this ->connection ->get_pdo () );
994+ $ this ->user_defined_functions ->set_connection ( $ this ->connection ->get_pdo () );
958995
959996 // Load MySQL grammar.
960997 if ( null === self ::$ mysql_grammar ) {
@@ -1112,6 +1149,17 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
11121149 $ this ->flush ();
11131150 $ this ->last_mysql_query = $ query ;
11141151
1152+ /*
1153+ * Push the stashed per-connection state into the user-defined
1154+ * functions. flush() above reset their per-statement copies, while
1155+ * the session values below live on until replaced.
1156+ */
1157+ $ this ->user_defined_functions ->set_row_count_value ( $ this ->row_count_value );
1158+ $ this ->user_defined_functions ->set_last_insert_id_state (
1159+ $ this ->last_insert_id_session_value ,
1160+ $ this ->last_insert_id_session_is_set
1161+ );
1162+
11151163 try {
11161164 // Parse the MySQL query.
11171165 $ parser = $ this ->create_parser ( $ query );
@@ -1200,6 +1248,59 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
12001248 true
12011249 );
12021250
1251+ /*
1252+ * Stash the ROW_COUNT() value for the next statement and sync the
1253+ * LAST_INSERT_ID(expr) session value possibly set by this one.
1254+ */
1255+ if ( $ reports_affected_rows ) {
1256+ $ affected_rows = $ this ->last_affected_rows ;
1257+ if ( null === $ affected_rows && null !== $ this ->last_result_statement ) {
1258+ $ affected_rows = $ this ->last_result_statement ->rowCount ();
1259+ }
1260+ $ this ->row_count_value = (int ) $ affected_rows ;
1261+ } elseif (
1262+ null !== $ statement_node
1263+ && in_array (
1264+ $ statement_node ->rule_name ,
1265+ array ( 'selectStatement ' , 'showStatement ' ),
1266+ true
1267+ )
1268+ ) {
1269+ $ this ->row_count_value = -1 ;
1270+ } else {
1271+ $ this ->row_count_value = 0 ;
1272+ }
1273+
1274+ /*
1275+ * A successful INSERT or REPLACE generating a new AUTO_INCREMENT
1276+ * value resets the LAST_INSERT_ID() session value in MySQL.
1277+ * Approximate that by clearing our stash. Any other statement
1278+ * keeps the value possibly set through last_insert_id() while
1279+ * it was executing, and mirrors it to lastInsertId() like MySQL's
1280+ * mysql_insert_id() does.
1281+ */
1282+ if (
1283+ null !== $ statement_node
1284+ && in_array (
1285+ $ statement_node ->rule_name ,
1286+ array ( 'insertStatement ' , 'replaceStatement ' ),
1287+ true
1288+ )
1289+ ) {
1290+ $ this ->last_insert_id_session_value = null ;
1291+ $ this ->last_insert_id_session_is_set = false ;
1292+ } else {
1293+ $ session_state = $ this ->user_defined_functions ->get_last_insert_id_state ();
1294+ $ this ->last_insert_id_session_value = $ session_state [0 ];
1295+ $ this ->last_insert_id_session_is_set = $ session_state [1 ];
1296+ if (
1297+ $ this ->last_insert_id_session_is_set
1298+ && null !== $ this ->last_insert_id_session_value
1299+ ) {
1300+ $ this ->last_insert_id = (string ) $ this ->last_insert_id_session_value ;
1301+ }
1302+ }
1303+
12031304 $ stmt = new WP_MySQL_On_SQLite_Statement (
12041305 $ this ->last_result_statement ,
12051306 $ query ,
@@ -4439,6 +4540,8 @@ private function translate( $node ): ?string {
44394540 return $ this ->translate_runtime_function_call ( $ node );
44404541 case 'functionCall ' :
44414542 return $ this ->translate_function_call ( $ node );
4543+ case 'jsonFunction ' :
4544+ return $ this ->translate_json_function ( $ node );
44424545 case 'substringFunction ' :
44434546 $ nodes = $ node ->get_child_nodes ();
44444547 if ( count ( $ nodes ) === 2 ) {
@@ -5283,6 +5386,34 @@ private function translate_interval_arithmetic( string $expr, array $intervals )
52835386 );
52845387 }
52855388
5389+ /**
5390+ * Translate a MySQL JSON aggregate function to SQLite.
5391+ *
5392+ * JSON_ARRAYAGG() and JSON_OBJECTAGG() map onto SQLite's native
5393+ * json_group_array() and json_group_object() aggregates.
5394+ *
5395+ * NB: The driver's MySQL grammar rejects DISTINCT inside these
5396+ * aggregates, so only the basic forms are supported here.
5397+ *
5398+ * @param WP_Parser_Node $node The "jsonFunction" AST node.
5399+ * @return string The translated value.
5400+ */
5401+ private function translate_json_function ( WP_Parser_Node $ node ): string {
5402+ $ name_token = $ node ->get_first_child_token ();
5403+ $ args = array ();
5404+ foreach ( $ node ->get_child_nodes () as $ child ) {
5405+ $ args [] = $ this ->translate ( $ child );
5406+ }
5407+
5408+ if (
5409+ null !== $ name_token
5410+ && WP_MySQL_Lexer::JSON_ARRAYAGG_SYMBOL === $ name_token ->id
5411+ ) {
5412+ return 'JSON_GROUP_ARRAY( ' . implode ( ', ' , $ args ) . ') ' ;
5413+ }
5414+ return 'JSON_GROUP_OBJECT( ' . implode ( ', ' , $ args ) . ') ' ;
5415+ }
5416+
52865417 /**
52875418 * Translate a MySQL function call to SQLite.
52885419 *
@@ -5387,6 +5518,28 @@ private function translate_function_call( WP_Parser_Node $node ): string {
53875518 }
53885519 case 'VERSION ' :
53895520 return $ this ->quote_sqlite_value ( $ this ->get_mysql_server_version_string () );
5521+ case 'VERSION_WITH_UPDATE ' :
5522+ /*
5523+ * MySQL's four-part form: the three-part server version with
5524+ * an extra ".0", keeping the "-mysql-on-sqlite" suffix.
5525+ */
5526+ return $ this ->quote_sqlite_value (
5527+ sprintf (
5528+ '%s.0-mysql-on-sqlite-%s ' ,
5529+ $ this ->get_mysql_version_string (),
5530+ SQLITE_DRIVER_VERSION
5531+ )
5532+ );
5533+ case 'ANY_VALUE ' :
5534+ /*
5535+ * ANY_VALUE() only exists to satisfy ONLY_FULL_GROUP_BY.
5536+ * Approximate it with MIN(): any single grouped value is
5537+ * acceptable to MySQL, and MIN() is deterministic here.
5538+ */
5539+ if ( 1 !== count ( $ args ) ) {
5540+ return $ this ->translate_sequence ( $ node ->get_children () );
5541+ }
5542+ return 'MIN( ' . $ args [0 ] . ') ' ;
53905543 default :
53915544 return $ this ->translate_sequence ( $ node ->get_children () );
53925545 }
0 commit comments