Skip to content

Commit d36091c

Browse files
committed
Implement PDO::exec() API
1 parent 3e974b3 commit d36091c

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

tests/WP_PDO_MySQL_On_SQLite_PDO_API_Tests.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,38 @@ public function test_connection(): void {
1515
$this->assertInstanceOf( PDO::class, $driver );
1616
}
1717

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

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,8 @@ public function query( string $query, ?int $fetch_mode = PDO::FETCH_COLUMN, ...$
772772
$this->commit_wrapper_transaction();
773773
}
774774

775-
return new WP_PDO_Synthetic_Statement();
775+
$affected_rows = is_int( $this->last_return_value ) ? $this->last_return_value : 0;
776+
return new WP_PDO_Synthetic_Statement( $affected_rows );
776777
} catch ( Throwable $e ) {
777778
try {
778779
$this->rollback_user_transaction();
@@ -789,6 +790,17 @@ public function query( string $query, ?int $fetch_mode = PDO::FETCH_COLUMN, ...$
789790
}
790791
}
791792

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

wp-includes/sqlite-ast/class-wp-pdo-synthetic-statement.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ public function fetchAll( $mode = PDO::FETCH_DEFAULT, ...$args ): array {
8888
class WP_PDO_Synthetic_Statement extends PDOStatement {
8989
use WP_PDO_Synthetic_Statement_PHP_Compat;
9090

91+
/**
92+
* The number of affected rows.
93+
*
94+
* @var int
95+
*/
96+
private $affected_rows;
97+
98+
/**
99+
* Constructor.
100+
*
101+
* @param int $affected_rows The number of affected rows.
102+
*/
103+
public function __construct(
104+
int $affected_rows = 0
105+
) {
106+
$this->affected_rows = $affected_rows;
107+
}
108+
91109
/**
92110
* Execute a prepared statement.
93111
*
@@ -113,7 +131,7 @@ public function columnCount(): int {
113131
* @return int The number of rows affected by the statement.
114132
*/
115133
public function rowCount(): int {
116-
throw new RuntimeException( 'Not implemented' );
134+
return $this->affected_rows;
117135
}
118136

119137
/**

0 commit comments

Comments
 (0)