diff --git a/src/Database/DriverException.php b/src/Database/DriverException.php index 1ab98d27c..425c688bf 100644 --- a/src/Database/DriverException.php +++ b/src/Database/DriverException.php @@ -18,18 +18,11 @@ class DriverException extends \Exception public function __construct( string $message, private readonly ?string $sqlState = null, - private int $driverCode = 0, + int $code = 0, private readonly ?SqlLiteral $query = null, ?\Throwable $previous = null, ) { - parent::__construct($message, 0, $previous); - $this->code = $sqlState ?: null; - } - - - public function getDriverCode(): int|string|null - { - return $this->driverCode ?: null; + parent::__construct($message, $code, $previous); } diff --git a/tests/Database/Connection.exceptions.mysql.phpt b/tests/Database/Connection.exceptions.mysql.phpt index 71258ea4e..95c69772c 100644 --- a/tests/Database/Connection.exceptions.mysql.phpt +++ b/tests/Database/Connection.exceptions.mysql.phpt @@ -20,20 +20,21 @@ test('Exception thrown for invalid database credentials', function () { fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'), Nette\Database\ConnectionException::class, '%a% Access denied for user %a%', + 1045, ); - Assert::same(1045, $e->getDriverCode()); Assert::contains($e->getSqlState(), ['HY000', '28000']); - Assert::same($e->getCode(), $e->getSqlState()); }); -testException( - 'Exception thrown when calling rollback with no active transaction', - fn() => $connection->rollback(), - Nette\Database\DriverException::class, - 'There is no active transaction', -); +test('Exception thrown when calling rollback with no active transaction', function () use ($connection) { + $e = Assert::exception( + fn() => $connection->rollback(), + Nette\Database\DriverException::class, + 'There is no active transaction', + ); + Assert::null($e->getSqlState()); +}); test('Exception thrown for syntax error in SQL query', function () use ($connection) { @@ -41,11 +42,9 @@ test('Exception thrown for syntax error in SQL query', function () use ($connect fn() => $connection->query('SELECT'), Nette\Database\DriverException::class, '%a% Syntax error %a%', - '42000', + 1064, ); - - Assert::same(1064, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('42000', $e->getSqlState()); }); @@ -54,11 +53,9 @@ test('Exception thrown for unique constraint violation', function () use ($conne fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'), Nette\Database\UniqueConstraintViolationException::class, '%a% Integrity constraint violation: %a%', - '23000', + 1062, ); - - Assert::same(1062, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('23000', $e->getSqlState()); }); @@ -67,11 +64,9 @@ test('Exception thrown for not null constraint violation', function () use ($con fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'), Nette\Database\NotNullConstraintViolationException::class, '%a% Integrity constraint violation: %a%', - '23000', + 1048, ); - - Assert::same(1048, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('23000', $e->getSqlState()); }); @@ -80,9 +75,7 @@ test('Exception thrown for foreign key constraint violation', function () use ($ fn() => $connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")'), Nette\Database\ForeignKeyConstraintViolationException::class, '%a% a foreign key constraint fails %a%', - '23000', + 1452, ); - - Assert::same(1452, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('23000', $e->getSqlState()); }); diff --git a/tests/Database/Connection.exceptions.postgre.phpt b/tests/Database/Connection.exceptions.postgre.phpt index efd3cec43..c6c005747 100644 --- a/tests/Database/Connection.exceptions.postgre.phpt +++ b/tests/Database/Connection.exceptions.postgre.phpt @@ -21,21 +21,20 @@ test('Exception thrown for invalid database credentials', function () { fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'), Nette\Database\ConnectionException::class, null, - '08006', + 7, ); - - Assert::same(7, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('08006', $e->getSqlState()); }); -testException( - 'Exception thrown when calling rollback with no active transaction', - fn() => $connection->rollback(), - Nette\Database\DriverException::class, - 'There is no active transaction', - null, -); +test('Exception thrown when calling rollback with no active transaction', function () use ($connection) { + $e = Assert::exception( + fn() => $connection->rollback(), + Nette\Database\DriverException::class, + 'There is no active transaction', + ); + Assert::null($e->getSqlState()); +}); test('Exception thrown for syntax error in SQL query', function () use ($connection) { @@ -43,11 +42,9 @@ test('Exception thrown for syntax error in SQL query', function () use ($connect fn() => $connection->query('SELECT INTO'), Nette\Database\DriverException::class, '%a% syntax error %A%', - '42601', + 7, ); - - Assert::same(7, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('42601', $e->getSqlState()); }); @@ -56,11 +53,9 @@ test('Exception thrown for unique constraint violation', function () use ($conne fn() => $connection->query("INSERT INTO author (id, name, web, born) VALUES (11, '', '', NULL)"), Nette\Database\UniqueConstraintViolationException::class, '%a% Unique violation: %A%', - '23505', + 7, ); - - Assert::same(7, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('23505', $e->getSqlState()); }); @@ -69,11 +64,9 @@ test('Exception thrown for not null constraint violation', function () use ($con fn() => $connection->query("INSERT INTO author (name, web, born) VALUES (NULL, '', NULL)"), Nette\Database\NotNullConstraintViolationException::class, '%a% Not null violation: %A%', - '23502', + 7, ); - - Assert::same(7, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('23502', $e->getSqlState()); }); @@ -82,9 +75,7 @@ test('Exception thrown for foreign key constraint violation', function () use ($ fn() => $connection->query("INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, '')"), Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Foreign key violation: %A%', - '23503', + 7, ); - - Assert::same(7, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('23503', $e->getSqlState()); }); diff --git a/tests/Database/Connection.exceptions.sqlite.phpt b/tests/Database/Connection.exceptions.sqlite.phpt index a3989acd5..8f45637b0 100644 --- a/tests/Database/Connection.exceptions.sqlite.phpt +++ b/tests/Database/Connection.exceptions.sqlite.phpt @@ -20,20 +20,21 @@ test('Exception thrown for unable to open database file', function () { fn() => new Nette\Database\Connection('sqlite:.'), Nette\Database\ConnectionException::class, 'SQLSTATE[HY000] [14] unable to open database file', - 'HY000', + 14, ); - Assert::same(14, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('HY000', $e->getSqlState()); }); -testException( - 'Exception thrown when calling rollback with no active transaction', - fn() => $connection->rollback(), - Nette\Database\DriverException::class, - 'There is no active transaction', -); +test('Exception thrown when calling rollback with no active transaction', function () use ($connection) { + $e = Assert::exception( + fn() => $connection->rollback(), + Nette\Database\DriverException::class, + 'There is no active transaction', + ); + Assert::null($e->getSqlState()); +}); test('Exception thrown for error in SQL query', function () use ($connection) { @@ -41,11 +42,9 @@ test('Exception thrown for error in SQL query', function () use ($connection) { fn() => $connection->query('SELECT'), Nette\Database\DriverException::class, '%a% error%a%', - 'HY000', + 7, ); - - Assert::same(1, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('HY000', $e->getSqlState()); }); @@ -54,11 +53,9 @@ test('Exception thrown for unique constraint violation', function () use ($conne fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'), Nette\Database\UniqueConstraintViolationException::class, '%a% Integrity constraint violation: %a%', - '23000', + 19, ); - - Assert::same(19, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('23000', $e->getSqlState()); }); @@ -67,11 +64,9 @@ test('Exception thrown for not null constraint violation', function () use ($con fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'), Nette\Database\NotNullConstraintViolationException::class, '%a% Integrity constraint violation: %a%', - '23000', + 19, ); - - Assert::same(19, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + Assert::same('23000', $e->getSqlState()); }); @@ -79,8 +74,6 @@ test('Exception thrown for foreign key constraint violation', function () use ($ $e = Assert::exception(function () use ($connection) { $connection->query('PRAGMA foreign_keys=true'); $connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")'); - }, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', '23000'); - - Assert::same(19, $e->getDriverCode()); - Assert::same($e->getCode(), $e->getSqlState()); + }, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', 19); + Assert::same('23000', $e->getSqlState()); }); diff --git a/tests/Database/Explorer/Explorer.update().phpt b/tests/Database/Explorer/Explorer.update().phpt index 11ffd1a81..008bae316 100644 --- a/tests/Database/Explorer/Explorer.update().phpt +++ b/tests/Database/Explorer/Explorer.update().phpt @@ -79,7 +79,7 @@ $tag2 = $explorer->table('tag')->insert([ 'name' => 'PS4 Game', ]); // INSERT INTO `tag` (`name`) VALUES ('PS4 Game') -// SQL Server throw PDOException because does not allow to update identity column +// SQL Server throw exception because does not allow to update identity column if ($driverName !== 'sqlsrv') { $tag2->update([ 'id' => 1,