aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/SQLQuery.php
diff options
context:
space:
mode:
authorPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
committerPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
commit4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch)
tree5c07151ae61276d334e88f6309c30d439a85c12e /lib/classes/SQLQuery.php
parentda0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff)
parent97a188592c679890a25c37ab78463add76a52ff7 (diff)
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'lib/classes/SQLQuery.php')
-rw-r--r--lib/classes/SQLQuery.php43
1 files changed, 33 insertions, 10 deletions
diff --git a/lib/classes/SQLQuery.php b/lib/classes/SQLQuery.php
index 0442604..6c8f49e 100644
--- a/lib/classes/SQLQuery.php
+++ b/lib/classes/SQLQuery.php
@@ -215,14 +215,22 @@ class SQLQuery
/**
* Fetches the whole resultset as an array of associative arrays. If you define
* a sorm_class the result will be an array of the sorm-objects.
- * @param $sorm_class_or_column : column name, a class of SimpleORMap or null for associative array.
- * @return array of arrays or array of objects or array of values.
+ *
+ * @template T of SimpleORMap
+ * @param class-string<T>|string|null $sorm_class_or_column : column name, a class of SimpleORMap or null for associative array.
+ * @param int|null $max_results Maximum number of results to return
+ * @return array[]|T[]|mixed[] arrays or array of objects or array of values.
+ *
+ * @throws OverflowException if number of found rows is greater than $max_results
*/
- public function fetchAll($sorm_class_or_column = null)
+ public function fetchAll($sorm_class_or_column = null, ?int $max_results = null)
{
NotificationCenter::postNotification('SQLQueryWillExecute', $this);
- if (is_string($sorm_class_or_column) && !is_subclass_of($sorm_class_or_column, "SimpleORMap")) {
+ if (
+ is_string($sorm_class_or_column)
+ && !is_subclass_of($sorm_class_or_column, SimpleORMap::class)
+ ) {
$sql = "SELECT `{$this->settings['table']}`.`{$sorm_class_or_column}` ";
} else {
$sql = "SELECT `{$this->settings['table']}`.* ";
@@ -239,16 +247,31 @@ class SQLQuery
NotificationCenter::postNotification('SQLQueryDidExecute', $this);
- if (is_string($sorm_class_or_column) && !is_subclass_of($sorm_class_or_column, "SimpleORMap")) {
- return $statement->fetchAll(PDO::FETCH_COLUMN, 0);
+ if (
+ is_string($sorm_class_or_column)
+ && !is_subclass_of($sorm_class_or_column, SimpleORMap::class)
+ ) {
+ return $statement->fetchAll(PDO::FETCH_COLUMN);
}
- $alldata = $statement->fetchAll(PDO::FETCH_ASSOC);
- if (!$sorm_class_or_column) {
- return $alldata;
+ $statement->setFetchMode(PDO::FETCH_ASSOC);
+
+ $result = [];
+ $count = 0;
+ foreach ($statement as $row) {
+ $result[$count++] = $sorm_class_or_column ? $sorm_class_or_column::buildExisting($row) : $row;
+
+ if ($max_results && $count > $max_results) {
+ // Count remaining rows
+ $statement->setFetchMode(PDO::FETCH_COLUMN, 0);
+ while ($statement->fetch()) {
+ $count += 1;
+ }
+ throw new OverflowException($count);
+ }
}
- return array_map("{$sorm_class_or_column}::buildExisting", $alldata);
+ return $result;
}
/**