Skip to content

Commit 77e052e

Browse files
committed
Implement PDO::exec() API
1 parent bc77336 commit 77e052e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

tests/WP_MySQL_On_SQLite_PDO_API_Tests.php renamed to tests/WP_PDO_MySQL_On_SQLite_PDO_API_Tests.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,38 @@ public function test_query(): void {
2121
$this->assertEquals( 1, $result->fetchColumn() );
2222
}
2323

24+
public function test_exec(): void {
25+
$result = $this->driver->exec( 'SELECT 1' );
26+
$this->assertEquals( 0, $result );
27+
28+
$result = $this->driver->exec( 'CREATE TABLE t (id INT)' );
29+
$this->assertEquals( 0, $result );
30+
31+
$result = $this->driver->exec( 'INSERT INTO t (id) VALUES (1)' );
32+
$this->assertEquals( 1, $result );
33+
34+
$result = $this->driver->exec( 'INSERT INTO t (id) VALUES (2), (3)' );
35+
$this->assertEquals( 2, $result );
36+
37+
$result = $this->driver->exec( 'UPDATE t SET id = 10 + id WHERE id = 0' );
38+
$this->assertEquals( 0, $result );
39+
40+
$result = $this->driver->exec( 'UPDATE t SET id = 10 + id WHERE id = 1' );
41+
$this->assertEquals( 1, $result );
42+
43+
$result = $this->driver->exec( 'UPDATE t SET id = 10 + id WHERE id < 10' );
44+
$this->assertEquals( 2, $result );
45+
46+
$result = $this->driver->exec( 'DELETE FROM t WHERE id = 11' );
47+
$this->assertEquals( 1, $result );
48+
49+
$result = $this->driver->exec( 'DELETE FROM t' );
50+
$this->assertEquals( 2, $result );
51+
52+
$result = $this->driver->exec( 'DROP TABLE t' );
53+
$this->assertEquals( 0, $result );
54+
}
55+
2456
public function test_begin_transaction(): void {
2557
$result = $this->driver->beginTransaction();
2658
$this->assertTrue( $result );

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,17 @@ public function query( string $query, ?int $fetch_mode = PDO::FETCH_COLUMN, ...$
792792
}
793793
}
794794

795+
/**
796+
* PDO API: Execute a MySQL statement and return the number of affected rows.
797+
*
798+
* @return int|false The number of affected rows or false on failure.
799+
*/
800+
#[ReturnTypeWillChange]
801+
public function exec( string $query ) {
802+
$stmt = $this->query( $query );
803+
return $stmt->rowCount();
804+
}
805+
795806
/**
796807
* PDO API: Begin a new transaction or nested transaction.
797808
*

0 commit comments

Comments
 (0)