Skip to content

Commit bcd0293

Browse files
authored
Updated StatementHandler to throw exceptions on duplicate key errors (#11)
1 parent 7ca8e7d commit bcd0293

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

lib/PicoDb/StatementHandler.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,6 @@ public function handleSqlError(PDOException $e)
344344
$this->db->cancelTransaction();
345345
$this->db->setLogMessage($e->getMessage());
346346

347-
if ($this->db->getDriver()->isDuplicateKeyError($e->getCode())) {
348-
return false;
349-
}
350-
351347
throw new SQLException('SQL Error: '.$e->getMessage());
352348
}
353349
}

tests/MysqlDatabaseTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ public function testBadSQLQuery()
5151

5252
public function testDuplicateKey()
5353
{
54+
$this->expectException(\PicoDb\SQLException::class);
55+
5456
$this->db->getConnection()->exec('CREATE TABLE foobar (something CHAR(1) UNIQUE) ENGINE=InnoDB');
5557

5658
$this->assertNotFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
57-
$this->assertFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
58-
59-
$this->assertEquals(1, $this->db->execute('SELECT COUNT(*) FROM foobar WHERE something=?', array('a'))->fetchColumn());
59+
$this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
6060
}
6161

6262
public function testThatTransactionReturnsAValue()
@@ -89,11 +89,13 @@ public function testThatTransactionThrowExceptionWhenRollbacked()
8989

9090
public function testThatTransactionReturnsFalseWhithDuplicateKey()
9191
{
92-
$this->assertFalse($this->db->transaction(function (Database $db) {
92+
$this->expectException(\PicoDb\SQLException::class);
93+
94+
$this->db->transaction(function (Database $db) {
9395
$db->getConnection()->exec('CREATE TABLE foobar (something CHAR(1) UNIQUE) ENGINE=InnoDB');
9496
$r1 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
9597
$r2 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
9698
return $r1 && $r2;
97-
}));
99+
});
98100
}
99101
}

tests/PostgresDatabaseTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public function testBadSQLQuery()
5050

5151
public function testDuplicateKey()
5252
{
53+
$this->expectException(\PicoDb\SQLException::class);
54+
5355
$this->db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
5456

5557
$this->assertNotFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
56-
$this->assertFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
57-
58-
$this->assertEquals(1, $this->db->execute('SELECT COUNT(*) FROM foobar WHERE something=?', array('a'))->fetchColumn());
58+
$this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
5959
}
6060

6161
public function testThatTransactionReturnsAValue()
@@ -88,11 +88,13 @@ public function testThatTransactionThrowExceptionWhenRollbacked()
8888

8989
public function testThatTransactionReturnsFalseWhithDuplicateKey()
9090
{
91-
$this->assertFalse($this->db->transaction(function (Database $db) {
91+
$this->expectException(\PicoDb\SQLException::class);
92+
93+
$this->db->transaction(function (Database $db) {
9294
$db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
9395
$r1 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
9496
$r2 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
9597
return $r1 && $r2;
96-
}));
98+
});
9799
}
98100
}

tests/SqliteDatabaseTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public function testBadSQLQuery()
4848

4949
public function testDuplicateKey()
5050
{
51+
$this->expectException(\PicoDb\SQLException::class);
52+
5153
$this->db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
5254

5355
$this->assertNotFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
54-
$this->assertFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
55-
56-
$this->assertEquals(1, $this->db->execute('SELECT COUNT(*) FROM foobar WHERE something=?', array('a'))->fetchColumn());
56+
$this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
5757
}
5858

5959
public function testThatTransactionReturnsAValue()
@@ -86,12 +86,14 @@ public function testThatTransactionThrowExceptionWhenRollbacked()
8686

8787
public function testThatTransactionReturnsFalseWhithDuplicateKey()
8888
{
89-
$this->assertFalse($this->db->transaction(function (Database $db) {
89+
$this->expectException(\PicoDb\SQLException::class);
90+
91+
$this->db->transaction(function (Database $db) {
9092
$db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
9193
$r1 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
9294
$r2 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
9395
return $r1 && $r2;
94-
}));
96+
});
9597
}
9698

9799
public function testGetInstance()

0 commit comments

Comments
 (0)