Skip to content

Commit 16bf97e

Browse files
committed
Improve transaction behavior compatibility, add PDO-like APIs
1 parent b0ca303 commit 16bf97e

File tree

4 files changed

+297
-196
lines changed

4 files changed

+297
-196
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class WP_SQLite_Driver_PDO_API_Tests extends TestCase {
6+
/** @var WP_SQLite_Driver */
7+
private $driver;
8+
9+
public function setUp(): void {
10+
$connection = new WP_SQLite_Connection( array( 'path' => ':memory:' ) );
11+
$this->driver = new WP_SQLite_Driver( $connection, 'wp' );
12+
}
13+
14+
public function test_begin_transaction(): void {
15+
$result = $this->driver->beginTransaction();
16+
$this->assertTrue( $result );
17+
}
18+
19+
public function test_begin_transaction_already_active(): void {
20+
$this->driver->beginTransaction();
21+
22+
$this->expectException( PDOException::class );
23+
$this->expectExceptionMessage( 'There is already an active transaction' );
24+
$this->expectExceptionCode( 0 );
25+
$this->driver->beginTransaction();
26+
}
27+
28+
public function test_commit(): void {
29+
$this->driver->beginTransaction();
30+
$result = $this->driver->commit();
31+
$this->assertTrue( $result );
32+
}
33+
34+
public function test_commit_no_active_transaction(): void {
35+
$this->expectException( PDOException::class );
36+
$this->expectExceptionMessage( 'There is no active transaction' );
37+
$this->expectExceptionCode( 0 );
38+
$this->driver->commit();
39+
}
40+
41+
public function test_rollback(): void {
42+
$this->driver->beginTransaction();
43+
$result = $this->driver->rollBack();
44+
$this->assertTrue( $result );
45+
}
46+
47+
public function test_rollback_no_active_transaction(): void {
48+
$this->expectException( PDOException::class );
49+
$this->expectExceptionMessage( 'There is no active transaction' );
50+
$this->expectExceptionCode( 0 );
51+
$this->driver->rollBack();
52+
}
53+
}

tests/WP_SQLite_Driver_Tests.php

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,89 +2490,6 @@ public function testStartTransactionCommand() {
24902490
$this->assertCount( 0, $this->engine->get_query_results() );
24912491
}
24922492

2493-
public function testNestedTransactionWork() {
2494-
$this->assertQuery( 'BEGIN' );
2495-
$this->assertQuery( "INSERT INTO _options (option_name) VALUES ('first');" );
2496-
$this->assertQuery( 'START TRANSACTION' );
2497-
$this->assertQuery( "INSERT INTO _options (option_name) VALUES ('second');" );
2498-
$this->assertQuery( 'START TRANSACTION' );
2499-
$this->assertQuery( "INSERT INTO _options (option_name) VALUES ('third');" );
2500-
$this->assertQuery( 'SELECT * FROM _options;' );
2501-
$this->assertCount( 3, $this->engine->get_query_results() );
2502-
2503-
$this->assertQuery( 'ROLLBACK' );
2504-
$this->assertQuery( 'SELECT * FROM _options;' );
2505-
$this->assertCount( 2, $this->engine->get_query_results() );
2506-
2507-
$this->assertQuery( 'ROLLBACK' );
2508-
$this->assertQuery( 'SELECT * FROM _options;' );
2509-
$this->assertCount( 1, $this->engine->get_query_results() );
2510-
2511-
$this->assertQuery( 'COMMIT' );
2512-
$this->assertQuery( 'SELECT * FROM _options;' );
2513-
$this->assertCount( 1, $this->engine->get_query_results() );
2514-
}
2515-
2516-
public function testNestedTransactionWorkComplexModify() {
2517-
$this->assertQuery( 'BEGIN' );
2518-
// Create a complex ALTER Table query where the first
2519-
// column is added successfully, but the second fails.
2520-
// Behind the scenes, this single MySQL query is split
2521-
// into multiple SQLite queries – some of them will
2522-
// succeed, some will fail.
2523-
$error = '';
2524-
try {
2525-
$this->engine->query(
2526-
'
2527-
ALTER TABLE _options
2528-
ADD COLUMN test varchar(20),
2529-
ADD COLUMN test varchar(20)
2530-
'
2531-
);
2532-
} catch ( Throwable $e ) {
2533-
$error = $e->getMessage();
2534-
}
2535-
$this->assertStringContainsString( "Duplicate column name 'test'", $error );
2536-
2537-
// Commit the transaction.
2538-
$this->assertQuery( 'COMMIT' );
2539-
2540-
// Confirm the entire query failed atomically and no column was
2541-
// added to the table.
2542-
$this->assertQuery( 'DESCRIBE _options;' );
2543-
$fields = $this->engine->get_query_results();
2544-
2545-
$this->assertEquals(
2546-
array(
2547-
(object) array(
2548-
'Field' => 'ID',
2549-
'Type' => 'int',
2550-
'Null' => 'NO',
2551-
'Key' => 'PRI',
2552-
'Default' => null,
2553-
'Extra' => 'auto_increment',
2554-
),
2555-
(object) array(
2556-
'Field' => 'option_name',
2557-
'Type' => 'text',
2558-
'Null' => 'NO',
2559-
'Key' => '',
2560-
'Default' => '',
2561-
'Extra' => '',
2562-
),
2563-
(object) array(
2564-
'Field' => 'option_value',
2565-
'Type' => 'text',
2566-
'Null' => 'NO',
2567-
'Key' => '',
2568-
'Default' => '',
2569-
'Extra' => '',
2570-
),
2571-
),
2572-
$fields
2573-
);
2574-
}
2575-
25762493
public function testCount() {
25772494
$this->assertQuery( "INSERT INTO _options (option_name) VALUES ('first');" );
25782495
$this->assertQuery( "INSERT INTO _options (option_name) VALUES ('second');" );

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ public function query( string $sql, array $params = array() ): PDOStatement {
118118
return $stmt;
119119
}
120120

121+
/**
122+
* Prepare a SQLite query for execution.
123+
*
124+
* @param string $sql The query to prepare.
125+
* @return PDOStatement The prepared statement.
126+
* @throws PDOException When the query preparation fails.
127+
*/
128+
public function prepare( string $sql ): PDOStatement {
129+
if ( $this->query_logger ) {
130+
( $this->query_logger )( $sql, array() );
131+
}
132+
return $this->pdo->prepare( $sql );
133+
}
134+
121135
/**
122136
* Returns the ID of the last inserted row.
123137
*

0 commit comments

Comments
 (0)