Skip to content

Commit d47bb69

Browse files
committed
Implement PDO::exec() API
1 parent 011a628 commit d47bb69

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
@@ -20,6 +20,38 @@ public function test_query(): void {
2020
$this->assertInstanceOf( PDOStatement::class, $result );
2121
}
2222

23+
public function test_exec(): void {
24+
$result = $this->driver->exec( 'SELECT 1' );
25+
$this->assertEquals( 0, $result );
26+
27+
$result = $this->driver->exec( 'CREATE TABLE t (id INT)' );
28+
$this->assertEquals( 0, $result );
29+
30+
$result = $this->driver->exec( 'INSERT INTO t (id) VALUES (1)' );
31+
$this->assertEquals( 1, $result );
32+
33+
$result = $this->driver->exec( 'INSERT INTO t (id) VALUES (2), (3)' );
34+
$this->assertEquals( 2, $result );
35+
36+
$result = $this->driver->exec( 'UPDATE t SET id = 10 + id WHERE id = 0' );
37+
$this->assertEquals( 0, $result );
38+
39+
$result = $this->driver->exec( 'UPDATE t SET id = 10 + id WHERE id = 1' );
40+
$this->assertEquals( 1, $result );
41+
42+
$result = $this->driver->exec( 'UPDATE t SET id = 10 + id WHERE id < 10' );
43+
$this->assertEquals( 2, $result );
44+
45+
$result = $this->driver->exec( 'DELETE FROM t WHERE id = 11' );
46+
$this->assertEquals( 1, $result );
47+
48+
$result = $this->driver->exec( 'DELETE FROM t' );
49+
$this->assertEquals( 2, $result );
50+
51+
$result = $this->driver->exec( 'DROP TABLE t' );
52+
$this->assertEquals( 0, $result );
53+
}
54+
2355
public function test_begin_transaction(): void {
2456
$result = $this->driver->beginTransaction();
2557
$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
@@ -87,6 +87,24 @@ public function fetchAll( $mode = PDO::FETCH_DEFAULT, ...$args ): array {
8787
class WP_PDO_Synthetic_Statement extends PDOStatement {
8888
use WP_PDO_Synthetic_Statement_PHP_Compat;
8989

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

118136
/**

0 commit comments

Comments
 (0)