]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17129 dml: implemented exceptions in new drivers, yay!
authorskodak <skodak>
Tue, 4 Nov 2008 23:07:14 +0000 (23:07 +0000)
committerskodak <skodak>
Tue, 4 Nov 2008 23:07:14 +0000 (23:07 +0000)
lib/accesslib.php
lib/dml/moodle_database.php
lib/setup.php
lib/weblib.php

index 05647862688d12cd994083b11a0f62e723b0da13..c37780b0a69dc98b27f3b9d0c41444999a7f179b 100755 (executable)
@@ -2106,7 +2106,14 @@ function get_system_context($cache=true) {
         return $cached;
     }
 
-    if (!$context = $DB->get_record('context', array('contextlevel'=>CONTEXT_SYSTEM))) {
+    try {
+        $context = $DB->get_record('context', array('contextlevel'=>CONTEXT_SYSTEM));
+    } catch (dml_read_exception $e) {
+        //table does not exist yet, sorry
+        return null;
+    }
+
+    if (!$context) {
         $context = new object();
         $context->contextlevel = CONTEXT_SYSTEM;
         $context->instanceid   = 0;
index 851d15c3824dea302627d89763d075cafc579ac5..6a33192acdf7301044eea4077a2a01867a47717d 100644 (file)
@@ -68,7 +68,10 @@ abstract class moodle_database {
      */
     protected $writes = 0;
 
-    // TODO: do we really need record caching??
+    protected $last_sql;
+    protected $last_params;
+    protected $last_type;
+    protected $last_extrainfo;
 
     /**
      * Contructor - instantiates the database, specifying if it's external (connect to other systems) or no (Moodle DB)
@@ -247,6 +250,11 @@ abstract class moodle_database {
      * @return void
      */
     protected function query_start($sql, array $params=null, $type, $extrainfo=null) {
+        $this->last_sql       = $sql;
+        $this->last_params    = $params;
+        $this->last_type      = $type;
+        $this->last_extrainfo = $extrainfo;
+
         switch ($type) {
             case SQL_QUERY_SELECT:
             case SQL_QUERY_AUX:
@@ -257,7 +265,8 @@ abstract class moodle_database {
             case SQL_QUERY_STRUCTURE:
                 $this->writes++;
         }
-        //TODO
+
+        $this->print_debug($sql, $params);
     }
 
     /**
@@ -266,7 +275,19 @@ abstract class moodle_database {
      * @return void
      */
     protected function query_end($result) {
-        //TODO
+        if ($result !== false) {
+            return;
+        }
+
+        switch ($this->last_type) {
+            case SQL_QUERY_SELECT:
+            case SQL_QUERY_AUX:
+                throw new dml_read_exception($this->get_last_error(), $this->last_sql, $this->last_params);
+            case SQL_QUERY_INSERT:
+            case SQL_QUERY_UPDATE:
+            case SQL_QUERY_STRUCTURE:
+                throw new dml_write_exception($this->get_last_error(), $this->last_sql, $this->last_params);
+        }
     }
 
     /**
index 73e387a81a68fda98949c7366f0bff304c2dc994..b0b3de031555847fdae33a3abfdb45c6e163eea5 100644 (file)
@@ -160,7 +160,11 @@ global $HTTPSPAGEREQUIRED;
     }
 
 /// Load up any configuration from the config table
-    $CFG = get_config();
+    try {
+        $CFG = get_config();
+    } catch (dml_read_exception $e) {
+        // most probably empty db, going to install soon
+    }
 
 /// Verify upgrade is not running unless we are in a script that needs to execute in any case
     if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) {
@@ -191,7 +195,13 @@ global $HTTPSPAGEREQUIRED;
     }
 
 /// Defining the site
-    if ($SITE = get_site()) {
+    try {
+        $SITE = get_site();
+    } catch (dml_read_exception $e) {
+        $SITE = null;
+    }
+
+    if ($SITE) {
         /**
          * If $SITE global from {@link get_site()} is set then SITEID to $SITE->id, otherwise set to 1.
          */
@@ -210,7 +220,11 @@ global $HTTPSPAGEREQUIRED;
 
     // define SYSCONTEXTID in config.php if you want to save some queries (after install or upgrade!)
     if (!defined('SYSCONTEXTID')) {
-        get_system_context();
+        try {
+            get_system_context();
+        } catch (dml_read_exception $e) {
+            // not available yet
+        }
     }
 
 /// Set error reporting back to normal
index edc7b2fdea019f6fbe651713215433d5d4a93149..34607c743522a0226a34785cb7b2ff86c38c0cc4 100644 (file)
@@ -2928,7 +2928,7 @@ function print_header_simple($title='', $heading='', $navigation='', $focus='',
  * @return mixed string or void
  */
 function print_footer($course=NULL, $usercourse=NULL, $return=false) {
-    global $USER, $CFG, $THEME, $COURSE;
+    global $USER, $CFG, $THEME, $COURSE, $SITE;
 
     if (defined('ADMIN_EXT_HEADER_PRINTED') and !defined('ADMIN_EXT_FOOTER_PRINTED')) {
         admin_externalpage_print_footer();
@@ -2962,7 +2962,7 @@ function print_footer($course=NULL, $usercourse=NULL, $return=false) {
             $home  = false;
 
         } else if ($course === 'home') {   // special case for site home page - please do not remove
-            $course = get_site();
+            $course = $SITE;
             $homelink  = '<div class="sitelink">'.
                '<a title="Moodle '. $CFG->release .'" href="http://moodle.org/">'.
                '<img style="width:100px;height:30px" src="pix/moodlelogo.gif" alt="moodlelogo" /></a></div>';
@@ -2975,7 +2975,7 @@ function print_footer($course=NULL, $usercourse=NULL, $return=false) {
         }
 
     } else {
-        $course = get_site();  // Set course as site course by default
+        $course = $SITE;  // Set course as site course by default
         $homelink = '<div class="homelink"><a '.$CFG->frametarget.' href="'.$CFG->wwwroot.'/">'.get_string('home').'</a></div>';
         $home  = false;
     }
@@ -3651,7 +3651,7 @@ function get_separator() {
  * @param boolean $return False to echo the breadcrumb string (default), true to return it.
  */
 function print_navigation ($navigation, $separator=0, $return=false) {
-    global $CFG, $THEME;
+    global $CFG, $THEME, $SITE;
     $output = '';
 
     if (0 === $separator) {
@@ -3689,9 +3689,11 @@ function print_navigation ($navigation, $separator=0, $return=false) {
             }
         }
 
-        if (! $site = get_site()) {
+        if (!$SITE) {
             $site = new object();
             $site->shortname = get_string('home');
+        } else {
+            $site = $SITE;
         }
 
         //Accessibility: breadcrumb links now in a list, &raquo; replaced with a 'silent' character.
@@ -3773,7 +3775,7 @@ function print_navigation ($navigation, $separator=0, $return=false) {
  *      navigation strings.
  */
 function build_navigation($extranavlinks, $cm = null) {
-    global $CFG, $COURSE, $DB;
+    global $CFG, $COURSE, $DB, $SITE;
 
     if (is_string($extranavlinks)) {
         if ($extranavlinks == '') {
@@ -3786,9 +3788,9 @@ function build_navigation($extranavlinks, $cm = null) {
     $navlinks = array();
 
     //Site name
-    if ($site = get_site()) {
+    if ($SITE) {
         $navlinks[] = array(
-                'name' => format_string($site->shortname),
+                'name' => format_string($SITE->shortname),
                 'link' => "$CFG->wwwroot/",
                 'type' => 'home');
     }