Skip to content

Commit 8d6ed28

Browse files
committed
Connection, Context: added fetchFields()
1 parent cd45bf9 commit 8d6ed28

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

src/Database/Connection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ public function fetchField(string $sql, ...$params)
215215
}
216216

217217

218+
/**
219+
* Shortcut for query()->fetchFields()
220+
*/
221+
public function fetchFields(string $sql, ...$params): ?array
222+
{
223+
return $this->query($sql, ...$params)->fetchFields();
224+
}
225+
226+
218227
/**
219228
* Shortcut for query()->fetchPairs()
220229
*/

src/Database/Context.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ public function fetchField(string $sql, ...$params)
127127
}
128128

129129

130+
/**
131+
* Shortcut for query()->fetchFields()
132+
*/
133+
public function fetchFields(string $sql, ...$params): ?array
134+
{
135+
return $this->connection->query($sql, ...$params)->fetchFields();
136+
}
137+
138+
130139
/**
131140
* Shortcut for query()->fetchPairs()
132141
*/

src/Database/ResultSet.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ public function fetchField($column = 0)
261261
}
262262

263263

264+
/**
265+
* Fetches array of fields.
266+
*/
267+
public function fetchFields(): ?array
268+
{
269+
$row = $this->fetch();
270+
return $row ? array_values((array) $row) : null;
271+
}
272+
273+
264274
/**
265275
* @inheritDoc
266276
*/

tests/Database/Connection.fetch.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ test(function () use ($connection) { // fetchField
2929
});
3030

3131

32+
test(function () use ($connection) { // fetchFields
33+
Assert::same([11, 'Jakub Vrana'], $connection->fetchFields('SELECT id, name FROM author ORDER BY id'));
34+
});
35+
36+
3237
test(function () use ($connection) { // fetchPairs
3338
$pairs = $connection->fetchPairs('SELECT name, id FROM author WHERE id > ? ORDER BY id', 11);
3439
Assert::same([

tests/Database/Context.fetch.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ test(function () use ($context) { // fetchField
2929
});
3030

3131

32+
test(function () use ($context) { // fetchFields
33+
Assert::same([11, 'Jakub Vrana'], $context->fetchFields('SELECT id, name FROM author ORDER BY id'));
34+
});
35+
36+
3237
test(function () use ($context) { // fetchPairs
3338
$pairs = $context->fetchPairs('SELECT name, id FROM author WHERE id > ? ORDER BY id', 11);
3439
Assert::same([
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Database\ResultSet: Fetch fields.
5+
* @dataProvider? databases.ini
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/connect.inc.php'; // create $connection
13+
14+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
15+
16+
17+
test(function () use ($connection) {
18+
$res = $connection->query('SELECT name, id FROM author ORDER BY id');
19+
20+
Assert::same(['Jakub Vrana', 11], $res->fetchFields());
21+
});
22+
23+
24+
test(function () use ($connection) {
25+
$res = $connection->query('SELECT id FROM author WHERE id = ?', 666);
26+
27+
Assert::null($res->fetchFields());
28+
});

0 commit comments

Comments
 (0)