Skip to content

Commit 36154d1

Browse files
committed
DriverException::getCode() returns driver error code instead of SQLState (BC break)
1 parent 6aa8804 commit 36154d1

File tree

7 files changed

+44
-73
lines changed

7 files changed

+44
-73
lines changed

src/Database/DriverException.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ class DriverException extends \Exception
1919
{
2020
public static function from(self $e): static
2121
{
22-
return new static($e->getMessage(), $e->sqlState, $e->getDriverCode() ?? 0, $e->query, $e);
22+
return new static($e->getMessage(), $e->sqlState, $e->getCode(), $e->query, $e);
2323
}
2424

2525

2626
public function __construct(
2727
string $message,
2828
private readonly ?string $sqlState = null,
29-
private int $driverCode = 0,
29+
int $code = 0,
3030
private readonly ?SqlLiteral $query = null,
3131
?\Throwable $previous = null,
3232
) {
33-
parent::__construct($message, 0, $previous);
34-
$this->code = $sqlState ?: null;
33+
parent::__construct($message, $code, $previous);
3534
}
3635

3736

38-
public function getDriverCode(): int|string|null
37+
/** @deprecated use getCode() */
38+
public function getDriverCode(): int
3939
{
40-
return $this->driverCode ?: null;
40+
return $this->getCode();
4141
}
4242

4343

src/Database/Drivers/Engines/MySQLEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function isSupported(string $feature): bool
3939

4040
public function classifyException(Nette\Database\DriverException $e): ?string
4141
{
42-
$code = $e->getDriverCode();
42+
$code = $e->getCode();
4343
return match (true) {
4444
in_array($code, [1216, 1217, 1451, 1452, 1701], strict: true) => Nette\Database\ForeignKeyConstraintViolationException::class,
4545
in_array($code, [1062, 1557, 1569, 1586], strict: true) => Nette\Database\UniqueConstraintViolationException::class,

src/Database/Drivers/Engines/OracleEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function isSupported(string $feature): bool
3838

3939
public function classifyException(Nette\Database\DriverException $e): ?string
4040
{
41-
$code = $e->getDriverCode();
41+
$code = $e->getCode();
4242
return match (true) {
4343
in_array($code, [1, 2299, 38911], strict: true) => Nette\Database\UniqueConstraintViolationException::class,
4444
in_array($code, [1400], strict: true) => Nette\Database\NotNullConstraintViolationException::class,

src/Database/Drivers/Engines/SQLiteEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function isSupported(string $feature): bool
4040
public function classifyException(Nette\Database\DriverException $e): ?string
4141
{
4242
$message = $e->getMessage();
43-
if ($e->getDriverCode() !== 19) {
43+
if ($e->getCode() !== 19) {
4444
return null;
4545

4646
} elseif (

tests/Database/Connection.exceptions.mysql.phpt

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ test('Exception thrown for invalid database credentials', function () {
2020
fn() => (new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'))->connect(),
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

@@ -33,8 +31,9 @@ test('Exception thrown when calling rollback with no active transaction', functi
3331
fn() => $connection->rollback(),
3432
Nette\Database\DriverException::class,
3533
'There is no active transaction',
34+
0,
3635
);
37-
Assert::same(null, $e->getDriverCode());
36+
Assert::null($e->getSqlState());
3837
});
3938

4039

@@ -43,11 +42,9 @@ test('Exception thrown for syntax error in SQL query', function () use ($connect
4342
fn() => $connection->query('SELECT'),
4443
Nette\Database\DriverException::class,
4544
'%a% Syntax error %a%',
46-
'42000',
45+
1064,
4746
);
48-
49-
Assert::same(1064, $e->getDriverCode());
50-
Assert::same($e->getCode(), $e->getSqlState());
47+
Assert::same('42000', $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% Integrity constraint violation: %a%',
59-
'23000',
56+
1062,
6057
);
61-
62-
Assert::same(1062, $e->getDriverCode());
63-
Assert::same($e->getCode(), $e->getSqlState());
58+
Assert::same('23000', $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% Integrity constraint violation: %a%',
72-
'23000',
67+
1048,
7368
);
74-
75-
Assert::same(1048, $e->getDriverCode());
76-
Assert::same($e->getCode(), $e->getSqlState());
69+
Assert::same('23000', $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% a foreign key constraint fails %a%',
85-
'23000',
78+
1452,
8679
);
87-
88-
Assert::same(1452, $e->getDriverCode());
89-
Assert::same($e->getCode(), $e->getSqlState());
80+
Assert::same('23000', $e->getSqlState());
9081
});

tests/Database/Connection.exceptions.postgre.phpt

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ test('Exception thrown for invalid database credentials', function () {
2121
fn() => (new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'))->connect(),
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

@@ -34,9 +32,9 @@ test('Exception thrown when calling rollback with no active transaction', functi
3432
fn() => $connection->rollback(),
3533
Nette\Database\DriverException::class,
3634
'There is no active transaction',
35+
0,
3736
);
38-
39-
Assert::same(null, $e->getDriverCode());
37+
Assert::null($e->getSqlState());
4038
});
4139

4240

@@ -45,11 +43,9 @@ test('Exception thrown for syntax error in SQL query', function () use ($connect
4543
fn() => $connection->query('SELECT INTO'),
4644
Nette\Database\DriverException::class,
4745
'%a% syntax error %A%',
48-
'42601',
46+
7,
4947
);
50-
51-
Assert::same(7, $e->getDriverCode());
52-
Assert::same($e->getCode(), $e->getSqlState());
48+
Assert::same('42601', $e->getSqlState());
5349
});
5450

5551

@@ -58,11 +54,9 @@ test('Exception thrown for unique constraint violation', function () use ($conne
5854
fn() => $connection->query("INSERT INTO author (id, name, web, born) VALUES (11, '', '', NULL)"),
5955
Nette\Database\UniqueConstraintViolationException::class,
6056
'%a% Unique violation: %A%',
61-
'23505',
57+
7,
6258
);
63-
64-
Assert::same(7, $e->getDriverCode());
65-
Assert::same($e->getCode(), $e->getSqlState());
59+
Assert::same('23505', $e->getSqlState());
6660
});
6761

6862

@@ -71,11 +65,9 @@ test('Exception thrown for not null constraint violation', function () use ($con
7165
fn() => $connection->query("INSERT INTO author (name, web, born) VALUES (NULL, '', NULL)"),
7266
Nette\Database\NotNullConstraintViolationException::class,
7367
'%a% Not null violation: %A%',
74-
'23502',
68+
7,
7569
);
76-
77-
Assert::same(7, $e->getDriverCode());
78-
Assert::same($e->getCode(), $e->getSqlState());
70+
Assert::same('23502', $e->getSqlState());
7971
});
8072

8173

@@ -84,9 +76,7 @@ test('Exception thrown for foreign key constraint violation', function () use ($
8476
fn() => $connection->query("INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, '')"),
8577
Nette\Database\ForeignKeyConstraintViolationException::class,
8678
'%a% Foreign key violation: %A%',
87-
'23503',
79+
7,
8880
);
89-
90-
Assert::same(7, $e->getDriverCode());
91-
Assert::same($e->getCode(), $e->getSqlState());
81+
Assert::same('23503', $e->getSqlState());
9282
});

tests/Database/Connection.exceptions.sqlite.phpt

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ test('Exception thrown for unable to open database file', function () {
2020
fn() => (new Nette\Database\Connection('sqlite:.'))->connect(),
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

@@ -33,9 +31,9 @@ test('Exception thrown when calling rollback with no active transaction', functi
3331
fn() => $connection->rollback(),
3432
Nette\Database\DriverException::class,
3533
'There is no active transaction',
34+
0,
3635
);
37-
38-
Assert::same(null, $e->getDriverCode());
36+
Assert::null($e->getSqlState());
3937
});
4038

4139

@@ -44,11 +42,9 @@ test('Exception thrown for error in SQL query', function () use ($connection) {
4442
fn() => $connection->query('SELECT'),
4543
Nette\Database\DriverException::class,
4644
'%a% error%a%',
47-
'HY000',
45+
1,
4846
);
49-
50-
Assert::same(1, $e->getDriverCode());
51-
Assert::same($e->getCode(), $e->getSqlState());
47+
Assert::same('HY000', $e->getSqlState());
5248
});
5349

5450

@@ -57,11 +53,9 @@ test('Exception thrown for unique constraint violation', function () use ($conne
5753
fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'),
5854
Nette\Database\UniqueConstraintViolationException::class,
5955
'%a% Integrity constraint violation: %a%',
60-
'23000',
56+
19,
6157
);
62-
63-
Assert::same(19, $e->getDriverCode());
64-
Assert::same($e->getCode(), $e->getSqlState());
58+
Assert::same('23000', $e->getSqlState());
6559
});
6660

6761

@@ -70,20 +64,16 @@ test('Exception thrown for not null constraint violation', function () use ($con
7064
fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'),
7165
Nette\Database\NotNullConstraintViolationException::class,
7266
'%a% Integrity constraint violation: %a%',
73-
'23000',
67+
19,
7468
);
75-
76-
Assert::same(19, $e->getDriverCode());
77-
Assert::same($e->getCode(), $e->getSqlState());
69+
Assert::same('23000', $e->getSqlState());
7870
});
7971

8072

8173
test('Exception thrown for foreign key constraint violation', function () use ($connection) {
8274
$e = Assert::exception(function () use ($connection) {
8375
$connection->query('PRAGMA foreign_keys=true');
8476
$connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")');
85-
}, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', '23000');
86-
87-
Assert::same(19, $e->getDriverCode());
88-
Assert::same($e->getCode(), $e->getSqlState());
77+
}, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', 19);
78+
Assert::same('23000', $e->getSqlState());
8979
});

0 commit comments

Comments
 (0)