diff --git a/lib/dml/mssql_native_moodle_database.php b/lib/dml/mssql_native_moodle_database.php index ec4e983548b..e4ea6de2e71 100644 --- a/lib/dml/mssql_native_moodle_database.php +++ b/lib/dml/mssql_native_moodle_database.php @@ -903,6 +903,9 @@ class mssql_native_moodle_database extends moodle_database { $dataobject = (array)$dataobject; $columns = $this->get_columns($table); + if (empty($columns)) { + throw new dml_exception('ddltablenotexist', $table); + } $cleaned = array(); foreach ($dataobject as $field => $value) { diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index 5196484b5c9..35e6bf60117 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -1106,6 +1106,10 @@ class mysqli_native_moodle_database extends moodle_database { $dataobject = (array)$dataobject; $columns = $this->get_columns($table); + if (empty($columns)) { + throw new dml_exception('ddltablenotexist', $table); + } + $cleaned = array(); foreach ($dataobject as $field=>$value) { diff --git a/lib/dml/oci_native_moodle_database.php b/lib/dml/oci_native_moodle_database.php index 502a266b157..0c6f700d5b6 100644 --- a/lib/dml/oci_native_moodle_database.php +++ b/lib/dml/oci_native_moodle_database.php @@ -1256,6 +1256,10 @@ class oci_native_moodle_database extends moodle_database { $dataobject = (array)$dataobject; $columns = $this->get_columns($table); + if (empty($columns)) { + throw new dml_exception('ddltablenotexist', $table); + } + $cleaned = array(); foreach ($dataobject as $field=>$value) { diff --git a/lib/dml/pdo_moodle_database.php b/lib/dml/pdo_moodle_database.php index 58874bd016b..53fd8d5c74e 100644 --- a/lib/dml/pdo_moodle_database.php +++ b/lib/dml/pdo_moodle_database.php @@ -386,6 +386,10 @@ abstract class pdo_moodle_database extends moodle_database { $dataobject = (array)$dataobject; $columns = $this->get_columns($table); + if (empty($columns)) { + throw new dml_exception('ddltablenotexist', $table); + } + $cleaned = array(); foreach ($dataobject as $field=>$value) { diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index 3754d9b1df4..38fd6bc71b4 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -876,6 +876,10 @@ class pgsql_native_moodle_database extends moodle_database { $dataobject = (array)$dataobject; $columns = $this->get_columns($table); + if (empty($columns)) { + throw new dml_exception('ddltablenotexist', $table); + } + $cleaned = array(); $blobs = array(); diff --git a/lib/dml/sqlsrv_native_moodle_database.php b/lib/dml/sqlsrv_native_moodle_database.php index 6b9bce2fc8b..184329d7bfe 100644 --- a/lib/dml/sqlsrv_native_moodle_database.php +++ b/lib/dml/sqlsrv_native_moodle_database.php @@ -979,6 +979,10 @@ class sqlsrv_native_moodle_database extends moodle_database { $dataobject = (array)$dataobject; $columns = $this->get_columns($table); + if (empty($columns)) { + throw new dml_exception('ddltablenotexist', $table); + } + $cleaned = array (); foreach ($dataobject as $field => $value) { diff --git a/lib/dml/tests/dml_test.php b/lib/dml/tests/dml_test.php index 146f615914e..80032207def 100644 --- a/lib/dml/tests/dml_test.php +++ b/lib/dml/tests/dml_test.php @@ -2253,6 +2253,14 @@ class core_dml_testcase extends database_driver_testcase { } catch (moodle_exception $e) { $this->assertInstanceOf('dml_exception', $e); } + + // Try to insert a record into a non-existent table. dml_exception expected. + try { + $DB->insert_record('nonexistenttable', $record, true); + $this->fail("Expecting an exception, none occurred"); + } catch (exception $e) { + $this->assertTrue($e instanceof dml_exception); + } } public function test_import_record() {