MDL-49399 task: Remove hardcoded LIMIT
authorAndrew Nicols <andrew@nicols.co.uk>
Thu, 17 Jan 2019 00:02:24 +0000 (08:02 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Thu, 17 Jan 2019 00:02:24 +0000 (08:02 +0800)
lib/classes/task/database_logger.php

index 65af0ea..4aa4885 100644 (file)
@@ -128,21 +128,22 @@ class database_logger implements task_logger {
         $classes = $DB->get_fieldset_sql($sql, $params);
 
         foreach ($classes as $classname) {
+            $notinsql = "";
             $params = [
                 'classname' => $classname,
             ];
 
             $retaincount = (int) $CFG->task_logretainruns;
-            $keeplogs = $DB->get_fieldset_sql(
-                    "SELECT id FROM {task_log} WHERE classname = :classname ORDER BY timestart DESC LIMIT {$retaincount}",
-                    $params
-                );
-
-            $notinsql = "";
-            if ($keeplogs) {
-                list($notinsql, $params) = $DB->get_in_or_equal($keeplogs, SQL_PARAMS_NAMED, 'p', false);
-                $params['classname'] = $classname;
-                $notinsql = " AND id {$notinsql}";
+            if ($retaincount) {
+                $keeplogs = $DB->get_records('task_log', [
+                        'classname' => $classname,
+                    ], 'timestart DESC', 'id', 0, $retaincount);
+
+                if ($keeplogs) {
+                    list($notinsql, $params) = $DB->get_in_or_equal(array_keys($keeplogs), SQL_PARAMS_NAMED, 'p', false);
+                    $params['classname'] = $classname;
+                    $notinsql = " AND id {$notinsql}";
+                }
             }
 
             $logids = $DB->get_fieldset_select('task_log', 'id', "classname = :classname {$notinsql}", $params);