Skip to content

Commit 4d13b9c

Browse files
committed
Add support for on and off values for system variables
1 parent d3297af commit 4d13b9c

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

tests/WP_SQLite_Driver_Tests.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6100,6 +6100,131 @@ public function testSystemVariablesWithKeywords(): void {
61006100
$this->assertSame( 'FORCED', $result[0]->{'@@use_secondary_engine'} );
61016101
}
61026102

6103+
public function testSystemVariablesWithBooleanValues(): void {
6104+
$this->assertQuery( 'SET autocommit = ON, big_tables = OFF' );
6105+
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
6106+
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
6107+
$this->assertSame( '0', $result[0]->{'@@big_tables'} );
6108+
6109+
$this->assertQuery( 'SET autocommit = on, big_tables = off' );
6110+
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
6111+
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
6112+
$this->assertSame( '0', $result[0]->{'@@big_tables'} );
6113+
6114+
$this->assertQuery( "SET autocommit = 'ON', big_tables = 'OFF'" );
6115+
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
6116+
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
6117+
$this->assertSame( '0', $result[0]->{'@@big_tables'} );
6118+
6119+
$this->assertQuery( "SET autocommit = 'on', big_tables = 'off'" );
6120+
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
6121+
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
6122+
$this->assertSame( '0', $result[0]->{'@@big_tables'} );
6123+
6124+
$this->assertQuery( 'SET autocommit = TRUE, big_tables = FALSE' );
6125+
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
6126+
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
6127+
$this->assertSame( '0', $result[0]->{'@@big_tables'} );
6128+
6129+
$this->assertQuery( 'SET autocommit = true, big_tables = false' );
6130+
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
6131+
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
6132+
$this->assertSame( '0', $result[0]->{'@@big_tables'} );
6133+
6134+
$this->assertQuery( 'SET autocommit = 1, big_tables = 0' );
6135+
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
6136+
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
6137+
$this->assertSame( '0', $result[0]->{'@@big_tables'} );
6138+
}
6139+
6140+
public function testSystemVariablesWithOnOffValues(): void {
6141+
$this->assertQuery( 'SET autocommit = ON' );
6142+
$result = $this->assertQuery( 'SELECT @@autocommit' );
6143+
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
6144+
6145+
$this->assertQuery( 'SET big_tables = OFF' );
6146+
$result = $this->assertQuery( 'SELECT @@big_tables' );
6147+
$this->assertSame( '0', $result[0]->{'@@big_tables'} );
6148+
6149+
$this->assertQuery( 'SET end_markers_in_json = ON' );
6150+
$result = $this->assertQuery( 'SELECT @@end_markers_in_json' );
6151+
$this->assertSame( '1', $result[0]->{'@@end_markers_in_json'} );
6152+
6153+
$this->assertQuery( 'SET explicit_defaults_for_timestamp = OFF' );
6154+
$result = $this->assertQuery( 'SELECT @@explicit_defaults_for_timestamp' );
6155+
$this->assertSame( '0', $result[0]->{'@@explicit_defaults_for_timestamp'} );
6156+
6157+
$this->assertQuery( 'SET keep_files_on_create = ON' );
6158+
$result = $this->assertQuery( 'SELECT @@keep_files_on_create' );
6159+
$this->assertSame( '1', $result[0]->{'@@keep_files_on_create'} );
6160+
6161+
$this->assertQuery( 'SET old_alter_table = OFF' );
6162+
$result = $this->assertQuery( 'SELECT @@old_alter_table' );
6163+
$this->assertSame( '0', $result[0]->{'@@old_alter_table'} );
6164+
6165+
$this->assertQuery( 'SET print_identified_with_as_hex = ON' );
6166+
$result = $this->assertQuery( 'SELECT @@print_identified_with_as_hex' );
6167+
$this->assertSame( '1', $result[0]->{'@@print_identified_with_as_hex'} );
6168+
6169+
$this->assertQuery( 'SET require_row_format = OFF' );
6170+
$result = $this->assertQuery( 'SELECT @@require_row_format' );
6171+
$this->assertSame( '0', $result[0]->{'@@require_row_format'} );
6172+
6173+
$this->assertQuery( 'SET select_into_disk_sync = ON' );
6174+
$result = $this->assertQuery( 'SELECT @@select_into_disk_sync' );
6175+
$this->assertSame( '1', $result[0]->{'@@select_into_disk_sync'} );
6176+
6177+
$this->assertQuery( 'SET session_track_gtids = OFF' );
6178+
$result = $this->assertQuery( 'SELECT @@session_track_gtids' );
6179+
// @TODO: For session_track_gtids, the value should be OFF, not 0.
6180+
//$this->assertSame( 'OFF', $result[0]->{'@@session_track_gtids'} );
6181+
6182+
$this->assertQuery( 'SET session_track_schema = ON' );
6183+
$result = $this->assertQuery( 'SELECT @@session_track_schema' );
6184+
$this->assertSame( '1', $result[0]->{'@@session_track_schema'} );
6185+
6186+
$this->assertQuery( 'SET session_track_state_change = OFF' );
6187+
$result = $this->assertQuery( 'SELECT @@session_track_state_change' );
6188+
$this->assertSame( '0', $result[0]->{'@@session_track_state_change'} );
6189+
6190+
$this->assertQuery( 'SET session_track_transaction_info = OFF' );
6191+
$result = $this->assertQuery( 'SELECT @@session_track_transaction_info' );
6192+
// @TODO: For session_track_transaction_info, the value should be OFF, not 0.
6193+
//$this->assertSame( 'OFF', $result[0]->{'@@session_track_transaction_info'} );
6194+
6195+
$this->assertQuery( 'SET show_create_table_skip_secondary_engine = ON' );
6196+
$result = $this->assertQuery( 'SELECT @@show_create_table_skip_secondary_engine' );
6197+
$this->assertSame( '1', $result[0]->{'@@show_create_table_skip_secondary_engine'} );
6198+
6199+
$this->assertQuery( 'SET show_create_table_verbosity = OFF' );
6200+
$result = $this->assertQuery( 'SELECT @@show_create_table_verbosity' );
6201+
$this->assertSame( '0', $result[0]->{'@@show_create_table_verbosity'} );
6202+
6203+
$this->assertQuery( 'SET sql_auto_is_null = ON' );
6204+
$result = $this->assertQuery( 'SELECT @@sql_auto_is_null' );
6205+
$this->assertSame( '1', $result[0]->{'@@sql_auto_is_null'} );
6206+
6207+
$this->assertQuery( 'SET sql_big_selects = OFF' );
6208+
$result = $this->assertQuery( 'SELECT @@sql_big_selects' );
6209+
$this->assertSame( '0', $result[0]->{'@@sql_big_selects'} );
6210+
6211+
$this->assertQuery( 'SET sql_buffer_result = ON' );
6212+
$result = $this->assertQuery( 'SELECT @@sql_buffer_result' );
6213+
$this->assertSame( '1', $result[0]->{'@@sql_buffer_result'} );
6214+
6215+
$this->assertQuery( 'SET sql_safe_updates = OFF' );
6216+
$result = $this->assertQuery( 'SELECT @@sql_safe_updates' );
6217+
$this->assertSame( '0', $result[0]->{'@@sql_safe_updates'} );
6218+
6219+
$this->assertQuery( 'SET sql_warnings = ON' );
6220+
$result = $this->assertQuery( 'SELECT @@sql_warnings' );
6221+
$this->assertSame( '1', $result[0]->{'@@sql_warnings'} );
6222+
6223+
$this->assertQuery( 'SET transaction_read_only = OFF' );
6224+
$result = $this->assertQuery( 'SELECT @@transaction_read_only' );
6225+
$this->assertSame( '0', $result[0]->{'@@transaction_read_only'} );
6226+
}
6227+
61036228
public function testUserVariables(): void {
61046229
$this->assertQuery( 'SET @my_var = 1' );
61056230
$result = $this->assertQuery( 'SELECT @my_var' );

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,23 @@ private function execute_set_system_variable_statement(
22142214
$value = $this->evaluate_expression( $value_node );
22152215
}
22162216

2217+
/*
2218+
* Handle ON/OFF values. They are accepted as both strings and keywords.
2219+
*
2220+
* @TODO: This is actually variable-specific and depends on the its type.
2221+
* For example:
2222+
* SET autocommit = OFF; SELECT @@autocommit; -> 0
2223+
* SET autocommit = false; SELECT @@autocommit; -> 0
2224+
* SET session_track_gtids = OFF; SELECT @@session_track_gtids; -> OFF
2225+
* SET session_track_gtids = false; SELECT @@session_track_gtids; -> OFF
2226+
* SET updatable_views_with_limit = OFF; ERROR 1231 (42000)
2227+
* SET updatable_views_with_limit = false; SELECT @@updatable_views_with_limit; -> NO
2228+
*/
2229+
$lowercase_value = strtolower( $value );
2230+
if ( 'on' === $lowercase_value || 'off' === $lowercase_value ) {
2231+
$value = 'on' === $lowercase_value ? 1 : 0;
2232+
}
2233+
22172234
if ( WP_MySQL_Lexer::SESSION_SYMBOL === $type ) {
22182235
if ( 'sql_mode' === $name ) {
22192236
$modes = explode( ',', strtoupper( $value ) );

0 commit comments

Comments
 (0)