Skip to content

Commit 12ef39a

Browse files
committed
DriverException::getCode() returns driver error code instead of SQLState (BC break)
1 parent f527cac commit 12ef39a

5 files changed

Lines changed: 56 additions & 88 deletions

File tree

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: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,30 @@ test('Exception thrown for invalid database credentials', function () {
2020
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
2121
Nette\Database\ConnectionException::class,
2222
'%a% Access denied for user %a%',
23+
1045,
2324
);
24-
25-
Assert::same(1045, $e->getDriverCode());
2625
Assert::contains($e->getSqlState(), ['HY000', '28000']);
27-
Assert::same($e->getCode(), $e->getSqlState());
2826
});
2927

3028

31-
testException(
32-
'Exception thrown when calling rollback with no active transaction',
33-
fn() => $connection->rollback(),
34-
Nette\Database\DriverException::class,
35-
'There is no active transaction',
36-
);
29+
test('Exception thrown when calling rollback with no active transaction', function () use ($connection) {
30+
$e = Assert::exception(
31+
fn() => $connection->rollback(),
32+
Nette\Database\DriverException::class,
33+
'There is no active transaction',
34+
);
35+
Assert::null($e->getSqlState());
36+
});
3737

3838

3939
test('Exception thrown for syntax error in SQL query', function () use ($connection) {
4040
$e = Assert::exception(
4141
fn() => $connection->query('SELECT'),
4242
Nette\Database\DriverException::class,
4343
'%a% Syntax error %a%',
44-
'42000',
44+
1064,
4545
);
46-
47-
Assert::same(1064, $e->getDriverCode());
48-
Assert::same($e->getCode(), $e->getSqlState());
46+
Assert::same('42000', $e->getSqlState());
4947
});
5048

5149

@@ -54,11 +52,9 @@ test('Exception thrown for unique constraint violation', function () use ($conne
5452
fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'),
5553
Nette\Database\UniqueConstraintViolationException::class,
5654
'%a% Integrity constraint violation: %a%',
57-
'23000',
55+
1062,
5856
);
59-
60-
Assert::same(1062, $e->getDriverCode());
61-
Assert::same($e->getCode(), $e->getSqlState());
57+
Assert::same('23000', $e->getSqlState());
6258
});
6359

6460

@@ -67,11 +63,9 @@ test('Exception thrown for not null constraint violation', function () use ($con
6763
fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'),
6864
Nette\Database\NotNullConstraintViolationException::class,
6965
'%a% Integrity constraint violation: %a%',
70-
'23000',
66+
1048,
7167
);
72-
73-
Assert::same(1048, $e->getDriverCode());
74-
Assert::same($e->getCode(), $e->getSqlState());
68+
Assert::same('23000', $e->getSqlState());
7569
});
7670

7771

@@ -80,9 +74,7 @@ test('Exception thrown for foreign key constraint violation', function () use ($
8074
fn() => $connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")'),
8175
Nette\Database\ForeignKeyConstraintViolationException::class,
8276
'%a% a foreign key constraint fails %a%',
83-
'23000',
77+
1452,
8478
);
85-
86-
Assert::same(1452, $e->getDriverCode());
87-
Assert::same($e->getCode(), $e->getSqlState());
79+
Assert::same('23000', $e->getSqlState());
8880
});

tests/Database/Connection.exceptions.postgre.phpt

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,30 @@ test('Exception thrown for invalid database credentials', function () {
2121
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
2222
Nette\Database\ConnectionException::class,
2323
null,
24-
'08006',
24+
7,
2525
);
26-
27-
Assert::same(7, $e->getDriverCode());
28-
Assert::same($e->getCode(), $e->getSqlState());
26+
Assert::same('08006', $e->getSqlState());
2927
});
3028

3129

32-
testException(
33-
'Exception thrown when calling rollback with no active transaction',
34-
fn() => $connection->rollback(),
35-
Nette\Database\DriverException::class,
36-
'There is no active transaction',
37-
null,
38-
);
30+
test('Exception thrown when calling rollback with no active transaction', function () use ($connection) {
31+
$e = Assert::exception(
32+
fn() => $connection->rollback(),
33+
Nette\Database\DriverException::class,
34+
'There is no active transaction',
35+
);
36+
Assert::null($e->getSqlState());
37+
});
3938

4039

4140
test('Exception thrown for syntax error in SQL query', function () use ($connection) {
4241
$e = Assert::exception(
4342
fn() => $connection->query('SELECT INTO'),
4443
Nette\Database\DriverException::class,
4544
'%a% syntax error %A%',
46-
'42601',
45+
7,
4746
);
48-
49-
Assert::same(7, $e->getDriverCode());
50-
Assert::same($e->getCode(), $e->getSqlState());
47+
Assert::same('42601', $e->getSqlState());
5148
});
5249

5350

@@ -56,11 +53,9 @@ test('Exception thrown for unique constraint violation', function () use ($conne
5653
fn() => $connection->query("INSERT INTO author (id, name, web, born) VALUES (11, '', '', NULL)"),
5754
Nette\Database\UniqueConstraintViolationException::class,
5855
'%a% Unique violation: %A%',
59-
'23505',
56+
7,
6057
);
61-
62-
Assert::same(7, $e->getDriverCode());
63-
Assert::same($e->getCode(), $e->getSqlState());
58+
Assert::same('23505', $e->getSqlState());
6459
});
6560

6661

@@ -69,11 +64,9 @@ test('Exception thrown for not null constraint violation', function () use ($con
6964
fn() => $connection->query("INSERT INTO author (name, web, born) VALUES (NULL, '', NULL)"),
7065
Nette\Database\NotNullConstraintViolationException::class,
7166
'%a% Not null violation: %A%',
72-
'23502',
67+
7,
7368
);
74-
75-
Assert::same(7, $e->getDriverCode());
76-
Assert::same($e->getCode(), $e->getSqlState());
69+
Assert::same('23502', $e->getSqlState());
7770
});
7871

7972

@@ -82,9 +75,7 @@ test('Exception thrown for foreign key constraint violation', function () use ($
8275
fn() => $connection->query("INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, '')"),
8376
Nette\Database\ForeignKeyConstraintViolationException::class,
8477
'%a% Foreign key violation: %A%',
85-
'23503',
78+
7,
8679
);
87-
88-
Assert::same(7, $e->getDriverCode());
89-
Assert::same($e->getCode(), $e->getSqlState());
80+
Assert::same('23503', $e->getSqlState());
9081
});

tests/Database/Connection.exceptions.sqlite.phpt

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,30 @@ test('Exception thrown for unable to open database file', function () {
2020
fn() => new Nette\Database\Connection('sqlite:.'),
2121
Nette\Database\ConnectionException::class,
2222
'SQLSTATE[HY000] [14] unable to open database file',
23-
'HY000',
23+
14,
2424
);
25-
26-
Assert::same(14, $e->getDriverCode());
27-
Assert::same($e->getCode(), $e->getSqlState());
25+
Assert::same('HY000', $e->getSqlState());
2826
});
2927

3028

31-
testException(
32-
'Exception thrown when calling rollback with no active transaction',
33-
fn() => $connection->rollback(),
34-
Nette\Database\DriverException::class,
35-
'There is no active transaction',
36-
);
29+
test('Exception thrown when calling rollback with no active transaction', function () use ($connection) {
30+
$e = Assert::exception(
31+
fn() => $connection->rollback(),
32+
Nette\Database\DriverException::class,
33+
'There is no active transaction',
34+
);
35+
Assert::null($e->getSqlState());
36+
});
3737

3838

3939
test('Exception thrown for error in SQL query', function () use ($connection) {
4040
$e = Assert::exception(
4141
fn() => $connection->query('SELECT'),
4242
Nette\Database\DriverException::class,
4343
'%a% error%a%',
44-
'HY000',
44+
1,
4545
);
46-
47-
Assert::same(1, $e->getDriverCode());
48-
Assert::same($e->getCode(), $e->getSqlState());
46+
Assert::same('HY000', $e->getSqlState());
4947
});
5048

5149

@@ -54,11 +52,9 @@ test('Exception thrown for unique constraint violation', function () use ($conne
5452
fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'),
5553
Nette\Database\UniqueConstraintViolationException::class,
5654
'%a% Integrity constraint violation: %a%',
57-
'23000',
55+
19,
5856
);
59-
60-
Assert::same(19, $e->getDriverCode());
61-
Assert::same($e->getCode(), $e->getSqlState());
57+
Assert::same('23000', $e->getSqlState());
6258
});
6359

6460

@@ -67,20 +63,16 @@ test('Exception thrown for not null constraint violation', function () use ($con
6763
fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'),
6864
Nette\Database\NotNullConstraintViolationException::class,
6965
'%a% Integrity constraint violation: %a%',
70-
'23000',
66+
19,
7167
);
72-
73-
Assert::same(19, $e->getDriverCode());
74-
Assert::same($e->getCode(), $e->getSqlState());
68+
Assert::same('23000', $e->getSqlState());
7569
});
7670

7771

7872
test('Exception thrown for foreign key constraint violation', function () use ($connection) {
7973
$e = Assert::exception(function () use ($connection) {
8074
$connection->query('PRAGMA foreign_keys=true');
8175
$connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")');
82-
}, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', '23000');
83-
84-
Assert::same(19, $e->getDriverCode());
85-
Assert::same($e->getCode(), $e->getSqlState());
76+
}, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', 19);
77+
Assert::same('23000', $e->getSqlState());
8678
});

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,

0 commit comments

Comments
 (0)