Skip to content

Commit

Permalink
ResultSet: changed empty result return type of fetch() and fetchField…
Browse files Browse the repository at this point in the history
…() methods from FALSE to NULL [BC Break]
  • Loading branch information
foxycode authored and dg committed Jan 25, 2017
1 parent 10bbfe8 commit f7a6af0
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getConventions()
/**
* Shortcut for query()->fetch()
* @param string
* @return Row
* @return Row|NULL
*/
public function fetch($sql, ...$params)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Database/IRowContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function valid()
return TRUE;
}

return $this->fetch() !== FALSE;
return $this->fetch() !== NULL;
}


Expand All @@ -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);
Expand All @@ -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;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Database/Table/ActiveRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Table/IRowContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -238,7 +238,7 @@ public function fetchField($column = NULL)
return $column ? $row[$column] : array_values($row->toArray())[0];
}

return FALSE;
return NULL;
}


Expand Down Expand Up @@ -1004,7 +1004,7 @@ public function rewind()
}


/** @return IRow */
/** @return IRow|bool */
public function current()
{
if (($key = current($this->keys)) !== FALSE) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/Context.transaction.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
7 changes: 7 additions & 0 deletions tests/Database/ResultSet.fetch().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
7 changes: 7 additions & 0 deletions tests/Database/ResultSet.fetchField().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
1 change: 1 addition & 0 deletions tests/Database/Table/Selection.fetch().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ test(function () use ($context) {
}

Assert::same(['PHP'], $tags);
Assert::null($context->table('book')->where('title', 'Nonexistent')->fetch());
});
2 changes: 2 additions & 0 deletions tests/Database/Table/Selection.fetchField().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
2 changes: 1 addition & 1 deletion tests/Database/Table/Table.related().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f7a6af0

Please sign in to comment.