Skip to content

Commit

Permalink
Use factory methods for lastInsertId() exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Nov 27, 2018
1 parent 3be0f5c commit dd27900
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
36 changes: 36 additions & 0 deletions lib/Doctrine/DBAL/Driver/AbstractDriverException.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,42 @@ public function __construct($message, $sqlState = null, $errorCode = null)
$this->sqlState = $sqlState;
}

/**
* @return AbstractDriverException
*/
public static function noInsertId() : self
{
$message = 'No identity value was generated by the last statement.';

if (static::class === self::class) {
// WIP regarding exceptions, see:
// https://github.com/doctrine/dbal/pull/3335#discussion_r234381175
return new class($message) extends AbstractDriverException {
};
}

return new static($message);
}

/**
* @param string $name The sequence name.
*
* @return AbstractDriverException
*/
public static function noSuchSequence(string $name) : self
{
$message = 'No sequence with name "' . $name . '" found.';

if (static::class === self::class) {
// WIP regarding exceptions, see:
// https://github.com/doctrine/dbal/pull/3335#discussion_r234381175
return new class($message) extends AbstractDriverException {
};
}

return new static($message);
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function lastInsertId(?string $name = null) : string
$lastInsertId = db2_last_insert_id($this->conn);

if ($lastInsertId === '') {
throw new DB2Exception('The last statement did not return an insert ID.');
throw DB2Exception::noInsertId();
}

return $lastInsertId;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function lastInsertId(?string $name = null) : string
$insertId = $this->conn->insert_id;

if ($insertId === 0) {
throw new MysqliException('The last statement did not return an insert ID.');
throw MysqliException::noInsertId();
}

return (string) $insertId;
Expand Down
5 changes: 1 addition & 4 deletions lib/Doctrine/DBAL/Driver/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ public function lastInsertId(?string $name = null) : string

// pdo_mysql and others return '0', pdo_sqlsrv returns ''
if ($lastInsertId === '0' || $lastInsertId === '') {
// WIP regarding exceptions, see:
// https://github.com/doctrine/dbal/pull/3335#discussion_r234381175
throw new class ('The last statement did not return an insert ID.') extends AbstractDriverException {
};
throw AbstractDriverException::noInsertId();
}

return $lastInsertId;
Expand Down
5 changes: 1 addition & 4 deletions lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public function lastInsertId(?string $name = null) : string
$result = $stmt->fetchColumn();

if ($result === false) {
// WIP regarding exceptions, see:
// https://github.com/doctrine/dbal/pull/3335#discussion_r234381175
throw new class ('No sequence with name "' . $name . '" found.') extends AbstractDriverException {
};
throw AbstractDriverException::noSuchSequence($name);
}

return (string) $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function lastInsertId(?string $name = null) : string
$result = $this->query('SELECT ' . $name . '.CURRVAL')->fetchColumn();

if ($result === false) {
throw new SQLAnywhereException('No sequence with name "' . $name . '" found.');
throw SQLAnywhereException::noSuchSequence($name);
}

return (string) $result;
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ public function lastInsertId(?string $name = null) : string

if ($name !== null) {
if ($result === false) {
throw new SQLSrvException('No sequence with name "' . $name . '" found.');
throw SQLSrvException::noSuchSequence($name);
}
} else {
if ($result === null) {
throw new SQLSrvException('No identity value was generated by the last statement.');
throw SQLSrvException::noInsertId();
}
}

Expand Down

0 comments on commit dd27900

Please sign in to comment.