Skip to content

Commit

Permalink
MDL-19689 fixed strictness constants, thanks Tim
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jul 4, 2009
1 parent a13c6d6 commit e6c6531
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
8 changes: 4 additions & 4 deletions lib/ddl/simpletest/testddl.php
Expand Up @@ -433,7 +433,7 @@ public function test_add_field() {
$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');
Expand All @@ -450,7 +450,7 @@ public function test_add_field() {
$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');
Expand All @@ -466,7 +466,7 @@ public function test_add_field() {
$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');
Expand All @@ -483,7 +483,7 @@ public function test_add_field() {
$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');
Expand Down
44 changes: 22 additions & 22 deletions lib/dml/moodle_database.php
Expand Up @@ -1128,9 +1128,9 @@ public function get_records_sql_menu($sql, array $params=null, $limitfrom=0, $li
* @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
*/
Expand All @@ -1145,9 +1145,9 @@ public function get_record($table, array $conditions, $fields='*', $strictness=0
* @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
*/
Expand All @@ -1171,29 +1171,29 @@ public function get_record_select($table, $select, array $params=null, $fields='
*
* @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!');
Expand All @@ -1209,9 +1209,9 @@ public function get_record_sql($sql, array $params=null, $strictness=0) {
* @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
*/
Expand All @@ -1227,9 +1227,9 @@ public function get_field($table, $return, array $conditions, $strictness=0) {
* @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
*/
Expand All @@ -1252,9 +1252,9 @@ public function get_field_select($table, $return, $select, array $params=null, $
* @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
*/
Expand Down
8 changes: 4 additions & 4 deletions lib/dml/oci_native_moodle_database.php
Expand Up @@ -661,15 +661,15 @@ public function execute($sql, array $params=null) {
*
* @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;
Expand Down
16 changes: 10 additions & 6 deletions lib/dml/simpletest/testdml.php
Expand Up @@ -930,23 +930,27 @@ public function test_get_record_sql() {

$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);
}

// 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);
Expand Down
6 changes: 5 additions & 1 deletion lib/dmllib.php
Expand Up @@ -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);

/**
Expand Down

0 comments on commit e6c6531

Please sign in to comment.