From: skodak Date: Tue, 2 Sep 2008 20:32:03 +0000 (+0000) Subject: MDL-14589 minor database driver loading refactoring X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=8aff848213fc2efe4a2047f936631cc1dbe73bba;p=moodle.git MDL-14589 minor database driver loading refactoring --- diff --git a/lib/datalib.php b/lib/datalib.php index 5445860e73..7f422292b7 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -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 = '

'.$dberr.'

'; - } - 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. diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 470d082743..8539eba448 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -1,6 +1,7 @@ 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(); } /** diff --git a/lib/dmllib.php b/lib/dmllib.php index b8ff1e692e..64a39ae80e 100644 --- a/lib/dmllib.php +++ b/lib/dmllib.php @@ -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 = '

'.$dberr.'

'; + } + 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; +} diff --git a/lib/setup.php b/lib/setup.php index 3d2c82902a..f408b0de9f 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -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 diff --git a/lib/simpletest/fixtures/gradetest.php b/lib/simpletest/fixtures/gradetest.php index ee02310d43..380d42e22a 100644 --- a/lib/simpletest/fixtures/gradetest.php +++ b/lib/simpletest/fixtures/gradetest.php @@ -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); }