Skip to content

Commit

Permalink
DB: fix twice call fetch row mutator for fetchAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest79 committed Dec 5, 2024
1 parent 3fbb2be commit ed2d839
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions docs/db.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,11 @@ table($rowsFetchAll);
----------------------------------------------------------------------------------------------------------------------
| nick | inserted_datetime | height_cm | height_inch | date |
|====================================================================================================================|
| (string) 'Bob' | (Date) 2020-01-01 09:00:00 | (integer) 178 | (double) 70.07874015748 | (string) '2020-01-01' |
| (string) 'Brandon' | (Date) 2020-01-02 12:05:00 | (integer) 180 | (double) 70.866141732283 | (string) '2020-01-02' |
| (string) 'Bob' | (Date) 2020-01-01 09:00:00 | (integer) 178 | (double) 70.157480314961 | (string) '2020-01-01' |
| (string) 'Brandon' | (Date) 2020-01-02 12:05:00 | (integer) 180 | (double) 71.023622047244 | (string) '2020-01-02' |
| (string) 'Steve' | (Date) 2020-01-02 12:05:00 | (integer) 168 | (double) 66.141732283465 | (string) '2020-01-02' |
| (string) 'Monica' | (Date) 2020-01-03 13:10:00 | (integer) 175 | (double) 68.897637795276 | (string) '2020-01-03' |
| (string) 'Ingrid' | (Date) 2020-01-04 14:15:00 | (integer) 168 | (double) 66.141732283465 | (string) '2020-01-04' |
| (string) 'Monica' | (Date) 2020-01-03 13:10:00 | (integer) 175 | (double) 69.173228346457 | (string) '2020-01-03' |
| (string) 'Ingrid' | (Date) 2020-01-04 14:15:00 | (integer) 168 | (double) 66.220472440945 | (string) '2020-01-04' |
----------------------------------------------------------------------------------------------------------------------
*/

Expand Down
4 changes: 0 additions & 4 deletions src/Db/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ public function fetchAll(int|NULL $offset = NULL, int|NULL $limit = NULL): array
}
$limit--;

if ($this->rowFetchMutator !== NULL) {
call_user_func($this->rowFetchMutator, $row);
}

$data[] = $row;

$row = $this->fetch();
Expand Down
27 changes: 15 additions & 12 deletions tests/Integration/FetchMutatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,23 @@ public function testFetchAllRowMutator(): void
->query('SELECT id, name FROM test ORDER BY id')
->setRowFetchMutator(static function (Db\Row $row): void {
$row->new_column = $row->id . '-' . $row->name;
$row->repeat = ($row->repeat ?? 0) + 1;
});

$rows = $result->fetchAll();

Tester\Assert::same(['id' => 1, 'name' => 'name3', 'new_column' => '1-name3'], $rows[0]->toArray());
Tester\Assert::same(['id' => 2, 'name' => 'name2', 'new_column' => '2-name2'], $rows[1]->toArray());
Tester\Assert::same(['id' => 3, 'name' => 'name1', 'new_column' => '3-name1'], $rows[2]->toArray());
Tester\Assert::same(['id' => 1, 'name' => 'name3', 'new_column' => '1-name3', 'repeat' => 1], $rows[0]->toArray());
Tester\Assert::same(['id' => 2, 'name' => 'name2', 'new_column' => '2-name2', 'repeat' => 1], $rows[1]->toArray());
Tester\Assert::same(['id' => 3, 'name' => 'name1', 'new_column' => '3-name1', 'repeat' => 1], $rows[2]->toArray());

$rows = $result->fetchAll(1);

Tester\Assert::same(['id' => 2, 'name' => 'name2', 'new_column' => '2-name2'], $rows[0]->toArray());
Tester\Assert::same(['id' => 3, 'name' => 'name1', 'new_column' => '3-name1'], $rows[1]->toArray());
Tester\Assert::same(['id' => 2, 'name' => 'name2', 'new_column' => '2-name2', 'repeat' => 1], $rows[0]->toArray());
Tester\Assert::same(['id' => 3, 'name' => 'name1', 'new_column' => '3-name1', 'repeat' => 1], $rows[1]->toArray());

$rows = $result->fetchAll(1, 1);

Tester\Assert::same(['id' => 2, 'name' => 'name2', 'new_column' => '2-name2'], $rows[0]->toArray());
Tester\Assert::same(['id' => 2, 'name' => 'name2', 'new_column' => '2-name2', 'repeat' => 1], $rows[0]->toArray());

$result->free();
}
Expand Down Expand Up @@ -234,12 +235,13 @@ public function testFetchAssocRowMutator(): void
$rows1 = $result1
->setRowFetchMutator(static function (Db\Row $row): void {
$row->new_column = $row->id . '-' . $row->name;
$row->repeat = ($row->repeat ?? 0) + 1;
})
->fetchAssoc('type');

Tester\Assert::same(['id' => 1, 'type' => 3, 'name' => 'name3', 'new_column' => '1-name3'], $rows1[3]->toArray());
Tester\Assert::same(['id' => 2, 'type' => 2, 'name' => 'name2', 'new_column' => '2-name2'], $rows1[2]->toArray());
Tester\Assert::same(['id' => 3, 'type' => 1, 'name' => 'name1', 'new_column' => '3-name1'], $rows1[1]->toArray());
Tester\Assert::same(['id' => 1, 'type' => 3, 'name' => 'name3', 'new_column' => '1-name3', 'repeat' => 1], $rows1[3]->toArray());
Tester\Assert::same(['id' => 2, 'type' => 2, 'name' => 'name2', 'new_column' => '2-name2', 'repeat' => 1], $rows1[2]->toArray());
Tester\Assert::same(['id' => 3, 'type' => 1, 'name' => 'name1', 'new_column' => '3-name1', 'repeat' => 1], $rows1[1]->toArray());

$result1->free();

Expand All @@ -250,12 +252,13 @@ public function testFetchAssocRowMutator(): void
$rows2 = $result2
->setRowFetchMutator(static function (Db\Row $row): void {
$row->new_column = $row->id . '-' . $row->name;
$row->repeat = ($row->repeat ?? 0) + 1;
})
->fetchAssoc('type=[]');

Tester\Assert::same(['id' => 1, 'type' => 3, 'name' => 'name3', 'new_column' => '1-name3'], $rows2[3]);
Tester\Assert::same(['id' => 2, 'type' => 2, 'name' => 'name2', 'new_column' => '2-name2'], $rows2[2]);
Tester\Assert::same(['id' => 3, 'type' => 1, 'name' => 'name1', 'new_column' => '3-name1'], $rows2[1]);
Tester\Assert::same(['id' => 1, 'type' => 3, 'name' => 'name3', 'new_column' => '1-name3', 'repeat' => 1], $rows2[3]);
Tester\Assert::same(['id' => 2, 'type' => 2, 'name' => 'name2', 'new_column' => '2-name2', 'repeat' => 1], $rows2[2]);
Tester\Assert::same(['id' => 3, 'type' => 1, 'name' => 'name1', 'new_column' => '3-name1', 'repeat' => 1], $rows2[1]);

$result2->free();
}
Expand Down

0 comments on commit ed2d839

Please sign in to comment.