Skip to content

Commit 0e0dc86

Browse files
committed
PR 2: Suporte a INSERT e REPLACE sem a palavra-chave INTO
1 parent 783790c commit 0e0dc86

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

tests/WP_SQLite_Driver_Tests.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function setUp(): void {
2121
$this->engine->query(
2222
"CREATE TABLE _options (
2323
ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
24-
option_name TEXT NOT NULL default '',
24+
option_name TEXT NOT NULL UNIQUE default '',
2525
option_value TEXT NOT NULL default ''
2626
);"
2727
);
@@ -116,6 +116,29 @@ public function testInsertDateNow() {
116116
$this->assertEquals( gmdate( 'Y' ), $results[0]->y );
117117
}
118118

119+
public function testLockingClauses() {
120+
$this->assertQuery( "SELECT * FROM _options FOR UPDATE" );
121+
$this->assertQuery( "SELECT * FROM _options FOR UPDATE SKIP LOCKED" );
122+
$this->assertQuery( "SELECT * FROM _options FOR UPDATE NOWAIT" );
123+
$this->assertQuery( "SELECT * FROM _options LOCK IN SHARE MODE" );
124+
125+
// Complex queries.
126+
$this->assertQuery( "SELECT * FROM _options WHERE ID IN (SELECT ID FROM _options FOR UPDATE)" );
127+
$this->assertQuery( "SELECT * FROM _options UNION SELECT * FROM _options FOR UPDATE" );
128+
}
129+
130+
public function testInsertWithoutInto() {
131+
$this->assertQuery( "INSERT _options (option_name, option_value) VALUES ('test_name', 'test_value')" );
132+
$results = $this->engine->query( "SELECT * FROM _options WHERE option_name = 'test_name'" );
133+
$this->assertCount( 1, $results );
134+
$this->assertEquals( 'test_value', $results[0]->option_value );
135+
136+
$this->assertQuery( "REPLACE _options (option_name, option_value) VALUES ('test_name', 'replaced_value')" );
137+
$results = $this->engine->query( "SELECT * FROM _options WHERE option_name = 'test_name'" );
138+
$this->assertCount( 1, $results );
139+
$this->assertEquals( 'replaced_value', $results[0]->option_value );
140+
}
141+
119142
public function testUpdateWithLimit() {
120143
$this->assertQuery(
121144
"INSERT INTO _dates (option_name, option_value) VALUES ('first', '2003-05-27 00:00:45');"

wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,11 +1830,20 @@ private function execute_select_statement( WP_Parser_Node $node ): void {
18301830
private function execute_insert_or_replace_statement( WP_Parser_Node $node ): void {
18311831
$parts = array();
18321832
$on_conflict_update_list = null;
1833+
$has_into = false;
18331834
foreach ( $node->get_children() as $child ) {
18341835
$is_token = $child instanceof WP_MySQL_Token;
18351836
$is_node = $child instanceof WP_Parser_Node;
18361837

1838+
if ( $is_token && WP_MySQL_Lexer::INTO_SYMBOL === $child->id ) {
1839+
$has_into = true;
1840+
}
1841+
18371842
if ( $child instanceof WP_Parser_Node && 'tableRef' === $child->rule_name ) {
1843+
if ( ! $has_into ) {
1844+
$parts[] = 'INTO';
1845+
$has_into = true;
1846+
}
18381847
$database = $this->get_database_name( $child );
18391848
if ( 'information_schema' === strtolower( $database ) ) {
18401849
throw $this->new_access_denied_to_information_schema_exception();

0 commit comments

Comments
 (0)