]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14589 minor database driver loading refactoring
authorskodak <skodak>
Tue, 2 Sep 2008 20:32:03 +0000 (20:32 +0000)
committerskodak <skodak>
Tue, 2 Sep 2008 20:32:03 +0000 (20:32 +0000)
lib/datalib.php
lib/dml/moodle_database.php
lib/dmllib.php
lib/setup.php
lib/simpletest/fixtures/gradetest.php

index 5445860e73d36ea5355b1c1e9628e050b8365a04..7f422292b78ab6af119b494fdd1fd9e2d2198441 100644 (file)
 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.
index 470d082743fd206bf84e9068504a32b75cb784c6..8539eba44818dd9de4460621c5c97dcc740de36e 100644 (file)
@@ -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();
     }
 
     /**
index b8ff1e692e1946f280a1c6a16cb5aa98d3ca21f3..64a39ae80ec2ce0b1f44554a86f254da0fd397d2 100644 (file)
 ///     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;
+}
index 3d2c82902a547536670ec1a72b4e48e4ce37b702..f408b0de9fc64fb411e66f5cdd989f9e9da74462 100644 (file)
@@ -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
index ee02310d4362b9956929c165cbb003cfda4b06d0..380d42e22aeda734c3e6a81659dc74491d854102 100644 (file)
@@ -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);
         }