diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 67de46de3..5c46fb2bf 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -213,7 +213,7 @@ public function preprocess($sql, ...$params) /** * Shortcut for query()->fetch() * @param string - * @return Row + * @return Row|NULL */ public function fetch($sql, ...$params) { diff --git a/src/Database/Context.php b/src/Database/Context.php index 5b3f0d8f7..76c4ccee9 100644 --- a/src/Database/Context.php +++ b/src/Database/Context.php @@ -131,7 +131,7 @@ public function getConventions() /** * Shortcut for query()->fetch() * @param string - * @return Row + * @return Row|NULL */ public function fetch($sql, ...$params) { diff --git a/src/Database/IRowContainer.php b/src/Database/IRowContainer.php index 8d76b07eb..8b62d19ac 100644 --- a/src/Database/IRowContainer.php +++ b/src/Database/IRowContainer.php @@ -18,7 +18,7 @@ interface IRowContainer extends \Traversable /** * Fetches single row object. - * @return IRow|bool if there is no row + * @return IRow|NULL if there is no row */ function fetch(); diff --git a/src/Database/ResultSet.php b/src/Database/ResultSet.php index e045b2bdf..931ae045d 100644 --- a/src/Database/ResultSet.php +++ b/src/Database/ResultSet.php @@ -239,7 +239,7 @@ public function valid() return TRUE; } - return $this->fetch() !== FALSE; + return $this->fetch() !== NULL; } @@ -254,7 +254,7 @@ public function fetch() $data = $this->pdoStatement ? $this->pdoStatement->fetch() : NULL; if (!$data) { $this->pdoStatement->closeCursor(); - return FALSE; + return NULL; } elseif ($this->result === NULL && count($data) !== $this->pdoStatement->columnCount()) { $duplicates = Helpers::findDuplicates($this->pdoStatement); @@ -276,12 +276,12 @@ public function fetch() /** * Fetches single field. * @param int - * @return mixed|FALSE + * @return mixed */ public function fetchField($column = 0) { $row = $this->fetch(); - return $row ? $row[$column] : FALSE; + return $row ? $row[$column] : NULL; } diff --git a/src/Database/Table/ActiveRow.php b/src/Database/Table/ActiveRow.php index 531083a46..04e747b09 100644 --- a/src/Database/Table/ActiveRow.php +++ b/src/Database/Table/ActiveRow.php @@ -183,7 +183,7 @@ public function update($data) ->wherePrimary($tmp + $primary); } $selection->select('*'); - if (($row = $selection->fetch()) === FALSE) { + if (($row = $selection->fetch()) === NULL) { throw new Nette\InvalidStateException('Database refetch failed; row does not exist!'); } $this->data = $row->data; diff --git a/src/Database/Table/IRowContainer.php b/src/Database/Table/IRowContainer.php index bda5b7019..64eea10e6 100644 --- a/src/Database/Table/IRowContainer.php +++ b/src/Database/Table/IRowContainer.php @@ -15,7 +15,7 @@ /** * Container of database result fetched into IRow objects. * - * @method IRow|bool fetch() Fetches single row object. + * @method IRow|NULL fetch() Fetches single row object. * @method IRow[] fetchAll() Fetches all rows. */ interface IRowContainer extends Database\IRowContainer diff --git a/src/Database/Table/Selection.php b/src/Database/Table/Selection.php index 9a8ede275..98851d955 100644 --- a/src/Database/Table/Selection.php +++ b/src/Database/Table/Selection.php @@ -170,7 +170,7 @@ public function getSql() /** * Loads cache of previous accessed columns and returns it. * @internal - * @return array|false + * @return array|bool */ public function getPreviousAccessedColumns() { @@ -201,7 +201,7 @@ public function getSqlBuilder() /** * Returns row specified by primary key. * @param mixed primary key - * @return IRow or FALSE if there is no such row + * @return IRow|NULL if there is no such row */ public function get($key) { @@ -218,14 +218,14 @@ public function fetch() $this->execute(); $return = current($this->data); next($this->data); - return $return; + return $return === FALSE ? NULL : $return; } /** * Fetches single field. * @param string|NULL - * @return mixed|FALSE + * @return mixed */ public function fetchField($column = NULL) { @@ -238,7 +238,7 @@ public function fetchField($column = NULL) return $column ? $row[$column] : array_values($row->toArray())[0]; } - return FALSE; + return NULL; } @@ -1004,7 +1004,7 @@ public function rewind() } - /** @return IRow */ + /** @return IRow|bool */ public function current() { if (($key = current($this->keys)) !== FALSE) { diff --git a/tests/Database/Context.transaction.phpt b/tests/Database/Context.transaction.phpt index 972391ede..c5d293845 100644 --- a/tests/Database/Context.transaction.phpt +++ b/tests/Database/Context.transaction.phpt @@ -28,5 +28,5 @@ test(function () use ($context) { $context->query('DELETE FROM book'); $context->commit(); - Assert::false($context->fetchField('SELECT id FROM book WHERE id = ', 3)); + Assert::null($context->fetchField('SELECT id FROM book WHERE id = ', 3)); }); diff --git a/tests/Database/ResultSet.fetch().phpt b/tests/Database/ResultSet.fetch().phpt index edce00ed4..0b9005ecb 100644 --- a/tests/Database/ResultSet.fetch().phpt +++ b/tests/Database/ResultSet.fetch().phpt @@ -76,3 +76,10 @@ test(function () use ($context, $driverName) { $res->fetch(); }, E_USER_NOTICE, $message); }); + + +test(function () use ($context, $driverName) { + $res = $context->query('SELECT id FROM author WHERE id = ?', 666); + + Assert::null($res->fetch()); +}); diff --git a/tests/Database/ResultSet.fetchField().phpt b/tests/Database/ResultSet.fetchField().phpt index 9d8ea095d..26736d3a0 100644 --- a/tests/Database/ResultSet.fetchField().phpt +++ b/tests/Database/ResultSet.fetchField().phpt @@ -21,3 +21,10 @@ test(function () use ($context) { Assert::same(12, $res->fetchField(1)); Assert::same('Geek', $res->fetchField('name')); }); + + +test(function () use ($context) { + $res = $context->query('SELECT id FROM author WHERE id = ?', 666); + + Assert::null($res->fetchField()); +}); diff --git a/tests/Database/Table/Selection.fetch().phpt b/tests/Database/Table/Selection.fetch().phpt index 684cf62c8..999338e59 100644 --- a/tests/Database/Table/Selection.fetch().phpt +++ b/tests/Database/Table/Selection.fetch().phpt @@ -22,4 +22,5 @@ test(function () use ($context) { } Assert::same(['PHP'], $tags); + Assert::null($context->table('book')->where('title', 'Nonexistent')->fetch()); }); diff --git a/tests/Database/Table/Selection.fetchField().phpt b/tests/Database/Table/Selection.fetchField().phpt index 1ea2da329..5fe60a909 100644 --- a/tests/Database/Table/Selection.fetchField().phpt +++ b/tests/Database/Table/Selection.fetchField().phpt @@ -17,10 +17,12 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverN test(function () use ($context) { $title = $context->table('book')->where('id', 1)->fetchField('title'); // SELECT `title` FROM `book` WHERE `id` = 1 Assert::same('1001 tipu a triku pro PHP', $title); + Assert::null($context->table('book')->where('title', 'Nonexistent')->fetchField()); }); test(function () use ($context) { $title = $context->table('book')->where('id', 1)->select('title')->fetchField(); // SELECT `title` FROM `book` WHERE `id` = 1 Assert::same('1001 tipu a triku pro PHP', $title); + Assert::null($context->table('book')->where('title', 'Nonexistent')->select('title')->fetchField()); }); diff --git a/tests/Database/Table/Table.related().phpt b/tests/Database/Table/Table.related().phpt index c067314ff..c7d3ef903 100644 --- a/tests/Database/Table/Table.related().phpt +++ b/tests/Database/Table/Table.related().phpt @@ -92,7 +92,7 @@ test(function () use ($context) { $author = $context->table('author')->get(11); $books = $author->related('book')->where('translator_id', 11); Assert::same('1001 tipu a triku pro PHP', $books->fetch()->title); - Assert::false($books->fetch()); + Assert::null($books->fetch()); Assert::same('1001 tipu a triku pro PHP', $author->related('book')->fetch()->title);