From eb09fdeba1dd6259fe8a6beab1fe0413b74d8317 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Willms <tleilax+github@gmail.com> Date: Thu, 26 Jan 2023 14:01:15 +0100 Subject: [PATCH] Revert "adjust sorm methods to count/return only distinct values, re #1885" This reverts commit 73c350e21d800fa7bf91e651ffcf851e7312259f. --- lib/models/BlubberThread.php | 6 +-- lib/models/OERHost.php | 4 +- lib/models/SimpleORMap.class.php | 74 +++++++++++++------------------- 3 files changed, 34 insertions(+), 50 deletions(-) diff --git a/lib/models/BlubberThread.php b/lib/models/BlubberThread.php index f266223bdc3..d0f2f89ecd7 100644 --- a/lib/models/BlubberThread.php +++ b/lib/models/BlubberThread.php @@ -101,11 +101,11 @@ class BlubberThread extends SimpleORMap implements PrivacyObject /** * @return BlubberThread[] */ - public static function findBySQL($condition, $params = []) + public static function findBySQL($sql, $params = []) { - return parent::findAndMapBySQL(function (BlubberThread $thread) { + return parent::findAndMapBySQL(function ($thread) { return self::upgradeThread($thread); - }, $condition, $params); + }, $sql, $params); } /** diff --git a/lib/models/OERHost.php b/lib/models/OERHost.php index f4303e33f18..d0b1f6928a6 100644 --- a/lib/models/OERHost.php +++ b/lib/models/OERHost.php @@ -46,9 +46,9 @@ class OERHost extends OERIdentity ]); } - public static function findBySQL($condition, $params = []) + public static function findBySQL($sql, $params = []) { - $hosts = parent::findBySQL($condition, $params); + $hosts = parent::findBySQL($sql, $params); foreach ($hosts as $key => $host) { $class = $host['sorm_class']; if ($class && ($class !== 'OERHost') && is_subclass_of($class, 'OERHost')) { diff --git a/lib/models/SimpleORMap.class.php b/lib/models/SimpleORMap.class.php index 80925c32dad..7af9dece840 100644 --- a/lib/models/SimpleORMap.class.php +++ b/lib/models/SimpleORMap.class.php @@ -484,29 +484,22 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate /** * returns number of records * - * @param string $condition sql clause to use on the right side of WHERE - * @param array $params params for query + * @param ?string $sql sql clause to use on the right side of WHERE + * @param ?array $params params for query * @return int */ - public static function countBySql($condition = '1', $params = []) + public static function countBySql($sql = '1', $params = []) { $db_table = static::db_table(); - - $has_join = stripos($condition, 'JOIN '); + $db = DBManager::get(); + $has_join = stripos($sql, 'JOIN '); if ($has_join === false || $has_join > 10) { - $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 = 'WHERE ' . $sql; } - - return (int) DBManager::get()->fetchColumn($sql, $params); + $sql = "SELECT count(*) FROM `" . $db_table . "` " . $sql; + $st = $db->prepare($sql); + $st->execute($params); + return (int)$st->fetchColumn(); } /** @@ -615,31 +608,26 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate /** * returns array of instances of given class filtered by given sql - * @param string $condition sql clause to use on the right side of WHERE - * @param array $params parameters for query + * @param string $sql 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($condition, $params = []) + public static function findBySQL($sql, $params = []) { $db_table = static::db_table(); - - $has_join = stripos($condition, 'JOIN '); + $class = get_called_class(); + $record = new $class(); + $db = DBManager::get(); + $has_join = stripos($sql, 'JOIN '); if ($has_join === false || $has_join > 10) { - $condition = "WHERE {$condition}"; - $distinct = ''; - } else { - $distinct = 'DISTINCT'; + $sql = 'WHERE ' . $sql; } - $sql = "SELECT {$distinct} `{$db_table}`.* FROM `{$db_table}` {$condition}"; - - $record = new static(); - $record->setNew(false); - + $sql = "SELECT `" . $db_table . "`.* FROM `" . $db_table . "` " . $sql; + $ret = []; $stmt = DBManager::get()->prepare($sql); $stmt->execute($params); $stmt->setFetchMode(PDO::FETCH_INTO , $record); - - $ret = []; + $record->setNew(false); while ($record = $stmt->fetch()) { // Reset all relations $record->cleanup(); @@ -707,27 +695,23 @@ 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 $condition where clause of sql + * @param string $sql where clause of sql * @param ?array $params sql statement parameters * @return integer number of found records */ - public static function findEachBySQL($callable, $condition, $params = []) + public static function findEachBySQL($callable, $sql, $params = []) { - $db_table = static::db_table(); - - $has_join = stripos($condition, 'JOIN '); + $has_join = stripos($sql, 'JOIN '); if ($has_join === false || $has_join > 10) { - $condition = "WHERE {$condition}"; - $distinct = ''; - } else { - $distinct = 'DISTINCT'; + $sql = "WHERE {$sql}"; } - $sql = "SELECT {$distinct} `{$db_table}`.* FROM `{$db_table}` {$condition}"; - $record = new static(); + $class = get_called_class(); + $record = new $class(); $record->setNew(false); - $st = DBManager::get()->prepare($sql); + $db_table = static::db_table(); + $st = DBManager::get()->prepare("SELECT `{$db_table}`.* FROM `{$db_table}` {$sql}"); $st->execute($params); $st->setFetchMode(PDO::FETCH_INTO , $record); -- GitLab