forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmllib.php
322 lines (285 loc) · 9.31 KB
/
dmllib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This library contains all the Data Manipulation Language (DML) functions
* used to interact with the DB
*
* This library contains all the Data Manipulation Language (DML) functions
* used to interact with the DB. All the dunctions in this library must be
* generic and work against the major number of RDBMS possible. This is the
* list of currently supported and tested DBs: mysql, postresql, mssql, oracle
* This library is automatically included by Moodle core so you never need to
* include it yourself.
* For more info about the functions available in this library, please visit:
* http://docs.moodle.org/en/DML_functions
* (feel free to modify, improve and document such page, thanks!)
*
* @package core
* @subpackage dml
* @copyright 2008 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Require the essential
require_once($CFG->libdir.'/dml/moodle_database.php');
/** Return false if record not found, show debug warning if multiple records found */
define('IGNORE_MISSING', 0);
/** Similar to IGNORE_MISSING but does not show debug warning if multiple records found, not recommended to be used */
define('IGNORE_MULTIPLE', 1);
/** Indicates exactly one record must exist */
define('MUST_EXIST', 2);
/**
* DML exception class, use instead of error() in dml code.
*/
class dml_exception extends moodle_exception {
/**
* @param string $errorcode
* @param string $a
* @param string $debuginfo
*/
function __construct($errorcode, $a=NULL, $debuginfo=null) {
parent::__construct($errorcode, '', '', $a, $debuginfo);
}
}
/**
* DML db connection exception - triggered if database not accessible.
*/
class dml_connection_exception extends dml_exception {
/**
* Constructor
* @param string $error
*/
function __construct($error) {
$errorinfo = $error;
parent::__construct('dbconnectionfailed', NULL, $errorinfo);
}
}
/**
* DML db session wait exception - triggered when session lock request times out.
*/
class dml_sessionwait_exception extends dml_exception {
/**
* Constructor
*/
function __construct() {
parent::__construct('sessionwaiterr');
}
}
/**
* DML read exception - triggered by some SQL syntax errors, etc.
*/
class dml_read_exception extends dml_exception {
/** @var string */
public $error;
/** @var string */
public $sql;
/** @var array */
public $params;
/**
* Constructor
* @param string $error
* @param string $sql
* @param array $params
*/
function __construct($error, $sql=null, array $params=null) {
$this->error = $error;
$this->sql = $sql;
$this->params = $params;
$errorinfo = $error."\n".$sql."\n[".var_export($params, true).']';
parent::__construct('dmlreadexception', NULL, $errorinfo);
}
}
/**
* Caused by multiple records found in get_record() call.
*/
class dml_multiple_records_exception extends dml_exception {
/** @var string */
public $sql;
/** @var array */
public $params;
/**
* Constructor
* @param string $table table name if known, '' if unknown
* @param string $sql
* @param array $params
*/
function __construct($sql='', array $params=null) {
$errorinfo = $sql."\n[".var_export($params, true).']';
parent::__construct('multiplerecordsfound', null, $errorinfo);
}
}
/**
* Caused by missing record that is required for normal operation.
*/
class dml_missing_record_exception extends dml_exception {
/** @var string */
public $table;
/** @var string */
public $sql;
/** @var array */
public $params;
/**
* Constructor
* @param string $table table name if known, '' if unknown
* @param string $sql
* @param array $params
*/
function __construct($tablename, $sql='', array $params=null) {
if (empty($tablename)) {
$tablename = null;
}
$this->tablename = $tablename;
$this->sql = $sql;
$this->params = $params;
switch ($tablename) {
case null:
$errcode = 'invalidrecordunknown';
break;
case 'course':
$errcode = empty($sql) ? 'invalidcourseid' : 'invalidrecord';
break;
case 'course_module':
$errcode = 'invalidcoursemodule';
break;
case 'user':
$errcode = 'invaliduser';
break;
default:
$errcode = 'invalidrecord';
break;
}
$errorinfo = $sql."\n[".var_export($params, true).']';
parent::__construct($errcode, $tablename, $errorinfo);
}
}
/**
* DML write exception - triggered by some SQL syntax errors, etc.
*/
class dml_write_exception extends dml_exception {
/** @var string */
public $error;
/** @var string */
public $sql;
/** @var array */
public $params;
/**
* Constructor
* @param string $error
* @param string $sql
* @param array $params
*/
function __construct($error, $sql=null, array $params=null) {
$this->error = $error;
$this->sql = $sql;
$this->params = $params;
$errorinfo = $error."\n".$sql."\n[".var_export($params, true).']';
parent::__construct('dmlwriteexception', NULL, $errorinfo);
}
}
/**
* DML transaction exception - triggered by problems related to DB transactions
*/
class dml_transaction_exception extends dml_exception {
/** @var moodle_transaction */
public $transaction;
/**
* Constructor
* @param array $start_backtrace
*/
function __construct($debuginfo=null, $transaction=null) {
$this->transaction = $transaction; // TODO: MDL-20625 use the info from $transaction for debugging purposes
parent::__construct('dmltransactionexception', NULL, $debuginfo);
}
}
/**
* Sets up global $DB moodle_database instance
*
* @global object
* @global object
* @return void
*/
function setup_DB() {
global $CFG, $DB;
if (isset($DB)) {
return;
}
if (!isset($CFG->dbuser)) {
$CFG->dbuser = '';
}
if (!isset($CFG->dbpass)) {
$CFG->dbpass = '';
}
if (!isset($CFG->dbname)) {
$CFG->dbname = '';
}
if (!isset($CFG->dblibrary)) {
$CFG->dblibrary = 'native';
// use new drivers instead of the old adodb driver names
switch ($CFG->dbtype) {
case 'postgres7' :
$CFG->dbtype = 'pgsql';
break;
case 'mssql_n':
$CFG->dbtype = 'mssql';
break;
case 'oci8po':
$CFG->dbtype = 'oci';
break;
case 'mysql' :
$CFG->dbtype = 'mysqli';
break;
}
}
if (!isset($CFG->dboptions)) {
$CFG->dboptions = array();
}
if (isset($CFG->dbpersist)) {
$CFG->dboptions['dbpersist'] = $CFG->dbpersist;
}
if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) {
throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype");
}
try {
$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
} catch (moodle_exception $e) {
if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) {
if (file_exists($CFG->dataroot.'/emailcount')){
$fp = @fopen($CFG->dataroot.'/emailcount', 'r');
$content = @fread($fp, 24);
@fclose($fp);
if((time() - (int)$content) > 600){
//email directly rather than using messaging
@mail($CFG->emailconnectionerrorsto,
'WARNING: Database connection error: '.$CFG->wwwroot,
'Connection error: '.$CFG->wwwroot);
$fp = @fopen($CFG->dataroot.'/emailcount', 'w');
@fwrite($fp, time());
}
} else {
//email directly rather than using messaging
@mail($CFG->emailconnectionerrorsto,
'WARNING: Database connection error: '.$CFG->wwwroot,
'Connection error: '.$CFG->wwwroot);
$fp = @fopen($CFG->dataroot.'/emailcount', 'w');
@fwrite($fp, time());
}
}
// rethrow the exception
throw $e;
}
$CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now
return true;
}