aboutsummaryrefslogtreecommitdiff
path: root/lib/models/SimpleORMap.class.php
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2023-01-26 12:05:57 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2023-01-26 12:05:57 +0000
commit73c350e21d800fa7bf91e651ffcf851e7312259f (patch)
tree6e6312fa6beab9fbec2b2e12f43ed0a782923de1 /lib/models/SimpleORMap.class.php
parent205b1e60c00e62dca444f740e9b847bd2380f1fb (diff)
adjust sorm methods to count/return only distinct values, fixes #1885
Closes #1885 Merge request studip/studip!1233
Diffstat (limited to 'lib/models/SimpleORMap.class.php')
-rw-r--r--lib/models/SimpleORMap.class.php74
1 files changed, 45 insertions, 29 deletions
diff --git a/lib/models/SimpleORMap.class.php b/lib/models/SimpleORMap.class.php
index 7af9dec..80925c3 100644
--- a/lib/models/SimpleORMap.class.php
+++ b/lib/models/SimpleORMap.class.php
@@ -484,22 +484,29 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
/**
* returns number of records
*
- * @param ?string $sql sql clause to use on the right side of WHERE
- * @param ?array $params params for query
+ * @param string $condition sql clause to use on the right side of WHERE
+ * @param array $params params for query
* @return int
*/
- public static function countBySql($sql = '1', $params = [])
+ public static function countBySql($condition = '1', $params = [])
{
$db_table = static::db_table();
- $db = DBManager::get();
- $has_join = stripos($sql, 'JOIN ');
+
+ $has_join = stripos($condition, 'JOIN ');
if ($has_join === false || $has_join > 10) {
- $sql = 'WHERE ' . $sql;
+ $sql = "SELECT COUNT(*) FROM `{$db_table}` WHERE {$condition}";
+ } else {
+ $pk = implode(',', array_map(
+ function ($key) use ($db_table) {
+ return "`{$db_table}`.`{$key}`";
+ },
+ static::pk()
+ ));
+
+ $sql = "SELECT COUNT(DISTINCT {$pk}) FROM `{$db_table}` {$condition}";
}
- $sql = "SELECT count(*) FROM `" . $db_table . "` " . $sql;
- $st = $db->prepare($sql);
- $st->execute($params);
- return (int)$st->fetchColumn();
+
+ return (int) DBManager::get()->fetchColumn($sql, $params);
}
/**
@@ -608,26 +615,31 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
/**
* returns array of instances of given class filtered by given sql
- * @param string $sql sql clause to use on the right side of WHERE
- * @param ?array $params parameters for query
+ * @param string $condition sql clause to use on the right side of WHERE
+ * @param array $params parameters for query
* @return array array of "self" objects
*/
- public static function findBySQL($sql, $params = [])
+ public static function findBySQL($condition, $params = [])
{
$db_table = static::db_table();
- $class = get_called_class();
- $record = new $class();
- $db = DBManager::get();
- $has_join = stripos($sql, 'JOIN ');
+
+ $has_join = stripos($condition, 'JOIN ');
if ($has_join === false || $has_join > 10) {
- $sql = 'WHERE ' . $sql;
+ $condition = "WHERE {$condition}";
+ $distinct = '';
+ } else {
+ $distinct = 'DISTINCT';
}
- $sql = "SELECT `" . $db_table . "`.* FROM `" . $db_table . "` " . $sql;
- $ret = [];
+ $sql = "SELECT {$distinct} `{$db_table}`.* FROM `{$db_table}` {$condition}";
+
+ $record = new static();
+ $record->setNew(false);
+
$stmt = DBManager::get()->prepare($sql);
$stmt->execute($params);
$stmt->setFetchMode(PDO::FETCH_INTO , $record);
- $record->setNew(false);
+
+ $ret = [];
while ($record = $stmt->fetch()) {
// Reset all relations
$record->cleanup();
@@ -695,23 +707,27 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
* passes objects for given sql through given callback
*
* @param callable $callable callback which gets the current record as param
- * @param string $sql where clause of sql
+ * @param string $condition where clause of sql
* @param ?array $params sql statement parameters
* @return integer number of found records
*/
- public static function findEachBySQL($callable, $sql, $params = [])
+ public static function findEachBySQL($callable, $condition, $params = [])
{
- $has_join = stripos($sql, 'JOIN ');
+ $db_table = static::db_table();
+
+ $has_join = stripos($condition, 'JOIN ');
if ($has_join === false || $has_join > 10) {
- $sql = "WHERE {$sql}";
+ $condition = "WHERE {$condition}";
+ $distinct = '';
+ } else {
+ $distinct = 'DISTINCT';
}
+ $sql = "SELECT {$distinct} `{$db_table}`.* FROM `{$db_table}` {$condition}";
- $class = get_called_class();
- $record = new $class();
+ $record = new static();
$record->setNew(false);
- $db_table = static::db_table();
- $st = DBManager::get()->prepare("SELECT `{$db_table}`.* FROM `{$db_table}` {$sql}");
+ $st = DBManager::get()->prepare($sql);
$st->execute($params);
$st->setFetchMode(PDO::FETCH_INTO , $record);