From 8695881527ba030c60dcd2a7c1cddd3adca578fc Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 2 May 2018 18:39:25 +0200 Subject: [PATCH] Connection::query() and Fluent::execute() returns only resultset, not the number of affected rows (BC break) --- src/Dibi/Connection.php | 13 +++---------- src/Dibi/Fluent.php | 5 +---- src/Dibi/dibi.php | 4 ++-- src/Dibi/interfaces.php | 3 +-- tests/dibi/Fluent.fetch.phpt | 3 ++- 5 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 2ec22ed36..41964625f 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -187,10 +187,9 @@ final public function getDriver(): Driver /** * Generates (translates) and executes SQL query. * @param mixed ...$args - * @return Result|int|null result set or number of affected rows * @throws Exception */ - final public function query(...$args) + final public function query(...$args): ?Result { return $this->nativeQuery($this->translateArgs($args)); } @@ -257,10 +256,9 @@ protected function translateArgs(array $args): string /** * Executes the SQL query. - * @return Result|int|null result set or number of affected rows * @throws Exception */ - final public function nativeQuery(string $sql) + final public function nativeQuery(string $sql): ?Result { if (!$this->driver) { $this->connect(); @@ -278,12 +276,7 @@ final public function nativeQuery(string $sql) throw $e; } - if ($res) { - $res = $this->createResultSet($res); - } else { - $res = $this->driver->getAffectedRows(); - } - + $res = $res ? $this->createResultSet($res) : null; if ($event) { $this->onEvent($event->done($res)); } diff --git a/src/Dibi/Fluent.php b/src/Dibi/Fluent.php index 95b9d1689..9b927ab13 100644 --- a/src/Dibi/Fluent.php +++ b/src/Dibi/Fluent.php @@ -380,10 +380,7 @@ public function count(): int } - /** - * @return Result|int - */ - private function query($args) + private function query($args): ?Result { $res = $this->connection->query($args); foreach ($this->setups as $setup) { diff --git a/src/Dibi/dibi.php b/src/Dibi/dibi.php index f77a26ca3..98da62b82 100644 --- a/src/Dibi/dibi.php +++ b/src/Dibi/dibi.php @@ -13,8 +13,8 @@ * store connections info. * * @method void disconnect() - * @method Dibi\Result|int|null query(...$args) - * @method Dibi\Result|int|null nativeQuery(...$args) + * @method Dibi\Result|null query(...$args) + * @method Dibi\Result|null nativeQuery(...$args) * @method bool test(...$args) * @method Dibi\DataSource dataSource(...$args) * @method Dibi\Row|null fetch(...$args) diff --git a/src/Dibi/interfaces.php b/src/Dibi/interfaces.php index f04e710d8..a5245022c 100644 --- a/src/Dibi/interfaces.php +++ b/src/Dibi/interfaces.php @@ -214,10 +214,9 @@ function getDriver(): Driver; /** * Generates (translates) and executes SQL query. - * @return Result|int|null result set or number of affected rows * @throws Exception */ - function query(...$args); + function query(...$args): ?Result; /** * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query. diff --git a/tests/dibi/Fluent.fetch.phpt b/tests/dibi/Fluent.fetch.phpt index a5537ca56..29c618117 100644 --- a/tests/dibi/Fluent.fetch.phpt +++ b/tests/dibi/Fluent.fetch.phpt @@ -54,4 +54,5 @@ if (!in_array($config['system'], ['odbc', 'sqlsrv'], true)) { // affected rows $res = $conn->update('products', ['title' => 'new'])->execute(); -Assert::same(3, $res); +Assert::null($res); +Assert::same(3, $conn->getAffectedRows());