Skip to content

Commit abacea1

Browse files
committed
Connection::getInsertId() returns int|string or exception (BC break)
1 parent 25dfa57 commit abacea1

File tree

7 files changed

+51
-49
lines changed

7 files changed

+51
-49
lines changed

src/Database/Connection.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,7 @@ public function setRowNormalizer(?callable $normalizer): static
169169
}
170170

171171

172-
/**
173-
* Returns last inserted ID.
174-
*/
175-
public function getInsertId(?string $sequence = null): string
172+
public function getInsertId(?string $sequence = null): int|string
176173
{
177174
try {
178175
return $this->getConnection()->getInsertId($sequence);

src/Database/Drivers/PDO/Connection.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,16 @@ public function rollBack(): void
8686
}
8787

8888

89-
public function getInsertId(?string $sequence = null): string
89+
public function getInsertId(?string $sequence = null): int|string
9090
{
9191
try {
92-
$res = $this->pdo->lastInsertId($sequence);
93-
return $res === false ? '0' : $res;
92+
$id = $this->pdo->lastInsertId($sequence);
93+
if ($id === '0' || $id === '' || $id === false) {
94+
throw new DriverException('Cannot retrieve last generated ID.');
95+
}
96+
$int = (int) $id;
97+
return $id === (string) $int ? $int : $id;
98+
9499
} catch (PDOException $e) {
95100
throw new DriverException(...Driver::exceptionArgs($e));
96101
}

src/Database/Explorer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function transaction(callable $callback): mixed
5757
}
5858

5959

60-
public function getInsertId(?string $sequence = null): string
60+
public function getInsertId(?string $sequence = null): int|string
6161
{
6262
return $this->connection->getInsertId($sequence);
6363
}

tests/Database/Connection.getInsertId().mysql.phpt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ $connection->query('
1919
) ENGINE=InnoDB
2020
');
2121

22-
$connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)');
23-
Assert::same('0', $connection->getInsertId());
24-
2522
$connection->query('INSERT INTO noprimarykey (col) VALUES (3)');
26-
Assert::same('0', $connection->getInsertId());
23+
Assert::exception(
24+
fn() => $connection->getInsertId(),
25+
Nette\Database\DriverException::class,
26+
);
2727

2828

2929
$connection->query('
@@ -34,10 +34,10 @@ $connection->query('
3434
');
3535

3636
$connection->query('INSERT INTO primarykey (prim) VALUES (5)');
37-
Assert::same('0', $connection->getInsertId());
38-
39-
$connection->query('INSERT INTO primarykey (prim) VALUES (6)');
40-
Assert::same('0', $connection->getInsertId());
37+
Assert::exception(
38+
fn() => $connection->getInsertId(),
39+
Nette\Database\DriverException::class,
40+
);
4141

4242

4343
$connection->query('
@@ -49,13 +49,13 @@ $connection->query('
4949
');
5050

5151
$connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
52-
Assert::same('1', $connection->getInsertId());
52+
Assert::same(1, $connection->getInsertId());
5353

5454
$connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
55-
Assert::same('2', $connection->getInsertId());
55+
Assert::same(2, $connection->getInsertId());
5656

5757
$connection->query('INSERT INTO autoprimarykey (prim, col) VALUES (10, NULL)');
58-
Assert::same('10', $connection->getInsertId());
58+
Assert::same(10, $connection->getInsertId());
5959

6060

6161
$connection->query('
@@ -67,10 +67,10 @@ $connection->query('
6767
');
6868

6969
$connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
70-
Assert::same('1', $connection->getInsertId());
70+
Assert::same(1, $connection->getInsertId());
7171

7272
$connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
73-
Assert::same('2', $connection->getInsertId());
73+
Assert::same(2, $connection->getInsertId());
7474

7575
$connection->query('INSERT INTO multiautoprimarykey (prim1, prim2) VALUES (10, 3)');
76-
Assert::same('10', $connection->getInsertId());
76+
Assert::same(10, $connection->getInsertId());

tests/Database/Connection.getInsertId().postgre.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ $connection->query('
3636
');
3737

3838
$connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
39-
Assert::same('1', $connection->getInsertId('autoprimarykey_prim_seq'));
39+
Assert::same(1, $connection->getInsertId('autoprimarykey_prim_seq'));
4040

4141
$connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
42-
Assert::same('2', $connection->getInsertId('autoprimarykey_prim_seq'));
42+
Assert::same(2, $connection->getInsertId('autoprimarykey_prim_seq'));
4343

4444
$connection->query('INSERT INTO autoprimarykey (prim, col) VALUES (10, NULL)');
45-
Assert::same('2', $connection->getInsertId('autoprimarykey_prim_seq'));
45+
Assert::same(2, $connection->getInsertId('autoprimarykey_prim_seq'));
4646

4747

4848
$connection->query('
@@ -54,10 +54,10 @@ $connection->query('
5454
');
5555

5656
$connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
57-
Assert::same('1', $connection->getInsertId('multiautoprimarykey_prim1_seq'));
57+
Assert::same(1, $connection->getInsertId('multiautoprimarykey_prim1_seq'));
5858

5959
$connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
60-
Assert::same('2', $connection->getInsertId('multiautoprimarykey_prim1_seq'));
60+
Assert::same(2, $connection->getInsertId('multiautoprimarykey_prim1_seq'));
6161

6262
$connection->query('INSERT INTO multiautoprimarykey (prim1, prim2) VALUES (10, 3)');
63-
Assert::same('2', $connection->getInsertId('multiautoprimarykey_prim1_seq'));
63+
Assert::same(2, $connection->getInsertId('multiautoprimarykey_prim1_seq'));

tests/Database/Connection.getInsertId().sqlite.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ $connection->query('
2020
');
2121

2222
$connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)');
23-
Assert::same('1', $connection->getInsertId());
23+
Assert::same(1, $connection->getInsertId());
2424

2525
$connection->query('INSERT INTO noprimarykey (col) VALUES (3)');
26-
Assert::same('2', $connection->getInsertId());
26+
Assert::same(2, $connection->getInsertId());
2727

2828

2929
$connection->query('
@@ -33,10 +33,10 @@ $connection->query('
3333
');
3434

3535
$connection->query('INSERT INTO primarykey (prim) VALUES (5)');
36-
Assert::same('5', $connection->getInsertId());
36+
Assert::same(5, $connection->getInsertId());
3737

3838
$connection->query('INSERT INTO primarykey (prim) VALUES (6)');
39-
Assert::same('6', $connection->getInsertId());
39+
Assert::same(6, $connection->getInsertId());
4040

4141

4242
$connection->query('
@@ -47,10 +47,10 @@ $connection->query('
4747
');
4848

4949
$connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
50-
Assert::same('1', $connection->getInsertId());
50+
Assert::same(1, $connection->getInsertId());
5151

5252
$connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
53-
Assert::same('2', $connection->getInsertId());
53+
Assert::same(2, $connection->getInsertId());
5454

5555
$connection->query('INSERT INTO autoprimarykey (prim, col) VALUES (10, NULL)');
56-
Assert::same('10', $connection->getInsertId());
56+
Assert::same(10, $connection->getInsertId());

tests/Database/Connection.getInsertId().sqlsrv.phpt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ $connection->query('
2121
');
2222

2323
$connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)');
24-
Assert::same('', $connection->getInsertId());
25-
26-
$connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)');
27-
Assert::same('', $connection->getInsertId());
24+
Assert::exception(
25+
fn() => $connection->getInsertId(),
26+
Nette\Database\DriverException::class,
27+
);
2828

2929

3030
$connection->query("IF OBJECT_ID('primarykey', 'U') IS NOT NULL DROP TABLE primarykey");
@@ -36,10 +36,10 @@ $connection->query('
3636
');
3737

3838
$connection->query('INSERT INTO primarykey (prim) VALUES (5)');
39-
Assert::same('', $connection->getInsertId());
40-
41-
$connection->query('INSERT INTO primarykey (prim) VALUES (6)');
42-
Assert::same('', $connection->getInsertId());
39+
Assert::exception(
40+
fn() => $connection->getInsertId(),
41+
Nette\Database\DriverException::class,
42+
);
4343

4444

4545
$connection->query("IF OBJECT_ID('autoprimarykey', 'U') IS NOT NULL DROP TABLE autoprimarykey");
@@ -52,13 +52,13 @@ $connection->query('
5252
');
5353

5454
$connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
55-
Assert::same('1', $connection->getInsertId());
55+
Assert::same(1, $connection->getInsertId());
5656

5757
$connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
58-
Assert::same('2', $connection->getInsertId());
58+
Assert::same(2, $connection->getInsertId());
5959

6060
$connection->query('SET IDENTITY_INSERT autoprimarykey ON; INSERT INTO autoprimarykey (prim, col) VALUES (10, NULL)');
61-
Assert::same('10', $connection->getInsertId());
61+
Assert::same(10, $connection->getInsertId());
6262

6363

6464
$connection->query("IF OBJECT_ID('multiautoprimarykey', 'U') IS NOT NULL DROP TABLE multiautoprimarykey");
@@ -71,10 +71,10 @@ $connection->query('
7171
');
7272

7373
$connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
74-
Assert::same('1', $connection->getInsertId());
74+
Assert::same(1, $connection->getInsertId());
7575

7676
$connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
77-
Assert::same('2', $connection->getInsertId());
77+
Assert::same(2, $connection->getInsertId());
7878

7979
$connection->query('SET IDENTITY_INSERT multiautoprimarykey ON; INSERT INTO multiautoprimarykey (prim1, prim2) VALUES (10, 3)');
80-
Assert::same('10', $connection->getInsertId());
80+
Assert::same(10, $connection->getInsertId());

0 commit comments

Comments
 (0)