diff --git a/src/AbstractStatement.php b/src/AbstractStatement.php index 5ece56c..be80868 100644 --- a/src/AbstractStatement.php +++ b/src/AbstractStatement.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO; use PDO; @@ -26,7 +24,7 @@ public function __construct(PDO $dbh) } /** - * @throws PDOException + * @throws DatabaseException * * @return mixed */ @@ -37,14 +35,12 @@ public function execute() try { $success = $stmt->execute($this->getValues()); if (!$success) { - list($state, $code, $message) = $stmt->errorInfo(); + $info = $stmt->errorInfo(); - // We are not in exception mode, raise error. - user_error("SQLSTATE[{$state}] [{$code}] {$message}", E_USER_ERROR); + throw new DatabaseException($info[2], $info[0]); } } catch (PDOException $e) { - // We are in exception mode, carry on. - throw $e; + throw new DatabaseException($e->getMessage(), $e->getCode(), $e); } return $stmt; diff --git a/src/AdvancedStatement.php b/src/AdvancedStatement.php index a1cf97d..1b685bb 100644 --- a/src/AdvancedStatement.php +++ b/src/AdvancedStatement.php @@ -5,13 +5,11 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO; abstract class AdvancedStatement extends AbstractStatement { - /** @var Clause\Join[] $join */ + /** @var Clause\Join[] */ protected $join = []; /** @var Clause\Conditional $where */ diff --git a/src/Clause/Conditional.php b/src/Clause/Conditional.php index 5d56617..93aa2dd 100644 --- a/src/Clause/Conditional.php +++ b/src/Clause/Conditional.php @@ -5,10 +5,9 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\QueryInterface; class Conditional implements QueryInterface @@ -48,6 +47,8 @@ public function getValues(): array } /** + * @throws DatabaseException + * * @return string */ public function __toString(): string @@ -57,7 +58,7 @@ public function __toString(): string case 'BETWEEN': case 'NOT BETWEEN': if (count($this->getValues()) != 2) { - trigger_error('Conditional operator "BETWEEN" requires two arguments', E_USER_ERROR); + throw new DatabaseException('Conditional operator "BETWEEN" requires two arguments'); } $sql .= ' (? AND ?)'; @@ -66,7 +67,7 @@ public function __toString(): string case 'IN': case 'NOT IN': if (count($this->getValues()) < 1) { - trigger_error('Conditional operator "IN" requires at least one argument', E_USER_ERROR); + throw new DatabaseException('Conditional operator "IN" requires at least one argument'); } $sql .= ' (' . substr(str_repeat('?, ', count($this->getValues())), 0, -2) . ')'; diff --git a/src/Clause/Grouping.php b/src/Clause/Grouping.php index cfac393..2d7bd8b 100644 --- a/src/Clause/Grouping.php +++ b/src/Clause/Grouping.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Clause; class Grouping extends Conditional diff --git a/src/Clause/Join.php b/src/Clause/Join.php index a4a1787..a9f9e56 100644 --- a/src/Clause/Join.php +++ b/src/Clause/Join.php @@ -5,10 +5,9 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\QueryInterface; use FaaPz\PDO\Statement\Select; @@ -44,6 +43,8 @@ public function getValues(): array } /** + * @throws DatabaseException + * * @return string */ public function __toString(): string @@ -53,7 +54,7 @@ public function __toString(): string reset($this->subject); $alias = key($this->subject); if (!is_string($alias)) { - trigger_error('Invalid subject array, use string keys for alias', E_USER_ERROR); + throw new DatabaseException('Invalid subject array, use string keys for alias'); } $table = $this->subject[$alias]; @@ -62,7 +63,7 @@ public function __toString(): string } $table .= " AS {$alias}"; } elseif (!is_string($this->subject)) { - trigger_error('Invalid subject value, use array with string key for alias', E_USER_ERROR); + throw new DatabaseException('Invalid subject value, use array with string key for alias'); } $sql = "JOIN {$table} ON {$this->on}"; diff --git a/src/Clause/Limit.php b/src/Clause/Limit.php index c678884..4aed2cb 100644 --- a/src/Clause/Limit.php +++ b/src/Clause/Limit.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Clause; use FaaPz\PDO\QueryInterface; @@ -23,7 +21,7 @@ class Limit implements QueryInterface * @param int $size * @param int|null $offset */ - public function __construct(int $size, ?int $offset = null) + public function __construct(int $size, int $offset = null) { $this->size = $size; $this->offset = $offset; @@ -35,7 +33,7 @@ public function __construct(int $size, ?int $offset = null) public function getValues(): array { $values = []; - if ($this->offset !== null) { + if (isset($this->offset)) { $values[] = $this->offset; } $values[] = $this->size; @@ -48,8 +46,8 @@ public function getValues(): array */ public function __toString(): string { - $sql = 'LIMIT ?'; - if ($this->offset !== null) { + $sql = '?'; + if (isset($this->offset)) { $sql .= ', ?'; } diff --git a/src/Clause/Method.php b/src/Clause/Method.php index 1bdcac8..c1c7f60 100644 --- a/src/Clause/Method.php +++ b/src/Clause/Method.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Clause; use FaaPz\PDO\QueryInterface; diff --git a/src/Clause/Raw.php b/src/Clause/Raw.php index a851306..8dafda5 100644 --- a/src/Clause/Raw.php +++ b/src/Clause/Raw.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Clause; use FaaPz\PDO\QueryInterface; diff --git a/src/Database.php b/src/Database.php index db5ce52..660f131 100644 --- a/src/Database.php +++ b/src/Database.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO; use PDO; diff --git a/src/DatabaseException.php b/src/DatabaseException.php new file mode 100644 index 0000000..3013748 --- /dev/null +++ b/src/DatabaseException.php @@ -0,0 +1,28 @@ +code = $code; + } +} diff --git a/src/QueryInterface.php b/src/QueryInterface.php index 99cacd9..69ee846 100644 --- a/src/QueryInterface.php +++ b/src/QueryInterface.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO; interface QueryInterface diff --git a/src/Statement/Call.php b/src/Statement/Call.php index c735765..1b31151 100644 --- a/src/Statement/Call.php +++ b/src/Statement/Call.php @@ -5,12 +5,11 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Statement; use FaaPz\PDO\AbstractStatement; use FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use PDO; use PDOStatement; @@ -56,14 +55,18 @@ public function getValues(): array } /** + * @throws DatabaseException + * * @return string */ public function __toString(): string { if (!$this->method instanceof Clause\Method) { - trigger_error('No method set for call statement', E_USER_ERROR); + throw new DatabaseException('No method is set for stored procedure call'); } - return "CALL {$this->method}"; + $sql = "CALL {$this->method}"; + + return $sql; } } diff --git a/src/Statement/Delete.php b/src/Statement/Delete.php index 5b2eefb..b17f77a 100644 --- a/src/Statement/Delete.php +++ b/src/Statement/Delete.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Statement; use FaaPz\PDO\AdvancedStatement; +use FaaPz\PDO\DatabaseException; use PDO; class Delete extends AdvancedStatement @@ -68,12 +67,14 @@ public function getValues(): array } /** + * @throws DatabaseException + * * @return string */ public function __toString(): string { if (empty($this->table)) { - trigger_error('No table set for delete statement', E_USER_ERROR); + throw new DatabaseException('No table is set for deletion'); } $sql = 'DELETE'; @@ -107,13 +108,15 @@ public function __toString(): string } if ($this->limit != null) { - $sql .= " {$this->limit}"; + $sql .= " LIMIT {$this->limit}"; } return $sql; } /** + * @throws DatabaseException + * * @return int */ public function execute() diff --git a/src/Statement/Insert.php b/src/Statement/Insert.php index af7bc1e..ae27e8f 100644 --- a/src/Statement/Insert.php +++ b/src/Statement/Insert.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Statement; use FaaPz\PDO\AbstractStatement; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\QueryInterface; use PDO; @@ -98,20 +97,22 @@ public function ignore(): self } /** + * @throws DatabaseException + * * @return string */ public function __toString(): string { if (empty($this->table)) { - trigger_error('No table set for insert statement', E_USER_ERROR); + throw new DatabaseException('No table is set for insertion'); } if (empty($this->columns)) { - trigger_error('No columns set for insert statement', E_USER_ERROR); + throw new DatabaseException('Missing columns for insertion'); } if (empty($this->values) || count($this->columns) != count($this->values)) { - trigger_error('No values set for insert statement', E_USER_ERROR); + throw new DatabaseException('Missing values for insertion'); } $placeholders = ''; @@ -157,6 +158,8 @@ public function getValues(): array } /** + * @throws DatabaseException + * * @return int|string */ public function execute() diff --git a/src/Statement/Select.php b/src/Statement/Select.php index e8af809..2a69b51 100644 --- a/src/Statement/Select.php +++ b/src/Statement/Select.php @@ -5,12 +5,11 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Statement; use FaaPz\PDO\AdvancedStatement; use FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\QueryInterface; use PDO; use PDOStatement; @@ -29,8 +28,8 @@ class Select extends AdvancedStatement /** @var bool $distinct */ protected $distinct = false; - /** @var array $union */ - protected $union = []; + /** @var array */ + private $union = []; /** @var array $groupBy */ protected $groupBy = []; @@ -188,12 +187,14 @@ protected function getColumns(): string } /** + * @throws DatabaseException + * * @return string */ public function __toString(): string { if (empty($this->table)) { - trigger_error('No table set for select statement', E_USER_ERROR); + throw new DatabaseException('No table is set for selection'); } $sql = 'SELECT'; @@ -246,7 +247,7 @@ public function __toString(): string } if ($this->limit != null) { - $sql .= " {$this->limit}"; + $sql .= " LIMIT {$this->limit}"; } if (!empty($this->union)) { diff --git a/src/Statement/Update.php b/src/Statement/Update.php index 584fcf9..e1ea2b5 100644 --- a/src/Statement/Update.php +++ b/src/Statement/Update.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Statement; use FaaPz\PDO\AdvancedStatement; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\QueryInterface; use PDO; @@ -109,16 +108,18 @@ protected function getColumns(): string } /** + * @throws DatabaseException + * * @return string */ public function __toString(): string { if (!isset($this->table)) { - trigger_error('No table set for update statement', E_USER_ERROR); + throw new DatabaseException('No table is set for update'); } if (empty($this->pairs)) { - trigger_error('No column / value pairs set for update statement', E_USER_ERROR); + throw new DatabaseException('Missing columns and values for update'); } $sql = "UPDATE {$this->table}"; @@ -140,13 +141,15 @@ public function __toString(): string } if ($this->limit != null) { - $sql .= " {$this->limit}"; + $sql .= " LIMIT {$this->limit}"; } return $sql; } /** + * @throws DatabaseException + * * @return int */ public function execute() diff --git a/tests/AbstractStatementTest.php b/tests/AbstractStatementTest.php index ac54238..cf82bd7 100644 --- a/tests/AbstractStatementTest.php +++ b/tests/AbstractStatementTest.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\AbstractStatement; +use FaaPz\PDO\DatabaseException; use PDO; use PDOException; use PDOStatement; @@ -69,7 +68,7 @@ public function testExecuteException() ->method('execute') ->willThrowException(new PDOException('message', 100)); - $this->expectException(PDOException::class); + $this->expectException(DatabaseException::class); $this->expectExceptionCode(100); $this->expectExceptionMessage('message'); @@ -82,8 +81,9 @@ public function testExecuteFailure() ->method('execute') ->willReturn(false); - $this->expectError(); - $this->expectErrorMessageMatches('/^SQLSTATE\[HY100\] \[100\] .* syntax error$/'); + $this->expectException(DatabaseException::class); + $this->expectExceptionCode('HY100'); + $this->expectExceptionMessage('near "bogus": syntax error'); $this->subject->execute(); } diff --git a/tests/Clause/ConditionalTest.php b/tests/Clause/ConditionalTest.php index 6170928..49f0fd8 100644 --- a/tests/Clause/ConditionalTest.php +++ b/tests/Clause/ConditionalTest.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use PHPUnit\Framework\TestCase; class ConditionalTest extends TestCase @@ -32,9 +31,7 @@ public function testToStringWithInException() { $subject = new Clause\Conditional('col', 'IN', []); - $this->expectError(); - $this->expectErrorMessageMatches('//'); - + $this->expectException(DatabaseException::class); $subject->__toString(); } @@ -49,9 +46,7 @@ public function testToStringWithBetweenException() { $subject = new Clause\Conditional('col', 'BETWEEN', [1, 2, 3]); - $this->expectError(); - $this->expectErrorMessageMatches('//'); - + $this->expectException(DatabaseException::class); $subject->__toString(); } diff --git a/tests/Clause/GroupingTest.php b/tests/Clause/GroupingTest.php index 396cc4f..f5d9df7 100644 --- a/tests/Clause/GroupingTest.php +++ b/tests/Clause/GroupingTest.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; diff --git a/tests/Clause/JoinTest.php b/tests/Clause/JoinTest.php index 58c4910..4c3f429 100644 --- a/tests/Clause/JoinTest.php +++ b/tests/Clause/JoinTest.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\Statement; use PDO; use PHPUnit\Framework\TestCase; @@ -54,9 +53,7 @@ public function testToStringWithTableAliasException() new Clause\Conditional('column1', '=', 'value1') ); - $this->expectError(); - $this->expectErrorMessageMatches('//'); - + $this->expectException(DatabaseException::class); $subject->__toString(); } @@ -77,9 +74,7 @@ public function testToStringWithInvalidTableAlias() new Clause\Conditional('column1', '=', 'value1') ); - $this->expectError(); - $this->expectErrorMessageMatches('//'); - + $this->expectException(DatabaseException::class); $subject->__toString(); } diff --git a/tests/Clause/LimitTest.php b/tests/Clause/LimitTest.php index 121995e..215a26e 100644 --- a/tests/Clause/LimitTest.php +++ b/tests/Clause/LimitTest.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; @@ -18,14 +16,14 @@ public function testToStringWithOffset() { $subject = new Clause\Limit(10, 25); - $this->assertEquals('LIMIT ?, ?', $subject->__toString()); + $this->assertEquals('?, ?', $subject->__toString()); } public function testToStringWithoutOffset() { $subject = new Clause\Limit(10); - $this->assertEquals('LIMIT ?', $subject->__toString()); + $this->assertEquals('?', $subject->__toString()); } public function testGetValuesWithOffset() diff --git a/tests/Clause/MethodTest.php b/tests/Clause/MethodTest.php index 24c7aed..7fb0204 100644 --- a/tests/Clause/MethodTest.php +++ b/tests/Clause/MethodTest.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; diff --git a/tests/Clause/RawTest.php b/tests/Clause/RawTest.php index 8d03e1d..af96e32 100644 --- a/tests/Clause/RawTest.php +++ b/tests/Clause/RawTest.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; diff --git a/tests/DatabaseExceptionTest.php b/tests/DatabaseExceptionTest.php new file mode 100644 index 0000000..99ede33 --- /dev/null +++ b/tests/DatabaseExceptionTest.php @@ -0,0 +1,40 @@ +subject = new DatabaseException('test', 'error', new Exception()); + } + + public function testMessage() + { + $this->assertEquals('test', $this->subject->getMessage()); + } + + public function testCode() + { + $this->assertEquals('error', $this->subject->getCode()); + } + + public function testPrevious() + { + $this->assertInstanceOf(Exception::class, $this->subject->getPrevious()); + } +} diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index b25b0f5..b8a018f 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -5,8 +5,6 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; diff --git a/tests/Statement/CallTest.php b/tests/Statement/CallTest.php index fe825f4..a5f7023 100644 --- a/tests/Statement/CallTest.php +++ b/tests/Statement/CallTest.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\Statement; use PDO; use PHPUnit\Framework\TestCase; @@ -35,8 +34,7 @@ public function testToString() public function testToStringWithoutMethod() { - $this->expectError(); - $this->expectErrorMessageMatches('/^No method .* call statement$/'); + $this->expectException(DatabaseException::class); $this->subject->__toString(); } diff --git a/tests/Statement/DeleteTest.php b/tests/Statement/DeleteTest.php index 4f3ac98..5e38020 100644 --- a/tests/Statement/DeleteTest.php +++ b/tests/Statement/DeleteTest.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\Statement; use PDO; use PDOStatement; @@ -66,8 +65,7 @@ public function testToStringWithConstructorTable() public function testToStringWithoutTable() { - $this->expectError(); - $this->expectErrorMessageMatches('/delete statement$/'); + $this->expectException(DatabaseException::class); $this->subject->__toString(); } diff --git a/tests/Statement/InsertTest.php b/tests/Statement/InsertTest.php index e16002e..356ac88 100644 --- a/tests/Statement/InsertTest.php +++ b/tests/Statement/InsertTest.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\Statement; use PDO; use PDOStatement; @@ -51,8 +50,7 @@ public function testToString() public function testToStringWithoutTable() { - $this->expectError(); - $this->expectErrorMessageMatches('/insert statement$/'); + $this->expectException(DatabaseException::class); $this->subject ->columns('one', 'two') @@ -62,8 +60,7 @@ public function testToStringWithoutTable() public function testToStringWithoutColumns() { - $this->expectError(); - $this->expectErrorMessageMatches('/insert statement$/'); + $this->expectException(DatabaseException::class); $this->subject ->into('test') @@ -73,8 +70,7 @@ public function testToStringWithoutColumns() public function testToStringWithoutValues() { - $this->expectError(); - $this->expectErrorMessageMatches('/insert statement$/'); + $this->expectException(DatabaseException::class); $this->subject ->into('test') diff --git a/tests/Statement/SelectTest.php b/tests/Statement/SelectTest.php index f473183..5dec9d7 100644 --- a/tests/Statement/SelectTest.php +++ b/tests/Statement/SelectTest.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaapZ\PDO\Clause; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\Statement; use PDO; use PHPUnit\Framework\TestCase; @@ -165,8 +164,7 @@ public function testToStringWithLimit() public function testToStringWithoutTable() { - $this->expectError(); - $this->expectErrorMessageMatches('/^No table .* select statement/'); + $this->expectException(DatabaseException::class); $this->subject->execute(); } diff --git a/tests/Statement/UpdateTest.php b/tests/Statement/UpdateTest.php index d5380e9..a28acb7 100644 --- a/tests/Statement/UpdateTest.php +++ b/tests/Statement/UpdateTest.php @@ -5,11 +5,10 @@ * @license http://opensource.org/licenses/MIT */ -declare(strict_types=1); - namespace FaaPz\PDO\Test; use FaaPz\PDO\Clause; +use FaaPz\PDO\DatabaseException; use FaaPz\PDO\Statement; use PDO; use PDOStatement; @@ -118,16 +117,14 @@ public function testToStringWithLimit() public function testToStringWithoutTable() { - $this->expectError(); - $this->expectErrorMessageMatches('/^No table .* update statement$/'); + $this->expectException(DatabaseException::class); $this->subject->execute(); } public function testToStringWithoutPairs() { - $this->expectError(); - $this->expectErrorMessageMatches('/^No column \/ value pairs .* update statement$/'); + $this->expectException(DatabaseException::class); $this->subject ->table('test')