diff --git a/lib/Connection.php b/lib/Connection.php index 19014de5..17302abd 100644 --- a/lib/Connection.php +++ b/lib/Connection.php @@ -6,8 +6,6 @@ namespace ActiveRecord; -use ActiveRecord\Adapter\SqliteAdapter; -use ActiveRecord\Exception\ConnectionException; use ActiveRecord\Exception\DatabaseException; use Psr\Log\LoggerInterface; @@ -363,10 +361,7 @@ public function query(string $sql, array &$values = []) throw new DatabaseException(); } } catch (\PDOException $e) { - if ($this instanceof SqliteAdapter && 'HY000' === $e->getCode()) { - throw new DatabaseException($e); - } - throw new ConnectionException($e); + throw new DatabaseException($e); } $sth->setFetchMode(\PDO::FETCH_ASSOC); @@ -452,9 +447,7 @@ public function transaction(): void */ public function commit(): void { - if (!$this->connection->commit()) { - throw new DatabaseException(); - } + assert($this->connection->commit(), new DatabaseException('Failed to commit')); } /** @@ -462,9 +455,7 @@ public function commit(): void */ public function rollback(): void { - if (!$this->connection->rollback()) { - throw new DatabaseException(); - } + assert($this->connection->rollback(), new DatabaseException('Failed to roll back')); } /** diff --git a/lib/Table.php b/lib/Table.php index 78bc9ea7..c0b72cf2 100644 --- a/lib/Table.php +++ b/lib/Table.php @@ -367,7 +367,7 @@ public function get_fully_qualified_table_name(): string { $table = $this->conn->quote_name($this->table); - if (isset($this->db_name)) { + if (!empty($this->db_name)) { $table = $this->conn->quote_name($this->db_name) . ".$table"; } diff --git a/test/ActiveRecordWriteTest.php b/test/ActiveRecordWriteTest.php index e48674af..a4190714 100644 --- a/test/ActiveRecordWriteTest.php +++ b/test/ActiveRecordWriteTest.php @@ -6,8 +6,10 @@ use ActiveRecord\Exception\DatabaseException; use ActiveRecord\Exception\ReadOnlyException; use ActiveRecord\Exception\UndefinedPropertyException; +use ActiveRecord\Table; use test\models\Author; use test\models\Book; +use test\models\Course; use test\models\Venue; class DirtyAuthor extends ActiveRecord\Model @@ -90,10 +92,22 @@ public function testSaveAutoIncrementId() $this->assertTrue($venue->id > 0); } + public function testFullyQualifiedNameWithExplicitDbName() + { + Course::$db = 'test'; + $name = Table::load(Course::class) + ->get_fully_qualified_table_name(); + $this->assert_sql_includes('`test`.`courses`', $name); + Course::$db = ''; + } + public function testSequenceWasSet() { if (ConnectionManager::get_connection()->supports_sequences()) { - $this->assertEquals(ConnectionManager::get_connection()->get_sequence_name('authors', 'author_id'), Author::table()->sequence); + $this->assertEquals( + ConnectionManager::get_connection()->get_sequence_name('authors', 'author_id'), + Author::table()->sequence + ); } else { $this->assertNull(Author::table()->sequence); } @@ -363,36 +377,16 @@ public function testSetDateFlagsDirtyWithPhpDatetime() $this->assertArrayHasKey('some_date', $author->dirty_attributes()); } - public function testWhereDeleteAll() + public function testDeleteAll() { - $num_affected = Author::where('parent_author_id = ?', 2)->delete_all(); + $num_affected = Author::where('parent_author_id = 2')->delete_all(); $this->assertEquals(2, $num_affected); } - public function testDistinctDeleteAllNotSupported() + public function testDeleteAllWithDistinct() { $this->expectException(ActiveRecordException::class); - Author::distinct()->delete_all(); - } - - public function testDeleteAll() - { - $num_affected = Author::delete_all(); - $this->assertEquals(5, $num_affected); - } - - public function testDeleteAllWithLimitAndOrder() - { - if (!ConnectionManager::get_connection()->accepts_limit_and_order_for_update_and_delete()) { - $this->markTestSkipped('Only MySQL & Sqlite accept limit/order with UPDATE clause'); - } - - $num_affected = Author::limit(1) - ->order('name asc') - ->where(['parent_author_id = ?', 2]) - ->delete_all(); - $this->assertEquals(1, $num_affected); - $this->assertTrue(false !== strpos(Author::table()->last_sql, 'ORDER BY name asc LIMIT 1')); + Author::distinct()->where('parent_author_id = 2')->delete_all(); } public function testUpdateAllWithSetAsString()