3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
26 /// This library contains all the Data Manipulation Language (DML) functions
27 /// used to interact with the DB. All the dunctions in this library must be
28 /// generic and work against the major number of RDBMS possible. This is the
29 /// list of currently supported and tested DBs: mysql, postresql, mssql, oracle
31 /// This library is automatically included by Moodle core so you never need to
32 /// include it yourself.
34 /// For more info about the functions available in this library, please visit:
35 /// http://docs.moodle.org/en/DML_functions
36 /// (feel free to modify, improve and document such page, thanks!)
38 require_once($CFG->libdir.'/dml/moodle_database.php');
41 * DML exception class, use instead of error() in dml code.
43 class dml_exception extends moodle_exception {
44 function __construct($errorcode, $a=NULL, $debuginfo=null) {
45 parent::__construct($errorcode, '', '', $a, $debuginfo);
50 * DML db connection exception - triggered if database not accessible.
52 class dml_connection_exception extends dml_exception {
53 function __construct($error) {
54 $errorinfo = '<em>'.s($error).'</em>';
55 parent::__construct('dbconnectionfailed', NULL, $errorinfo);
60 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
62 class dml_read_exception extends dml_exception {
67 function __construct($error, $sql=null, array $params=null) {
68 $this->error = $error;
70 $this->params = $params;
71 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
72 parent::__construct('dmlreadexception', NULL, $errorinfo);
77 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
79 class dml_write_exception extends dml_exception {
84 function __construct($error, $sql=null, array $params=null) {
85 $this->error = $error;
87 $this->params = $params;
88 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
89 parent::__construct('dmlwriteexception', NULL, $errorinfo);
94 * Sets up global $DB moodle_database instance
104 if (!isset($CFG->dbuser)) {
108 if (!isset($CFG->dbpass)) {
112 if (!isset($CFG->dbname)) {
116 if (!isset($CFG->dblibrary)) {
117 switch ($CFG->dbtype) {
119 $CFG->dbtype = 'pgsql';
120 // continue, no break here
122 $CFG->dblibrary = 'native';
126 if (!extension_loaded('mysqli')) {
127 $CFG->dblibrary = 'adodb';
130 $CFG->dbtype = 'mysqli';
131 // continue, no break here
133 $CFG->dblibrary = 'native';
137 // the rest of drivers is not converted yet - keep adodb for now
138 $CFG->dblibrary = 'adodb';
142 if (!isset($CFG->dboptions)) {
143 $CFG->dboptions = array();
146 if (isset($CFG->dbpersist)) {
147 $CFG->dboptions['dbpersist'] = $CFG->dbpersist;
150 if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) {
151 throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
155 $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
156 } catch (moodle_exception $e) {
157 if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
158 if (file_exists($CFG->dataroot.'/emailcount')){
159 $fp = @fopen($CFG->dataroot.'/emailcount', 'r');
160 $content = @fread($fp, 24);
162 if((time() - (int)$content) > 600){
163 @mail($CFG->emailconnectionerrorsto,
164 'WARNING: Database connection error: '.$CFG->wwwroot,
165 'Connection error: '.$CFG->wwwroot);
166 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
167 @fwrite($fp, time());
170 @mail($CFG->emailconnectionerrorsto,
171 'WARNING: Database connection error: '.$CFG->wwwroot,
172 'Connection error: '.$CFG->wwwroot);
173 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
174 @fwrite($fp, time());
177 // rethrow the exception
181 $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now