Skip to content

Commit

Permalink
Merge pull request #4691 from morozov/issues/4687
Browse files Browse the repository at this point in the history
[GH-4687] Remove support for Connection::lastInsertId($name)
  • Loading branch information
morozov authored Jun 26, 2021
2 parents ea319d6 + b2e0427 commit ca22fa3
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 231 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ awareness about deprecated code.

# Upgrade to 4.0

## Removed support for `Connection::lastInsertId($name)`

The `Connection::lastInsertId()` method no longer accepts a sequence name.

## Removed defaults for MySQL table charset, collation and engine

The library no longer provides the default values for MySQL table charset, collation and engine.
Expand Down
20 changes: 4 additions & 16 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\SQL\Parser;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
use Throwable;
use Traversable;

Expand Down Expand Up @@ -1104,31 +1103,20 @@ public function getTransactionNestingLevel(): int
}

/**
* Returns the ID of the last inserted row, or the last value from a sequence object,
* depending on the underlying driver.
* Returns the ID of the last inserted row.
*
* Note: This method may not return a meaningful or consistent result across different drivers,
* because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY
* columns or sequences.
*
* @param string|null $name Name of the sequence object from which the ID should be returned.
* columns.
*
* @return string A string representation of the last inserted ID.
*
* @throws Exception
*/
public function lastInsertId(?string $name = null): string
public function lastInsertId(): string
{
if ($name !== null) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);
}

try {
return $this->getWrappedConnection()->lastInsertId($name);
return $this->getWrappedConnection()->lastInsertId();
} catch (Driver\Exception $e) {
throw $this->convertException($e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Driver/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public function quote(string $value): string;
public function exec(string $sql): int;

/**
* Returns the ID of the last inserted row or sequence value.
* Returns the ID of the last inserted row.
*
* @throws Exception
*/
public function lastInsertId(?string $name = null): string;
public function lastInsertId(): string;

/**
* Initiates a transaction.
Expand Down
11 changes: 1 addition & 10 deletions src/Driver/IBMDB2/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\Deprecations\Deprecation;
use stdClass;

use function assert;
Expand Down Expand Up @@ -103,16 +102,8 @@ public function exec(string $sql): int
return db2_num_rows($stmt);
}

public function lastInsertId(?string $name = null): string
public function lastInsertId(): string
{
if ($name !== null) {
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);
}

return db2_last_insert_id($this->conn);
}

Expand Down
11 changes: 1 addition & 10 deletions src/Driver/Mysqli/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\Deprecations\Deprecation;
use mysqli;

use function assert;
Expand Down Expand Up @@ -126,16 +125,8 @@ public function exec(string $sql): int
return $this->conn->affected_rows;
}

public function lastInsertId(?string $name = null): string
public function lastInsertId(): string
{
if ($name !== null) {
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);
}

return (string) $this->conn->insert_id;
}

Expand Down
22 changes: 2 additions & 20 deletions src/Driver/OCI8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
use Doctrine\DBAL\Driver\Exception\IdentityColumnsNotSupported;
use Doctrine\DBAL\Driver\OCI8\Exception\ConnectionFailed;
use Doctrine\DBAL\Driver\OCI8\Exception\Error;
use Doctrine\DBAL\Driver\OCI8\Exception\SequenceDoesNotExist;
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\Deprecations\Deprecation;

use function addcslashes;
use function assert;
Expand Down Expand Up @@ -97,25 +95,9 @@ public function exec(string $sql): int
return $this->prepare($sql)->execute()->rowCount();
}

public function lastInsertId(?string $name = null): string
public function lastInsertId(): string
{
if ($name === null) {
throw IdentityColumnsNotSupported::new();
}

Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);

$result = $this->query('SELECT ' . $name . '.CURRVAL FROM DUAL')->fetchOne();

if ($result === false) {
throw SequenceDoesNotExist::new();
}

return $result;
throw IdentityColumnsNotSupported::new();
}

public function beginTransaction(): void
Expand Down
15 changes: 2 additions & 13 deletions src/Driver/PDO/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\Deprecations\Deprecation;
use PDO;
use PDOException;
use PDOStatement;
Expand Down Expand Up @@ -89,20 +88,10 @@ public function quote(string $value): string
return $this->connection->quote($value);
}

public function lastInsertId(?string $name = null): string
public function lastInsertId(): string
{
try {
if ($name === null) {
return $this->connection->lastInsertId();
}

Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);

return $this->connection->lastInsertId($name);
return $this->connection->lastInsertId();
} catch (PDOException $exception) {
throw Exception::new($exception);
}
Expand Down
17 changes: 2 additions & 15 deletions src/Driver/PDO/SQLSrv/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\Deprecations\Deprecation;
use PDO;

final class Connection implements ServerInfoAwareConnection
Expand Down Expand Up @@ -43,21 +42,9 @@ public function exec(string $sql): int
return $this->connection->exec($sql);
}

public function lastInsertId(?string $name = null): string
public function lastInsertId(): string
{
if ($name === null) {
return $this->connection->lastInsertId($name);
}

Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);

return $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?')
->execute([$name])
->fetchOne();
return $this->connection->lastInsertId();
}

public function beginTransaction(): void
Expand Down
18 changes: 2 additions & 16 deletions src/Driver/SQLSrv/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\SQLSrv\Exception\Error;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\Deprecations\Deprecation;

use function sqlsrv_begin_transaction;
use function sqlsrv_commit;
Expand Down Expand Up @@ -87,22 +86,9 @@ public function exec(string $sql): int
return $rowsAffected;
}

public function lastInsertId(?string $name = null): string
public function lastInsertId(): string
{
if ($name !== null) {
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);

$result = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?')
->execute([$name]);
} else {
$result = $this->query('SELECT @@IDENTITY');
}

return $result->fetchOne();
return $this->query('SELECT @@IDENTITY')->fetchOne();
}

public function beginTransaction(): void
Expand Down
13 changes: 2 additions & 11 deletions src/Portability/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Result as DriverResult;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\Deprecations\Deprecation;

/**
* Portability wrapper for a Connection.
Expand Down Expand Up @@ -58,17 +57,9 @@ public function exec(string $sql): int
return $this->connection->exec($sql);
}

public function lastInsertId(?string $name = null): string
public function lastInsertId(): string
{
if ($name !== null) {
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4687',
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
);
}

return $this->connection->lastInsertId($name);
return $this->connection->lastInsertId();
}

public function beginTransaction(): void
Expand Down
45 changes: 0 additions & 45 deletions tests/Functional/Driver/OCI8/ConnectionTest.php

This file was deleted.

14 changes: 7 additions & 7 deletions tests/Functional/Ticket/DBAL630Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ protected function setUp(): void
}

try {
$this->connection->executeStatement('CREATE TABLE dbal630 (id SERIAL, bool_col BOOLEAN NOT NULL);');
$this->connection->executeStatement('CREATE TABLE dbal630_allow_nulls (id SERIAL, bool_col BOOLEAN);');
$this->connection->executeStatement('CREATE TABLE dbal630 (id SERIAL, bool_col BOOLEAN NOT NULL)');
$this->connection->executeStatement('CREATE TABLE dbal630_allow_nulls (id SERIAL, bool_col BOOLEAN)');
} catch (Exception $e) {
}

Expand All @@ -47,7 +47,7 @@ protected function tearDown(): void
public function testBooleanConversionSqlLiteral(): void
{
$this->connection->executeStatement('INSERT INTO dbal630 (bool_col) VALUES(false)');
$id = $this->connection->lastInsertId('dbal630_id_seq');
$id = $this->connection->lastInsertId();
self::assertNotEmpty($id);

$row = $this->connection->fetchAssociative('SELECT bool_col FROM dbal630 WHERE id = ?', [$id]);
Expand All @@ -63,7 +63,7 @@ public function testBooleanConversionBoolParamRealPrepares(): void
['false'],
[ParameterType::BOOLEAN]
);
$id = $this->connection->lastInsertId('dbal630_id_seq');
$id = $this->connection->lastInsertId();
self::assertNotEmpty($id);

$row = $this->connection->fetchAssociative('SELECT bool_col FROM dbal630 WHERE id = ?', [$id]);
Expand All @@ -84,7 +84,7 @@ public function testBooleanConversionBoolParamEmulatedPrepares(): void
$stmt->bindValue(1, $platform->convertBooleansToDatabaseValue('false'), ParameterType::BOOLEAN);
$stmt->execute();

$id = $this->connection->lastInsertId('dbal630_id_seq');
$id = $this->connection->lastInsertId();

self::assertNotEmpty($id);

Expand All @@ -111,7 +111,7 @@ public function testBooleanConversionNullParamEmulatedPrepares(
$stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue));
$stmt->execute();

$id = $this->connection->lastInsertId('dbal630_allow_nulls_id_seq');
$id = $this->connection->lastInsertId();

self::assertNotEmpty($id);

Expand Down Expand Up @@ -142,7 +142,7 @@ public function testBooleanConversionNullParamEmulatedPreparesWithBooleanTypeInB
);
$stmt->execute();

$id = $this->connection->lastInsertId('dbal630_allow_nulls_id_seq');
$id = $this->connection->lastInsertId();

self::assertNotEmpty($id);

Expand Down
Loading

0 comments on commit ca22fa3

Please sign in to comment.