From f237134bf76d39f176cbac6629922ffd0e3ed025 Mon Sep 17 00:00:00 2001 From: Brandon Payton Date: Wed, 1 May 2024 15:53:04 -0400 Subject: [PATCH] Tolerate selecting MySQL system variables (#109) The purpose of this PR is to avoid errors when selecting MySQL system variables. Fixes #104. Related to: https://github.com/WordPress/wordpress-playground/issues/1272 - "UpdraftPlus plugins doesn't fully work in Playground." --- tests/WP_SQLite_Translator_Tests.php | 29 +++++++++++++++++++ .../sqlite/class-wp-sqlite-translator.php | 5 ++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/tests/WP_SQLite_Translator_Tests.php b/tests/WP_SQLite_Translator_Tests.php index 95b8db2..f0b7251 100644 --- a/tests/WP_SQLite_Translator_Tests.php +++ b/tests/WP_SQLite_Translator_Tests.php @@ -2247,4 +2247,33 @@ public function testDefaultNullValue() { $result ); } + + /** + * @dataProvider mysqlVariablesToTest + */ + public function testSelectVariable( $variable_name ) { + // Make sure the query does not error + $this->assertQuery( "SELECT $variable_name;" ); + } + + public static function mysqlVariablesToTest() { + return array( + // NOTE: This list was derived from the variables used by the UpdraftPlus plugin. + // We will start here and plan to expand supported variables over time. + array( '@@character_set_client' ), + array( '@@character_set_results' ), + array( '@@collation_connection' ), + array( '@@GLOBAL.gtid_purged' ), + array( '@@GLOBAL.log_bin' ), + array( '@@GLOBAL.log_bin_trust_function_creators' ), + array( '@@GLOBAL.sql_mode' ), + array( '@@SESSION.max_allowed_packet' ), + array( '@@SESSION.sql_mode' ), + + // Intentionally mix letter casing to help demonstrate case-insensitivity + array( '@@cHarActer_Set_cLient' ), + array( '@@gLoBAL.gTiD_purGed' ), + array( '@@sEssIOn.sqL_moDe' ), + ); + } } diff --git a/wp-includes/sqlite/class-wp-sqlite-translator.php b/wp-includes/sqlite/class-wp-sqlite-translator.php index 6ef1eed..542ee6c 100644 --- a/wp-includes/sqlite/class-wp-sqlite-translator.php +++ b/wp-includes/sqlite/class-wp-sqlite-translator.php @@ -1453,8 +1453,9 @@ private function execute_select() { $updated_query = $this->get_information_schema_query( $updated_query ); $params = array(); } elseif ( - strpos( $updated_query, '@@SESSION.sql_mode' ) !== false - || strpos( $updated_query, 'CONVERT( ' ) !== false + // Examples: @@SESSION.sql_mode, @@GLOBAL.max_allowed_packet, @@character_set_client + preg_match( '/@@((SESSION|GLOBAL)\s*\.\s*)?\w+\b/i', $updated_query ) === 1 || + strpos( $updated_query, 'CONVERT( ' ) !== false ) { /* * If the query contains a function that is not supported by SQLite,