Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 96 additions & 6 deletions tests/WP_SQLite_Driver_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4144,27 +4144,27 @@ public function getReservedPrefixTestData(): array {
return array(
array(
'SELECT * FROM _wp_sqlite_t',
"Invalid identifier `_wp_sqlite_t`, prefix '_wp_sqlite_' is reserved",
"Invalid identifier '_wp_sqlite_t', prefix '_wp_sqlite_' is reserved",
),
array(
'SELECT _wp_sqlite_t FROM t',
"Invalid identifier `_wp_sqlite_t`, prefix '_wp_sqlite_' is reserved",
"Invalid identifier '_wp_sqlite_t', prefix '_wp_sqlite_' is reserved",
),
array(
'SELECT t._wp_sqlite_t FROM t',
"Invalid identifier `t`.`_wp_sqlite_t`, prefix '_wp_sqlite_' is reserved",
"Invalid identifier '_wp_sqlite_t', prefix '_wp_sqlite_' is reserved",
),
array(
'CREATE TABLE _wp_sqlite_t (id INT)',
"Invalid identifier `_wp_sqlite_t`, prefix '_wp_sqlite_' is reserved",
"Invalid identifier '_wp_sqlite_t', prefix '_wp_sqlite_' is reserved",
),
array(
'ALTER TABLE _wp_sqlite_t ADD COLUMN name TEXT',
"Invalid identifier `_wp_sqlite_t`, prefix '_wp_sqlite_' is reserved",
"Invalid identifier '_wp_sqlite_t', prefix '_wp_sqlite_' is reserved",
),
array(
'DROP TABLE _wp_sqlite_t',
"Invalid identifier `_wp_sqlite_t`, prefix '_wp_sqlite_' is reserved",
"Invalid identifier '_wp_sqlite_t', prefix '_wp_sqlite_' is reserved",
),
);
}
Expand Down Expand Up @@ -6238,4 +6238,94 @@ public function testUserVariables(): void {
$result = $this->assertQuery( 'SELECT @my_var' );
$this->assertEquals( 3, $result[0]->{'@my_var'} );
}

public function testLockingStatements(): void {
$this->assertQuery( 'CREATE TABLE t (id INT)' );

// When there is no lock, UNLOCK statement shouldn't fail.
$this->assertQuery( 'UNLOCK TABLES' );

// READ LOCK.
$this->assertQuery( 'LOCK TABLES t READ' );
$this->assertQuery( 'UNLOCK TABLES' );

// WRITE LOCK.
$this->assertQuery( 'LOCK TABLES t WRITE' );
$this->assertQuery( 'UNLOCK TABLES' );

// LOCK inside a transaction.
$this->assertQuery( 'BEGIN' );
$this->assertQuery( 'LOCK TABLES t WRITE' );
$this->assertQuery( 'UNLOCK TABLES' );
$this->assertQuery( 'COMMIT' );

// Transaction inside LOCK statements.
$this->assertQuery( 'LOCK TABLES t WRITE' );
$this->assertQuery( 'BEGIN' );
$this->assertQuery( 'COMMIT' );
$this->assertQuery( 'UNLOCK TABLES' );
}

public function testLockNonExistentTableForRead(): void {
$this->expectException( 'WP_SQLite_Driver_Exception' );
$this->expectExceptionMessage( "Table 'wp.t' doesn't exist" );
$this->assertQuery( 'LOCK TABLES t READ' );
}

public function testLockNonExistentTableForWrite(): void {
$this->expectException( 'WP_SQLite_Driver_Exception' );
$this->expectExceptionMessage( "Table 'wp.t' doesn't exist" );
$this->assertQuery( 'LOCK TABLES t WRITE' );
}

public function testLockMultipleWithNonExistentTable(): void {
$this->assertQuery( 'CREATE TABLE t1 (id INT)' );
$this->assertQuery( 'CREATE TABLE t3 (id INT)' );

$this->expectException( 'WP_SQLite_Driver_Exception' );
$this->expectExceptionMessage( "Table 'wp.t2' doesn't exist" );
$this->assertQuery( 'LOCK TABLES t1 READ, t2 READ, t3 WRITE' );
}

public function testLockTemporaryTables(): void {
$this->assertQuery( 'CREATE TEMPORARY TABLE t1 (id INT)' );
$this->assertQuery( 'CREATE TABLE t2 (id INT)' );
$this->assertQuery( 'CREATE TEMPORARY TABLE t3 (id INT)' );
$this->assertQuery( 'LOCK TABLES t1 READ, t2 READ, t3 WRITE' );
$this->assertQuery( 'UNLOCK TABLES' );
}

public function testTransactionSavepoints(): void {
$this->assertQuery( 'CREATE TABLE t (id INT)' );

$this->assertQuery( 'BEGIN' );
$this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
$result = $this->assertQuery( 'SELECT * FROM t' );
$this->assertSame( array( '1' ), (array) array_column( $result, 'id' ) );

$this->assertQuery( 'SAVEPOINT sp1' );
$this->assertQuery( 'INSERT INTO t (id) VALUES (2)' );
$result = $this->assertQuery( 'SELECT * FROM t' );
$this->assertSame( array( '1', '2' ), (array) array_column( $result, 'id' ) );

$this->assertQuery( 'SAVEPOINT sp2' );
$this->assertQuery( 'INSERT INTO t (id) VALUES (3)' );
$result = $this->assertQuery( 'SELECT * FROM t' );
$this->assertSame( array( '1', '2', '3' ), (array) array_column( $result, 'id' ) );

$this->assertQuery( 'ROLLBACK TO SAVEPOINT sp1' );
$result = $this->assertQuery( 'SELECT * FROM t' );
$this->assertSame( array( '1' ), (array) array_column( $result, 'id' ) );

$this->assertQuery( 'RELEASE SAVEPOINT sp1' );
$this->assertQuery( 'ROLLBACK' );
$result = $this->assertQuery( 'SELECT * FROM t' );
$this->assertSame( array(), (array) array_column( $result, 'id' ) );
}

public function testRollbackNonExistentTransactionSavepoint(): void {
$this->expectException( 'WP_SQLite_Driver_Exception' );
$this->expectExceptionMessage( 'no such savepoint: sp1' );
$this->assertQuery( 'ROLLBACK TO SAVEPOINT sp1' );
}
}
Loading