/** * Object constructor. * * @param moodle_database $mdb Connection to the target database (a * @see moodle_database object). Use null to use the current $DB connection. * @param boolean $check_schema - whether or not to check that XML database * schema matches the RDBMS database schema before importing (inside * @see begin_database_import). */ public function __construct(moodle_database $mdb, $check_schema = true) { $this->mdb = $mdb; $this->manager = $mdb->get_manager(); $this->schema = $this->manager->get_install_xml_schema(); $this->check_schema = $check_schema; }
public function test_execute() { $DB = $this->tdb; $dbman = $this->tdb->get_manager(); $table1 = $this->get_test_table('1'); $tablename1 = $table1->getName(); $table1->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table1->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); $table1->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, '0'); $table1->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); $table1->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $dbman->create_table($table1); $table2 = $this->get_test_table('2'); $tablename2 = $table2->getName(); $table2->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table2->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); $table2->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $dbman->create_table($table2); $DB->insert_record($tablename1, array('course' => 3, 'name' => 'aaa')); $DB->insert_record($tablename1, array('course' => 1, 'name' => 'bbb')); $DB->insert_record($tablename1, array('course' => 7, 'name' => 'ccc')); $DB->insert_record($tablename1, array('course' => 3, 'name' => 'ddd')); // select results are ignored $sql = "SELECT * FROM {{$tablename1}} WHERE course = :course"; $this->assertTrue($DB->execute($sql, array('course'=>3))); // throw exception on error $sql = "XXUPDATE SET XSSD"; try { $DB->execute($sql); $this->fail("Expecting an exception, none occurred"); } catch (Exception $e) { $this->assertTrue($e instanceof dml_write_exception); } // update records $sql = "UPDATE {{$tablename1}} SET course = 6 WHERE course = ?"; $this->assertTrue($DB->execute($sql, array('3'))); $this->assertEqual($DB->count_records($tablename1, array('course' => 6)), 2); // insert from one into second table $sql = "INSERT INTO {{$tablename2}} (course) SELECT course FROM {{$tablename1}}"; $this->assertTrue($DB->execute($sql)); $this->assertEqual($DB->count_records($tablename2), 4); }
public function test_execute() { $DB = $this->tdb; $dbman = $this->tdb->get_manager(); $table1 = $this->get_test_table('1'); $tablename1 = $table1->getName(); $table1->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table1->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); $table1->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, '0'); $table1->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); $table1->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $dbman->create_table($table1); $table2 = $this->get_test_table('2'); $tablename2 = $table2->getName(); $table2->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table2->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); $table2->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $dbman->create_table($table2); $DB->insert_record($tablename1, array('course' => 3, 'name' => 'aaa')); $DB->insert_record($tablename1, array('course' => 1, 'name' => 'bbb')); $DB->insert_record($tablename1, array('course' => 7, 'name' => 'ccc')); $DB->insert_record($tablename1, array('course' => 3, 'name' => 'ddd')); // select results are ignored $sql = "SELECT * FROM {{$tablename1}} WHERE course = :course"; $this->assertTrue($DB->execute($sql, array('course' => 3))); // throw exception on error $sql = "XXUPDATE SET XSSD"; try { $DB->execute($sql); $this->fail("Expecting an exception, none occurred"); } catch (Exception $e) { $this->assertTrue($e instanceof dml_exception); } // update records $sql = "UPDATE {{$tablename1}}\n SET course = 6\n WHERE course = ?"; $this->assertTrue($DB->execute($sql, array('3'))); $this->assertEqual($DB->count_records($tablename1, array('course' => 6)), 2); // update records with subquery condition // confirm that the option not using table aliases is cross-db $sql = "UPDATE {{$tablename1}}\n SET course = 0\n WHERE NOT EXISTS (\n SELECT course\n FROM {{$tablename2}} tbl2\n WHERE tbl2.course = {{$tablename1}}.course\n AND 1 = 0)"; // Really we don't update anything, but verify the syntax is allowed $this->assertTrue($DB->execute($sql)); // insert from one into second table $sql = "INSERT INTO {{$tablename2}} (course)\n\n SELECT course\n FROM {{$tablename1}}"; $this->assertTrue($DB->execute($sql)); $this->assertEqual($DB->count_records($tablename2), 4); }
public function test_concurrent_temp_tables() { $DB = $this->tdb; // do not use global $DB! $dbman = $this->tdb->get_manager(); // Define 2 records $record1 = (object) array('course' => 1, 'secondname' => '11 important', 'intro' => '111 important'); $record2 = (object) array('course' => 2, 'secondname' => '22 important', 'intro' => '222 important'); // Create temp table1 and insert 1 record (in DB) $table = $this->tables['test_table1']; $dbman->create_temp_table($table); $this->assertTrue($dbman->table_exists('test_table1')); $inserted = $DB->insert_record('test_table1', $record1); // Switch to new connection $cfg = $DB->export_dbconfig(); if (!isset($cfg->dboptions)) { $cfg->dboptions = array(); } $DB2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); $DB2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); $dbman2 = $DB2->get_manager(); $this->assertFalse($dbman2->table_exists('test_table1')); // Temp table not exists in DB2 // Create temp table1 and insert 1 record (in DB2) $table = $this->tables['test_table1']; $dbman2->create_temp_table($table); $this->assertTrue($dbman2->table_exists('test_table1')); $inserted = $DB2->insert_record('test_table1', $record2); $dbman2->drop_temp_table($table); // Drop temp table before closing DB2 $this->assertFalse($dbman2->table_exists('test_table1')); $DB2->dispose(); // Close DB2 $this->assertTrue($dbman->table_exists('test_table1')); // Check table continues existing for DB $dbman->drop_temp_table($table); // Drop temp table $this->assertFalse($dbman->table_exists('test_table1')); }