2006-08-15 09:08:17 +00:00
|
|
|
<?php // $Id$
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
|
|
|
// http://moodle.com //
|
|
|
|
// //
|
2007-11-07 17:59:33 +00:00
|
|
|
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
|
2006-08-15 09:08:17 +00:00
|
|
|
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details: //
|
|
|
|
// //
|
|
|
|
// http://www.gnu.org/copyleft/gpl.html //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// This library includes all the required functions used to handle the DB
|
2006-08-18 22:56:08 +00:00
|
|
|
// structure (DDL) independently of the underlying RDBMS in use. All the functions
|
2006-09-20 21:00:45 +00:00
|
|
|
// rely on the XMLDBDriver classes to be able to generate the correct SQL
|
2006-08-15 09:08:17 +00:00
|
|
|
// syntax needed by each DB.
|
|
|
|
//
|
2006-09-20 21:00:45 +00:00
|
|
|
// To define any structure to be created we'll use the schema defined
|
2006-08-15 09:08:17 +00:00
|
|
|
// by the XMLDB classes, for tables, fields, indexes, keys and other
|
|
|
|
// statements instead of direct handling of SQL sentences.
|
|
|
|
//
|
|
|
|
// This library should be used, exclusively, by the installation and
|
|
|
|
// upgrade process of Moodle.
|
|
|
|
//
|
|
|
|
// For further documentation, visit http://docs.moodle.org/en/DDL_functions
|
|
|
|
|
2006-08-17 16:59:14 +00:00
|
|
|
/// Add required XMLDB constants
|
2008-05-20 17:06:26 +00:00
|
|
|
require_once($CFG->libdir.'/xmldb/xmldb_constants.php');
|
2006-08-17 16:59:14 +00:00
|
|
|
|
|
|
|
/// Add required XMLDB DB classes
|
2008-05-20 17:06:26 +00:00
|
|
|
require_once($CFG->libdir.'/xmldb/xmldb_object.php');
|
|
|
|
require_once($CFG->libdir.'/xmldb/xmldb_file.php');
|
|
|
|
require_once($CFG->libdir.'/xmldb/xmldb_structure.php');
|
2008-05-20 23:24:40 +00:00
|
|
|
require_once($CFG->libdir.'/xmldb/xmldb_table.php');
|
|
|
|
require_once($CFG->libdir.'/xmldb/xmldb_field.php');
|
|
|
|
require_once($CFG->libdir.'/xmldb/xmldb_key.php');
|
|
|
|
require_once($CFG->libdir.'/xmldb/xmldb_index.php');
|
|
|
|
require_once($CFG->libdir.'/xmldb/xmldb_statement.php');
|
2006-10-09 22:28:22 +00:00
|
|
|
|
2006-08-20 15:50:27 +00:00
|
|
|
/// Add other libraries
|
2008-05-15 21:40:00 +00:00
|
|
|
require_once($CFG->libdir.'/xmlize.php');
|
2008-01-03 15:03:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all plugin tables
|
|
|
|
* @name string name of plugin, used as table prefix
|
|
|
|
* @file string path to install.xml file
|
|
|
|
* @feedback boolean
|
|
|
|
*/
|
|
|
|
function drop_plugin_tables($name, $file, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $CFG, $DB;
|
2008-01-03 15:03:32 +00:00
|
|
|
|
|
|
|
// first try normal delete
|
2008-05-15 21:40:00 +00:00
|
|
|
if ($DB->get_manager()->delete_tables_from_xmldb_file($file, $feedback)) {
|
2008-01-03 15:03:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// then try to find all tables that start with name and are not in any xml file
|
|
|
|
$used_tables = get_used_table_names();
|
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
$tables = $DB->get_tables();
|
|
|
|
|
2008-01-03 15:03:32 +00:00
|
|
|
/// Iterate over, fixing id fields as necessary
|
|
|
|
foreach ($tables as $table) {
|
|
|
|
if (in_array($table, $used_tables)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// found orphan table --> delete it
|
2008-05-15 21:40:00 +00:00
|
|
|
if ($DB->get_manager()->table_exists($table)) {
|
2008-05-20 23:24:40 +00:00
|
|
|
$xmldb_table = new xmldb_table($table);
|
2008-05-15 21:40:00 +00:00
|
|
|
$DB->get_manager()->drop_table($xmldb_table, true, $feedback);
|
2008-01-03 15:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns names of all known tables == tables that moodle knowns about.
|
|
|
|
* @return array of lowercase table names
|
|
|
|
*/
|
|
|
|
function get_used_table_names() {
|
|
|
|
$table_names = array();
|
|
|
|
$dbdirs = get_db_directories();
|
|
|
|
|
|
|
|
foreach ($dbdirs as $dbdir) {
|
|
|
|
$file = $dbdir.'/install.xml';
|
|
|
|
|
2008-05-20 17:06:26 +00:00
|
|
|
$xmldb_file = new xmldb_file($file);
|
2008-01-03 15:03:32 +00:00
|
|
|
|
|
|
|
if (!$xmldb_file->fileExists()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$loaded = $xmldb_file->loadXMLStructure();
|
|
|
|
$structure =& $xmldb_file->getStructure();
|
|
|
|
|
|
|
|
if ($loaded and $tables = $structure->getTables()) {
|
|
|
|
foreach($tables as $table) {
|
|
|
|
$table_names[] = strtolower($table->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $table_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns list of all directories where we expect install.xml files
|
|
|
|
* @return array of paths
|
|
|
|
*/
|
|
|
|
function get_db_directories() {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$dbdirs = array();
|
|
|
|
|
|
|
|
/// First, the main one (lib/db)
|
|
|
|
$dbdirs[] = $CFG->libdir.'/db';
|
|
|
|
|
|
|
|
/// Now, activity modules (mod/xxx/db)
|
|
|
|
if ($plugins = get_list_of_plugins('mod')) {
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/mod/'.$plugin.'/db';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Now, assignment submodules (mod/assignment/type/xxx/db)
|
|
|
|
if ($plugins = get_list_of_plugins('mod/assignment/type')) {
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/mod/assignment/type/'.$plugin.'/db';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Now, question types (question/type/xxx/db)
|
|
|
|
if ($plugins = get_list_of_plugins('question/type')) {
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/question/type/'.$plugin.'/db';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Now, backup/restore stuff (backup/db)
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/backup/db';
|
|
|
|
|
|
|
|
/// Now, block system stuff (blocks/db)
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/blocks/db';
|
|
|
|
|
|
|
|
/// Now, blocks (blocks/xxx/db)
|
|
|
|
if ($plugins = get_list_of_plugins('blocks', 'db')) {
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/blocks/'.$plugin.'/db';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Now, course formats (course/format/xxx/db)
|
|
|
|
if ($plugins = get_list_of_plugins('course/format', 'db')) {
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/course/format/'.$plugin.'/db';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Now, enrolment plugins (enrol/xxx/db)
|
|
|
|
if ($plugins = get_list_of_plugins('enrol', 'db')) {
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/enrol/'.$plugin.'/db';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-26 10:34:51 +00:00
|
|
|
/// Now admin report plugins (admin/report/xxx/db)
|
|
|
|
if ($plugins = get_list_of_plugins($CFG->admin.'/report', 'db')) {
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/'.$CFG->admin.'/report/'.$plugin.'/db';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-03 15:03:32 +00:00
|
|
|
/// Local database changes, if the local folder exists.
|
|
|
|
if (file_exists($CFG->dirroot . '/local')) {
|
|
|
|
$dbdirs[] = $CFG->dirroot.'/local/db';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dbdirs;
|
|
|
|
}
|
|
|
|
|
2006-09-02 23:48:02 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
// DEPRECATED - to be removed soon
|
2006-09-02 23:48:02 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function table_exists($table) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->table_exists($table);
|
|
|
|
}
|
2006-09-02 23:48:02 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function field_exists($table, $field) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->field_exists($table, $field);
|
|
|
|
}
|
2006-09-30 15:19:56 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function find_index_name($table, $index) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->find_index_name($table, $index);
|
|
|
|
}
|
2006-08-20 15:50:27 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function index_exists($table, $index) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->index_exists($table, $index);
|
2006-08-20 15:50:27 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function find_check_constraint_name($table, $field) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->find_check_constraint_name($table, $field);
|
|
|
|
}
|
2006-09-07 17:28:46 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function check_constraint_exists($table, $field) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->check_constraint_exists($table, $field);
|
|
|
|
}
|
2006-09-07 17:28:46 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function find_key_name($table, $xmldb_key) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->find_key_name($table, $xmldb_key);
|
|
|
|
}
|
2006-09-07 17:28:46 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function find_sequence_name($table) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->find_sequence_name($table);
|
|
|
|
}
|
2006-09-07 17:28:46 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function drop_table($table, $continue=true, $feedback=true) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->drop_table($table, $continue, $feedback);
|
|
|
|
}
|
2006-09-30 15:19:56 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function install_from_xmldb_file($file) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->install_from_xmldb_file($file);
|
|
|
|
}
|
2006-09-07 17:28:46 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function create_table($table, $continue=true, $feedback=true) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->create_table($table, $continue, $feedback);
|
2006-09-07 17:28:46 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 20:31:47 +00:00
|
|
|
function create_temp_table($table, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->create_temp_table($table, $continue, $feedback);
|
2007-04-29 20:31:47 +00:00
|
|
|
}
|
|
|
|
|
2006-09-30 22:40:15 +00:00
|
|
|
function rename_table($table, $newname, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->rename_table($table, $newname, $continue, $feedback);
|
2006-09-30 22:40:15 +00:00
|
|
|
}
|
|
|
|
|
2006-09-12 22:23:00 +00:00
|
|
|
function add_field($table, $field, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->add_field($table, $field, $continue, $feedback);
|
2006-09-12 22:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function drop_field($table, $field, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->drop_field($table, $field, $continue, $feedback);
|
2006-09-12 22:23:00 +00:00
|
|
|
}
|
|
|
|
|
2006-09-28 23:14:52 +00:00
|
|
|
function change_field_type($table, $field, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->change_field_type($table, $field, $continue, $feedback);
|
2006-09-23 21:24:51 +00:00
|
|
|
}
|
|
|
|
|
2006-09-28 23:14:52 +00:00
|
|
|
function change_field_precision($table, $field, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->change_field_precision($table, $field, $continue, $feedback);
|
2006-09-28 23:14:52 +00:00
|
|
|
}
|
|
|
|
|
2006-09-23 21:24:51 +00:00
|
|
|
function change_field_unsigned($table, $field, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->change_field_unsigned($table, $field, $continue, $feedback);
|
2006-09-23 21:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function change_field_notnull($table, $field, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->change_field_notnull($table, $field, $continue, $feedback);
|
2006-09-23 21:24:51 +00:00
|
|
|
}
|
|
|
|
|
2006-09-30 17:06:20 +00:00
|
|
|
function change_field_enum($table, $field, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->change_field_enum($table, $field, $continue, $feedback);
|
2006-09-30 17:06:20 +00:00
|
|
|
}
|
2006-09-23 21:24:51 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function change_field_default($table, $field, $continue=true, $feedback=true) {
|
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->change_field_default($table, $field, $continue, $feedback);
|
2006-10-01 23:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function rename_field($table, $field, $newname, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->rename_field($table, $field, $continue, $feedback);
|
2006-09-23 21:24:51 +00:00
|
|
|
}
|
|
|
|
|
2006-09-28 17:30:14 +00:00
|
|
|
function add_key($table, $key, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->add_key($table, $key, $continue, $feedback);
|
2006-09-28 17:30:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function drop_key($table, $key, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->drop_key($table, $key, $continue, $feedback);
|
2006-09-28 17:30:14 +00:00
|
|
|
}
|
|
|
|
|
2006-09-30 19:44:49 +00:00
|
|
|
function rename_key($table, $key, $newname, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->rename_key($table, $key, $newname, $continue, $feedback);
|
2006-09-30 19:44:49 +00:00
|
|
|
}
|
|
|
|
|
2006-09-26 23:17:24 +00:00
|
|
|
function add_index($table, $index, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->add_index($table, $index, $continue, $feedback);
|
2006-09-26 23:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function drop_index($table, $index, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->drop_index($table, $index, $continue, $feedback);
|
2006-09-26 23:17:24 +00:00
|
|
|
}
|
|
|
|
|
2006-09-30 18:24:14 +00:00
|
|
|
function rename_index($table, $index, $newname, $continue=true, $feedback=true) {
|
2008-05-15 21:40:00 +00:00
|
|
|
global $DB;
|
|
|
|
debugging('Deprecated ddllib function used!');
|
|
|
|
return $DB->get_manager()->rename_index($table, $index, $newname, $continue, $feedback);
|
|
|
|
}
|
2006-09-30 18:24:14 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
/// DELETED !!
|
2006-09-30 18:24:14 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
function table_column($table, $oldfield, $field, $type='integer', $size='10',
|
|
|
|
$signed='unsigned', $default='0', $null='not null', $after='') {
|
|
|
|
error('table_column() was removed, please use new ddl functions');
|
2006-09-30 18:24:14 +00:00
|
|
|
}
|
|
|
|
|
2006-10-18 07:26:04 +00:00
|
|
|
|
2008-05-20 17:06:26 +00:00
|
|
|
?>
|