Skip to content

Commit

Permalink
Merge pull request #330 from utopia-php/fetchall-single-row
Browse files Browse the repository at this point in the history
fetchAll getDocument
  • Loading branch information
abnegate authored Oct 13, 2023
2 parents e9fd6a9 + c404815 commit a37c8e7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ public function updateDocument(string $collection, Document $document): Document
$permissionsStmt->bindValue(':_uid', $document->getId());
$permissionsStmt->execute();
$permissions = $permissionsStmt->fetchAll();
$permissionsStmt->closeCursor();

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

$results = $stmt->fetchAll();
$stmt->closeCursor();

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

$stmt->execute();

$result = $stmt->fetch();
$result = $stmt->fetchAll();
$stmt->closeCursor();
if (!empty($result)) {
$result = $result[0];
}

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

$stmt->execute();

$result = $stmt->fetch();
$result = $stmt->fetchAll();
$stmt->closeCursor();
if (!empty($result)) {
$result = $result[0];
}

return $result['sum'] ?? 0;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ public function updateDocument(string $collection, Document $document): Document
$permissionsStmt->bindValue(':_uid', $document->getId());
$permissionsStmt->execute();
$permissions = $permissionsStmt->fetchAll();
$permissionsStmt->closeCursor();

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

$results = $stmt->fetchAll();
$stmt->closeCursor();

foreach ($results as $index => $document) {
if (\array_key_exists('_uid', $document)) {
Expand Down
11 changes: 9 additions & 2 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public function exists(string $database, ?string $collection): bool

$stmt->execute();

$document = $stmt->fetch();
$document = $stmt->fetchAll();
$stmt->closeCursor();
if (!empty($document)) {
$document = $document[0];
}

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

$document = $stmt->fetch();
$document = $stmt->fetchAll();
$stmt->closeCursor();

if (empty($document)) {
return new Document([]);
}

$document = $document[0];

if (\array_key_exists('_id', $document)) {
$document['$internalId'] = $document['_id'];
unset($document['_id']);
Expand Down
7 changes: 6 additions & 1 deletion src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public function exists(string $database, ?string $collection): bool

$stmt->execute();

$document = $stmt->fetch();
$document = $stmt->fetchAll();
$stmt->closeCursor();
if (!empty($document)) {
$document = $document[0];
}

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

$initial = [];
foreach (Database::PERMISSIONS as $type) {
Expand Down

0 comments on commit a37c8e7

Please sign in to comment.