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
15 changes: 3 additions & 12 deletions lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

namespace ActiveRecord;

use ActiveRecord\Adapter\SqliteAdapter;
use ActiveRecord\Exception\ConnectionException;
use ActiveRecord\Exception\DatabaseException;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -452,19 +447,15 @@ public function transaction(): void
*/
public function commit(): void
{
if (!$this->connection->commit()) {
throw new DatabaseException();
}
assert($this->connection->commit(), new DatabaseException('Failed to commit'));
}

/**
* Rollback a transaction.
*/
public function rollback(): void
{
if (!$this->connection->rollback()) {
throw new DatabaseException();
}
assert($this->connection->rollback(), new DatabaseException('Failed to roll back'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
44 changes: 19 additions & 25 deletions test/ActiveRecordWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()
Expand Down