Permalink
Browse files

Merge branch 'w38_MDL-35129_m23_dbmigration' of git://github.com/skod…

…ak/moodle into MOODLE_23_STABLE
  • Loading branch information...
2 parents 1530136 + fe5a985 commit a0a02f4e0d887ac7c0f38aa3d4dabf353af41bad @stronk7 stronk7 committed Sep 17, 2012
@@ -1102,6 +1102,20 @@ public function get_recordset_select($table, $select, array $params=null, $sort=
public abstract function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0);
/**
+ * Get all records from a table.
+ *
+ * This method works around potential memory problems and may improve performance,
+ * this method may block access to table until the recordset is closed.
+ *
+ * @param string $table Name of database table.
+ * @return moodle_recordset A moodle_recordset instance {@link function get_recordset}.
+ * @throws dml_exception A DML specific exception is thrown for any errors.
+ */
+ public function export_table_recordset($table) {
+ return $this->get_recordset($table, array());
+ }
+
+ /**
* Get a number of records as an array of objects where all the given conditions met.
*
* If the query succeeds and returns at least one record, the
@@ -905,6 +905,27 @@ public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limit
return $this->create_recordset($result);
}
+ /**
+ * Get all records from a table.
+ *
+ * This method works around potential memory problems and may improve performance,
+ * this method may block access to table until the recordset is closed.
+ *
+ * @param string $table Name of database table.
+ * @return moodle_recordset A moodle_recordset instance {@link function get_recordset}.
+ * @throws dml_exception A DML specific exception is thrown for any errors.
+ */
+ public function export_table_recordset($table) {
+ $sql = $this->fix_table_names("SELECT * FROM {{$table}}");
+
+ $this->query_start($sql, array(), SQL_QUERY_SELECT);
+ // MYSQLI_STORE_RESULT may eat all memory for large tables, unfortunately MYSQLI_USE_RESULT blocks other queries.
+ $result = $this->mysqli->query($sql, MYSQLI_USE_RESULT);
+ $this->query_end($result);
+
+ return $this->create_recordset($result);
+ }
+
protected function create_recordset($result) {
return new mysqli_native_moodle_recordset($result);
}
View
@@ -1300,6 +1300,36 @@ public function test_get_recordset_sql() {
// note: fetching nulls, empties, LOBs already tested by test_insert_record() no needed here
}
+ public function test_export_table_recordset() {
+ $DB = $this->tdb;
+ $dbman = $DB->get_manager();
+
+ $table = $this->get_test_table();
+ $tablename = $table->getName();
+
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+ $dbman->create_table($table);
+
+ $ids = array();
+ $ids[] = $DB->insert_record($tablename, array('course' => 3));
+ $ids[] = $DB->insert_record($tablename, array('course' => 5));
+ $ids[] = $DB->insert_record($tablename, array('course' => 4));
+ $ids[] = $DB->insert_record($tablename, array('course' => 3));
+ $ids[] = $DB->insert_record($tablename, array('course' => 2));
+ $ids[] = $DB->insert_record($tablename, array('course' => 1));
+ $ids[] = $DB->insert_record($tablename, array('course' => 0));
+
+ $rs = $DB->export_table_recordset($tablename);
+ $rids = array();
+ foreach ($rs as $record) {
+ $rids[] = $record->id;
+ }
+ $rs->close();
+ $this->assertEquals($ids, $rids, '', 0, 0, true);
+ }
+
public function test_get_records() {
$DB = $this->tdb;
$dbman = $DB->get_manager();
@@ -144,7 +144,7 @@ public function export_database($description=null) {
$tables = $this->schema->getTables();
$this->begin_database_export($CFG->version, $CFG->release, date('c'), $description);
foreach ($tables as $table) {
- $rs = $this->mdb->get_recordset_sql('SELECT * FROM {'.$table->getName().'}');
+ $rs = $this->mdb->export_table_recordset($table->getName());
if (!$rs) {
throw new ddl_table_missing_exception($table->getName());
}
@@ -153,6 +153,7 @@ public function export_database($description=null) {
$this->export_table_data($table, $row);
}
$this->finish_table_export($table);
+ $rs->close();
}
$this->finish_database_export();
}

0 comments on commit a0a02f4

Please sign in to comment.