Skip to content

Commit 897ab96

Browse files
committed
DriverException::getCode() returns driver error code instead of SQLState (BC break)
1 parent 65cb5c2 commit 897ab96

8 files changed

Lines changed: 35 additions & 73 deletions

src/Database/DriverException.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,11 @@ class DriverException extends \Exception
1818
public function __construct(
1919
string $message,
2020
private readonly ?string $sqlState = null,
21-
private int $driverCode = 0,
21+
int $code = 0,
2222
private readonly ?SqlLiteral $query = null,
2323
?\Throwable $previous = null,
2424
) {
25-
parent::__construct($message, 0, $previous);
26-
$this->code = $sqlState ?: null;
27-
}
28-
29-
30-
public function getDriverCode(): int|string|null
31-
{
32-
return $this->driverCode ?: null;
25+
parent::__construct($message, $code, $previous);
3326
}
3427

3528

tests/Database/Connection.exceptions.mysql.phpt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ $e = Assert::exception(
1818
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
1919
Nette\Database\ConnectionException::class,
2020
'%a% Access denied for user %a%',
21+
1045,
2122
);
2223

23-
Assert::same(1045, $e->getDriverCode());
2424
Assert::contains($e->getSqlState(), ['HY000', '28000']);
25-
Assert::same($e->getCode(), $e->getSqlState());
2625

2726

2827
$e = Assert::exception(
@@ -32,4 +31,4 @@ $e = Assert::exception(
3231
null,
3332
);
3433

35-
Assert::same(null, $e->getDriverCode());
34+
Assert::null($e->getSqlState());

tests/Database/Connection.exceptions.postgre.phpt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ $e = Assert::exception(
1818
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
1919
Nette\Database\ConnectionException::class,
2020
null,
21-
'08006',
21+
7,
2222
);
23-
24-
Assert::same(7, $e->getDriverCode());
25-
Assert::same($e->getCode(), $e->getSqlState());
23+
Assert::same('08006', $e->getSqlState());
2624

2725

2826
$e = Assert::exception(
@@ -31,5 +29,4 @@ $e = Assert::exception(
3129
'There is no active transaction',
3230
null,
3331
);
34-
35-
Assert::same(null, $e->getDriverCode());
32+
Assert::null($e->getSqlState());

tests/Database/Connection.exceptions.sqlite.phpt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ $e = Assert::exception(
1717
fn() => new Nette\Database\Connection('sqlite:.'),
1818
Nette\Database\ConnectionException::class,
1919
'SQLSTATE[HY000] [14] unable to open database file',
20-
'HY000',
20+
14,
2121
);
22-
23-
Assert::same(14, $e->getDriverCode());
24-
Assert::same($e->getCode(), $e->getSqlState());
22+
Assert::same('HY000', $e->getSqlState());
2523

2624

2725
$e = Assert::exception(
@@ -30,5 +28,4 @@ $e = Assert::exception(
3028
'There is no active transaction',
3129
null,
3230
);
33-
34-
Assert::same(null, $e->getDriverCode());
31+
Assert::null($e->getSqlState());

tests/Database/Explorer/Explorer.update().phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ $tag2 = $explorer->table('tag')->insert([
7979
'name' => 'PS4 Game',
8080
]); // INSERT INTO `tag` (`name`) VALUES ('PS4 Game')
8181

82-
// SQL Server throw PDOException because does not allow to update identity column
82+
// SQL Server throw exception because does not allow to update identity column
8383
if ($driverName !== 'sqlsrv') {
8484
$tag2->update([
8585
'id' => 1,

tests/Database/ResultSet.exceptions.mysql.phpt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,33 @@ $e = Assert::exception(
1919
fn() => $connection->query('SELECT'),
2020
Nette\Database\DriverException::class,
2121
'%a% Syntax error %a%',
22-
'42000',
22+
1064,
2323
);
24-
25-
Assert::same(1064, $e->getDriverCode());
26-
Assert::same($e->getCode(), $e->getSqlState());
24+
Assert::same('42000', $e->getSqlState());
2725

2826

2927
$e = Assert::exception(
3028
fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'),
3129
Nette\Database\UniqueConstraintViolationException::class,
3230
'%a% Integrity constraint violation: %a%',
33-
'23000',
31+
1062,
3432
);
35-
36-
Assert::same(1062, $e->getDriverCode());
37-
Assert::same($e->getCode(), $e->getSqlState());
33+
Assert::same('23000', $e->getSqlState());
3834

3935

4036
$e = Assert::exception(
4137
fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'),
4238
Nette\Database\NotNullConstraintViolationException::class,
4339
'%a% Integrity constraint violation: %a%',
44-
'23000',
40+
1048,
4541
);
46-
47-
Assert::same(1048, $e->getDriverCode());
48-
Assert::same($e->getCode(), $e->getSqlState());
42+
Assert::same('23000', $e->getSqlState());
4943

5044

5145
$e = Assert::exception(
5246
fn() => $connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")'),
5347
Nette\Database\ForeignKeyConstraintViolationException::class,
5448
'%a% a foreign key constraint fails %a%',
55-
'23000',
49+
1452,
5650
);
57-
58-
Assert::same(1452, $e->getDriverCode());
59-
Assert::same($e->getCode(), $e->getSqlState());
51+
Assert::same('23000', $e->getSqlState());

tests/Database/ResultSet.exceptions.postgre.phpt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,33 @@ $e = Assert::exception(
1919
fn() => $connection->query('SELECT INTO'),
2020
Nette\Database\DriverException::class,
2121
'%a% syntax error %A%',
22-
'42601',
22+
7,
2323
);
24-
25-
Assert::same(7, $e->getDriverCode());
26-
Assert::same($e->getCode(), $e->getSqlState());
24+
Assert::same('42601', $e->getSqlState());
2725

2826

2927
$e = Assert::exception(
3028
fn() => $connection->query("INSERT INTO author (id, name, web, born) VALUES (11, '', '', NULL)"),
3129
Nette\Database\UniqueConstraintViolationException::class,
3230
'%a% Unique violation: %A%',
33-
'23505',
31+
7,
3432
);
35-
36-
Assert::same(7, $e->getDriverCode());
37-
Assert::same($e->getCode(), $e->getSqlState());
33+
Assert::same('23505', $e->getSqlState());
3834

3935

4036
$e = Assert::exception(
4137
fn() => $connection->query("INSERT INTO author (name, web, born) VALUES (NULL, '', NULL)"),
4238
Nette\Database\NotNullConstraintViolationException::class,
4339
'%a% Not null violation: %A%',
44-
'23502',
40+
7,
4541
);
46-
47-
Assert::same(7, $e->getDriverCode());
48-
Assert::same($e->getCode(), $e->getSqlState());
42+
Assert::same('23502', $e->getSqlState());
4943

5044

5145
$e = Assert::exception(
5246
fn() => $connection->query("INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, '')"),
5347
Nette\Database\ForeignKeyConstraintViolationException::class,
5448
'%a% Foreign key violation: %A%',
55-
'23503',
49+
7,
5650
);
57-
58-
Assert::same(7, $e->getDriverCode());
59-
Assert::same($e->getCode(), $e->getSqlState());
51+
Assert::same('23503', $e->getSqlState());

tests/Database/ResultSet.exceptions.sqlite.phpt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,31 @@ $e = Assert::exception(
1919
fn() => $connection->query('SELECT'),
2020
Nette\Database\DriverException::class,
2121
'%a% error%a%',
22-
'HY000',
22+
1,
2323
);
24-
25-
Assert::same(1, $e->getDriverCode());
26-
Assert::same($e->getCode(), $e->getSqlState());
24+
Assert::same('HY000', $e->getSqlState());
2725

2826

2927
$e = Assert::exception(
3028
fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'),
3129
Nette\Database\UniqueConstraintViolationException::class,
3230
'%a% Integrity constraint violation: %a%',
33-
'23000',
31+
19,
3432
);
35-
36-
Assert::same(19, $e->getDriverCode());
37-
Assert::same($e->getCode(), $e->getSqlState());
33+
Assert::same('23000', $e->getSqlState());
3834

3935

4036
$e = Assert::exception(
4137
fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'),
4238
Nette\Database\NotNullConstraintViolationException::class,
4339
'%a% Integrity constraint violation: %a%',
44-
'23000',
40+
19,
4541
);
46-
47-
Assert::same(19, $e->getDriverCode());
48-
Assert::same($e->getCode(), $e->getSqlState());
42+
Assert::same('23000', $e->getSqlState());
4943

5044

5145
$e = Assert::exception(function () use ($connection) {
5246
$connection->query('PRAGMA foreign_keys=true');
5347
$connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")');
54-
}, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', '23000');
55-
56-
Assert::same(19, $e->getDriverCode());
57-
Assert::same($e->getCode(), $e->getSqlState());
48+
}, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', 19);
49+
Assert::same('23000', $e->getSqlState());

0 commit comments

Comments
 (0)