Skip to content

Commit d3297af

Browse files
committed
Add support for unquoted identifiers for system variable values
1 parent 27d1109 commit d3297af

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

tests/WP_SQLite_Driver_Tests.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6070,6 +6070,36 @@ public function testSessionSystemVariables(): void {
60706070
$this->assertSame( 'utf8mb4', $result[0]->{'@@session.character_set_client'} );
60716071
}
60726072

6073+
public function testSystemVariablesWithKeywords(): void {
6074+
$this->assertQuery( 'SET default_storage_engine = InnoDB' );
6075+
$result = $this->assertQuery( 'SELECT @@default_storage_engine' );
6076+
$this->assertSame( 'InnoDB', $result[0]->{'@@default_storage_engine'} );
6077+
6078+
$this->assertQuery( 'SET default_collation_for_utf8mb4 = utf8mb4_0900_ai_ci' );
6079+
$result = $this->assertQuery( 'SELECT @@default_collation_for_utf8mb4' );
6080+
$this->assertSame( 'utf8mb4_0900_ai_ci', $result[0]->{'@@default_collation_for_utf8mb4'} );
6081+
6082+
$this->assertQuery( 'SET resultset_metadata = FULL' );
6083+
$result = $this->assertQuery( 'SELECT @@resultset_metadata' );
6084+
$this->assertSame( 'FULL', $result[0]->{'@@resultset_metadata'} );
6085+
6086+
$this->assertQuery( 'SET session_track_gtids = OWN_GTID' );
6087+
$result = $this->assertQuery( 'SELECT @@session_track_gtids' );
6088+
$this->assertSame( 'OWN_GTID', $result[0]->{'@@session_track_gtids'} );
6089+
6090+
$this->assertQuery( 'SET session_track_transaction_info = STATE' );
6091+
$result = $this->assertQuery( 'SELECT @@session_track_transaction_info' );
6092+
$this->assertSame( 'STATE', $result[0]->{'@@session_track_transaction_info'} );
6093+
6094+
$this->assertQuery( 'SET transaction_isolation = SERIALIZABLE' );
6095+
$result = $this->assertQuery( 'SELECT @@transaction_isolation' );
6096+
$this->assertSame( 'SERIALIZABLE', $result[0]->{'@@transaction_isolation'} );
6097+
6098+
$this->assertQuery( 'SET use_secondary_engine = FORCED' );
6099+
$result = $this->assertQuery( 'SELECT @@use_secondary_engine' );
6100+
$this->assertSame( 'FORCED', $result[0]->{'@@use_secondary_engine'} );
6101+
}
6102+
60736103
public function testUserVariables(): void {
60746104
$this->assertQuery( 'SET @my_var = 1' );
60756105
$result = $this->assertQuery( 'SELECT @my_var' );

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,31 @@ private function execute_set_system_variable_statement(
21882188
$type = $var_ident_type->get_first_child_token()->id;
21892189
}
21902190

2191-
$value = $this->evaluate_expression( $value_node );
2191+
/*
2192+
* Some MySQL system variables values can be set using an unquoted pure
2193+
* identifier rather than a string literal. This includes non-reserved
2194+
* keywords. This is equivalent to using a corresponding string literal.
2195+
*
2196+
* For example, the following statement pairs are equivalent:
2197+
*
2198+
* SET default_storage_engine = InnoDB
2199+
* SET default_storage_engine = 'InnoDB'
2200+
*
2201+
* SET default_collation_for_utf8mb4 = utf8mb4_0900_ai_ci
2202+
* SET default_collation_for_utf8mb4 = 'utf8mb4_0900_ai_ci'
2203+
*
2204+
* In this cases, we need to use the value directly without attempting
2205+
* to evaluate the expression, as that would result in a query error.
2206+
* In the grammar, unquoted identifiers are captured by "columnRef".
2207+
*/
2208+
$identifier = $this->translate( $value_node->get_first_descendant_node( 'columnRef' ) );
2209+
if ( $identifier && $identifier === $this->translate( $value_node ) ) {
2210+
$value = $this->unquote_sqlite_identifier( $identifier );
2211+
} elseif ( ! $value_node->has_child_node( 'expr' ) ) {
2212+
$value = $this->unquote_sqlite_identifier( $this->translate( $value_node ) );
2213+
} else {
2214+
$value = $this->evaluate_expression( $value_node );
2215+
}
21922216

21932217
if ( WP_MySQL_Lexer::SESSION_SYMBOL === $type ) {
21942218
if ( 'sql_mode' === $name ) {

0 commit comments

Comments
 (0)