From e6c6531ccee880b46d69d0e849bf5dadaf63e412 Mon Sep 17 00:00:00 2001 From: skodak Date: Sat, 4 Jul 2009 09:30:59 +0000 Subject: [PATCH] MDL-19689 fixed strictness constants, thanks Tim --- lib/ddl/simpletest/testddl.php | 8 ++--- lib/dml/moodle_database.php | 44 +++++++++++++------------- lib/dml/oci_native_moodle_database.php | 8 ++--- lib/dml/simpletest/testdml.php | 16 ++++++---- lib/dmllib.php | 6 +++- 5 files changed, 45 insertions(+), 37 deletions(-) diff --git a/lib/ddl/simpletest/testddl.php b/lib/ddl/simpletest/testddl.php index 62a8cd08e1..f4e080c8af 100755 --- a/lib/ddl/simpletest/testddl.php +++ b/lib/ddl/simpletest/testddl.php @@ -433,7 +433,7 @@ class ddl_test extends UnitTestCase { $this->assertEqual($columns['oneinteger']->has_default , true); $this->assertEqual($columns['oneinteger']->default_value, 2); $this->assertEqual($columns['oneinteger']->meta_type ,'I'); - $this->assertEqual($DB->get_field('test_table1', 'oneinteger', array(), 1), 2); //check default has been applied + $this->assertEqual($DB->get_field('test_table1', 'oneinteger', array(), IGNORE_MULTIPLE), 2); //check default has been applied /// add one numeric field and check it $field = new xmldb_field('onenumber'); @@ -450,7 +450,7 @@ class ddl_test extends UnitTestCase { $this->assertEqual($columns['onenumber']->has_default , true); $this->assertEqual($columns['onenumber']->default_value, 2.550); $this->assertEqual($columns['onenumber']->meta_type ,'N'); - $this->assertEqual($DB->get_field('test_table1', 'onenumber', array(), 1), 2.550); //check default has been applied + $this->assertEqual($DB->get_field('test_table1', 'onenumber', array(), IGNORE_MULTIPLE), 2.550); //check default has been applied /// add one float field and check it (not official type - must work as number) $field = new xmldb_field('onefloat'); @@ -466,7 +466,7 @@ class ddl_test extends UnitTestCase { $this->assertEqual($columns['onefloat']->has_default , true); $this->assertEqual($columns['onefloat']->default_value, 3.550); $this->assertEqual($columns['onefloat']->meta_type ,'N'); - $this->assertEqual($DB->get_field('test_table1', 'onefloat', array(), 1), 3.550); //check default has been applied + $this->assertEqual($DB->get_field('test_table1', 'onefloat', array(), IGNORE_MULTIPLE), 3.550); //check default has been applied /// add one char field and check it $field = new xmldb_field('onechar'); @@ -483,7 +483,7 @@ class ddl_test extends UnitTestCase { $this->assertEqual($columns['onechar']->has_default , true); $this->assertEqual($columns['onechar']->default_value,'Nice dflt!'); $this->assertEqual($columns['onechar']->meta_type ,'C'); - $this->assertEqual($DB->get_field('test_table1', 'onechar', array(), 1), 'Nice dflt!'); //check default has been applied + $this->assertEqual($DB->get_field('test_table1', 'onechar', array(), IGNORE_MULTIPLE), 'Nice dflt!'); //check default has been applied /// add one text field and check it $field = new xmldb_field('onetext'); diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 82fb4cafb9..cc6998bfa7 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -1128,9 +1128,9 @@ abstract class moodle_database { * @param string $table The table to select from. * @param array $conditions optional array $fieldname=>requestedvalue with AND in between * @param string $fields A comma separated list of fields to be returned from the chosen table. - * @param int $strictness 0 means compatible mode, false returned if record not found, debug message if more found; - * 1 means ignore multiple records found, return first (not recommended); - * 2 means throw exception if no record or multiple records found (MUST_EXIST constant) + * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; + * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended); + * MUST_EXIST means throw exception if no record or multiple records found * @return mixed a fieldset object containing the first matching record, false or exception if error not found depending on mode * @throws dml_exception if error */ @@ -1145,9 +1145,9 @@ abstract class moodle_database { * @param string $table The database table to be checked against. * @param string $select A fragment of SQL to be used in a where clause in the SQL call. * @param array $params array of sql parameters - * @param int $strictness 0 means compatible mode, false returned if record not found, debug message if more found; - * 1 means ignore multiple records found, return first (not recommended); - * 2 means throw exception if no record or multiple records found (MUST_EXIST constant) + * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; + * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended); + * MUST_EXIST means throw exception if no record or multiple records found * @return mixed a fieldset object containing the first matching record, false or exception if error not found depending on mode * @throws dml_exception if error */ @@ -1171,29 +1171,29 @@ abstract class moodle_database { * * @param string $sql The SQL string you wish to be executed, should normally only return one record. * @param array $params array of sql parameters - * @param int $strictness 0 means compatible mode, false returned if record not found, debug message if more found; - * 1 means ignore multiple records found, return first (not recommended); - * 2 means throw exception if no record or multiple records found (MUST_EXIST constant) + * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; + * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended); + * MUST_EXIST means throw exception if no record or multiple records found * @return mixed a fieldset object containing the first matching record, false or exception if error not found depending on mode * @throws dml_exception if error */ public function get_record_sql($sql, array $params=null, $strictness=0) { - $strictness = (int)$strictness; - if ($strictness == 1) { + $strictness = (int)$strictness; // we support true/false for BC reasons too + if ($strictness == IGNORE_MULTIPLE) { $count = 1; } else { $count = 0; } if (!$records = $this->get_records_sql($sql, $params, 0, $count)) { // not found - if ($strictness == 2) { //MUST_EXIST + if ($strictness == MUST_EXIST) { throw new dml_missing_record_exception('', $sql, $params); } return false; } if (count($records) > 1) { - if ($strictness == 2) { //MUST_EXIST + if ($strictness == MUST_EXIST) { throw new dml_multiple_records_exception($sql, $params); } debugging('Error: mdb->get_record() found more than one record!'); @@ -1209,9 +1209,9 @@ abstract class moodle_database { * @param string $table the table to query. * @param string $return the field to return the value of. * @param array $conditions optional array $fieldname=>requestedvalue with AND in between - * @param int $strictness 0 means compatible mode, false returned if record not found, debug message if more found; - * 1 means ignore multiple records found, return first; - * 2 means throw exception if no record or multiple records found (MUST_EXIST constant) + * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; + * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended); + * MUST_EXIST means throw exception if no record or multiple records found * @return mixed the specified value false if not found * @throws dml_exception if error */ @@ -1227,9 +1227,9 @@ abstract class moodle_database { * @param string $return the field to return the value of. * @param string $select A fragment of SQL to be used in a where clause returning one row with one column * @param array $params array of sql parameters - * @param int $strictness 0 means compatible mode, false returned if record not found, debug message if more found; - * 1 means ignore multiple records found, return first; - * 2 means throw exception if no record or multiple records found (MUST_EXIST constant) + * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; + * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended); + * MUST_EXIST means throw exception if no record or multiple records found * @return mixed the specified value false if not found * @throws dml_exception if error */ @@ -1252,9 +1252,9 @@ abstract class moodle_database { * @param string $return the field to return the value of. * @param string $sql The SQL query returning one row with one column * @param array $params array of sql parameters - * @param int $strictness 0 means compatible mode, false returned if record not found, debug message if more found; - * 1 means ignore multiple records found, return first; - * 2 means throw exception if no record or multiple records found (MUST_EXIST constant) + * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; + * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended); + * MUST_EXIST means throw exception if no record or multiple records found * @return mixed the specified value false if not found * @throws dml_exception if error */ diff --git a/lib/dml/oci_native_moodle_database.php b/lib/dml/oci_native_moodle_database.php index 4700d2c169..317110d955 100644 --- a/lib/dml/oci_native_moodle_database.php +++ b/lib/dml/oci_native_moodle_database.php @@ -661,15 +661,15 @@ class oci_native_moodle_database extends moodle_database { * * @param string $sql The SQL string you wish to be executed, should normally only return one record. * @param array $params array of sql parameters - * @param int $mode 0 means compatible mode, false returned if record not found, debug message if more found; - * 1 means ignore multiple records found, return first (not recommended); - * 2 means throw exception if no record or multiple records found (MUST_EXIST constant) + * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; + * IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended); + * MUST_EXIST means throw exception if no record or multiple records found * @return mixed a fieldset object containing the first matching record, false or exception if error not found depending on mode * @throws dml_exception if error */ public function get_record_sql($sql, array $params=null, $mode=0) { $mode = (int)$mode; - if ($mode == 1) { + if ($mode == IGNORE_MULTIPLE) { // do not limit here - ORA does not like that if (!$rs = $this->get_recordset_sql($sql, $params)) { return false; diff --git a/lib/dml/simpletest/testdml.php b/lib/dml/simpletest/testdml.php index 76fb7ba169..952e828393 100755 --- a/lib/dml/simpletest/testdml.php +++ b/lib/dml/simpletest/testdml.php @@ -930,11 +930,15 @@ class dml_test extends UnitTestCase { $this->assertEqual(2, $record->course); + // backwards compatibility with $ignoremultiple + $this->assertFalse(IGNORE_MISSING); + $this->assertTrue(IGNORE_MULTIPLE); + // record not found - $this->assertFalse($record = $DB->get_record_sql("SELECT * FROM {".$tablename."} WHERE id = ?", array(666), 0)); - $this->assertFalse($record = $DB->get_record_sql("SELECT * FROM {".$tablename."} WHERE id = ?", array(666), 1)); + $this->assertFalse($record = $DB->get_record_sql("SELECT * FROM {".$tablename."} WHERE id = ?", array(666), IGNORE_MISSING)); + $this->assertFalse($record = $DB->get_record_sql("SELECT * FROM {".$tablename."} WHERE id = ?", array(666), IGNORE_MULTIPLE)); try { - $DB->get_record_sql("SELECT * FROM {".$tablename."} WHERE id = ?", array(666), 2); + $DB->get_record_sql("SELECT * FROM {".$tablename."} WHERE id = ?", array(666), MUST_EXIST); $this->fail("Exception expected"); } catch (dml_missing_record_exception $e) { $this->assertTrue(true); @@ -942,11 +946,11 @@ class dml_test extends UnitTestCase { // multiple matches ob_start(); // hide debug warning - $this->assertTrue($record = $DB->get_record_sql("SELECT * FROM {".$tablename."}", array(), 0)); + $this->assertTrue($record = $DB->get_record_sql("SELECT * FROM {".$tablename."}", array(), IGNORE_MISSING)); ob_end_clean(); - $this->assertTrue($record = $DB->get_record_sql("SELECT * FROM {".$tablename."}", array(), 1)); + $this->assertTrue($record = $DB->get_record_sql("SELECT * FROM {".$tablename."}", array(), IGNORE_MULTIPLE)); try { - $DB->get_record_sql("SELECT * FROM {".$tablename."}", array(), 2); + $DB->get_record_sql("SELECT * FROM {".$tablename."}", array(), MUST_EXIST); $this->fail("Exception expected"); } catch (dml_multiple_records_exception $e) { $this->assertTrue(true); diff --git a/lib/dmllib.php b/lib/dmllib.php index ba52ad57cd..9ac2deb90a 100644 --- a/lib/dmllib.php +++ b/lib/dmllib.php @@ -41,7 +41,11 @@ // Require the essential require_once($CFG->libdir.'/dml/moodle_database.php'); -/** Indicates some record is required to exist */ +/** Return false if record not found, show debug warning if multiple records found */ +define('IGNORE_MISSING', 0); +/** Similar to IGNORE_MISSING but does not show debug warning if multiple records found, not recommended to be used */ +define('IGNORE_MULTIPLE', 1); +/** Indicates exactly one record must exist */ define('MUST_EXIST', 2); /** -- 2.39.5