MDL-17317 DML: Exceptions used by all drivers now :-) expect some more commits/cleanu...
[moodle.git] / lib / dmllib.php
CommitLineData
7e13be08 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
ddaadc7c 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
7e13be08 11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
eef868d1 26/// This library contains all the Data Manipulation Language (DML) functions
7e13be08 27/// used to interact with the DB. All the dunctions in this library must be
28/// generic and work against the major number of RDBMS possible. This is the
29/// list of currently supported and tested DBs: mysql, postresql, mssql, oracle
30
9f948d11 31/// This library is automatically included by Moodle core so you never need to
32/// include it yourself.
7e13be08 33
34/// For more info about the functions available in this library, please visit:
35/// http://docs.moodle.org/en/DML_functions
36/// (feel free to modify, improve and document such page, thanks!)
37
8aff8482 38require_once($CFG->libdir.'/dml/moodle_database.php');
39
49459eb0 40/**
41 * DML exception class, use instead of error() in dml code.
42 */
43class dml_exception extends moodle_exception {
44 function __construct($errorcode, $a=NULL, $debuginfo=null) {
45 parent::__construct($errorcode, '', '', $a, $debuginfo);
46 }
47}
48
9214025e 49/**
50 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
51 */
52class dml_read_exception extends dml_exception {
53 public $error;
54 public $sql;
55 public $params;
56
57 function __construct($error, $sql=null, array $params=null) {
58 $this->error = $error;
59 $this->sql = $sql;
60 $this->params = $params;
61 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
62 parent::__construct('dmlreadexception', NULL, $errorinfo);
63 }
64}
65
66/**
67 * DML read exception - triggered by SQL syntax errors, missing tables, etc.
68 */
69class dml_write_exception extends dml_exception {
70 public $error;
71 public $sql;
72 public $params;
73
74 function __construct($error, $sql=null, array $params=null) {
75 $this->error = $error;
76 $this->sql = $sql;
77 $this->params = $params;
78 $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']';
79 parent::__construct('dmlwriteexception', NULL, $errorinfo);
80 }
81}
82
8aff8482 83/**
84 * Sets up global $DB moodle_database instance
85 * @return void
86 */
87function setup_DB() {
88 global $CFG, $DB;
89
90 if (isset($DB)) {
91 return;
92 }
93
94 if (!isset($CFG->dbuser)) {
95 $CFG->dbuser = '';
96 }
97
98 if (!isset($CFG->dbpass)) {
99 $CFG->dbpass = '';
100 }
101
102 if (!isset($CFG->dbname)) {
103 $CFG->dbname = '';
104 }
105
8aff8482 106 if (!isset($CFG->dblibrary)) {
107 $CFG->dblibrary = 'adodb';
108 }
109
110 if (!isset($CFG->dboptions)) {
111 $CFG->dboptions = array();
112 }
113
beaa43db 114 if (isset($CFG->dbpersist)) {
115 $CFG->dboptions['dbpersist'] = $CFG->dbpersist;
116 }
117
118
8aff8482 119 $DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
120
121 $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
122
123 $driverstatus = $DB->driver_installed();
124
125 if ($driverstatus !== true) {
1fe1d104 126 throw new dml_exception('dbdriverproblem', $driverstatus);
8aff8482 127 }
128
129 if (debugging('', DEBUG_ALL)) {
130 // catch errors
131 ob_start();
132 } else {
133 $prevdebug = error_reporting(0);
134 }
beaa43db 135 if (!$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions)) {
8aff8482 136 if (debugging('', DEBUG_ALL)) {
137 if ($dberr = ob_get_contents()) {
138 $dberr = '<p><em>'.$dberr.'</em></p>';
139 }
140 ob_end_clean();
141 } else {
142 $dberr = '';
143 }
144 if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
145 if (file_exists($CFG->dataroot.'/emailcount')){
146 $fp = fopen($CFG->dataroot.'/emailcount', 'r');
147 $content = fread($fp, 24);
148 fclose($fp);
149 if((time() - (int)$content) > 600){
150 @mail($CFG->emailconnectionerrorsto,
151 'WARNING: Database connection error: '.$CFG->wwwroot,
152 'Connection error: '.$CFG->wwwroot);
153 $fp = fopen($CFG->dataroot.'/emailcount', 'w');
154 fwrite($fp, time());
155 }
156 } else {
157 @mail($CFG->emailconnectionerrorsto,
158 'WARNING: Database connection error: '.$CFG->wwwroot,
159 'Connection error: '.$CFG->wwwroot);
160 $fp = fopen($CFG->dataroot.'/emailcount', 'w');
161 fwrite($fp, time());
162 }
163 }
1fe1d104 164 throw new dml_exception('dbconnectionfailed', $dberr);
8aff8482 165 }
166 if (debugging('', DEBUG_ALL)) {
167 ob_end_clean();
168 } else {
169 error_reporting($prevdebug);
170 }
171
172 return true;
173}