2006-08-18 22:56:08 +00:00
|
|
|
<?php // $Id$
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
|
|
|
// http://moodle.com //
|
|
|
|
// //
|
2007-10-12 19:13:18 +00:00
|
|
|
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
|
2006-08-18 22:56:08 +00:00
|
|
|
// //
|
|
|
|
// 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 //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2006-09-20 21:00:45 +00:00
|
|
|
/// This library contains all the Data Manipulation Language (DML) functions
|
2006-08-18 22:56:08 +00:00
|
|
|
/// used to interact with the DB. All the dunctions in this library must be
|
|
|
|
/// generic and work against the major number of RDBMS possible. This is the
|
|
|
|
/// list of currently supported and tested DBs: mysql, postresql, mssql, oracle
|
|
|
|
|
2006-08-19 02:58:07 +00:00
|
|
|
/// This library is automatically included by Moodle core so you never need to
|
|
|
|
/// include it yourself.
|
2006-08-18 22:56:08 +00:00
|
|
|
|
|
|
|
/// For more info about the functions available in this library, please visit:
|
|
|
|
/// http://docs.moodle.org/en/DML_functions
|
|
|
|
/// (feel free to modify, improve and document such page, thanks!)
|
|
|
|
|
|
|
|
/// GLOBAL CONSTANTS /////////////////////////////////////////////////////////
|
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
/**
|
|
|
|
* Bitmask, indicates only :name type parameters are supported by db backend.
|
|
|
|
*/
|
|
|
|
define('SQL_PARAMS_NAMED', 1);
|
2006-08-18 22:56:08 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
/**
|
|
|
|
* Bitmask, indicates only ? type parameters are supported by db backend.
|
|
|
|
*/
|
|
|
|
define('SQL_PARAMS_QM', 2);
|
2006-08-18 22:56:08 +00:00
|
|
|
|
|
|
|
/**
|
2008-05-15 21:40:00 +00:00
|
|
|
* Bitmask, indicates only $1, $2.. type parameters are supported by db backend.
|
2006-08-18 22:56:08 +00:00
|
|
|
*/
|
2008-05-21 14:59:33 +00:00
|
|
|
define('SQL_PARAMS_DOLLAR', 4);
|
2006-08-18 22:56:08 +00:00
|
|
|
|
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
/**
|
|
|
|
* Sets up global $DB moodle_database instance
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function setup_DB() {
|
|
|
|
global $CFG, $DB;
|
2006-08-18 22:56:08 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
if (isset($DB)) {
|
|
|
|
return;
|
2006-08-18 22:56:08 +00:00
|
|
|
}
|
2008-05-15 21:40:00 +00:00
|
|
|
|
|
|
|
if (!isset($CFG->dbuser)) {
|
|
|
|
$CFG->dbuser = '';
|
2008-01-07 01:54:28 +00:00
|
|
|
}
|
2006-08-18 22:56:08 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
if (!isset($CFG->dbpass)) {
|
|
|
|
$CFG->dbpass = '';
|
2007-01-26 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
if (!isset($CFG->dbname)) {
|
|
|
|
$CFG->dbname = '';
|
|
|
|
}
|
2006-08-18 22:56:08 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
if (!isset($CFG->dbpersist)) {
|
|
|
|
$CFG->dbpersist = false;
|
|
|
|
}
|
2006-08-18 22:56:08 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
if (!isset($CFG->dblibrary)) {
|
|
|
|
$CFG->dblibrary = 'adodb';
|
|
|
|
}
|
2006-08-18 22:56:08 +00:00
|
|
|
|
2008-05-19 18:02:33 +00:00
|
|
|
if (!isset($CFG->dboptions)) {
|
|
|
|
$CFG->dboptions = array();
|
|
|
|
}
|
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
if ($CFG->dblibrary == 'adodb') {
|
|
|
|
$classname = $CFG->dbtype.'_adodb_moodle_database';
|
|
|
|
require_once($CFG->libdir.'/dml/'.$classname.'.php');
|
2008-05-19 18:02:33 +00:00
|
|
|
$DB = new $classname();
|
2006-08-18 22:56:08 +00:00
|
|
|
|
|
|
|
} else {
|
2008-05-15 21:40:00 +00:00
|
|
|
error('Not implemented db library yet: '.$CFG->dblibrary);
|
2006-08-18 22:56:08 +00:00
|
|
|
}
|
2006-09-22 20:05:44 +00:00
|
|
|
|
2008-05-15 21:40:00 +00:00
|
|
|
$CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
|
2006-09-22 20:05:44 +00:00
|
|
|
|
2008-05-19 18:02:33 +00:00
|
|
|
$driverstatus = $DB->driver_installed();
|
|
|
|
|
|
|
|
if ($driverstatus !== true) {
|
|
|
|
print_error('dbdriverproblem', 'error', '', $driverstatus);
|
|
|
|
}
|
2006-08-18 22:56:08 +00:00
|
|
|
|
2008-05-19 18:02:33 +00:00
|
|
|
if (debugging('', DEBUG_ALL)) {
|
|
|
|
// catch errors
|
|
|
|
ob_start();
|
|
|
|
} else {
|
|
|
|
$prevdebug = error_reporting(0);
|
|
|
|
}
|
|
|
|
if (!$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, $CFG->prefix, $CFG->dboptions)) {
|
|
|
|
if (debugging('', DEBUG_ALL)) {
|
|
|
|
if ($dberr = ob_get_contents()) {
|
|
|
|
$dberr = '<p><em>'.$dberr.'</em></p>';
|
|
|
|
}
|
|
|
|
ob_end_clean();
|
|
|
|
} else {
|
|
|
|
$dberr = '';
|
|
|
|
}
|
2008-05-15 21:40:00 +00:00
|
|
|
if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
|
2008-05-19 18:02:33 +00:00
|
|
|
@mail($CFG->emailconnectionerrorsto,
|
|
|
|
'WARNING: Database connection error: '.$CFG->wwwroot,
|
|
|
|
'Connection error: '.$CFG->wwwroot);
|
2008-05-15 21:40:00 +00:00
|
|
|
}
|
2008-05-19 18:02:33 +00:00
|
|
|
print_error('dbconnectionfailed', 'error', '', $dberr);
|
|
|
|
}
|
2008-06-08 15:25:04 +00:00
|
|
|
if (debugging('', DEBUG_ALL)) {
|
2008-05-19 18:02:33 +00:00
|
|
|
ob_end_clean();
|
|
|
|
} else {
|
|
|
|
error_reporting($prevdebug);
|
2008-05-15 21:40:00 +00:00
|
|
|
}
|
2006-09-22 20:05:44 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2006-08-18 22:56:08 +00:00
|
|
|
|