mirror of
https://github.com/moodle/moodle.git
synced 2025-04-18 06:58:08 +02:00
MDL-14589 minor database driver loading refactoring
This commit is contained in:
parent
988fc25546
commit
8aff848213
lib
@ -14,99 +14,6 @@
|
||||
define('MAX_COURSES_IN_CATEGORY', 10000); // MAX_COURSES_IN_CATEGORY * MAX_COURSE_CATEGORIES must not be more than max integer!
|
||||
define('MAX_COURSE_CATEGORIES', 10000);
|
||||
|
||||
/**
|
||||
* Sets up global $DB moodle_database instance
|
||||
* @return void
|
||||
*/
|
||||
function setup_DB() {
|
||||
global $CFG, $DB;
|
||||
|
||||
if (isset($DB)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($CFG->dbuser)) {
|
||||
$CFG->dbuser = '';
|
||||
}
|
||||
|
||||
if (!isset($CFG->dbpass)) {
|
||||
$CFG->dbpass = '';
|
||||
}
|
||||
|
||||
if (!isset($CFG->dbname)) {
|
||||
$CFG->dbname = '';
|
||||
}
|
||||
|
||||
if (!isset($CFG->dbpersist)) {
|
||||
$CFG->dbpersist = false;
|
||||
}
|
||||
|
||||
if (!isset($CFG->dblibrary)) {
|
||||
$CFG->dblibrary = 'adodb';
|
||||
}
|
||||
|
||||
if (!isset($CFG->dboptions)) {
|
||||
$CFG->dboptions = array();
|
||||
}
|
||||
|
||||
$classname = $CFG->dbtype.'_'.$CFG->dblibrary.'_moodle_database';
|
||||
require_once($CFG->libdir.'/dml/'.$classname.'.php');
|
||||
$DB = new $classname();
|
||||
|
||||
$CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
|
||||
|
||||
$driverstatus = $DB->driver_installed();
|
||||
|
||||
if ($driverstatus !== true) {
|
||||
print_error('dbdriverproblem', 'error', '', $driverstatus);
|
||||
}
|
||||
|
||||
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 = '';
|
||||
}
|
||||
if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
|
||||
if (file_exists($CFG->dataroot.'/emailcount')){
|
||||
$fp = fopen($CFG->dataroot.'/emailcount', 'r');
|
||||
$content = fread($fp, 24);
|
||||
fclose($fp);
|
||||
if((time() - (int)$content) > 600){
|
||||
@mail($CFG->emailconnectionerrorsto,
|
||||
'WARNING: Database connection error: '.$CFG->wwwroot,
|
||||
'Connection error: '.$CFG->wwwroot);
|
||||
$fp = fopen($CFG->dataroot.'/emailcount', 'w');
|
||||
fwrite($fp, time());
|
||||
}
|
||||
} else {
|
||||
@mail($CFG->emailconnectionerrorsto,
|
||||
'WARNING: Database connection error: '.$CFG->wwwroot,
|
||||
'Connection error: '.$CFG->wwwroot);
|
||||
$fp = fopen($CFG->dataroot.'/emailcount', 'w');
|
||||
fwrite($fp, time());
|
||||
}
|
||||
}
|
||||
print_error('dbconnectionfailed', 'error', '', $dberr);
|
||||
}
|
||||
if (debugging('', DEBUG_ALL)) {
|
||||
ob_end_clean();
|
||||
} else {
|
||||
error_reporting($prevdebug);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Some constants
|
||||
define('LASTACCESS_UPDATE_SECS', 60); /// Number of seconds to wait before
|
||||
/// updating lastaccess information in DB.
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php //$Id$
|
||||
|
||||
require_once($CFG->libdir.'/dml/database_column_info.php');
|
||||
require_once($CFG->libdir.'/dml/moodle_recordset.php');
|
||||
|
||||
/// GLOBAL CONSTANTS /////////////////////////////////////////////////////////
|
||||
|
||||
@ -86,16 +87,23 @@ abstract class moodle_database {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and returns a driver instance with the specified type and library.
|
||||
* Loads and returns a database instance with the specified type and library.
|
||||
* @param string $type database type of the driver (mysql, postgres7, mssql, etc)
|
||||
* @param string $library database library of the driver (adodb, pdo, etc)
|
||||
* @return moodle_database driver object
|
||||
* @param string $library database library of the driver (adodb, pdo, native, etc)
|
||||
* @return moodle_database driver object or null if error
|
||||
*/
|
||||
public static function get_driver($type, $library = 'adodb') {
|
||||
public static function get_driver_instance($type, $library) {
|
||||
global $CFG;
|
||||
$classname = $type . '_' . $library . '_moodle_database';
|
||||
require_once ("$CFG->libdir/dml/$classname.php");
|
||||
return new $classname ();
|
||||
|
||||
$classname = $type.'_'.$library.'_moodle_database';
|
||||
$libfile = "$CFG->libdir/dml/$classname.php";
|
||||
|
||||
if (!file_exists($libfile)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
require_once($libfile);
|
||||
return new $classname();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,3 +35,95 @@
|
||||
/// http://docs.moodle.org/en/DML_functions
|
||||
/// (feel free to modify, improve and document such page, thanks!)
|
||||
|
||||
require_once($CFG->libdir.'/dml/moodle_database.php');
|
||||
|
||||
/**
|
||||
* Sets up global $DB moodle_database instance
|
||||
* @return void
|
||||
*/
|
||||
function setup_DB() {
|
||||
global $CFG, $DB;
|
||||
|
||||
if (isset($DB)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($CFG->dbuser)) {
|
||||
$CFG->dbuser = '';
|
||||
}
|
||||
|
||||
if (!isset($CFG->dbpass)) {
|
||||
$CFG->dbpass = '';
|
||||
}
|
||||
|
||||
if (!isset($CFG->dbname)) {
|
||||
$CFG->dbname = '';
|
||||
}
|
||||
|
||||
if (!isset($CFG->dbpersist)) {
|
||||
$CFG->dbpersist = false;
|
||||
}
|
||||
|
||||
if (!isset($CFG->dblibrary)) {
|
||||
$CFG->dblibrary = 'adodb';
|
||||
}
|
||||
|
||||
if (!isset($CFG->dboptions)) {
|
||||
$CFG->dboptions = array();
|
||||
}
|
||||
|
||||
$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
|
||||
|
||||
$CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
|
||||
|
||||
$driverstatus = $DB->driver_installed();
|
||||
|
||||
if ($driverstatus !== true) {
|
||||
print_error('dbdriverproblem', 'error', '', $driverstatus);
|
||||
}
|
||||
|
||||
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 = '';
|
||||
}
|
||||
if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
|
||||
if (file_exists($CFG->dataroot.'/emailcount')){
|
||||
$fp = fopen($CFG->dataroot.'/emailcount', 'r');
|
||||
$content = fread($fp, 24);
|
||||
fclose($fp);
|
||||
if((time() - (int)$content) > 600){
|
||||
@mail($CFG->emailconnectionerrorsto,
|
||||
'WARNING: Database connection error: '.$CFG->wwwroot,
|
||||
'Connection error: '.$CFG->wwwroot);
|
||||
$fp = fopen($CFG->dataroot.'/emailcount', 'w');
|
||||
fwrite($fp, time());
|
||||
}
|
||||
} else {
|
||||
@mail($CFG->emailconnectionerrorsto,
|
||||
'WARNING: Database connection error: '.$CFG->wwwroot,
|
||||
'Connection error: '.$CFG->wwwroot);
|
||||
$fp = fopen($CFG->dataroot.'/emailcount', 'w');
|
||||
fwrite($fp, time());
|
||||
}
|
||||
}
|
||||
print_error('dbconnectionfailed', 'error', '', $dberr);
|
||||
}
|
||||
if (debugging('', DEBUG_ALL)) {
|
||||
ob_end_clean();
|
||||
} else {
|
||||
error_reporting($prevdebug);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -127,6 +127,7 @@ global $HTTPSPAGEREQUIRED;
|
||||
/// Load up standard libraries
|
||||
require_once($CFG->libdir .'/textlib.class.php'); // Functions to handle multibyte strings
|
||||
require_once($CFG->libdir .'/weblib.php'); // Functions for producing HTML
|
||||
require_once($CFG->libdir .'/dmllib.php'); // Database access
|
||||
require_once($CFG->libdir .'/datalib.php'); // Legacy lib with a big-mix of functions.
|
||||
require_once($CFG->libdir .'/accesslib.php'); // Access control functions
|
||||
require_once($CFG->libdir .'/deprecatedlib.php'); // Deprecated functions included for backward compatibility
|
||||
|
@ -87,7 +87,7 @@ class grade_test extends UnitTestCase {
|
||||
|
||||
if (is_null(grade_test::$db)) {
|
||||
$this->realdb = $DB;
|
||||
grade_test::$db = moodle_database::get_driver($CFG->dbtype, $CFG->dblibrary);
|
||||
grade_test::$db = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
|
||||
grade_test::$db->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, "tst_", $CFG->dboptions);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user