Skip to content

Commit

Permalink
Updated StatementHandler to throw exceptions on duplicate key errors (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmcrae authored Nov 15, 2021
1 parent 7ca8e7d commit bcd0293
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
4 changes: 0 additions & 4 deletions lib/PicoDb/StatementHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,6 @@ public function handleSqlError(PDOException $e)
$this->db->cancelTransaction();
$this->db->setLogMessage($e->getMessage());

if ($this->db->getDriver()->isDuplicateKeyError($e->getCode())) {
return false;
}

throw new SQLException('SQL Error: '.$e->getMessage());
}
}
12 changes: 7 additions & 5 deletions tests/MysqlDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public function testBadSQLQuery()

public function testDuplicateKey()
{
$this->expectException(\PicoDb\SQLException::class);

$this->db->getConnection()->exec('CREATE TABLE foobar (something CHAR(1) UNIQUE) ENGINE=InnoDB');

$this->assertNotFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
$this->assertFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));

$this->assertEquals(1, $this->db->execute('SELECT COUNT(*) FROM foobar WHERE something=?', array('a'))->fetchColumn());
$this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
}

public function testThatTransactionReturnsAValue()
Expand Down Expand Up @@ -89,11 +89,13 @@ public function testThatTransactionThrowExceptionWhenRollbacked()

public function testThatTransactionReturnsFalseWhithDuplicateKey()
{
$this->assertFalse($this->db->transaction(function (Database $db) {
$this->expectException(\PicoDb\SQLException::class);

$this->db->transaction(function (Database $db) {
$db->getConnection()->exec('CREATE TABLE foobar (something CHAR(1) UNIQUE) ENGINE=InnoDB');
$r1 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
$r2 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
return $r1 && $r2;
}));
});
}
}
12 changes: 7 additions & 5 deletions tests/PostgresDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public function testBadSQLQuery()

public function testDuplicateKey()
{
$this->expectException(\PicoDb\SQLException::class);

$this->db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');

$this->assertNotFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
$this->assertFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));

$this->assertEquals(1, $this->db->execute('SELECT COUNT(*) FROM foobar WHERE something=?', array('a'))->fetchColumn());
$this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
}

public function testThatTransactionReturnsAValue()
Expand Down Expand Up @@ -88,11 +88,13 @@ public function testThatTransactionThrowExceptionWhenRollbacked()

public function testThatTransactionReturnsFalseWhithDuplicateKey()
{
$this->assertFalse($this->db->transaction(function (Database $db) {
$this->expectException(\PicoDb\SQLException::class);

$this->db->transaction(function (Database $db) {
$db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
$r1 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
$r2 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
return $r1 && $r2;
}));
});
}
}
12 changes: 7 additions & 5 deletions tests/SqliteDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public function testBadSQLQuery()

public function testDuplicateKey()
{
$this->expectException(\PicoDb\SQLException::class);

$this->db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');

$this->assertNotFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
$this->assertFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));

$this->assertEquals(1, $this->db->execute('SELECT COUNT(*) FROM foobar WHERE something=?', array('a'))->fetchColumn());
$this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
}

public function testThatTransactionReturnsAValue()
Expand Down Expand Up @@ -86,12 +86,14 @@ public function testThatTransactionThrowExceptionWhenRollbacked()

public function testThatTransactionReturnsFalseWhithDuplicateKey()
{
$this->assertFalse($this->db->transaction(function (Database $db) {
$this->expectException(\PicoDb\SQLException::class);

$this->db->transaction(function (Database $db) {
$db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
$r1 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
$r2 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
return $r1 && $r2;
}));
});
}

public function testGetInstance()
Expand Down

0 comments on commit bcd0293

Please sign in to comment.