Skip to content

Commit a37c8e7

Browse files
authored
Merge pull request #330 from utopia-php/fetchall-single-row
fetchAll getDocument
2 parents e9fd6a9 + c404815 commit a37c8e7

5 files changed

Lines changed: 34 additions & 10 deletions

File tree

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Database/Adapter/MariaDB.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ public function updateDocument(string $collection, Document $document): Document
714714
$permissionsStmt->bindValue(':_uid', $document->getId());
715715
$permissionsStmt->execute();
716716
$permissions = $permissionsStmt->fetchAll();
717+
$permissionsStmt->closeCursor();
717718

718719
$initial = [];
719720
foreach (Database::PERMISSIONS as $type) {
@@ -1098,6 +1099,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
10981099
}
10991100

11001101
$results = $stmt->fetchAll();
1102+
$stmt->closeCursor();
11011103

11021104
foreach ($results as $index => $document) {
11031105
if (\array_key_exists('_uid', $document)) {
@@ -1181,7 +1183,11 @@ public function count(string $collection, array $queries = [], ?int $max = null,
11811183

11821184
$stmt->execute();
11831185

1184-
$result = $stmt->fetch();
1186+
$result = $stmt->fetchAll();
1187+
$stmt->closeCursor();
1188+
if (!empty($result)) {
1189+
$result = $result[0];
1190+
}
11851191

11861192
return $result['sum'] ?? 0;
11871193
}
@@ -1238,7 +1244,11 @@ public function sum(string $collection, string $attribute, array $queries = [],
12381244

12391245
$stmt->execute();
12401246

1241-
$result = $stmt->fetch();
1247+
$result = $stmt->fetchAll();
1248+
$stmt->closeCursor();
1249+
if (!empty($result)) {
1250+
$result = $result[0];
1251+
}
12421252

12431253
return $result['sum'] ?? 0;
12441254
}

src/Database/Adapter/Postgres.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ public function updateDocument(string $collection, Document $document): Document
723723
$permissionsStmt->bindValue(':_uid', $document->getId());
724724
$permissionsStmt->execute();
725725
$permissions = $permissionsStmt->fetchAll();
726+
$permissionsStmt->closeCursor();
726727

727728
$initial = [];
728729
foreach (Database::PERMISSIONS as $type) {
@@ -1104,6 +1105,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
11041105
}
11051106

11061107
$results = $stmt->fetchAll();
1108+
$stmt->closeCursor();
11071109

11081110
foreach ($results as $index => $document) {
11091111
if (\array_key_exists('_uid', $document)) {

src/Database/Adapter/SQL.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ public function exists(string $database, ?string $collection): bool
8181

8282
$stmt->execute();
8383

84-
$document = $stmt->fetch();
84+
$document = $stmt->fetchAll();
85+
$stmt->closeCursor();
86+
if (!empty($document)) {
87+
$document = $document[0];
88+
}
8589

8690
return (($document[$select] ?? '') === $match) || // case insensitive check
8791
(($document[strtolower($select)] ?? '') === $match);
@@ -121,12 +125,15 @@ public function getDocument(string $collection, string $id, array $queries = [])
121125
$stmt->bindValue(':_uid', $id);
122126
$stmt->execute();
123127

124-
$document = $stmt->fetch();
128+
$document = $stmt->fetchAll();
129+
$stmt->closeCursor();
125130

126131
if (empty($document)) {
127132
return new Document([]);
128133
}
129134

135+
$document = $document[0];
136+
130137
if (\array_key_exists('_id', $document)) {
131138
$document['$internalId'] = $document['_id'];
132139
unset($document['_id']);

src/Database/Adapter/SQLite.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public function exists(string $database, ?string $collection): bool
5353

5454
$stmt->execute();
5555

56-
$document = $stmt->fetch();
56+
$document = $stmt->fetchAll();
57+
$stmt->closeCursor();
58+
if (!empty($document)) {
59+
$document = $document[0];
60+
}
5761

5862
return (($document['name'] ?? '') === "{$this->getNamespace()}_{$collection}");
5963
}
@@ -495,6 +499,7 @@ public function updateDocument(string $collection, Document $document): Document
495499
$permissionsStmt->bindValue(':_uid', $document->getId());
496500
$permissionsStmt->execute();
497501
$permissions = $permissionsStmt->fetchAll();
502+
$permissionsStmt->closeCursor();
498503

499504
$initial = [];
500505
foreach (Database::PERMISSIONS as $type) {

0 commit comments

Comments
 (0)