diff --git a/lib/datalib.php b/lib/datalib.php index 285729c23c3..88066531899 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -1710,7 +1710,7 @@ function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user try { $DB->insert_record_raw('log', $log, false); - } catch (dml_write_exception $e) { + } catch (dml_exception $e) { debugging('Error: Could not insert a new entry to the Moodle log', DEBUG_ALL); // MDL-11893, alert $CFG->supportemail if insert into log failed if ($CFG->supportemail and empty($CFG->noemailever)) { diff --git a/lib/ddl/simpletest/testddl.php b/lib/ddl/simpletest/testddl.php index 7ea1275bbd4..a9377534fc9 100644 --- a/lib/ddl/simpletest/testddl.php +++ b/lib/ddl/simpletest/testddl.php @@ -180,7 +180,7 @@ class ddl_test extends UnitTestCase { ob_start(); // hide debug warning try { $result = $DB->get_records('test_table0'); - } catch (dml_read_exception $e) { + } catch (dml_exception $e) { $result = false; } ob_end_clean(); @@ -203,7 +203,7 @@ class ddl_test extends UnitTestCase { ob_start(); // hide debug warning try { $result = $DB->get_records('test_table0'); - } catch (dml_read_exception $e) { + } catch (dml_exception $e) { $result = false; } ob_end_clean(); @@ -337,14 +337,14 @@ class ddl_test extends UnitTestCase { try { // columns cache must be empty, so sentence throw exception $columns = $DB->get_columns('test_table0'); - } catch (dml_read_exception $e) { + } catch (dml_exception $e) { $columns = false; } $this->assertFalse($columns); try { /// throw exception $indexes = $DB->get_indexes('test_table0'); - } catch (dml_read_exception $e) { + } catch (dml_exception $e) { $indexes = false; } $this->assertFalse($indexes); @@ -931,7 +931,7 @@ class ddl_test extends UnitTestCase { ob_start(); // hide debug warning try { $result = $DB->insert_record('test_table_cust0', $record, false); - } catch (dml_write_exception $e) { + } catch (dml_exception $e) { $result = false; } ob_end_clean(); @@ -953,7 +953,7 @@ class ddl_test extends UnitTestCase { ob_start(); // hide debug warning try { $result = $DB->insert_record('test_table_cust0', $record, false); - } catch (dml_write_exception $e) { + } catch (dml_exception $e) { $result = false; } ob_end_clean(); @@ -1022,7 +1022,7 @@ class ddl_test extends UnitTestCase { ob_start(); // hide debug warning try { $result = $DB->insert_record('test_table_cust0', $record, false); - } catch (dml_write_exception $e) { + } catch (dml_exception $e) { $result = false;; } ob_end_clean(); @@ -1644,7 +1644,7 @@ class ddl_test extends UnitTestCase { $rec = $DB->get_record($tablename, array('id'=>$id)); $this->assertIdentical($rec->name, $maxstr); - } catch (dml_write_exception $e) { + } catch (dml_exception $e) { if ($DB->get_dbfamily() === 'oracle') { $this->fail('Oracle does not support text fields larger than 4000 bytes, this is not a big problem for mostly ascii based languages'); } else { diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 00bbd96b409..6e00b4c184b 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -495,15 +495,15 @@ abstract class moodle_database { * @return array sql part and params */ protected function where_clause($table, array $conditions=null) { - $allowed_types = $this->allowed_param_types(); - if (empty($conditions)) { - return array('', array()); - } - $where = array(); - $params = array(); - + // We accept nulls in conditions + $conditions = is_null($conditions) ? array() : $conditions; + // Some checks performed under debugging only if (debugging()) { $columns = $this->get_columns($table); + if (empty($columns)) { + // no supported columns means most probably table does not exist + throw new dml_exception('ddltablenotexist', $table); + } foreach ($conditions as $key=>$value) { if (!isset($columns[$key])) { $a = new stdClass(); @@ -519,6 +519,13 @@ abstract class moodle_database { } } + $allowed_types = $this->allowed_param_types(); + if (empty($conditions)) { + return array('', array()); + } + $where = array(); + $params = array(); + foreach ($conditions as $key=>$value) { if (is_int($key)) { throw new dml_exception('invalidnumkey'); diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index 7b6f2f3d9d4..fabfb3f1b70 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -435,7 +435,7 @@ class mysqli_native_moodle_database extends moodle_database { $sql = "SHOW COLUMNS FROM {$this->prefix}$table"; $this->query_start($sql, null, SQL_QUERY_AUX); $result = $this->mysqli->query($sql); - $this->query_end($result); + $this->query_end(true); // Don't want to throw anything here ever. MDL-30147 if ($result === false) { return array(); diff --git a/lib/dml/simpletest/testdml.php b/lib/dml/simpletest/testdml.php index 627c6c14638..fce8f4044f5 100644 --- a/lib/dml/simpletest/testdml.php +++ b/lib/dml/simpletest/testdml.php @@ -821,6 +821,10 @@ class dml_test extends UnitTestCase { $this->assertEqual($next_column->name, $next_field->name); } + + // Test get_columns for non-existing table returns empty array. MDL-30147 + $columns = $DB->get_columns('xxxx'); + $this->assertEqual(array(), $columns); } public function test_get_manager() { @@ -905,7 +909,7 @@ class dml_test extends UnitTestCase { $DB->execute($sql); $this->fail("Expecting an exception, none occurred"); } catch (Exception $e) { - $this->assertTrue($e instanceof dml_write_exception); + $this->assertTrue($e instanceof dml_exception); } // update records @@ -999,7 +1003,10 @@ class dml_test extends UnitTestCase { $conditions = array('onetext' => '1'); try { $rs = $DB->get_recordset($tablename, $conditions); - $this->fail('An Exception is missing, expected due to equating of text fields'); + if (debugging()) { + // only in debug mode - hopefully all devs test code in debug mode... + $this->fail('An Exception is missing, expected due to equating of text fields'); + } } catch (exception $e) { $this->assertTrue($e instanceof dml_exception); $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); @@ -1256,12 +1263,51 @@ class dml_test extends UnitTestCase { $conditions = array('onetext' => '1'); try { $records = $DB->get_records($tablename, $conditions); - $this->fail('An Exception is missing, expected due to equating of text fields'); + if (debugging()) { + // only in debug mode - hopefully all devs test code in debug mode... + $this->fail('An Exception is missing, expected due to equating of text fields'); + } } catch (exception $e) { $this->assertTrue($e instanceof dml_exception); $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); } + // test get_records passing non-existing table + // with params + try { + $records = $DB->get_records('xxxx', array('id' => 0)); + $this->fail('An Exception is missing, expected due to query against non-existing table'); + } catch (exception $e) { + $this->assertTrue($e instanceof dml_exception); + if (debugging()) { + // information for developers only, normal users get general error message + $this->assertEqual($e->errorcode, 'ddltablenotexist'); + } + } + // and without params + try { + $records = $DB->get_records('xxxx', array()); + $this->fail('An Exception is missing, expected due to query against non-existing table'); + } catch (exception $e) { + $this->assertTrue($e instanceof dml_exception); + if (debugging()) { + // information for developers only, normal users get general error message + $this->assertEqual($e->errorcode, 'ddltablenotexist'); + } + } + + // test get_records passing non-existing column + try { + $records = $DB->get_records($tablename, array('xxxx' => 0)); + $this->fail('An Exception is missing, expected due to query against non-existing column'); + } catch (exception $e) { + $this->assertTrue($e instanceof dml_exception); + if (debugging()) { + // information for developers only, normal users get general error message + $this->assertEqual($e->errorcode, 'ddlfieldnotexist'); + } + } + // note: delegate limits testing to test_get_records_sql() } @@ -1641,7 +1687,10 @@ class dml_test extends UnitTestCase { $conditions = array('onetext' => '1'); try { $DB->get_field($tablename, 'course', $conditions); - $this->fail('An Exception is missing, expected due to equating of text fields'); + if (debugging()) { + // only in debug mode - hopefully all devs test code in debug mode... + $this->fail('An Exception is missing, expected due to equating of text fields'); + } } catch (exception $e) { $this->assertTrue($e instanceof dml_exception); $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); @@ -1785,7 +1834,7 @@ class dml_test extends UnitTestCase { try { $DB->insert_record_raw($tablename, array('xxxxx' => 3, 'onechar' => 'bb')); $this->assertFail('Exception expected due to invalid column'); - } catch (dml_write_exception $ex) { + } catch (dml_exception $ex) { $this->assertTrue(true); } } @@ -2494,7 +2543,10 @@ class dml_test extends UnitTestCase { $conditions = array('onetext' => '1'); try { $DB->set_field($tablename, 'onechar', 'frog', $conditions); - $this->fail('An Exception is missing, expected due to equating of text fields'); + if (debugging()) { + // only in debug mode - hopefully all devs test code in debug mode... + $this->fail('An Exception is missing, expected due to equating of text fields'); + } } catch (exception $e) { $this->assertTrue($e instanceof dml_exception); $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); @@ -2704,7 +2756,10 @@ class dml_test extends UnitTestCase { $conditions = array('onetext' => '1'); try { $DB->count_records($tablename, $conditions); - $this->fail('An Exception is missing, expected due to equating of text fields'); + if (debugging()) { + // only in debug mode - hopefully all devs test code in debug mode... + $this->fail('An Exception is missing, expected due to equating of text fields'); + } } catch (exception $e) { $this->assertTrue($e instanceof dml_exception); $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); @@ -2779,7 +2834,10 @@ class dml_test extends UnitTestCase { $conditions = array('onetext' => '1'); try { $DB->record_exists($tablename, $conditions); - $this->fail('An Exception is missing, expected due to equating of text fields'); + if (debugging()) { + // only in debug mode - hopefully all devs test code in debug mode... + $this->fail('An Exception is missing, expected due to equating of text fields'); + } } catch (exception $e) { $this->assertTrue($e instanceof dml_exception); $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); @@ -2927,7 +2985,10 @@ class dml_test extends UnitTestCase { $conditions = array('onetext'=>'1'); try { $DB->delete_records($tablename, $conditions); - $this->fail('An Exception is missing, expected due to equating of text fields'); + if (debugging()) { + // only in debug mode - hopefully all devs test code in debug mode... + $this->fail('An Exception is missing, expected due to equating of text fields'); + } } catch (exception $e) { $this->assertTrue($e instanceof dml_exception); $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); @@ -2937,7 +2998,10 @@ class dml_test extends UnitTestCase { $conditions = array('onetext' => 1); try { $DB->delete_records($tablename, $conditions); - $this->fail('An Exception is missing, expected due to equating of text fields'); + if (debugging()) { + // only in debug mode - hopefully all devs test code in debug mode... + $this->fail('An Exception is missing, expected due to equating of text fields'); + } } catch (exception $e) { $this->assertTrue($e instanceof dml_exception); $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); diff --git a/lib/dmllib.php b/lib/dmllib.php index 19546aec986..2e5945463b5 100644 --- a/lib/dmllib.php +++ b/lib/dmllib.php @@ -91,7 +91,7 @@ class dml_sessionwait_exception extends dml_exception { } /** - * DML read exception - triggered by SQL syntax errors, missing tables, etc. + * DML read exception - triggered by some SQL syntax errors, etc. */ class dml_read_exception extends dml_exception { /** @var string */ @@ -185,7 +185,7 @@ class dml_missing_record_exception extends dml_exception { } /** - * DML write exception - triggered by SQL syntax errors, missing tables, etc. + * DML write exception - triggered by some SQL syntax errors, etc. */ class dml_write_exception extends dml_exception { /** @var string */ diff --git a/lib/setuplib.php b/lib/setuplib.php index bdb287d50dd..205cd3229e7 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -657,7 +657,7 @@ function initialise_cfg() { $CFG->{$name} = $value; } } - } catch (dml_read_exception $e) { + } catch (dml_exception $e) { // most probably empty db, going to install soon } }