Merge branch 'MDL-62181-33-enfix' of git://github.com/mudrd8mz/moodle into MOODLE_33_...
[moodle.git] / lib / dmllib.php
blob58ab46b81c11e3470be92f57c8841f087be089ba
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
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 /**
20 * This library contains all the Data Manipulation Language (DML) functions
21 * used to interact with the DB
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
28 * This library is automatically included by Moodle core so you never need to
29 * include it yourself.
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!)
35 * @package core
36 * @category dml
37 * @subpackage dml
38 * @copyright 2008 Petr Skoda (http://skodak.org)
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 defined('MOODLE_INTERNAL') || die();
44 // Require the essential
45 require_once($CFG->libdir.'/dml/moodle_database.php');
47 /** Return false if record not found, show debug warning if multiple records found */
48 define('IGNORE_MISSING', 0);
49 /** Similar to IGNORE_MISSING but does not show debug warning if multiple records found, not recommended to be used */
50 define('IGNORE_MULTIPLE', 1);
51 /** Indicates exactly one record must exist */
52 define('MUST_EXIST', 2);
54 /**
55 * DML exception class, use instead of print_error() in dml code.
57 * @package core
58 * @category dml
59 * @subpackage dml
60 * @copyright 2008 Petr Skoda (http://skodak.org)
61 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
63 class dml_exception extends moodle_exception {
64 /**
65 * @param string $errorcode The name of the string from error.php to print.
66 * @param string $a Extra words and phrases that might be required in the error string.
67 * @param string $debuginfo Optional debugging information.
69 function __construct($errorcode, $a=NULL, $debuginfo=null) {
70 parent::__construct($errorcode, '', '', $a, $debuginfo);
74 /**
75 * DML db connection exception - triggered if database not accessible.
77 * @package core
78 * @category dml
79 * @subpackage dml
80 * @copyright 2008 Petr Skoda (http://skodak.org)
81 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
83 class dml_connection_exception extends dml_exception {
84 /**
85 * Constructor
86 * @param string $error Optional debugging information.
88 function __construct($error) {
89 $errorinfo = $error;
90 parent::__construct('dbconnectionfailed', NULL, $errorinfo);
94 /**
95 * DML db session wait exception - triggered when session lock request times out.
97 * @package core
98 * @category dml
99 * @subpackage dml
100 * @copyright 2008 Petr Skoda (http://skodak.org)
101 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
103 class dml_sessionwait_exception extends dml_exception {
105 * Constructor
107 function __construct() {
108 parent::__construct('sessionwaiterr');
113 * DML read exception - triggered by some SQL syntax errors, etc.
115 * @package core
116 * @category dml
117 * @subpackage dml
118 * @copyright 2008 Petr Skoda (http://skodak.org)
119 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
121 class dml_read_exception extends dml_exception {
122 /** @var string The name of the string from error.php to print.*/
123 public $error;
124 /** @var string The SQL that ran just before this read error.*/
125 public $sql;
126 /** @var array The SQL's related parameters.*/
127 public $params;
130 * Constructor
131 * @param string $error The name of the string from error.php to print.
132 * @param string $sql The SQL that ran just before this read error.
133 * @param array $params The SQL's related parameters.(optional)
135 function __construct($error, $sql=null, array $params=null) {
136 $this->error = $error;
137 $this->sql = $sql;
138 $this->params = $params;
139 $errorinfo = $error."\n".$sql."\n[".var_export($params, true).']';
140 parent::__construct('dmlreadexception', NULL, $errorinfo);
145 * Caused by multiple records found in get_record() call.
147 * @package core
148 * @category dml
149 * @subpackage dml
150 * @copyright 2008 Petr Skoda (http://skodak.org)
151 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
153 class dml_multiple_records_exception extends dml_exception {
154 /** @var string The SQL that ran just before this read error.*/
155 public $sql;
156 /** @var array The SQL's related parameters.*/
157 public $params;
160 * Constructor
161 * @param string $sql The SQL that ran just before this read error.
162 * @param array $params The SQL's related parameters.(optional)
164 function __construct($sql='', array $params=null) {
165 $errorinfo = $sql."\n[".var_export($params, true).']';
166 parent::__construct('multiplerecordsfound', null, $errorinfo);
171 * Caused by missing record that is required for normal operation.
173 * @package core
174 * @category dml
175 * @subpackage dml
176 * @copyright 2008 Petr Skoda (http://skodak.org)
177 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
179 class dml_missing_record_exception extends dml_exception {
180 /** @var string A table's name.*/
181 public $table;
182 /** @var string An SQL query.*/
183 public $sql;
184 /** @var array The SQL's parameters.*/
185 public $params;
188 * Constructor
189 * @param string $tablename The table name if known, '' if unknown.
190 * @param string $sql Optional SQL query.
191 * @param array $params Optional SQL query's parameters.
193 function __construct($tablename, $sql='', array $params=null) {
194 if (empty($tablename)) {
195 $tablename = null;
197 $this->tablename = $tablename;
198 $this->sql = $sql;
199 $this->params = $params;
201 switch ($tablename) {
202 case null:
203 $errcode = 'invalidrecordunknown';
204 break;
205 case 'course':
206 $errcode = empty($sql) ? 'invalidcourseid' : 'invalidrecord';
207 break;
208 case 'course_modules':
209 $errcode = 'invalidcoursemodule';
210 break;
211 case 'user':
212 $errcode = 'invaliduser';
213 break;
214 default:
215 $errcode = 'invalidrecord';
216 break;
218 $errorinfo = $sql."\n[".var_export($params, true).']';
219 parent::__construct($errcode, $tablename, $errorinfo);
224 * DML write exception - triggered by some SQL syntax errors, etc.
226 * @package core
227 * @category dml
228 * @subpackage dml
229 * @copyright 2008 Petr Skoda (http://skodak.org)
230 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
232 class dml_write_exception extends dml_exception {
233 /** @var string The name of the string from error.php to print.*/
234 public $error;
235 /** @var string The SQL that ran just before this write error.*/
236 public $sql;
237 /** @var array The SQL's related parameters.*/
238 public $params;
241 * Constructor
242 * @param string $error The name of the string from error.php to print.
243 * @param string $sql The SQL that ran just before this write error.
244 * @param array $params The SQL's related parameters.(optional)
246 function __construct($error, $sql=null, array $params=null) {
247 $this->error = $error;
248 $this->sql = $sql;
249 $this->params = $params;
250 $errorinfo = $error."\n".$sql."\n[".var_export($params, true).']';
251 parent::__construct('dmlwriteexception', NULL, $errorinfo);
256 * DML transaction exception - triggered by problems related to DB transactions.
258 * @todo MDL-20625 Use the info from $transaction for debugging purposes.
260 * @package core
261 * @category dml
262 * @subpackage dml
263 * @copyright 2008 Petr Skoda (http://skodak.org)
264 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
266 class dml_transaction_exception extends dml_exception {
267 /** @var moodle_transaction An instance of a transaction.*/
268 public $transaction;
271 * Constructor
272 * @param array $debuginfo Optional debugging information.
273 * @param moodle_transaction $transaction The instance of the transaction.(Optional)
275 function __construct($debuginfo=null, $transaction=null) {
276 $this->transaction = $transaction; // TODO: MDL-20625 use the info from $transaction for debugging purposes
277 parent::__construct('dmltransactionexception', NULL, $debuginfo);
282 * Sets up global $DB moodle_database instance
284 * @global stdClass $CFG The global configuration instance.
285 * @see config.php
286 * @see config-dist.php
287 * @global stdClass $DB The global moodle_database instance.
288 * @return void|bool Returns true when finished setting up $DB. Returns void when $DB has already been set.
290 function setup_DB() {
291 global $CFG, $DB;
293 if (isset($DB)) {
294 return;
297 if (!isset($CFG->dbuser)) {
298 $CFG->dbuser = '';
301 if (!isset($CFG->dbpass)) {
302 $CFG->dbpass = '';
305 if (!isset($CFG->dbname)) {
306 $CFG->dbname = '';
309 if (!isset($CFG->dblibrary)) {
310 $CFG->dblibrary = 'native';
311 // use new drivers instead of the old adodb driver names
312 switch ($CFG->dbtype) {
313 case 'postgres7' :
314 $CFG->dbtype = 'pgsql';
315 break;
317 case 'mssql_n':
318 $CFG->dbtype = 'mssql';
319 break;
321 case 'oci8po':
322 $CFG->dbtype = 'oci';
323 break;
325 case 'mysql' :
326 $CFG->dbtype = 'mysqli';
327 break;
331 if (!isset($CFG->dboptions)) {
332 $CFG->dboptions = array();
335 if (isset($CFG->dbpersist)) {
336 $CFG->dboptions['dbpersist'] = $CFG->dbpersist;
339 if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) {
340 throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
343 try {
344 $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
345 } catch (moodle_exception $e) {
346 if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
347 $body = "Connection error: ".$CFG->wwwroot.
348 "\n\nInfo:".
349 "\n\tError code: ".$e->errorcode.
350 "\n\tDebug info: ".$e->debuginfo.
351 "\n\tServer: ".$_SERVER['SERVER_NAME']." (".$_SERVER['SERVER_ADDR'].")";
352 if (file_exists($CFG->dataroot.'/emailcount')){
353 $fp = @fopen($CFG->dataroot.'/emailcount', 'r');
354 $content = @fread($fp, 24);
355 @fclose($fp);
356 if((time() - (int)$content) > 600){
357 //email directly rather than using messaging
358 @mail($CFG->emailconnectionerrorsto,
359 'WARNING: Database connection error: '.$CFG->wwwroot,
360 $body);
361 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
362 @fwrite($fp, time());
364 } else {
365 //email directly rather than using messaging
366 @mail($CFG->emailconnectionerrorsto,
367 'WARNING: Database connection error: '.$CFG->wwwroot,
368 $body);
369 $fp = @fopen($CFG->dataroot.'/emailcount', 'w');
370 @fwrite($fp, time());
373 // rethrow the exception
374 throw $e;
377 $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
379 return true;