3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This library contains all the Data Manipulation Language (DML) functions
20 * used to interact with the DB
22 * This library contains all the Data Manipulation Language (DML) functions
23 * used to interact with the DB. All the dunctions in this library must be
24 * generic and work against the major number of RDBMS possible. This is the
25 * list of currently supported and tested DBs: mysql, postresql, mssql, oracle
27 * This library is automatically included by Moodle core so you never need to
28 * include it yourself.
30 * For more info about the functions available in this library, please visit:
31 * http://docs.moodle.org/en/DML_functions
32 * (feel free to modify, improve and document such page, thanks!)
34 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 /** Require the essential */
40 require_once($CFG->libdir.'/dml/moodle_database.php');
43 * DML exception class, use instead of error() in dml code.
45 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49 class dml_exception extends moodle_exception {
51 * @param string $errorcode
53 * @param string $debuginfo
55 function __construct($errorcode, $a=NULL, $debuginfo=null) {
56 parent::__construct($errorcode, '', '', $a, $debuginfo);
61 * DML db connection exception - triggered if database not accessible.
63 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
64 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
67 class dml_connection_exception extends dml_exception {
69 * @param string $error
71 function __construct($error) {
72 $errorinfo = '<em>'.s($error).'</em>';
73 parent::__construct('dbconnectionfailed', NULL, $errorinfo);
78 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
80 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
81 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
84 class dml_read_exception extends dml_exception {
92 * @param string $error
94 * @param array $params
96 function __construct($error, $sql=null, array $params=null) {
97 $this->error = $error;
99 $this->params = $params;
100 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
101 parent::__construct('dmlreadexception', NULL, $errorinfo);
106 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
108 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
109 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
110 * @package moodlecore
112 class dml_write_exception extends dml_exception {
120 * @param string $error
122 * @param array $params
124 function __construct($error, $sql=null, array $params=null) {
125 $this->error = $error;
127 $this->params = $params;
128 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
129 parent::__construct('dmlwriteexception', NULL, $errorinfo);
134 * Sets up global $DB moodle_database instance
140 function setup_DB() {
147 if (!isset($CFG->dbuser)) {
151 if (!isset($CFG->dbpass)) {
155 if (!isset($CFG->dbname)) {
159 if (!isset($CFG->dblibrary)) {
160 switch ($CFG->dbtype) {
162 $CFG->dbtype = 'pgsql';
163 // continue, no break here
165 $CFG->dblibrary = 'native';
169 if (!extension_loaded('mysqli')) {
170 $CFG->dblibrary = 'adodb';
173 $CFG->dbtype = 'mysqli';
174 // continue, no break here
176 $CFG->dblibrary = 'native';
180 // the rest of drivers is not converted yet - keep adodb for now
181 $CFG->dblibrary = 'adodb';
185 if (!isset($CFG->dboptions)) {
186 $CFG->dboptions = array();
189 if (isset($CFG->dbpersist)) {
190 $CFG->dboptions['dbpersist'] = $CFG->dbpersist;
193 if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) {
194 throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
198 $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
199 } catch (moodle_exception $e) {
200 if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
201 if (file_exists($CFG->dataroot.'/emailcount')){
202 $fp = @fopen($CFG->dataroot.'/emailcount', 'r');
203 $content = @fread($fp, 24);
205 if((time() - (int)$content) > 600){
206 @mail($CFG->emailconnectionerrorsto,
207 'WARNING: Database connection error: '.$CFG->wwwroot,
208 'Connection error: '.$CFG->wwwroot);
209 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
210 @fwrite($fp, time());
213 @mail($CFG->emailconnectionerrorsto,
214 'WARNING: Database connection error: '.$CFG->wwwroot,
215 'Connection error: '.$CFG->wwwroot);
216 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
217 @fwrite($fp, time());
220 // rethrow the exception
224 $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now