diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php index ceeaefdf8..88a7df6fa 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php @@ -605,6 +605,42 @@ class WP_MySQL_On_SQLite extends PDO { */ private $last_insert_id = '0'; + /** + * Rows changed by the previous row-changing statement, for ROW_COUNT(). + * + * MySQL's ROW_COUNT() reports the rows changed by the preceding INSERT, + * REPLACE, UPDATE, or DELETE statement; -1 when the preceding statement + * was a SELECT or SHOW; 0 for any other statement; -1 before any + * statement ran. + * + * This is deliberately not derived from SQLite's CHANGES(): a single + * MySQL statement can translate to multiple SQLite writes (e.g. a + * multi-table DELETE reports the sum of N DELETEs, while CHANGES() + * would only see the last one). + * + * @var int + */ + private $row_count_value = -1; + + /** + * Session value set by the most recent LAST_INSERT_ID(expr) call. + * + * MySQL scopes this to the session: it survives across statements until + * the next LAST_INSERT_ID(expr) call or an INSERT/REPLACE generating a + * new AUTO_INCREMENT value. Mirrored into the user-defined functions + * before every query and read back afterwards (see query()). + * + * @var mixed + */ + private $last_insert_id_session_value = null; + + /** + * Whether an explicit LAST_INSERT_ID(expr) session value is stored. + * + * @var bool + */ + private $last_insert_id_session_is_set = false; + /** * User-defined functions registered on the SQLite connection. * @@ -955,6 +991,7 @@ function ( $key ) { // Register SQLite functions. $this->user_defined_functions = WP_SQLite_PDO_User_Defined_Functions::register_for( $this->connection->get_pdo() ); + $this->user_defined_functions->set_connection( $this->connection->get_pdo() ); // Load MySQL grammar. if ( null === self::$mysql_grammar ) { @@ -1112,6 +1149,17 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar $this->flush(); $this->last_mysql_query = $query; + /* + * Push the stashed per-connection state into the user-defined + * functions. flush() above reset their per-statement copies, while + * the session values below live on until replaced. + */ + $this->user_defined_functions->set_row_count_value( $this->row_count_value ); + $this->user_defined_functions->set_last_insert_id_state( + $this->last_insert_id_session_value, + $this->last_insert_id_session_is_set + ); + try { // Parse the MySQL query. $parser = $this->create_parser( $query ); @@ -1200,6 +1248,89 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar true ); + /* + * Stash the ROW_COUNT() value for the next statement and sync the + * LAST_INSERT_ID(expr) session value possibly set by this one. + */ + if ( $reports_affected_rows ) { + $affected_rows = $this->last_affected_rows; + if ( null === $affected_rows && null !== $this->last_result_statement ) { + $affected_rows = $this->last_result_statement->rowCount(); + } + $this->row_count_value = (int) $affected_rows; + } elseif ( + null !== $statement_node + && in_array( + $statement_node->rule_name, + array( 'selectStatement', 'showStatement' ), + true + ) + ) { + $this->row_count_value = -1; + } else { + $this->row_count_value = 0; + } + + /* + * A successful INSERT or REPLACE sets the LAST_INSERT_ID() session + * value, unless the statement set one itself with + * LAST_INSERT_ID(expr). The upsert idiom + * + * INSERT ... ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id) + * + * relies on that: MySQL keeps returning the updated row's id. + * Any other statement keeps the value possibly set through + * last_insert_id() while it was executing, and mirrors it to + * lastInsertId() like MySQL's mysql_insert_id() does. + * + * The generated id is copied into the session state here rather + * than left to fall back on the raw SQLite connection state. The + * driver issues its own writes (information schema bookkeeping), + * which would otherwise replace the id from the user's INSERT. + * MySQL reports the FIRST id of a multi-row insert while SQLite + * reports the last row id, so the first is derived from the + * affected row count. + */ + $is_insert_or_replace = null !== $statement_node + && in_array( + $statement_node->rule_name, + array( 'insertStatement', 'replaceStatement' ), + true + ); + if ( $is_insert_or_replace ) { + if ( $this->user_defined_functions->has_last_insert_id_override() ) { + $session_state = $this->user_defined_functions->get_last_insert_id_state(); + $this->last_insert_id_session_value = $session_state[0]; + $this->last_insert_id_session_is_set = $session_state[1]; + } else { + $generated = (int) $this->last_insert_id; + $rows = null !== $this->last_affected_rows + ? (int) $this->last_affected_rows + : ( null !== $this->last_result_statement ? (int) $this->last_result_statement->rowCount() : 0 ); + if ( $generated > 0 && $rows > 1 ) { + $generated -= ( $rows - 1 ); + } + if ( $generated > 0 ) { + $this->last_insert_id_session_value = $generated; + $this->last_insert_id_session_is_set = true; + $this->last_insert_id = (string) $generated; + } else { + $this->last_insert_id_session_value = null; + $this->last_insert_id_session_is_set = false; + } + } + } else { + $session_state = $this->user_defined_functions->get_last_insert_id_state(); + $this->last_insert_id_session_value = $session_state[0]; + $this->last_insert_id_session_is_set = $session_state[1]; + if ( + $this->last_insert_id_session_is_set + && null !== $this->last_insert_id_session_value + ) { + $this->last_insert_id = (string) $this->last_insert_id_session_value; + } + } + $stmt = new WP_MySQL_On_SQLite_Statement( $this->last_result_statement, $query, @@ -4439,6 +4570,8 @@ private function translate( $node ): ?string { return $this->translate_runtime_function_call( $node ); case 'functionCall': return $this->translate_function_call( $node ); + case 'jsonFunction': + return $this->translate_json_function( $node ); case 'substringFunction': $nodes = $node->get_child_nodes(); if ( count( $nodes ) === 2 ) { @@ -5283,6 +5416,47 @@ private function translate_interval_arithmetic( string $expr, array $intervals ) ); } + /** + * Translate a MySQL JSON aggregate function to SQLite. + * + * JSON_ARRAYAGG() and JSON_OBJECTAGG() map onto SQLite's native + * json_group_array() and json_group_object() aggregates. + * + * NB: The driver's MySQL grammar rejects DISTINCT inside these + * aggregates, so only the basic forms are supported here. + * + * @param WP_Parser_Node $node The "jsonFunction" AST node. + * @return string The translated value. + */ + private function translate_json_function( WP_Parser_Node $node ): string { + $name_token = $node->get_first_child_token(); + $args = array(); + /* + * jsonFunction places an optional windowingClause after the closing + * parenthesis. It is not an aggregate argument, so translating it as + * one would produce invalid SQLite such as + * "JSON_GROUP_ARRAY(value, OVER (...))". + */ + $windowing_clause = $node->get_first_child_node( 'windowingClause' ); + foreach ( $node->get_child_nodes() as $child ) { + if ( $child === $windowing_clause ) { + continue; + } + $args[] = $this->translate( $child ); + } + $window_sql = null === $windowing_clause + ? '' + : ' ' . $this->translate( $windowing_clause ); + + if ( + null !== $name_token + && WP_MySQL_Lexer::JSON_ARRAYAGG_SYMBOL === $name_token->id + ) { + return 'JSON_GROUP_ARRAY(' . implode( ', ', $args ) . ')' . $window_sql; + } + return 'JSON_GROUP_OBJECT(' . implode( ', ', $args ) . ')' . $window_sql; + } + /** * Translate a MySQL function call to SQLite. * @@ -5387,6 +5561,28 @@ private function translate_function_call( WP_Parser_Node $node ): string { } case 'VERSION': return $this->quote_sqlite_value( $this->get_mysql_server_version_string() ); + case 'VERSION_WITH_UPDATE': + /* + * MySQL's four-part form: the three-part server version with + * an extra ".0", keeping the "-mysql-on-sqlite" suffix. + */ + return $this->quote_sqlite_value( + sprintf( + '%s.0-mysql-on-sqlite-%s', + $this->get_mysql_version_string(), + SQLITE_DRIVER_VERSION + ) + ); + case 'ANY_VALUE': + /* + * ANY_VALUE() only exists to satisfy ONLY_FULL_GROUP_BY. + * Approximate it with MIN(): any single grouped value is + * acceptable to MySQL, and MIN() is deterministic here. + */ + if ( 1 !== count( $args ) ) { + return $this->translate_sequence( $node->get_children() ); + } + return 'MIN(' . $args[0] . ')'; default: return $this->translate_sequence( $node->get_children() ); } diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index 7b8828e7b..219e4a4e4 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -67,6 +67,12 @@ public static function register_for( $pdo ): self { 'greatest' => 'greatest', 'get_lock' => 'get_lock', 'release_lock' => 'release_lock', + 'is_free_lock' => 'is_free_lock', + 'is_used_lock' => 'is_used_lock', + 'release_all_locks' => 'release_all_locks', + 'connection_id' => 'connection_id', + 'last_insert_id' => 'last_insert_id', + 'row_count' => 'row_count', 'ucase' => 'ucase', 'lcase' => 'lcase', 'unhex' => 'unhex', @@ -110,13 +116,122 @@ public static function register_for( $pdo ): self { */ private $rand_last_seed = null; + /** + * PDO connection used for connection-state fallbacks. + * + * Currently used by last_insert_id() to fall back to the connection's + * most recent AUTO_INCREMENT value when no LAST_INSERT_ID(expr) + * session value was set. Never reset by flush(). + * + * @var PDO|null + */ + private $pdo = null; + + /** + * Session value set by the most recent LAST_INSERT_ID(expr) call. + * + * The driver mirrors its own session stash into this property before + * every query and reads it back afterwards, so the value survives + * across statements like in MySQL. + * + * @var mixed + */ + private $last_insert_id_value = null; + + /** + * Whether an explicit LAST_INSERT_ID(expr) session value is stored. + * + * @var bool + */ + private $last_insert_id_is_set = false; + + /** + * Whether LAST_INSERT_ID(expr) was called during the current statement. + * + * @var bool + */ + private $last_insert_id_set_in_statement = false; + + /** + * Stashed ROW_COUNT() value pushed by the driver before every query. + * + * @var int + */ + private $row_count_value = -1; + /** * Clear any per-statement state held by the UDFs. + * + * NB: The PDO connection is deliberately not reset here; it is set once + * via set_connection() and lives as long as the connection itself. */ public function flush(): void { - $this->rand_seed1 = null; - $this->rand_seed2 = null; - $this->rand_last_seed = null; + $this->rand_seed1 = null; + $this->rand_seed2 = null; + $this->rand_last_seed = null; + $this->last_insert_id_value = null; + $this->last_insert_id_is_set = false; + $this->last_insert_id_set_in_statement = false; + $this->row_count_value = -1; + } + + /** + * Whether LAST_INSERT_ID(expr) ran during the current statement. + * + * The driver uses this to keep a value the statement set itself. The + * common case is an upsert such as: + * + * INSERT ... ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id) + * + * where MySQL leaves LAST_INSERT_ID() returning the updated row's id. + * Without this flag, the value would be discarded when the INSERT ends. + * + * @return bool Whether the session value was set by the statement. + */ + public function has_last_insert_id_override(): bool { + return $this->last_insert_id_set_in_statement; + } + + /** + * Store the PDO connection for use by functions needing connection state. + * + * @param PDO $pdo The PDO connection. + */ + public function set_connection( PDO $pdo ): void { + $this->pdo = $pdo; + } + + /** + * Push the driver's stashed ROW_COUNT() value for the upcoming query. + * + * @param int $value Rows changed by the previous row-changing statement. + */ + public function set_row_count_value( $value ): void { + $this->row_count_value = (int) $value; + } + + /** + * Push the LAST_INSERT_ID(expr) session value for the upcoming query. + * + * @param mixed $value The stashed session value. + * @param bool $is_set Whether an explicit session value was stashed. + */ + public function set_last_insert_id_state( $value, $is_set ): void { + $this->last_insert_id_value = $value; + $this->last_insert_id_is_set = (bool) $is_set; + $this->last_insert_id_set_in_statement = false; + } + + /** + * Read back the LAST_INSERT_ID(expr) session value. + * + * The driver calls this after each query to persist a value possibly + * set through last_insert_id() while the query was executing. + * + * @return array An array of the session value and whether it was explicitly set. + */ + public function get_last_insert_id_state() { + return array( $this->last_insert_id_value, $this->last_insert_id_is_set ); } /** @@ -713,6 +828,111 @@ public function release_lock( $name ) { return '1=1'; } + /** + * Method to dummy out MySQL IS_FREE_LOCK() function. + * + * Named locks are never actually held here – GET_LOCK() is a stub that + * acquires nothing – so every named lock is always free. Returns 1 for + * a non-NULL name and NULL for a NULL name. + * + * @param string|null $name Not used, only checked for NULL. + * + * @return int|null 1 when the lock is free, NULL on NULL input. + */ + public function is_free_lock( $name ) { + if ( null === $name ) { + return null; + } + return 1; + } + + /** + * Method to dummy out MySQL IS_USED_LOCK() function. + * + * MySQL returns the connection id holding the lock, or NULL when the + * lock is free. Locks are never held here – GET_LOCK() is a stub – + * so this always returns NULL. + * + * @param string|null $name Not used. + * + * @return null + */ + public function is_used_lock( $name ) { + return null; + } + + /** + * Method to dummy out MySQL RELEASE_ALL_LOCKS() function. + * + * MySQL returns the number of released locks. No locks are ever held + * here – GET_LOCK() is a stub – so this always returns 0. + * + * @return int + */ + public function release_all_locks() { + return 0; + } + + /** + * Method to dummy out MySQL CONNECTION_ID() function. + * + * SQLite has no sessions or connection threads, so there is no real + * connection id to report. Return a constant, mirroring the dummy + * GET_LOCK()/RELEASE_LOCK() stubs. + * + * @return int + */ + public function connection_id() { + return 1; + } + + /** + * Method to emulate MySQL LAST_INSERT_ID() function. + * + * With an argument, stores the argument as the new session value and + * returns it. This is the read-back idiom used, e.g., by rate limiters: + * UPDATE t SET hits = LAST_INSERT_ID(hits + 1). + * + * Without an argument, returns the stored session value when one was + * set, or the connection's most recent AUTO_INCREMENT value otherwise. + * + * A NULL argument stores NULL and returns NULL, matching the + * NULL-in-NULL-out contract of the other user-defined functions. + * + * @param mixed $expr Optional. New session value to store and return. + * + * @return mixed The session value, or NULL. + */ + public function last_insert_id( $expr = null ) { + if ( func_num_args() > 0 ) { + $this->last_insert_id_value = $expr; + $this->last_insert_id_is_set = true; + $this->last_insert_id_set_in_statement = true; + return $expr; + } + if ( $this->last_insert_id_is_set ) { + return $this->last_insert_id_value; + } + if ( null === $this->pdo ) { + return null; + } + return $this->pdo->lastInsertId(); + } + + /** + * Method to emulate MySQL ROW_COUNT() function. + * + * Returns the value stashed by the driver before the query: the rows + * changed by the previous INSERT, REPLACE, UPDATE, or DELETE statement; + * -1 when the previous statement was a SELECT or SHOW; 0 for any other + * statement; -1 before any statement ran. + * + * @return int + */ + public function row_count() { + return $this->row_count_value; + } + /** * Method to emulate MySQL UCASE() function. * diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php index e28a880b3..56d152e80 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php @@ -855,8 +855,10 @@ public function test_last_insert_id(): void { $this->assertSame( '1', $this->driver->lastInsertId() ); $this->assertSame( '1', $this->driver->lastInsertId( 'ignored_sequence_name' ) ); + // DDL does not clear the value: MySQL keeps returning the last + // generated id after a CREATE TABLE. $this->driver->query( 'CREATE TABLE another_table (id INT)' ); - $this->assertSame( '0', $this->driver->lastInsertId() ); + $this->assertSame( '1', $this->driver->lastInsertId() ); } public function test_last_insert_id_rejects_invalid_sequence_name(): void { diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index fff5bc6b5..af08b0d9f 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -6793,6 +6793,212 @@ public function testLastInsertId(): void { $this->assertSame( '2', $this->engine->lastInsertId() ); } + public function testLastInsertIdFunction(): void { + $this->assertQuery( + 'CREATE TABLE t ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT, + hits INT + );' + ); + + // No explicit session value yet: falls back to the PDO last insert id. + $this->assertQuery( 'INSERT INTO t (hits) VALUES (10)' ); + $this->assertSame( '1', $this->engine->lastInsertId() ); + $result = $this->query( 'SELECT LAST_INSERT_ID() AS lid' ); + $this->assertSame( '1', $result[0]->lid ); + + // The read-back idiom: UPDATE ... SET hits = LAST_INSERT_ID(hits + 1). + $this->assertQuery( 'UPDATE t SET hits = LAST_INSERT_ID(hits + 1) WHERE ID = 1' ); + $result = $this->query( 'SELECT LAST_INSERT_ID() AS lid' ); + $this->assertSame( '11', $result[0]->lid ); + $this->assertSame( '11', $this->engine->lastInsertId() ); + $result = $this->query( 'SELECT hits AS h FROM t WHERE ID = 1' ); + $this->assertSame( '11', $result[0]->h ); + + // An explicit argument is stored as the new session value and returned. + $result = $this->query( 'SELECT LAST_INSERT_ID(42) AS lid' ); + $this->assertSame( '42', $result[0]->lid ); + $result = $this->query( 'SELECT LAST_INSERT_ID() AS lid' ); + $this->assertSame( '42', $result[0]->lid ); + + // A successful INSERT resets the session value to the new id. + $this->assertQuery( 'INSERT INTO t (hits) VALUES (20)' ); + $result = $this->query( 'SELECT LAST_INSERT_ID() AS lid' ); + $this->assertSame( '2', $result[0]->lid ); + + // NULL in, NULL out. + $result = $this->query( 'SELECT LAST_INSERT_ID(NULL) AS lid' ); + $this->assertNull( $result[0]->lid ); + $result = $this->query( 'SELECT LAST_INSERT_ID() AS lid' ); + $this->assertNull( $result[0]->lid ); + } + + public function testLastInsertIdSurvivesUpsert(): void { + $this->assertQuery( + 'CREATE TABLE t ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(20) + );' + ); + + // The upsert idiom: an INSERT that ends up updating a row reports the + // updated row's id through LAST_INSERT_ID(id). MySQL keeps that value + // after the statement, so the plain INSERT reset must not discard it. + $this->assertQuery( "INSERT INTO t (name) VALUES ('a')" ); + $this->assertQuery( + "INSERT INTO t (ID, name) VALUES (1, 'b') ON DUPLICATE KEY UPDATE name = 'c', ID = LAST_INSERT_ID(ID)" + ); + $result = $this->query( 'SELECT LAST_INSERT_ID() AS lid' ); + $this->assertSame( '1', $result[0]->lid ); + + // A plain INSERT does reset the value to the newly generated id. + $this->assertQuery( "INSERT INTO t (name) VALUES ('d')" ); + $result = $this->query( 'SELECT LAST_INSERT_ID() AS lid' ); + $this->assertSame( '2', $result[0]->lid ); + } + + public function testLastInsertIdMultiRowInsert(): void { + $this->assertQuery( + 'CREATE TABLE t ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(20) + );' + ); + + $this->assertQuery( + "INSERT INTO t (name) VALUES ('a'), ('b'), ('c')" + ); + + // MySQL reports the FIRST id generated by a multi-row insert. + $result = $this->query( 'SELECT LAST_INSERT_ID() AS lid' ); + $this->assertSame( '1', $result[0]->lid ); + + // The session value also survives statements that write internally. + $this->assertQuery( 'CREATE TABLE another (id INT)' ); + $result = $this->query( 'SELECT LAST_INSERT_ID() AS lid' ); + $this->assertSame( '1', $result[0]->lid ); + } + + public function testRowCountFunction(): void { + // No previous statement yet: MySQL reports -1. + $pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class; + $fresh = new WP_MySQL_On_SQLite( + 'mysql-on-sqlite:dbname=wp', + null, + null, + array( 'sqlite_pdo' => new $pdo_class( 'sqlite::memory:' ) ) + ); + $result = $fresh->query( 'SELECT ROW_COUNT()' )->fetchAll( PDO::FETCH_NUM ); + $this->assertSame( '-1', (string) $result[0][0] ); + + // The setUp() CREATE TABLEs change no rows: MySQL reports 0. + $result = $this->query( 'SELECT ROW_COUNT() AS rc' ); + $this->assertSame( '0', $result[0]->rc ); + + $this->assertQuery( + 'CREATE TABLE t ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT, + g VARCHAR(20), + v INT + );' + ); + $this->assertQuery( "INSERT INTO t (g, v) VALUES ('a', 1), ('a', 2), ('b', 3)" ); + $result = $this->query( 'SELECT ROW_COUNT() AS rc' ); + $this->assertSame( '3', $result[0]->rc ); + + $this->assertQuery( "UPDATE t SET v = v + 10 WHERE g = 'a'" ); + $result = $this->query( 'SELECT ROW_COUNT() AS rc' ); + $this->assertSame( '2', $result[0]->rc ); + + $this->assertQuery( "DELETE FROM t WHERE g = 'b'" ); + $result = $this->query( 'SELECT ROW_COUNT() AS rc' ); + $this->assertSame( '1', $result[0]->rc ); + + // SELECT is not row-changing: MySQL reports -1. + $this->assertQuery( 'SELECT v FROM t' ); + $result = $this->query( 'SELECT ROW_COUNT() AS rc' ); + $this->assertSame( '-1', $result[0]->rc ); + } + + public function testRowCountFunctionMultiTableDelete(): void { + $this->assertQuery( 'CREATE TABLE t1 (ID INTEGER PRIMARY KEY AUTO_INCREMENT, v INT);' ); + $this->assertQuery( 'CREATE TABLE t2 (ID INTEGER PRIMARY KEY AUTO_INCREMENT, v INT);' ); + $this->assertQuery( 'INSERT INTO t1 (v) VALUES (1), (2)' ); + $this->assertQuery( 'INSERT INTO t2 (v) VALUES (1), (2)' ); + + $this->assertQuery( 'DELETE t1, t2 FROM t1 JOIN t2 ON t1.ID = t2.ID' ); + $result = $this->query( 'SELECT ROW_COUNT() AS rc' ); + + // Two rows from each table. SQLite's CHANGES() would only report the + // last DELETE (2); the driver stashes the sum instead. + $this->assertSame( '4', $result[0]->rc ); + } + + public function testConnectionIdAndLockStubs(): void { + $result = $this->query( 'SELECT CONNECTION_ID() AS cid' ); + $this->assertSame( '1', $result[0]->cid ); + + // Named locks are never held: always free, never used, none released. + $result = $this->query( "SELECT IS_FREE_LOCK('foo') AS l" ); + $this->assertSame( '1', $result[0]->l ); + $result = $this->query( "SELECT IS_USED_LOCK('foo') AS l" ); + $this->assertNull( $result[0]->l ); + $result = $this->query( 'SELECT RELEASE_ALL_LOCKS() AS l' ); + $this->assertSame( '0', $result[0]->l ); + + // NULL in, NULL out. + $result = $this->query( 'SELECT IS_FREE_LOCK(NULL) AS l' ); + $this->assertNull( $result[0]->l ); + $result = $this->query( 'SELECT IS_USED_LOCK(NULL) AS l' ); + $this->assertNull( $result[0]->l ); + } + + public function testAnyValueAndJsonAggregates(): void { + $this->assertQuery( + 'CREATE TABLE t ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT, + g VARCHAR(20), + v INT + );' + ); + $this->assertQuery( "INSERT INTO t (g, v) VALUES ('a', 1), ('a', 2), ('b', 3)" ); + + // ANY_VALUE() is approximated with MIN(). + $result = $this->query( 'SELECT g AS g, ANY_VALUE(v) AS av FROM t GROUP BY g ORDER BY g' ); + $this->assertSame( 'a', $result[0]->g ); + $this->assertSame( '1', $result[0]->av ); + $this->assertSame( 'b', $result[1]->g ); + $this->assertSame( '3', $result[1]->av ); + + // JSON_ARRAYAGG() maps to SQLite's json_group_array(). + $result = $this->query( 'SELECT g AS g, JSON_ARRAYAGG(v) AS j FROM t GROUP BY g ORDER BY g' ); + $this->assertSame( 'a', $result[0]->g ); + $values = json_decode( $result[0]->j ); + sort( $values ); + $this->assertEquals( array( 1, 2 ), $values ); + $this->assertSame( 'b', $result[1]->g ); + $this->assertEquals( array( 3 ), json_decode( $result[1]->j ) ); + + // JSON_OBJECTAGG() maps to SQLite's json_group_object(). + $result = $this->query( 'SELECT JSON_OBJECTAGG(ID, v) AS j FROM t' ); + $this->assertEquals( + array( + '1' => 1, + '2' => 2, + '3' => 3, + ), + json_decode( $result[0]->j, true ) + ); + } + + public function testVersionWithUpdate(): void { + $result = $this->query( 'SELECT VERSION_WITH_UPDATE() AS v' ); + $this->assertSame( '8.0.38.0-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $result[0]->v ); + + $result = $this->query( 'SELECT VERSION() AS v' ); + $this->assertSame( '8.0.38-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $result[0]->v ); + } + public function testCharLength(): void { $this->assertQuery( 'CREATE TABLE t ( diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php index d0d522db4..38ea562de 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php @@ -2210,6 +2210,66 @@ public function testSelectHavingAmbiguousColumnResolution(): void { ); } + public function testMiscInfoLockAndAggregateFunctions(): void { + // ANY_VALUE(x) → MIN(x) + $this->assertQuery( + 'SELECT MIN(`v`) AS `ANY_VALUE(v)` FROM `t`', + 'SELECT ANY_VALUE(v) FROM t' + ); + $this->assertQuery( + 'SELECT `g` , MIN(`v`) AS `av` FROM `t` GROUP BY `g` ORDER BY `g`', + 'SELECT g, ANY_VALUE(v) AS av FROM t GROUP BY g ORDER BY g' + ); + + // JSON_ARRAYAGG(x) → JSON_GROUP_ARRAY(x) + $this->assertQuery( + 'SELECT JSON_GROUP_ARRAY(`v`) AS `JSON_ARRAYAGG(v)` FROM `t`', + 'SELECT JSON_ARRAYAGG(v) FROM t' + ); + $this->assertQuery( + 'SELECT `g` , JSON_GROUP_ARRAY(`v`) AS `j` FROM `t` GROUP BY `g` ORDER BY `g`', + 'SELECT g, JSON_ARRAYAGG(v) AS j FROM t GROUP BY g ORDER BY g' + ); + + // JSON_OBJECTAGG(k, v) → JSON_GROUP_OBJECT(k, v) + $this->assertQuery( + 'SELECT JSON_GROUP_OBJECT(`g`, `v`) AS `JSON_OBJECTAGG(g, v)` FROM `t`', + 'SELECT JSON_OBJECTAGG(g, v) FROM t' + ); + + // VERSION_WITH_UPDATE() → four-part version literal + $this->assertQuery( + sprintf( + "SELECT '8.0.38.0-mysql-on-sqlite-%s' AS `VERSION_WITH_UPDATE()`", + SQLITE_DRIVER_VERSION + ), + 'SELECT VERSION_WITH_UPDATE()' + ); + + // ROW_COUNT(), CONNECTION_ID(), LAST_INSERT_ID(), and the lock stubs + // pass through to the user-defined functions of the same name. + $this->assertQuery( + 'SELECT `ROW_COUNT` ( ) AS `ROW_COUNT()`', + 'SELECT ROW_COUNT()' + ); + $this->assertQuery( + 'SELECT `CONNECTION_ID` ( ) AS `CONNECTION_ID()`', + 'SELECT CONNECTION_ID()' + ); + $this->assertQuery( + 'SELECT `LAST_INSERT_ID` ( 5 ) AS `LAST_INSERT_ID(5)`', + 'SELECT LAST_INSERT_ID(5)' + ); + $this->assertQuery( + "SELECT `IS_FREE_LOCK` ( 'x' ) AS `IS_FREE_LOCK('x')`", + "SELECT IS_FREE_LOCK('x')" + ); + $this->assertQuery( + 'SELECT `RELEASE_ALL_LOCKS` ( ) AS `RELEASE_ALL_LOCKS()`', + 'SELECT RELEASE_ALL_LOCKS()' + ); + } + private function assertQuery( $expected, string $query ): void { $error = null; try {