From cfa27f44725a43e037c98f8a6a072b7c79f4fe52 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Thu, 17 Aug 2006 16:28:29 +0000 Subject: [PATCH] Fix bug in new function get_field_select(), and this time add a unittest to see if the new function works! --- lib/datalib.php | 6 +++++- lib/simpletest/testdatalib.php | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/datalib.php b/lib/datalib.php index 6a216d8bb8..ae3de5228a 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -946,8 +946,9 @@ function get_records_sql_menu($sql) { * @return mixed the specified value, or false if an error occured. */ function get_field($table, $return, $field1, $value1, $field2='', $value2='', $field3='', $value3='') { + global $CFG; $select = where_clause($field1, $value1, $field2, $value2, $field3, $value3); - return get_field_select($table, $return, $select); + return get_field_sql('SELECT ' . $return . ' FROM ' . $CFG->prefix . $table . ' ' . $select); } /** @@ -961,6 +962,9 @@ function get_field($table, $return, $field1, $value1, $field2='', $value2='', $f */ function get_field_select($table, $return, $select) { global $CFG; + if ($select) { + $select = 'WHERE '. $select; + } return get_field_sql('SELECT ' . $return . ' FROM ' . $CFG->prefix . $table . ' ' . $select); } diff --git a/lib/simpletest/testdatalib.php b/lib/simpletest/testdatalib.php index bb3a89e9e5..f7fcec230c 100644 --- a/lib/simpletest/testdatalib.php +++ b/lib/simpletest/testdatalib.php @@ -172,6 +172,15 @@ class datalib_test extends prefix_changing_test_case { $this->assertEqual(get_field($this->table, 'number + id', 'text', 'tadpole', 'id', 4), 108); } + function test_get_field_select() { + $this->assertEqual(get_field_select($this->table, 'number', 'id = 1'), 101); + } + + function test_get_field_sql() { + global $CFG; + $this->assertEqual(get_field_sql("SELECT number FROM {$CFG->prefix}$this->table WHERE id = 1"), 101); + } + function test_set_field() { set_field($this->table, 'number', 12345, 'id', 1); $this->assertEqual(get_field($this->table, 'number', 'id', 1), 12345); -- 2.39.5