diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 130c16ea..9b6b3a68 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -187,10 +187,9 @@ class Connection implements IConnection /** * 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 @@ class Connection implements IConnection /** * 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 @@ class Connection implements IConnection throw $e; } - if ($res) { - $res = $this->createResultSet($res); - } else { - $res = $this->driver->getAffectedRows(); - } - + $res = $this->createResultSet($res ?: new Drivers\NoDataResult(max(0, $this->driver->getAffectedRows()))); if ($event) { $this->onEvent($event->done($res)); } diff --git a/src/Dibi/Drivers/NoDataResult.php b/src/Dibi/Drivers/NoDataResult.php new file mode 100644 index 00000000..cd51a1e3 --- /dev/null +++ b/src/Dibi/Drivers/NoDataResult.php @@ -0,0 +1,74 @@ +rows = $rows; + } + + + /** + * Returns the number of affected rows. + */ + public function getRowCount(): int + { + return $this->rows; + } + + + public function fetch(bool $assoc): ?array + { + return null; + } + + + public function seek(int $row): bool + { + return false; + } + + + public function free(): void + { + } + + + public function getResultColumns(): array + { + return []; + } + + + public function getResultResource() + { + return null; + } + + + public function unescapeBinary(string $value): string + { + return $value; + } +} diff --git a/src/Dibi/Fluent.php b/src/Dibi/Fluent.php index 95b9d168..c486730b 100644 --- a/src/Dibi/Fluent.php +++ b/src/Dibi/Fluent.php @@ -380,10 +380,7 @@ class Fluent implements IDataSource } - /** - * @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 f77a26ca..c15a7613 100644 --- a/src/Dibi/dibi.php +++ b/src/Dibi/dibi.php @@ -13,8 +13,8 @@ declare(strict_types=1); * store connections info. * * @method void disconnect() - * @method Dibi\Result|int|null query(...$args) - * @method Dibi\Result|int|null nativeQuery(...$args) + * @method Dibi\Result query(...$args) + * @method Dibi\Result 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 f04e710d..674ab4b3 100644 --- a/src/Dibi/interfaces.php +++ b/src/Dibi/interfaces.php @@ -214,10 +214,9 @@ interface IConnection /** * 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/Connection.affectedRows.phpt b/tests/dibi/Connection.affectedRows.phpt index 19d49be2..43f2e9d7 100644 --- a/tests/dibi/Connection.affectedRows.phpt +++ b/tests/dibi/Connection.affectedRows.phpt @@ -20,18 +20,22 @@ $conn->query('INSERT INTO products', [ Assert::same(1, $conn->getAffectedRows()); -$conn->query('UPDATE products SET title="xxx" WHERE product_id > 100'); +$res = $conn->query('UPDATE products SET title="xxx" WHERE product_id > 100'); Assert::same(0, $conn->getAffectedRows()); +Assert::same(0, $res->getRowCount()); -$conn->query('UPDATE products SET title="xxx"'); +$res = $conn->query('UPDATE products SET title="xxx"'); Assert::same(4, $conn->getAffectedRows()); +Assert::same(4, $res->getRowCount()); $conn->query('DELETE FROM orders'); -$conn->query('DELETE FROM products WHERE product_id > 100'); +$res = $conn->query('DELETE FROM products WHERE product_id > 100'); Assert::same(0, $conn->getAffectedRows()); +Assert::same(0, $res->getRowCount()); -$conn->query('DELETE FROM products WHERE product_id < 3'); +$res = $conn->query('DELETE FROM products WHERE product_id < 3'); Assert::same(2, $conn->getAffectedRows()); +Assert::same(2, $res->getRowCount()); diff --git a/tests/dibi/Fluent.fetch.phpt b/tests/dibi/Fluent.fetch.phpt index a5537ca5..993f3c07 100644 --- a/tests/dibi/Fluent.fetch.phpt +++ b/tests/dibi/Fluent.fetch.phpt @@ -54,4 +54,6 @@ if (!in_array($config['system'], ['odbc', 'sqlsrv'], true)) { // affected rows $res = $conn->update('products', ['title' => 'new'])->execute(); -Assert::same(3, $res); +Assert::same(3, $res->getRowCount()); +Assert::same(0, $res->getColumnCount()); +Assert::same(3, $conn->getAffectedRows());