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/>.
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!)
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);
55 * DML exception class, use instead of print_error() in dml code.
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
{
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);
75 * DML db connection exception - triggered if database not accessible.
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
{
86 * @param string $error Optional debugging information.
88 function __construct($error) {
90 parent
::__construct('dbconnectionfailed', NULL, $errorinfo);
95 * DML db session wait exception - triggered when session lock request times out.
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
{
107 function __construct() {
108 parent
::__construct('sessionwaiterr');
113 * DML read exception - triggered by some SQL syntax errors, etc.
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.*/
124 /** @var string The SQL that ran just before this read error.*/
126 /** @var array The SQL's related parameters.*/
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;
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.
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.*/
156 /** @var array The SQL's related parameters.*/
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.
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.*/
182 /** @var string An SQL query.*/
184 /** @var array The SQL's parameters.*/
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)) {
197 $this->tablename
= $tablename;
199 $this->params
= $params;
201 switch ($tablename) {
203 $errcode = 'invalidrecordunknown';
206 $errcode = empty($sql) ?
'invalidcourseid' : 'invalidrecord';
208 case 'course_modules':
209 $errcode = 'invalidcoursemodule';
212 $errcode = 'invaliduser';
215 $errcode = 'invalidrecord';
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.
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.*/
235 /** @var string The SQL that ran just before this write error.*/
237 /** @var array The SQL's related parameters.*/
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;
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.
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.*/
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.
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() {
297 if (!isset($CFG->dbuser
)) {
301 if (!isset($CFG->dbpass
)) {
305 if (!isset($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
) {
314 $CFG->dbtype
= 'pgsql';
318 $CFG->dbtype
= 'mssql';
322 $CFG->dbtype
= 'oci';
326 $CFG->dbtype
= 'mysqli';
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");
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
.
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);
356 if((time() - (int)$content) > 600){
357 //email directly rather than using messaging
358 @mail
($CFG->emailconnectionerrorsto
,
359 'WARNING: Database connection error: '.$CFG->wwwroot
,
361 $fp = @fopen
($CFG->dataroot
.'/emailcount', 'w');
362 @fwrite
($fp, time());
365 //email directly rather than using messaging
366 @mail
($CFG->emailconnectionerrorsto
,
367 'WARNING: Database connection error: '.$CFG->wwwroot
,
369 $fp = @fopen
($CFG->dataroot
.'/emailcount', 'w');
370 @fwrite
($fp, time());
373 // rethrow the exception
377 $CFG->dbfamily
= $DB->get_dbfamily(); // TODO: BC only for now