Skip to content

Commit

Permalink
fix sequence casing escaping & add support for FQN for last inserted …
Browse files Browse the repository at this point in the history
…id (BC break!)
  • Loading branch information
hrach committed Oct 15, 2023
1 parent 05ae6de commit 1e7fc26
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Dbal\Result\Result;
Expand Down Expand Up @@ -144,7 +145,7 @@ public function queryByQueryBuilder(QueryBuilder $queryBuilder): Result


/** @inheritdoc */
public function getLastInsertedId(?string $sequenceName = null)
public function getLastInsertedId(string|Fqn|null $sequenceName = null)
{
if (!$this->connected) {
$this->connect();
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/IDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Nextras\Dbal\Exception\NotSupportedException;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Result\Result;

Expand Down Expand Up @@ -64,7 +65,7 @@ public function query(string $query): Result;
* Returns the last inserted id.
* @internal
*/
public function getLastInsertedId(?string $sequenceName = null): mixed;
public function getLastInsertedId(string|Fqn|null $sequenceName = null): mixed;


/**
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Mysqli/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Nextras\Dbal\Exception\NotSupportedException;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Platforms\MySqlPlatform;
use Nextras\Dbal\Result\Result;
Expand Down Expand Up @@ -159,7 +160,7 @@ public function query(string $query): Result
}


public function getLastInsertedId(?string $sequenceName = null): mixed
public function getLastInsertedId(string|Fqn|null $sequenceName = null): mixed
{
$this->checkConnection();
assert($this->connection !== null);
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Pdo/PdoDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\Exception\InvalidStateException;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Result\IResultAdapter;
use Nextras\Dbal\Result\Result;
use Nextras\Dbal\Utils\LoggerHelper;
Expand Down Expand Up @@ -108,7 +109,7 @@ public function query(string $query): Result
}


public function getLastInsertedId(?string $sequenceName = null): mixed
public function getLastInsertedId(string|Fqn|null $sequenceName = null): mixed
{
$this->checkConnection();
assert($this->connection !== null);
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/PdoMysql/PdoMysqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Nextras\Dbal\Exception\NotSupportedException;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Platforms\MySqlPlatform;
use Nextras\Dbal\Result\IResultAdapter;
Expand Down Expand Up @@ -100,7 +101,7 @@ public function createPlatform(IConnection $connection): IPlatform
}


public function getLastInsertedId(?string $sequenceName = null): int
public function getLastInsertedId(string|Fqn|null $sequenceName = null): int
{
return (int) parent::getLastInsertedId($sequenceName);
}
Expand Down
11 changes: 9 additions & 2 deletions src/Drivers/PdoPgsql/PdoPgsqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Nextras\Dbal\Exception\NotSupportedException;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Platforms\PostgreSqlPlatform;
use Nextras\Dbal\Result\IResultAdapter;
Expand Down Expand Up @@ -72,15 +73,21 @@ public function createPlatform(IConnection $connection): IPlatform
}


public function getLastInsertedId(?string $sequenceName = null): mixed
public function getLastInsertedId(string|Fqn|null $sequenceName = null): mixed
{
if ($sequenceName === null) {
throw new InvalidArgumentException('PgsqlDriver requires to pass sequence name for getLastInsertedId() method.');
}

$this->checkConnection();
assert($this->connection !== null);
$sql = 'SELECT CURRVAL(' . $this->convertStringToSql($sequenceName) . ')';

$sequenceName = match (true) {
$sequenceName instanceOf Fqn => $this->convertIdentifierToSql($sequenceName->schema) . '.' .
$this->convertIdentifierToSql($sequenceName->name),
default => $this->convertIdentifierToSql($sequenceName),
};
$sql = 'SELECT CURRVAL(\'' . $sequenceName . '\')';
return $this->loggedQuery($sql)->fetchField();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/PdoSqlsrv/PdoSqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Nextras\Dbal\Exception\NotSupportedException;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Platforms\SqlServerPlatform;
use Nextras\Dbal\Result\IResultAdapter;
Expand Down Expand Up @@ -94,7 +95,7 @@ public function createPlatform(IConnection $connection): IPlatform
}


public function getLastInsertedId(?string $sequenceName = null): mixed
public function getLastInsertedId(string|Fqn|null $sequenceName = null): mixed
{
$this->checkConnection();
return $this->loggedQuery('SELECT SCOPE_IDENTITY()')->fetchField();
Expand Down
11 changes: 9 additions & 2 deletions src/Drivers/Pgsql/PgsqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Nextras\Dbal\Exception\NotSupportedException;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Platforms\PostgreSqlPlatform;
use Nextras\Dbal\Result\Result;
Expand Down Expand Up @@ -178,14 +179,20 @@ public function query(string $query): Result
}


public function getLastInsertedId(?string $sequenceName = null): mixed
public function getLastInsertedId(string|Fqn|null $sequenceName = null): mixed
{
if ($sequenceName === null) {
throw new InvalidArgumentException('PgsqlDriver requires to pass sequence name for getLastInsertedId() method.');
}
$this->checkConnection();
assert($this->connection !== null);
$sql = 'SELECT CURRVAL(' . pg_escape_literal($this->connection, $sequenceName) . ')';

$sequenceName = match (true) {
$sequenceName instanceOf Fqn => pg_escape_identifier($this->connection, $sequenceName->schema) . '.' .
pg_escape_identifier($this->connection, $sequenceName->name),
default => pg_escape_identifier($this->connection, $sequenceName),
};
$sql = 'SELECT CURRVAL(\'' . $sequenceName . '\')';
return $this->loggedQuery($sql)->fetchField();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Sqlsrv/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Nextras\Dbal\Exception\NotSupportedException;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Platforms\SqlServerPlatform;
use Nextras\Dbal\Result\Result;
Expand Down Expand Up @@ -183,7 +184,7 @@ public function query(string $query): Result
}


public function getLastInsertedId(?string $sequenceName = null): mixed
public function getLastInsertedId(string|Fqn|null $sequenceName = null): mixed
{
$this->checkConnection();
return $this->loggedQuery('SELECT SCOPE_IDENTITY()')->fetchField();
Expand Down
3 changes: 2 additions & 1 deletion src/IConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nextras\Dbal\Drivers\Exception\DriverException;
use Nextras\Dbal\Drivers\Exception\QueryException;
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Dbal\Result\Result;
Expand Down Expand Up @@ -88,7 +89,7 @@ public function queryByQueryBuilder(QueryBuilder $queryBuilder): Result;
* Returns last inserted ID.
* @return int|string|null
*/
public function getLastInsertedId(?string $sequenceName = null);
public function getLastInsertedId(string|Fqn|null $sequenceName = null);


/**
Expand Down
14 changes: 12 additions & 2 deletions tests/cases/integration/connection.postgres.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace NextrasTests\Dbal;


use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Platforms\Data\Fqn;
use Tester\Assert;


Expand All @@ -32,12 +33,21 @@ class ConnectionPostgresTest extends IntegrationTestCase

$this->connection->query('INSERT INTO publishers %values', ['name' => 'FOO']);
Assert::same(2, $this->connection->getLastInsertedId('publishers_id_seq'));
Assert::same(2, $this->connection->getLastInsertedId('public.publishers_id_seq'));
Assert::same(2, $this->connection->getLastInsertedId(new Fqn(name: 'publishers_id_seq', schema: 'public')));

Assert::exception(function () {
Assert::exception(function() {
$this->connection->getLastInsertedId();
}, InvalidArgumentException::class, 'PgsqlDriver requires to pass sequence name for getLastInsertedId() method.');
}


public function testSequenceCasing()
{
$this->lockConnection($this->connection);
$this->connection->query('CREATE SEQUENCE %column INCREMENT 5 START 10;', "MySequence");
$this->connection->query('SELECT NEXTVAL(\'%column\')', "MySequence");
Assert::same(10, $this->connection->getLastInsertedId("MySequence"));
}
}


Expand Down

0 comments on commit 1e7fc26

Please sign in to comment.