MDL-19689 fixed strictness constants, thanks Tim
This commit is contained in:
@@ -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');
|
||||
|
||||
+22
-22
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
+5
-1
@@ -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);
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user