From: skodak Date: Tue, 4 Nov 2008 23:07:14 +0000 (+0000) Subject: MDL-17129 dml: implemented exceptions in new drivers, yay! X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=c23b0ea11bf53ca80bfae8d1211379912d1e708a;p=moodle.git MDL-17129 dml: implemented exceptions in new drivers, yay! --- diff --git a/lib/accesslib.php b/lib/accesslib.php index 0564786268..c37780b0a6 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -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; diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 851d15c382..6a33192acd 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -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); + } } /** diff --git a/lib/setup.php b/lib/setup.php index 73e387a81a..b0b3de0315 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -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 diff --git a/lib/weblib.php b/lib/weblib.php index edc7b2fdea..34607c7435 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -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 = ''; @@ -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 = ''; $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, » 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'); }