MDL-19676 removing premature commit
[moodle.git] / lib / dmllib.php
CommitLineData
11e1f828
MH
1<?php
2
49926145 3// This file is part of Moodle - http://moodle.org/
4//
11e1f828
MH
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.
9//
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.
49926145 14//
11e1f828
MH
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/>.
7e13be08 17
49926145 18
11e1f828
MH
19/**
20 * This library contains all the Data Manipulation Language (DML) functions
21 * used to interact with the DB
22 *
23 * This library contains all the Data Manipulation Language (DML) functions
24 * used to interact with the DB. All the dunctions in this library must be
25 * generic and work against the major number of RDBMS possible. This is the
26 * list of currently supported and tested DBs: mysql, postresql, mssql, oracle
27
28 * This library is automatically included by Moodle core so you never need to
29 * include it yourself.
30
31 * For more info about the functions available in this library, please visit:
32 * http://docs.moodle.org/en/DML_functions
33 * (feel free to modify, improve and document such page, thanks!)
49926145 34 *
35 * @package moodlecore
36 * @subpackage DML
37 * @copyright 2008 Petr Skoda (http://skodak.org)
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
11e1f828
MH
39 */
40
49926145 41// Require the essential
8aff8482 42require_once($CFG->libdir.'/dml/moodle_database.php');
43
49459eb0 44/**
45 * DML exception class, use instead of error() in dml code.
46 */
47class dml_exception extends moodle_exception {
11e1f828
MH
48 /**
49 * @param string $errorcode
50 * @param string $a
51 * @param string $debuginfo
52 */
49459eb0 53 function __construct($errorcode, $a=NULL, $debuginfo=null) {
54 parent::__construct($errorcode, '', '', $a, $debuginfo);
55 }
56}
57
ce152606 58/**
59 * DML db connection exception - triggered if database not accessible.
60 */
61class dml_connection_exception extends dml_exception {
11e1f828
MH
62 /**
63 * @param string $error
64 */
ce152606 65 function __construct($error) {
66 $errorinfo = '<em>'.s($error).'</em>';
67 parent::__construct('dbconnectionfailed', NULL, $errorinfo);
68 }
69}
70
9214025e 71/**
72 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
73 */
74class dml_read_exception extends dml_exception {
11e1f828 75 /** @var string */
9214025e 76 public $error;
77 public $sql;
11e1f828 78 /** @var array */
9214025e 79 public $params;
11e1f828
MH
80
81 /**
82 * @param string $error
83 * @param string $sql
84 * @param array $params
85 */
9214025e 86 function __construct($error, $sql=null, array $params=null) {
87 $this->error = $error;
88 $this->sql = $sql;
89 $this->params = $params;
90 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
91 parent::__construct('dmlreadexception', NULL, $errorinfo);
92 }
93}
94
95/**
96 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
97 */
98class dml_write_exception extends dml_exception {
11e1f828 99 /** @var string */
9214025e 100 public $error;
101 public $sql;
11e1f828 102 /** @var array */
9214025e 103 public $params;
104
11e1f828
MH
105 /**
106 * @param string $error
107 * @param string $sql
108 * @param array $params
109 */
9214025e 110 function __construct($error, $sql=null, array $params=null) {
111 $this->error = $error;
112 $this->sql = $sql;
113 $this->params = $params;
114 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
115 parent::__construct('dmlwriteexception', NULL, $errorinfo);
116 }
117}
118
8aff8482 119/**
120 * Sets up global $DB moodle_database instance
11e1f828
MH
121 *
122 * @global object
123 * @global object
8aff8482 124 * @return void
125 */
126function setup_DB() {
127 global $CFG, $DB;
128
129 if (isset($DB)) {
130 return;
131 }
132
133 if (!isset($CFG->dbuser)) {
134 $CFG->dbuser = '';
135 }
136
137 if (!isset($CFG->dbpass)) {
138 $CFG->dbpass = '';
139 }
140
141 if (!isset($CFG->dbname)) {
142 $CFG->dbname = '';
143 }
144
8aff8482 145 if (!isset($CFG->dblibrary)) {
3eae57b7 146 switch ($CFG->dbtype) {
147 case 'postgres7' :
148 $CFG->dbtype = 'pgsql';
149 // continue, no break here
150 case 'pgsql' :
151 $CFG->dblibrary = 'native';
152 break;
153
154 case 'mysql' :
a7b4fc7f 155 if (!extension_loaded('mysqli')) {
156 $CFG->dblibrary = 'adodb';
157 break;
158 }
3eae57b7 159 $CFG->dbtype = 'mysqli';
160 // continue, no break here
161 case 'mysqli' :
162 $CFG->dblibrary = 'native';
163 break;
164
165 default:
166 // the rest of drivers is not converted yet - keep adodb for now
167 $CFG->dblibrary = 'adodb';
168 }
8aff8482 169 }
170
171 if (!isset($CFG->dboptions)) {
172 $CFG->dboptions = array();
173 }
174
beaa43db 175 if (isset($CFG->dbpersist)) {
176 $CFG->dboptions['dbpersist'] = $CFG->dbpersist;
177 }
178
3eae57b7 179 if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) {
180 throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
181 }
8aff8482 182
3eae57b7 183 try {
ce152606 184 $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
185 } catch (moodle_exception $e) {
8aff8482 186 if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
187 if (file_exists($CFG->dataroot.'/emailcount')){
ce152606 188 $fp = @fopen($CFG->dataroot.'/emailcount', 'r');
189 $content = @fread($fp, 24);
190 @fclose($fp);
8aff8482 191 if((time() - (int)$content) > 600){
192 @mail($CFG->emailconnectionerrorsto,
193 'WARNING: Database connection error: '.$CFG->wwwroot,
194 'Connection error: '.$CFG->wwwroot);
ce152606 195 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
196 @fwrite($fp, time());
8aff8482 197 }
198 } else {
199 @mail($CFG->emailconnectionerrorsto,
200 'WARNING: Database connection error: '.$CFG->wwwroot,
201 'Connection error: '.$CFG->wwwroot);
ce152606 202 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
203 @fwrite($fp, time());
8aff8482 204 }
205 }
ce152606 206 // rethrow the exception
207 throw $e;
8aff8482 208 }
209
ce152606 210 $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
211
8aff8482 212 return true;
213}