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.
This commit is contained in:
Eloy Lafuente (stronk7) 2014-10-06 17:40:12 +02:00 committed by Frederic Massart
parent 6597413d41
commit eadcdee936

View File

@ -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;
}
}