Skip to content

Commit 00ec46d

Browse files
committed
Add basic support for INSERT, UPDATE, REPLACE, DELETE
1 parent 65afd67 commit 00ec46d

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

tests/WP_SQLite_Driver_Translation_Tests.php

+93
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,99 @@ public function testSelect(): void {
8686
);
8787
}
8888

89+
public function testInsert(): void {
90+
$this->assertQuery(
91+
'INSERT INTO "t" ( "c" ) VALUES ( 1 )',
92+
'INSERT INTO t (c) VALUES (1)'
93+
);
94+
95+
$this->assertQuery(
96+
'INSERT INTO "s"."t" ( "c" ) VALUES ( 1 )',
97+
'INSERT INTO s.t (c) VALUES (1)'
98+
);
99+
100+
$this->assertQuery(
101+
'INSERT INTO "t" ( "c1" , "c2" ) VALUES ( 1 , 2 )',
102+
'INSERT INTO t (c1, c2) VALUES (1, 2)'
103+
);
104+
105+
$this->assertQuery(
106+
'INSERT INTO "t" ( "c" ) VALUES ( 1 ) , ( 2 )',
107+
'INSERT INTO t (c) VALUES (1), (2)'
108+
);
109+
110+
$this->assertQuery(
111+
'INSERT INTO "t1" SELECT * FROM "t2"',
112+
'INSERT INTO t1 SELECT * FROM t2'
113+
);
114+
}
115+
116+
public function testReplace(): void {
117+
$this->assertQuery(
118+
'REPLACE INTO "t" ( "c" ) VALUES ( 1 )',
119+
'REPLACE INTO t (c) VALUES (1)'
120+
);
121+
122+
$this->assertQuery(
123+
'REPLACE INTO "s"."t" ( "c" ) VALUES ( 1 )',
124+
'REPLACE INTO s.t (c) VALUES (1)'
125+
);
126+
127+
$this->assertQuery(
128+
'REPLACE INTO "t" ( "c1" , "c2" ) VALUES ( 1 , 2 )',
129+
'REPLACE INTO t (c1, c2) VALUES (1, 2)'
130+
);
131+
132+
$this->assertQuery(
133+
'REPLACE INTO "t" ( "c" ) VALUES ( 1 ) , ( 2 )',
134+
'REPLACE INTO t (c) VALUES (1), (2)'
135+
);
136+
137+
$this->assertQuery(
138+
'REPLACE INTO "t1" SELECT * FROM "t2"',
139+
'REPLACE INTO t1 SELECT * FROM t2'
140+
);
141+
}
142+
143+
public function testUpdate(): void {
144+
$this->assertQuery(
145+
'UPDATE "t" SET "c" = 1',
146+
'UPDATE t SET c = 1'
147+
);
148+
149+
$this->assertQuery(
150+
'UPDATE "s"."t" SET "c" = 1',
151+
'UPDATE s.t SET c = 1'
152+
);
153+
154+
$this->assertQuery(
155+
'UPDATE "t" SET "c1" = 1 , "c2" = 2',
156+
'UPDATE t SET c1 = 1, c2 = 2'
157+
);
158+
159+
$this->assertQuery(
160+
'UPDATE "t" SET "c" = 1 WHERE "c" = 2',
161+
'UPDATE t SET c = 1 WHERE c = 2'
162+
);
163+
}
164+
165+
public function testDelete(): void {
166+
$this->assertQuery(
167+
'DELETE FROM "t"',
168+
'DELETE FROM t'
169+
);
170+
171+
$this->assertQuery(
172+
'DELETE FROM "s"."t"',
173+
'DELETE FROM s.t'
174+
);
175+
176+
$this->assertQuery(
177+
'DELETE FROM "t" WHERE "c" = 1',
178+
'DELETE FROM t WHERE c = 1'
179+
);
180+
}
181+
89182
private function assertQuery( $expected, string $query ): void {
90183
$driver = new WP_SQLite_Driver( new PDO( 'sqlite::memory:' ) );
91184
$driver->query( $query );

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

+17
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,23 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
699699
$stmt->fetchAll( $this->pdo_fetch_mode )
700700
);
701701
break;
702+
case 'insertStatement':
703+
case 'updateStatement':
704+
case 'replaceStatement':
705+
case 'deleteStatement':
706+
if ( 'insertStatement' === $ast->rule_name ) {
707+
$this->query_type = 'INSERT';
708+
} elseif ( 'updateStatement' === $ast->rule_name ) {
709+
$this->query_type = 'UPDATE';
710+
} elseif ( 'replaceStatement' === $ast->rule_name ) {
711+
$this->query_type = 'REPLACE';
712+
} elseif ( 'deleteStatement' === $ast->rule_name ) {
713+
$this->query_type = 'DELETE';
714+
}
715+
$query = $this->translate( $ast );
716+
$this->execute_sqlite_query( $query );
717+
$this->set_result_from_affected_rows();
718+
break;
702719
default:
703720
throw new Exception( sprintf( 'Unsupported statement type: "%s"', $ast->rule_name ) );
704721
}

0 commit comments

Comments
 (0)