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!)
37 * @copyright 2008 Petr Skoda (http://skodak.org)
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 defined('MOODLE_INTERNAL') ||
die();
43 // Require the essential
44 require_once($CFG->libdir
.'/dml/moodle_database.php');
46 /** Return false if record not found, show debug warning if multiple records found */
47 define('IGNORE_MISSING', 0);
48 /** Similar to IGNORE_MISSING but does not show debug warning if multiple records found, not recommended to be used */
49 define('IGNORE_MULTIPLE', 1);
50 /** Indicates exactly one record must exist */
51 define('MUST_EXIST', 2);
54 * DML exception class, use instead of error() in dml code.
56 class dml_exception
extends moodle_exception
{
58 * @param string $errorcode
60 * @param string $debuginfo
62 function __construct($errorcode, $a=NULL, $debuginfo=null) {
63 parent
::__construct($errorcode, '', '', $a, $debuginfo);
68 * DML db connection exception - triggered if database not accessible.
70 class dml_connection_exception
extends dml_exception
{
73 * @param string $error
75 function __construct($error) {
77 parent
::__construct('dbconnectionfailed', NULL, $errorinfo);
82 * DML db session wait exception - triggered when session lock request times out.
84 class dml_sessionwait_exception
extends dml_exception
{
88 function __construct() {
89 parent
::__construct('sessionwaiterr');
94 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
96 class dml_read_exception
extends dml_exception
{
106 * @param string $error
108 * @param array $params
110 function __construct($error, $sql=null, array $params=null) {
111 $this->error
= $error;
113 $this->params
= $params;
114 $errorinfo = $error."\n".$sql."\n[".var_export($params, true).']';
115 parent
::__construct('dmlreadexception', NULL, $errorinfo);
120 * Caused by multiple records found in get_record() call.
122 class dml_multiple_records_exception
extends dml_exception
{
130 * @param string $table table name if known, '' if unknown
132 * @param array $params
134 function __construct($sql='', array $params=null) {
135 $errorinfo = $sql."\n[".var_export($params, true).']';
136 parent
::__construct('multiplerecordsfound', null, $errorinfo);
141 * Caused by missing record that is required for normal operation.
143 class dml_missing_record_exception
extends dml_exception
{
153 * @param string $table table name if known, '' if unknown
155 * @param array $params
157 function __construct($tablename, $sql='', array $params=null) {
158 if (empty($tablename)) {
161 $this->tablename
= $tablename;
163 $this->params
= $params;
165 switch ($tablename) {
167 $errcode = 'invalidrecordunknown';
170 $errcode = empty($sql) ?
'invalidcourseid' : 'invalidrecord';
172 case 'course_module':
173 $errcode = 'invalidcoursemodule';
176 $errcode = 'invaliduser';
179 $errcode = 'invalidrecord';
182 $errorinfo = $sql."\n[".var_export($params, true).']';
183 parent
::__construct($errcode, $tablename, $errorinfo);
188 * DML write exception - triggered by SQL syntax errors, missing tables, etc.
190 class dml_write_exception
extends dml_exception
{
200 * @param string $error
202 * @param array $params
204 function __construct($error, $sql=null, array $params=null) {
205 $this->error
= $error;
207 $this->params
= $params;
208 $errorinfo = $error."\n".$sql."\n[".var_export($params, true).']';
209 parent
::__construct('dmlwriteexception', NULL, $errorinfo);
214 * DML transaction exception - triggered by problems related to DB transactions
216 class dml_transaction_exception
extends dml_exception
{
217 /** @var moodle_transaction */
222 * @param array $start_backtrace
224 function __construct($debuginfo=null, $transaction=null) {
225 $this->transaction
= $transaction; // TODO: MDL-20625 use the info from $transaction for debugging purposes
226 parent
::__construct('dmltransactionexception', NULL, $debuginfo);
231 * Sets up global $DB moodle_database instance
237 function setup_DB() {
244 if (!isset($CFG->dbuser
)) {
248 if (!isset($CFG->dbpass
)) {
252 if (!isset($CFG->dbname
)) {
256 if (!isset($CFG->dblibrary
)) {
257 $CFG->dblibrary
= 'native';
258 // use new drivers instead of the old adodb driver names
259 switch ($CFG->dbtype
) {
261 $CFG->dbtype
= 'pgsql';
265 $CFG->dbtype
= 'mssql';
269 $CFG->dbtype
= 'oci';
273 $CFG->dbtype
= 'mysqli';
278 if (!isset($CFG->dboptions
)) {
279 $CFG->dboptions
= array();
282 if (isset($CFG->dbpersist
)) {
283 $CFG->dboptions
['dbpersist'] = $CFG->dbpersist
;
286 if (!$DB = moodle_database
::get_driver_instance($CFG->dbtype
, $CFG->dblibrary
)) {
287 throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
291 $DB->connect($CFG->dbhost
, $CFG->dbuser
, $CFG->dbpass
, $CFG->dbname
, $CFG->prefix
, $CFG->dboptions
);
292 } catch (moodle_exception
$e) {
293 if (empty($CFG->noemailever
) and !empty($CFG->emailconnectionerrorsto
)) {
294 if (file_exists($CFG->dataroot
.'/emailcount')){
295 $fp = @fopen
($CFG->dataroot
.'/emailcount', 'r');
296 $content = @fread
($fp, 24);
298 if((time() - (int)$content) > 600){
299 //email directly rather than using messaging
300 @mail
($CFG->emailconnectionerrorsto
,
301 'WARNING: Database connection error: '.$CFG->wwwroot
,
302 'Connection error: '.$CFG->wwwroot
);
303 $fp = @fopen
($CFG->dataroot
.'/emailcount', 'w');
304 @fwrite
($fp, time());
307 //email directly rather than using messaging
308 @mail
($CFG->emailconnectionerrorsto
,
309 'WARNING: Database connection error: '.$CFG->wwwroot
,
310 'Connection error: '.$CFG->wwwroot
);
311 $fp = @fopen
($CFG->dataroot
.'/emailcount', 'w');
312 @fwrite
($fp, time());
315 // rethrow the exception
319 $CFG->dbfamily
= $DB->get_dbfamily(); // TODO: BC only for now