Commit | Line | Data |
---|---|---|
11e1f828 MH |
1 | <?php |
2 | ||
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. | |
14 | // | |
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/>. | |
7e13be08 | 17 | |
11e1f828 MH |
18 | /** |
19 | * This library contains all the Data Manipulation Language (DML) functions | |
20 | * used to interact with the DB | |
21 | * | |
22 | * This library contains all the Data Manipulation Language (DML) functions | |
23 | * used to interact with the DB. All the dunctions in this library must be | |
24 | * generic and work against the major number of RDBMS possible. This is the | |
25 | * list of currently supported and tested DBs: mysql, postresql, mssql, oracle | |
26 | ||
27 | * This library is automatically included by Moodle core so you never need to | |
28 | * include it yourself. | |
29 | ||
30 | * For more info about the functions available in this library, please visit: | |
31 | * http://docs.moodle.org/en/DML_functions | |
32 | * (feel free to modify, improve and document such page, thanks!) | |
33 | * | |
34 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
35 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
36 | * @package moodlecore | |
37 | */ | |
38 | ||
39 | /** Require the essential */ | |
8aff8482 | 40 | require_once($CFG->libdir.'/dml/moodle_database.php'); |
41 | ||
49459eb0 | 42 | /** |
43 | * DML exception class, use instead of error() in dml code. | |
11e1f828 MH |
44 | * |
45 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
46 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
47 | * @package moodlecore | |
49459eb0 | 48 | */ |
49 | class dml_exception extends moodle_exception { | |
11e1f828 MH |
50 | /** |
51 | * @param string $errorcode | |
52 | * @param string $a | |
53 | * @param string $debuginfo | |
54 | */ | |
49459eb0 | 55 | function __construct($errorcode, $a=NULL, $debuginfo=null) { |
56 | parent::__construct($errorcode, '', '', $a, $debuginfo); | |
57 | } | |
58 | } | |
59 | ||
ce152606 | 60 | /** |
61 | * DML db connection exception - triggered if database not accessible. | |
11e1f828 MH |
62 | * |
63 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
64 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
65 | * @package moodlecore | |
ce152606 | 66 | */ |
67 | class dml_connection_exception extends dml_exception { | |
11e1f828 MH |
68 | /** |
69 | * @param string $error | |
70 | */ | |
ce152606 | 71 | function __construct($error) { |
72 | $errorinfo = '<em>'.s($error).'</em>'; | |
73 | parent::__construct('dbconnectionfailed', NULL, $errorinfo); | |
74 | } | |
75 | } | |
76 | ||
9214025e | 77 | /** |
78 | * DML read exception - triggered by SQL syntax errors, missing tables, etc. | |
11e1f828 MH |
79 | * |
80 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
81 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
82 | * @package moodlecore | |
9214025e | 83 | */ |
84 | class dml_read_exception extends dml_exception { | |
11e1f828 | 85 | /** @var string */ |
9214025e | 86 | public $error; |
87 | public $sql; | |
11e1f828 | 88 | /** @var array */ |
9214025e | 89 | public $params; |
11e1f828 MH |
90 | |
91 | /** | |
92 | * @param string $error | |
93 | * @param string $sql | |
94 | * @param array $params | |
95 | */ | |
9214025e | 96 | function __construct($error, $sql=null, array $params=null) { |
97 | $this->error = $error; | |
98 | $this->sql = $sql; | |
99 | $this->params = $params; | |
100 | $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']'; | |
101 | parent::__construct('dmlreadexception', NULL, $errorinfo); | |
102 | } | |
103 | } | |
104 | ||
105 | /** | |
106 | * DML read exception - triggered by SQL syntax errors, missing tables, etc. | |
11e1f828 MH |
107 | * |
108 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
109 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
110 | * @package moodlecore | |
9214025e | 111 | */ |
112 | class dml_write_exception extends dml_exception { | |
11e1f828 | 113 | /** @var string */ |
9214025e | 114 | public $error; |
115 | public $sql; | |
11e1f828 | 116 | /** @var array */ |
9214025e | 117 | public $params; |
118 | ||
11e1f828 MH |
119 | /** |
120 | * @param string $error | |
121 | * @param string $sql | |
122 | * @param array $params | |
123 | */ | |
9214025e | 124 | function __construct($error, $sql=null, array $params=null) { |
125 | $this->error = $error; | |
126 | $this->sql = $sql; | |
127 | $this->params = $params; | |
128 | $errorinfo = s($error).'<br /><br />'.s($sql).'<br />['.s(var_export($params, true)).']'; | |
129 | parent::__construct('dmlwriteexception', NULL, $errorinfo); | |
130 | } | |
131 | } | |
132 | ||
8aff8482 | 133 | /** |
134 | * Sets up global $DB moodle_database instance | |
11e1f828 MH |
135 | * |
136 | * @global object | |
137 | * @global object | |
8aff8482 | 138 | * @return void |
139 | */ | |
140 | function setup_DB() { | |
141 | global $CFG, $DB; | |
142 | ||
143 | if (isset($DB)) { | |
144 | return; | |
145 | } | |
146 | ||
147 | if (!isset($CFG->dbuser)) { | |
148 | $CFG->dbuser = ''; | |
149 | } | |
150 | ||
151 | if (!isset($CFG->dbpass)) { | |
152 | $CFG->dbpass = ''; | |
153 | } | |
154 | ||
155 | if (!isset($CFG->dbname)) { | |
156 | $CFG->dbname = ''; | |
157 | } | |
158 | ||
8aff8482 | 159 | if (!isset($CFG->dblibrary)) { |
3eae57b7 | 160 | switch ($CFG->dbtype) { |
161 | case 'postgres7' : | |
162 | $CFG->dbtype = 'pgsql'; | |
163 | // continue, no break here | |
164 | case 'pgsql' : | |
165 | $CFG->dblibrary = 'native'; | |
166 | break; | |
167 | ||
168 | case 'mysql' : | |
a7b4fc7f | 169 | if (!extension_loaded('mysqli')) { |
170 | $CFG->dblibrary = 'adodb'; | |
171 | break; | |
172 | } | |
3eae57b7 | 173 | $CFG->dbtype = 'mysqli'; |
174 | // continue, no break here | |
175 | case 'mysqli' : | |
176 | $CFG->dblibrary = 'native'; | |
177 | break; | |
178 | ||
179 | default: | |
180 | // the rest of drivers is not converted yet - keep adodb for now | |
181 | $CFG->dblibrary = 'adodb'; | |
182 | } | |
8aff8482 | 183 | } |
184 | ||
185 | if (!isset($CFG->dboptions)) { | |
186 | $CFG->dboptions = array(); | |
187 | } | |
188 | ||
beaa43db | 189 | if (isset($CFG->dbpersist)) { |
190 | $CFG->dboptions['dbpersist'] = $CFG->dbpersist; | |
191 | } | |
192 | ||
3eae57b7 | 193 | if (!$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary)) { |
194 | throw new dml_exception('dbdriverproblem', "Unknown driver $CFG->dblibrary/$CFG->dbtype"); | |
195 | } | |
8aff8482 | 196 | |
3eae57b7 | 197 | try { |
ce152606 | 198 | $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions); |
199 | } catch (moodle_exception $e) { | |
8aff8482 | 200 | if (empty($CFG->noemailever) and !empty($CFG->emailconnectionerrorsto)) { |
201 | if (file_exists($CFG->dataroot.'/emailcount')){ | |
ce152606 | 202 | $fp = @fopen($CFG->dataroot.'/emailcount', 'r'); |
203 | $content = @fread($fp, 24); | |
204 | @fclose($fp); | |
8aff8482 | 205 | if((time() - (int)$content) > 600){ |
206 | @mail($CFG->emailconnectionerrorsto, | |
207 | 'WARNING: Database connection error: '.$CFG->wwwroot, | |
208 | 'Connection error: '.$CFG->wwwroot); | |
ce152606 | 209 | $fp = @fopen($CFG->dataroot.'/emailcount', 'w'); |
210 | @fwrite($fp, time()); | |
8aff8482 | 211 | } |
212 | } else { | |
213 | @mail($CFG->emailconnectionerrorsto, | |
214 | 'WARNING: Database connection error: '.$CFG->wwwroot, | |
215 | 'Connection error: '.$CFG->wwwroot); | |
ce152606 | 216 | $fp = @fopen($CFG->dataroot.'/emailcount', 'w'); |
217 | @fwrite($fp, time()); | |
8aff8482 | 218 | } |
219 | } | |
ce152606 | 220 | // rethrow the exception |
221 | throw $e; | |
8aff8482 | 222 | } |
223 | ||
ce152606 | 224 | $CFG->dbfamily = $DB->get_dbfamily(); // TODO: BC only for now |
225 | ||
8aff8482 | 226 | return true; |
227 | } |