From eadcdee9365211414cd401d90765951ad39b9add Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 6 Oct 2014 17:40:12 +0200 Subject: [PATCH] MDL-46647 grades: Fix fetch_all_helper() towards cross-db That helper, used to fetch information from DB by all the grade_object chidren classes was not behaving properly handling TEXT/CLOB columns. Instead of creating a property within every class listing the existing columns, it seems to be a better solution to instrospect the database metadata (cached) to ensure the correct SQL is generated in every case. --- lib/grade/grade_object.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/grade/grade_object.php b/lib/grade/grade_object.php index cceb178a58a..1b9e349ecda 100644 --- a/lib/grade/grade_object.php +++ b/lib/grade/grade_object.php @@ -177,6 +177,8 @@ abstract class grade_object { * @return array|bool Array of object instances or false if not found */ public static function fetch_all_helper($table, $classname, $params) { + global $DB; // Need to introspect DB here. + $instance = new $classname(); $classvars = (array)$instance; @@ -185,14 +187,25 @@ abstract class grade_object { $wheresql = array(); $newparams = array(); + $columns = $DB->get_columns($table); // Cached, no worries. + foreach ($params as $var=>$value) { if (!in_array($var, $instance->required_fields) and !array_key_exists($var, $instance->optional_fields)) { continue; } + if (!array_key_exists($var, $columns)) { // TODO: Should this throw a coding exception? + continue; + } if (is_null($value)) { $wheresql[] = " $var IS NULL "; } else { - $wheresql[] = " $var = ? "; + if ($columns[$var]->meta_type === 'X') { + // We have a text/clob column, use the cross-db method for its comparison. + $wheresql[] = ' ' . $DB->sql_compare_text($var) . ' = ' . $DB->sql_compare_text('?') . ' '; + } else { + // Other columns (varchar, integers...). + $wheresql[] = " $var = ? "; + } $newparams[] = $value; } } -- 2.43.0