diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index fee0eca..e2db859 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -1623,12 +1623,13 @@ abstract class moodle_database { * * @param string $table The database table to be inserted into * @param array|Traversable $dataobjects list of objects to be inserted, must be compatible with foreach + * @param bool $customsequence True if 'id' included in $dataobjects * @return void does not return new record ids * * @throws coding_exception if data objects have different structure * @throws dml_exception A DML specific exception is thrown for any errors. */ - public function insert_records($table, $dataobjects) { + public function insert_records($table, $dataobjects, $customsequence = false) { if (!is_array($dataobjects) and !($dataobjects instanceof Traversable)) { throw new coding_exception('insert_records() passed non-traversable object'); } @@ -1645,7 +1646,11 @@ abstract class moodle_database { } else if ($fields !== array_keys($dataobject)) { throw new coding_exception('All dataobjects in insert_records() must have the same structure!'); } - $this->insert_record($table, $dataobject, false); + if ($customsequence) { + $this->import_record($table, $dataobject); + } else { + $this->insert_record($table, $dataobject, false); + } } } diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index 1874481..765f3b7 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -1223,12 +1223,13 @@ class mysqli_native_moodle_database extends moodle_database { * * @param string $table The database table to be inserted into * @param array|Traversable $dataobjects list of objects to be inserted, must be compatible with foreach + * @param bool $customsequence True if 'id' included in $dataobjects * @return void does not return new record ids * * @throws coding_exception if data objects have different structure * @throws dml_exception A DML specific exception is thrown for any errors. */ - public function insert_records($table, $dataobjects) { + public function insert_records($table, $dataobjects, $customsequence = false) { if (!is_array($dataobjects) and !$dataobjects instanceof Traversable) { throw new coding_exception('insert_records() passed non-traversable object'); } @@ -1276,7 +1277,9 @@ class mysqli_native_moodle_database extends moodle_database { if ($fields === null) { $fields = array_keys($dataobject); $columns = array_intersect_key($columns, $dataobject); - unset($columns['id']); + if (!$customsequence) { + unset($columns['id']); + } } else if ($fields !== array_keys($dataobject)) { throw new coding_exception('All dataobjects in insert_records() must have the same structure!'); } diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index e369d39..65e0599 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -954,12 +954,13 @@ class pgsql_native_moodle_database extends moodle_database { * * @param string $table The database table to be inserted into * @param array|Traversable $dataobjects list of objects to be inserted, must be compatible with foreach + * @param bool $customsequence True if 'id' included in $dataobjects * @return void does not return new record ids * * @throws coding_exception if data objects have different structure * @throws dml_exception A DML specific exception is thrown for any errors. */ - public function insert_records($table, $dataobjects) { + public function insert_records($table, $dataobjects, $customsequence = false) { if (!is_array($dataobjects) and !($dataobjects instanceof Traversable)) { throw new coding_exception('insert_records() passed non-traversable object'); } @@ -991,7 +992,9 @@ class pgsql_native_moodle_database extends moodle_database { if ($fields === null) { $fields = array_keys($dataobject); $columns = array_intersect_key($columns, $dataobject); - unset($columns['id']); + if (!$customsequence) { + unset($columns['id']); + } } else if ($fields !== array_keys($dataobject)) { throw new coding_exception('All dataobjects in insert_records() must have the same structure!'); }