From 9a3316687a22ddcfbd9fb271f6a7fe394dca9dec Mon Sep 17 00:00:00 2001 From: Marc Morera Date: Sat, 4 Apr 2020 16:07:49 +0200 Subject: [PATCH] Added write operations result - Last inserted ID - Number of affected rows !PgSQL is not implementing this yet. Values NULL for both variables! --- src/Connection.php | 25 +++++-- src/Driver/Mysql/MysqlDriver.php | 7 +- src/Driver/PostgreSQL/PostgreSQLDriver.php | 2 +- src/Driver/SQLite/SQLiteDriver.php | 7 +- src/Result.php | 60 +++++++++++++--- tests/ConnectionTest.php | 80 ++++++++++++++++++++-- 6 files changed, 156 insertions(+), 25 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index 66ce3d6..beaff70 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -358,6 +358,8 @@ public function upsert( * * @param string $name * @param array $fields + * @param array $extra + * @param bool $autoincrementId * * @return PromiseInterface * @@ -366,7 +368,9 @@ public function upsert( */ public function createTable( string $name, - array $fields + array $fields, + array $extra = [], + bool $autoincrementId = false ): PromiseInterface { if (empty($fields)) { throw InvalidArgumentException::fromEmptyFieldsArray(); @@ -375,15 +379,24 @@ public function createTable( $schema = new Schema(); $table = $schema->createTable($name); foreach ($fields as $field => $type) { - $extra = []; - if ($type = 'string') { - $extra = ['length' => 255]; + $extraField = ( + array_key_exists($field, $extra) && + is_array($extra[$field]) + ) ? $extra[$field] : []; + + if ( + $type == 'string' && + !array_key_exists('length', $extraField) + ) { + $extraField = ['length' => 255]; } - $table->addColumn($field, $type, $extra); + $table->addColumn($field, $type, $extraField); } - $table->setPrimaryKey([array_key_first($fields)]); + $id = array_key_first($fields); + $table->setPrimaryKey([$id]); + $table->getColumn($id)->setAutoincrement($autoincrementId); return $this->executeSchema($schema); } diff --git a/src/Driver/Mysql/MysqlDriver.php b/src/Driver/Mysql/MysqlDriver.php index 420f4c9..dd9eb28 100644 --- a/src/Driver/Mysql/MysqlDriver.php +++ b/src/Driver/Mysql/MysqlDriver.php @@ -82,7 +82,12 @@ public function query( ->connection ->query($sql, $parameters) ->then(function (QueryResult $queryResult) { - return new Result($queryResult->resultRows); + + return new Result( + $queryResult->resultRows, + $queryResult->insertId, + $queryResult->affectedRows + ); }) ->otherwise(function (Exception $exception) { $message = $exception->getMessage(); diff --git a/src/Driver/PostgreSQL/PostgreSQLDriver.php b/src/Driver/PostgreSQL/PostgreSQLDriver.php index 007c122..c95e5fa 100644 --- a/src/Driver/PostgreSQL/PostgreSQLDriver.php +++ b/src/Driver/PostgreSQL/PostgreSQLDriver.php @@ -118,7 +118,7 @@ public function query( return $deferred ->promise() ->then(function ($results) { - return new Result($results); + return new Result($results, null, null); }); } } diff --git a/src/Driver/SQLite/SQLiteDriver.php b/src/Driver/SQLite/SQLiteDriver.php index 82dc594..4159f98 100644 --- a/src/Driver/SQLite/SQLiteDriver.php +++ b/src/Driver/SQLite/SQLiteDriver.php @@ -78,7 +78,12 @@ public function query( ->database ->query($sql, $parameters) ->then(function (SQLiteResult $sqliteResult) { - return new Result($sqliteResult->rows); + + return new Result( + $sqliteResult->rows, + $sqliteResult->insertId, + $sqliteResult->changed + ); }) ->otherwise(function (RuntimeException $exception) { $message = $exception->getMessage(); diff --git a/src/Result.php b/src/Result.php index 6d67518..9ed1d20 100644 --- a/src/Result.php +++ b/src/Result.php @@ -23,32 +23,56 @@ class Result /** * @var mixed */ - private $data; + private $rows; + + /** + * @var int|null + */ + private $lastInsertedId; + + /** + * @var int|null + */ + private $affectedRows; /** * Result constructor. * - * @param mixed $data + * @param mixed $rows + * @param int|null $lastInsertedId + * @param int|null $affectedRows */ - public function __construct($data) + public function __construct( + $rows, + ?int $lastInsertedId, + ?int $affectedRows + ) { - $this->data = $data; + $this->rows = $rows; + $this->lastInsertedId = $lastInsertedId; + $this->affectedRows = $affectedRows; } /** * Fetch count. + * + * @return int */ - public function fetchCount() + public function fetchCount() : int { - return count($this->data); + return is_array($this->rows) + ? count($this->rows) + : 0; } /** * Fetch all rows. + * + * @return mixed */ public function fetchAllRows() { - return $this->data; + return $this->rows; } /** @@ -58,9 +82,25 @@ public function fetchAllRows() */ public function fetchFirstRow() { - return is_array($this->data) - && count($this->data) >= 1 - ? reset($this->data) + return is_array($this->rows) + && count($this->rows) >= 1 + ? reset($this->rows) : null; } + + /** + * @return int|null + */ + public function getLastInsertedId(): ?int + { + return $this->lastInsertedId; + } + + /** + * @return int|null + */ + public function getAffectedRows(): ?int + { + return $this->affectedRows; + } } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index b86c1cb..c6d5b5d 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -48,17 +48,21 @@ abstract protected function getConnection(LoopInterface $loop): Connection; /** * @param Connection $connection + * @param bool $autoincrementedId * * @return PromiseInterface */ - protected function createInfrastructure(Connection $connection): PromiseInterface + protected function createInfrastructure( + Connection $connection, + bool $autoincrementedId = false + ): PromiseInterface { return $connection ->createTable('test', [ - 'id' => 'string', + 'id' => $autoincrementedId ? 'integer' : 'string', 'field1' => 'string', 'field2' => 'string', - ]) + ], [], $autoincrementedId) ->otherwise(function (TableExistsException $_) use ($connection) { // Silent pass @@ -87,15 +91,19 @@ protected function dropInfrastructure(Connection $connection): PromiseInterface /** * @param Connection $connection + * @param bool $autoincrementedId * * @return PromiseInterface */ - protected function resetInfrastructure(Connection $connection): PromiseInterface + protected function resetInfrastructure( + Connection $connection, + bool $autoincrementedId = false + ): PromiseInterface { return $this ->dropInfrastructure($connection) - ->then(function () use ($connection) { - return $this->createInfrastructure($connection); + ->then(function () use ($connection, $autoincrementedId) { + return $this->createInfrastructure($connection, $autoincrementedId); }); } @@ -446,4 +454,64 @@ public function testDelete() await($promise, $loop, self::MAX_TIMEOUT); } + + /** + * Test get last inserted id + * + * @group lele + */ + public function testGetLastInsertedId() + { + if (get_class($this) === PostgreSQLConnectionTest::class) { + $this->markTestSkipped('Not implemented yet on postgresql'); + return; + } + + $loop = $this->createLoop(); + $connection = $this->getConnection($loop); + $promise = $this + ->resetInfrastructure($connection, true) + ->then(function (Connection $connection) { + return $connection->insert('test', [ + 'field1' => 'val1', + 'field2' => 'val2', + ]); + }) + ->then(function(Result $result) { + $this->assertEquals(1, $result->getLastInsertedId()); + }) + ->then(function () use ($connection) { + return all([ + $connection->insert('test', [ + 'field1' => 'val3', + 'field2' => 'val4', + ]), + $connection->insert('test', [ + 'field1' => 'val5', + 'field2' => 'val6', + ]) + ]); + }) + ->then(function(array $results) { + $this->assertEquals(1, $results[0]->getAffectedRows()); + $this->assertEquals(3, $results[1]->getLastInsertedId()); + }) + ->then(function () use ($connection) { + return all([ + $connection->delete('test', [ + 'id' => 2, + ]), + $connection->insert('test', [ + 'field1' => 'val7', + 'field2' => 'val8', + ]) + ]); + }) + ->then(function(array$results) { + $this->assertEquals(1, $results[0]->getAffectedRows()); + $this->assertEquals(4, $results[1]->getLastInsertedId()); + }); + + await($promise, $loop, self::MAX_TIMEOUT); + } }