Skip to content

Commit

Permalink
MDL-46647 grades: Fix fetch_all_helper() towards cross-db
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stronk7 authored and Frederic Massart committed Oct 7, 2014
1 parent 6597413 commit eadcdee
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/grade/grade_object.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ protected static function fetch_helper($table, $classname, $params) {
* @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;
Expand All @@ -185,14 +187,25 @@ public static function fetch_all_helper($table, $classname, $params) {
$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;
}
}
Expand Down

0 comments on commit eadcdee

Please sign in to comment.