Skip to content

Commit dc4c545

Browse files
foxycodedg
authored andcommitted
ResultSet: changed empty result return type of fetch() and fetchField() methods from FALSE to NULL [BC Break]
1 parent c3ca667 commit dc4c545

13 files changed

+34
-17
lines changed

src/Database/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function preprocess($sql, ...$params)
213213
/**
214214
* Shortcut for query()->fetch()
215215
* @param string
216-
* @return Row
216+
* @return Row|NULL
217217
*/
218218
public function fetch($sql, ...$params)
219219
{

src/Database/Context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function getConventions()
131131
/**
132132
* Shortcut for query()->fetch()
133133
* @param string
134-
* @return Row
134+
* @return Row|NULL
135135
*/
136136
public function fetch($sql, ...$params)
137137
{

src/Database/IRowContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface IRowContainer extends \Traversable
1818

1919
/**
2020
* Fetches single row object.
21-
* @return IRow|bool if there is no row
21+
* @return IRow|NULL if there is no row
2222
*/
2323
function fetch();
2424

src/Database/ResultSet.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function valid()
239239
return TRUE;
240240
}
241241

242-
return $this->fetch() !== FALSE;
242+
return $this->fetch() !== NULL;
243243
}
244244

245245

@@ -254,7 +254,7 @@ public function fetch()
254254
$data = $this->pdoStatement ? $this->pdoStatement->fetch() : NULL;
255255
if (!$data) {
256256
$this->pdoStatement->closeCursor();
257-
return FALSE;
257+
return NULL;
258258

259259
} elseif ($this->result === NULL && count($data) !== $this->pdoStatement->columnCount()) {
260260
$duplicates = Helpers::findDuplicates($this->pdoStatement);
@@ -276,12 +276,12 @@ public function fetch()
276276
/**
277277
* Fetches single field.
278278
* @param int
279-
* @return mixed|FALSE
279+
* @return mixed
280280
*/
281281
public function fetchField($column = 0)
282282
{
283283
$row = $this->fetch();
284-
return $row ? $row[$column] : FALSE;
284+
return $row ? $row[$column] : NULL;
285285
}
286286

287287

src/Database/Table/ActiveRow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function update($data)
183183
->wherePrimary($tmp + $primary);
184184
}
185185
$selection->select('*');
186-
if (($row = $selection->fetch()) === FALSE) {
186+
if (($row = $selection->fetch()) === NULL) {
187187
throw new Nette\InvalidStateException('Database refetch failed; row does not exist!');
188188
}
189189
$this->data = $row->data;

src/Database/Table/IRowContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Container of database result fetched into IRow objects.
1717
*
18-
* @method IRow|bool fetch() Fetches single row object.
18+
* @method IRow|NULL fetch() Fetches single row object.
1919
* @method IRow[] fetchAll() Fetches all rows.
2020
*/
2121
interface IRowContainer extends Database\IRowContainer

src/Database/Table/Selection.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function getSql()
170170
/**
171171
* Loads cache of previous accessed columns and returns it.
172172
* @internal
173-
* @return array|false
173+
* @return array|bool
174174
*/
175175
public function getPreviousAccessedColumns()
176176
{
@@ -201,7 +201,7 @@ public function getSqlBuilder()
201201
/**
202202
* Returns row specified by primary key.
203203
* @param mixed primary key
204-
* @return IRow or FALSE if there is no such row
204+
* @return IRow|NULL if there is no such row
205205
*/
206206
public function get($key)
207207
{
@@ -218,14 +218,14 @@ public function fetch()
218218
$this->execute();
219219
$return = current($this->data);
220220
next($this->data);
221-
return $return;
221+
return $return === FALSE ? NULL : $return;
222222
}
223223

224224

225225
/**
226226
* Fetches single field.
227227
* @param string|NULL
228-
* @return mixed|FALSE
228+
* @return mixed
229229
*/
230230
public function fetchField($column = NULL)
231231
{
@@ -238,7 +238,7 @@ public function fetchField($column = NULL)
238238
return $column ? $row[$column] : array_values($row->toArray())[0];
239239
}
240240

241-
return FALSE;
241+
return NULL;
242242
}
243243

244244

@@ -1004,7 +1004,7 @@ public function rewind()
10041004
}
10051005

10061006

1007-
/** @return IRow */
1007+
/** @return IRow|bool */
10081008
public function current()
10091009
{
10101010
if (($key = current($this->keys)) !== FALSE) {

tests/Database/Context.transaction.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ test(function () use ($context) {
2828
$context->query('DELETE FROM book');
2929
$context->commit();
3030

31-
Assert::false($context->fetchField('SELECT id FROM book WHERE id = ', 3));
31+
Assert::null($context->fetchField('SELECT id FROM book WHERE id = ', 3));
3232
});

tests/Database/ResultSet.fetch().phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,10 @@ test(function () use ($context, $driverName) {
7676
$res->fetch();
7777
}, E_USER_NOTICE, $message);
7878
});
79+
80+
81+
test(function () use ($context, $driverName) {
82+
$res = $context->query('SELECT id FROM author WHERE id = ?', 666);
83+
84+
Assert::null($res->fetch());
85+
});

tests/Database/ResultSet.fetchField().phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ test(function () use ($context) {
2121
Assert::same(12, $res->fetchField(1));
2222
Assert::same('Geek', $res->fetchField('name'));
2323
});
24+
25+
26+
test(function () use ($context) {
27+
$res = $context->query('SELECT id FROM author WHERE id = ?', 666);
28+
29+
Assert::null($res->fetchField());
30+
});

0 commit comments

Comments
 (0)