From b49800b8fd074da079420646e996a498bb30e758 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 4 May 2018 22:20:16 +0200 Subject: [PATCH] EmptyResult WIP --- src/Dibi/Connection.php | 6 +-- src/Dibi/Drivers/EmptyResult.php | 74 ++++++++++++++++++++++++++++++++ src/Dibi/Fluent.php | 2 +- src/Dibi/dibi.php | 4 +- src/Dibi/interfaces.php | 2 +- tests/dibi/Fluent.fetch.phpt | 3 +- 6 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 src/Dibi/Drivers/EmptyResult.php diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 41964625f..22e057071 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -189,7 +189,7 @@ final public function getDriver(): Driver * @param mixed ...$args * @throws Exception */ - final public function query(...$args): ?Result + final public function query(...$args): Result { return $this->nativeQuery($this->translateArgs($args)); } @@ -258,7 +258,7 @@ protected function translateArgs(array $args): string * Executes the SQL query. * @throws Exception */ - final public function nativeQuery(string $sql): ?Result + final public function nativeQuery(string $sql): Result { if (!$this->driver) { $this->connect(); @@ -276,7 +276,7 @@ final public function nativeQuery(string $sql): ?Result throw $e; } - $res = $res ? $this->createResultSet($res) : null; + $res = $this->createResultSet($res ?: new Drivers\EmptyResult(max(0, $this->driver->getAffectedRows()))); if ($event) { $this->onEvent($event->done($res)); } diff --git a/src/Dibi/Drivers/EmptyResult.php b/src/Dibi/Drivers/EmptyResult.php new file mode 100644 index 000000000..75969ba48 --- /dev/null +++ b/src/Dibi/Drivers/EmptyResult.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 9b927ab13..c486730b7 100644 --- a/src/Dibi/Fluent.php +++ b/src/Dibi/Fluent.php @@ -380,7 +380,7 @@ public function count(): int } - private function query($args): ?Result + 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 98da62b82..c15a76134 100644 --- a/src/Dibi/dibi.php +++ b/src/Dibi/dibi.php @@ -13,8 +13,8 @@ * store connections info. * * @method void disconnect() - * @method Dibi\Result|null query(...$args) - * @method Dibi\Result|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 a5245022c..674ab4b3d 100644 --- a/src/Dibi/interfaces.php +++ b/src/Dibi/interfaces.php @@ -216,7 +216,7 @@ function getDriver(): Driver; * Generates (translates) and executes SQL query. * @throws Exception */ - function query(...$args): ?Result; + 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 29c618117..993f3c07f 100644 --- a/tests/dibi/Fluent.fetch.phpt +++ b/tests/dibi/Fluent.fetch.phpt @@ -54,5 +54,6 @@ if (!in_array($config['system'], ['odbc', 'sqlsrv'], true)) { // affected rows $res = $conn->update('products', ['title' => 'new'])->execute(); -Assert::null($res); +Assert::same(3, $res->getRowCount()); +Assert::same(0, $res->getColumnCount()); Assert::same(3, $conn->getAffectedRows());