From bcd029301df1e4e6b7944320fa452fc11880ffbc Mon Sep 17 00:00:00 2001 From: Josh McRae Date: Tue, 16 Nov 2021 09:46:41 +1000 Subject: [PATCH] Updated StatementHandler to throw exceptions on duplicate key errors (#11) --- lib/PicoDb/StatementHandler.php | 4 ---- tests/MysqlDatabaseTest.php | 12 +++++++----- tests/PostgresDatabaseTest.php | 12 +++++++----- tests/SqliteDatabaseTest.php | 12 +++++++----- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/PicoDb/StatementHandler.php b/lib/PicoDb/StatementHandler.php index d0cdaa4..b06f940 100644 --- a/lib/PicoDb/StatementHandler.php +++ b/lib/PicoDb/StatementHandler.php @@ -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()); } } diff --git a/tests/MysqlDatabaseTest.php b/tests/MysqlDatabaseTest.php index a97c3e4..c688902 100644 --- a/tests/MysqlDatabaseTest.php +++ b/tests/MysqlDatabaseTest.php @@ -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() @@ -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; - })); + }); } } diff --git a/tests/PostgresDatabaseTest.php b/tests/PostgresDatabaseTest.php index 462400c..7475305 100644 --- a/tests/PostgresDatabaseTest.php +++ b/tests/PostgresDatabaseTest.php @@ -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() @@ -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; - })); + }); } } diff --git a/tests/SqliteDatabaseTest.php b/tests/SqliteDatabaseTest.php index 9b90e49..e5be5fb 100644 --- a/tests/SqliteDatabaseTest.php +++ b/tests/SqliteDatabaseTest.php @@ -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() @@ -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()