Skip to content

Commit

Permalink
Add basic support for INSERT, UPDATE, REPLACE, DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
JanJakes committed Nov 20, 2024
1 parent 65afd67 commit 00ec46d
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/WP_SQLite_Driver_Translation_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,99 @@ public function testSelect(): void {
);
}

public function testInsert(): void {
$this->assertQuery(
'INSERT INTO "t" ( "c" ) VALUES ( 1 )',
'INSERT INTO t (c) VALUES (1)'
);

$this->assertQuery(
'INSERT INTO "s"."t" ( "c" ) VALUES ( 1 )',
'INSERT INTO s.t (c) VALUES (1)'
);

$this->assertQuery(
'INSERT INTO "t" ( "c1" , "c2" ) VALUES ( 1 , 2 )',
'INSERT INTO t (c1, c2) VALUES (1, 2)'
);

$this->assertQuery(
'INSERT INTO "t" ( "c" ) VALUES ( 1 ) , ( 2 )',
'INSERT INTO t (c) VALUES (1), (2)'
);

$this->assertQuery(
'INSERT INTO "t1" SELECT * FROM "t2"',
'INSERT INTO t1 SELECT * FROM t2'
);
}

public function testReplace(): void {
$this->assertQuery(
'REPLACE INTO "t" ( "c" ) VALUES ( 1 )',
'REPLACE INTO t (c) VALUES (1)'
);

$this->assertQuery(
'REPLACE INTO "s"."t" ( "c" ) VALUES ( 1 )',
'REPLACE INTO s.t (c) VALUES (1)'
);

$this->assertQuery(
'REPLACE INTO "t" ( "c1" , "c2" ) VALUES ( 1 , 2 )',
'REPLACE INTO t (c1, c2) VALUES (1, 2)'
);

$this->assertQuery(
'REPLACE INTO "t" ( "c" ) VALUES ( 1 ) , ( 2 )',
'REPLACE INTO t (c) VALUES (1), (2)'
);

$this->assertQuery(
'REPLACE INTO "t1" SELECT * FROM "t2"',
'REPLACE INTO t1 SELECT * FROM t2'
);
}

public function testUpdate(): void {
$this->assertQuery(
'UPDATE "t" SET "c" = 1',
'UPDATE t SET c = 1'
);

$this->assertQuery(
'UPDATE "s"."t" SET "c" = 1',
'UPDATE s.t SET c = 1'
);

$this->assertQuery(
'UPDATE "t" SET "c1" = 1 , "c2" = 2',
'UPDATE t SET c1 = 1, c2 = 2'
);

$this->assertQuery(
'UPDATE "t" SET "c" = 1 WHERE "c" = 2',
'UPDATE t SET c = 1 WHERE c = 2'
);
}

public function testDelete(): void {
$this->assertQuery(
'DELETE FROM "t"',
'DELETE FROM t'
);

$this->assertQuery(
'DELETE FROM "s"."t"',
'DELETE FROM s.t'
);

$this->assertQuery(
'DELETE FROM "t" WHERE "c" = 1',
'DELETE FROM t WHERE c = 1'
);
}

private function assertQuery( $expected, string $query ): void {
$driver = new WP_SQLite_Driver( new PDO( 'sqlite::memory:' ) );
$driver->query( $query );
Expand Down
17 changes: 17 additions & 0 deletions wp-includes/sqlite-ast/class-wp-sqlite-driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,23 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
$stmt->fetchAll( $this->pdo_fetch_mode )
);
break;
case 'insertStatement':
case 'updateStatement':
case 'replaceStatement':
case 'deleteStatement':
if ( 'insertStatement' === $ast->rule_name ) {
$this->query_type = 'INSERT';
} elseif ( 'updateStatement' === $ast->rule_name ) {
$this->query_type = 'UPDATE';
} elseif ( 'replaceStatement' === $ast->rule_name ) {
$this->query_type = 'REPLACE';
} elseif ( 'deleteStatement' === $ast->rule_name ) {
$this->query_type = 'DELETE';
}
$query = $this->translate( $ast );
$this->execute_sqlite_query( $query );
$this->set_result_from_affected_rows();
break;
default:
throw new Exception( sprintf( 'Unsupported statement type: "%s"', $ast->rule_name ) );
}
Expand Down

0 comments on commit 00ec46d

Please sign in to comment.