Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
5ddeb07c | 3 | * Copyright (C) 2003-2007 Polytechnique.org * |
0337d704 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | **************************************************************************/ | |
21 | ||
08cce2ff | 22 | class XDB |
0337d704 | 23 | { |
32d9ae72 | 24 | private static $mysqli = null; |
25 | ||
821744e0 | 26 | public static function connect() |
32d9ae72 | 27 | { |
821744e0 | 28 | global $globals; |
29 | XDB::$mysqli = new mysqli($globals->dbhost, $globals->dbuser, $globals->dbpwd, $globals->dbdb); | |
81e9c63f | 30 | if ($globals->debug & DEBUG_BT) { |
d3f26be9 | 31 | $bt = new PlBacktrace('MySQL'); |
32 | if (mysqli_connect_errno()) { | |
33 | $bt->newEvent("MySQLI connection", 0, mysqli_connect_error()); | |
34 | return false; | |
35 | } | |
32d9ae72 | 36 | } |
37 | XDB::$mysqli->autocommit(true); | |
821744e0 | 38 | XDB::$mysqli->set_charset($globals->dbcharset); |
32d9ae72 | 39 | return true; |
40 | } | |
f1ca33de | 41 | |
ed3f4d3e | 42 | public static function _prepare($args) |
43 | { | |
f62bd784 | 44 | $query = array_map(Array('XDB', 'escape'), $args); |
0337d704 | 45 | $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0])); |
46 | return call_user_func_array('sprintf', $query); | |
47 | } | |
13a25546 | 48 | |
6995a9b9 | 49 | public static function _reformatQuery($query) |
7c571120 | 50 | { |
a4f89886 | 51 | $query = preg_split("/\n\\s*/", trim($query)); |
7c571120 | 52 | $length = 0; |
d3c52d30 | 53 | foreach ($query as $key=>$line) { |
54 | $local = -2; | |
a14159bf | 55 | if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches) |
85d3b330 | 56 | && $matches[1] != 'AND' && $matches[1] != 'OR') |
57 | { | |
d3c52d30 | 58 | $local = strlen($matches[1]); |
59 | $line = $matches[1] . ' ' . $matches[2]; | |
60 | $length = max($length, $local); | |
7c571120 | 61 | } |
d3c52d30 | 62 | $query[$key] = array($line, $local); |
7c571120 | 63 | } |
64 | $res = ''; | |
d3c52d30 | 65 | foreach ($query as $array) { |
66 | list($line, $local) = $array; | |
9630c649 | 67 | $local = max(0, $length - $local); |
7c571120 | 68 | $res .= str_repeat(' ', $local) . $line . "\n"; |
69 | $length += 2 * (substr_count($line, '(') - substr_count($line, ')')); | |
70 | } | |
71 | return $res; | |
72 | } | |
73 | ||
ed3f4d3e | 74 | public static function _query($query) |
75 | { | |
f1ca33de | 76 | global $globals; |
77 | ||
821744e0 | 78 | if (!XDB::$mysqli && !XDB::connect()) { |
79 | return false; | |
80 | } | |
81 | ||
81e9c63f | 82 | if ($globals->debug & DEBUG_BT) { |
f1ca33de | 83 | $explain = array(); |
5fb22b39 | 84 | if (strpos($query, 'FOUND_ROWS()') === false) { |
32d9ae72 | 85 | $res = XDB::$mysqli->query("EXPLAIN $query"); |
86 | if ($res) { | |
87 | while ($row = $res->fetch_assoc()) { | |
5fb22b39 | 88 | $explain[] = $row; |
89 | } | |
32d9ae72 | 90 | $res->free(); |
5fb22b39 | 91 | } |
f1ca33de | 92 | } |
d3f26be9 | 93 | PlBacktrace::$bt['MySQL']->start(XDB::_reformatQuery($query)); |
f1ca33de | 94 | } |
95 | ||
32d9ae72 | 96 | $res = XDB::$mysqli->query($query); |
0381e170 | 97 | |
81e9c63f | 98 | if ($globals->debug & DEBUG_BT) { |
d3f26be9 | 99 | PlBacktrace::$bt['MySQL']->stop(@$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows, |
100 | XDB::$mysqli->error, | |
101 | $explain); | |
f1ca33de | 102 | } |
f1ca33de | 103 | return $res; |
104 | } | |
105 | ||
6995a9b9 | 106 | public static function query() |
0337d704 | 107 | { |
08cce2ff | 108 | return new XOrgDBResult(XDB::_prepare(func_get_args())); |
0337d704 | 109 | } |
110 | ||
6995a9b9 | 111 | public static function execute() |
f1ca33de | 112 | { |
fe556813 FB |
113 | global $globals; |
114 | $args = func_get_args(); | |
115 | if ($globals->mode != 'rw' && !strpos($args[0], 'logger')) { | |
116 | return; | |
117 | } | |
118 | return XDB::_query(XDB::_prepare($args)); | |
0337d704 | 119 | } |
13a25546 | 120 | |
6995a9b9 | 121 | public static function iterator() |
0337d704 | 122 | { |
08cce2ff | 123 | return new XOrgDBIterator(XDB::_prepare(func_get_args())); |
0337d704 | 124 | } |
13a25546 | 125 | |
6995a9b9 | 126 | public static function iterRow() |
0337d704 | 127 | { |
08cce2ff | 128 | return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM); |
0337d704 | 129 | } |
13a25546 | 130 | |
6995a9b9 | 131 | public static function insertId() |
13a25546 | 132 | { |
32d9ae72 | 133 | return XDB::$mysqli->insert_id; |
13a25546 | 134 | } |
135 | ||
0380bf85 | 136 | public static function errno() |
137 | { | |
0380bf85 | 138 | return XDB::$mysqli->errno; |
139 | } | |
140 | ||
141 | public static function error() | |
834fd0f6 | 142 | { |
0380bf85 | 143 | return XDB::$mysqli->error; |
144 | } | |
145 | ||
146 | public static function affectedRows() | |
147 | { | |
148 | return XDB::$mysqli->affected_rows; | |
149 | } | |
150 | ||
f62bd784 | 151 | public static function escape($var) |
0337d704 | 152 | { |
153 | switch (gettype($var)) { | |
13a25546 | 154 | case 'boolean': |
155 | return $var ? 1 : 0; | |
156 | ||
157 | case 'integer': | |
158 | case 'double': | |
159 | case 'float': | |
160 | return $var; | |
161 | ||
162 | case 'string': | |
163 | return "'".addslashes($var)."'"; | |
164 | ||
165 | case 'NULL': | |
166 | return 'NULL'; | |
167 | ||
168 | case 'object': | |
169 | case 'array': | |
170 | return "'".addslashes(serialize($var))."'"; | |
171 | ||
172 | default: | |
173 | die(var_export($var, true).' is not a valid for a database entry'); | |
0337d704 | 174 | } |
175 | } | |
0337d704 | 176 | } |
177 | ||
0337d704 | 178 | class XOrgDBResult |
179 | { | |
0337d704 | 180 | |
32d9ae72 | 181 | private $_res; |
0337d704 | 182 | |
0381e170 | 183 | public function __construct($query) |
0337d704 | 184 | { |
755abda6 | 185 | $this->_res = XDB::_query($query); |
0337d704 | 186 | } |
187 | ||
0381e170 | 188 | public function free() |
0337d704 | 189 | { |
0381e170 | 190 | if ($this->_res) { |
191 | $this->_res->free(); | |
192 | } | |
0337d704 | 193 | unset($this); |
194 | } | |
195 | ||
0381e170 | 196 | protected function _fetchRow() |
0337d704 | 197 | { |
0381e170 | 198 | return $this->_res ? $this->_res->fetch_row() : null; |
0337d704 | 199 | } |
200 | ||
0381e170 | 201 | protected function _fetchAssoc() |
0337d704 | 202 | { |
0381e170 | 203 | return $this->_res ? $this->_res->fetch_assoc() : null; |
0337d704 | 204 | } |
205 | ||
0381e170 | 206 | public function fetchAllRow() |
0337d704 | 207 | { |
208 | $result = Array(); | |
0381e170 | 209 | if (!$this->_res) { |
210 | return $result; | |
211 | } | |
32d9ae72 | 212 | while ($result[] = $this->_res->fetch_row()); |
0337d704 | 213 | array_pop($result); |
214 | $this->free(); | |
215 | return $result; | |
216 | } | |
217 | ||
0381e170 | 218 | public function fetchAllAssoc() |
0337d704 | 219 | { |
220 | $result = Array(); | |
0381e170 | 221 | if (!$this->_res) { |
222 | return $result; | |
223 | } | |
32d9ae72 | 224 | while ($result[] = $this->_res->fetch_assoc()); |
0337d704 | 225 | array_pop($result); |
226 | $this->free(); | |
227 | return $result; | |
228 | } | |
229 | ||
0381e170 | 230 | public function fetchOneAssoc() |
0337d704 | 231 | { |
232 | $tmp = $this->_fetchAssoc(); | |
233 | $this->free(); | |
234 | return $tmp; | |
235 | } | |
236 | ||
0381e170 | 237 | public function fetchOneRow() |
0337d704 | 238 | { |
239 | $tmp = $this->_fetchRow(); | |
240 | $this->free(); | |
241 | return $tmp; | |
242 | } | |
243 | ||
0381e170 | 244 | public function fetchOneCell() |
0337d704 | 245 | { |
246 | $tmp = $this->_fetchRow(); | |
247 | $this->free(); | |
248 | return $tmp[0]; | |
249 | } | |
250 | ||
0381e170 | 251 | public function fetchColumn($key = 0) |
0337d704 | 252 | { |
253 | $res = Array(); | |
254 | if (is_numeric($key)) { | |
255 | while($tmp = $this->_fetchRow()) { | |
256 | $res[] = $tmp[$key]; | |
257 | } | |
258 | } else { | |
259 | while($tmp = $this->_fetchAssoc()) { | |
260 | $res[] = $tmp[$key]; | |
261 | } | |
262 | } | |
263 | $this->free(); | |
264 | return $res; | |
265 | } | |
266 | ||
0381e170 | 267 | public function fetchOneField() |
0380bf85 | 268 | { |
0381e170 | 269 | return $this->_res ? $this->_res->fetch_field() : null; |
0380bf85 | 270 | } |
271 | ||
0381e170 | 272 | public function fetchFields() |
0380bf85 | 273 | { |
274 | $res = array(); | |
275 | while ($res[] = $this->fetchOneField()); | |
276 | return $res; | |
277 | } | |
278 | ||
0381e170 | 279 | public function numRows() |
0337d704 | 280 | { |
0381e170 | 281 | return $this->_res ? $this->_res->num_rows : 0; |
0337d704 | 282 | } |
0380bf85 | 283 | |
0381e170 | 284 | public function fieldCount() |
0380bf85 | 285 | { |
0381e170 | 286 | return $this->_res ? $this->_res->field_count : 0; |
0380bf85 | 287 | } |
0337d704 | 288 | } |
289 | ||
2b1ee50b | 290 | require_once dirname(__FILE__) . '/pliterator.php'; |
291 | ||
0381e170 | 292 | class XOrgDBIterator extends XOrgDBResult implements PlIterator |
0337d704 | 293 | { |
85d3b330 | 294 | private $_result; |
295 | private $_pos; | |
296 | private $_total; | |
0380bf85 | 297 | private $_fpos; |
298 | private $_fields; | |
85d3b330 | 299 | private $_mode = MYSQL_ASSOC; |
0337d704 | 300 | |
0381e170 | 301 | public function __construct($query, $mode = MYSQL_ASSOC) |
0337d704 | 302 | { |
0381e170 | 303 | parent::__construct($query); |
0337d704 | 304 | $this->_pos = 0; |
0381e170 | 305 | $this->_total = $this->numRows(); |
0380bf85 | 306 | $this->_fpost = 0; |
0381e170 | 307 | $this->_fields = $this->fieldCount(); |
0337d704 | 308 | $this->_mode = $mode; |
309 | } | |
310 | ||
0381e170 | 311 | public function next() |
0337d704 | 312 | { |
313 | $this->_pos ++; | |
314 | if ($this->_pos > $this->_total) { | |
0381e170 | 315 | $this->free(); |
0337d704 | 316 | unset($this); |
317 | return null; | |
318 | } | |
0381e170 | 319 | return $this->_mode != MYSQL_ASSOC ? $this->_fetchRow() : $this->_fetchAssoc(); |
0337d704 | 320 | } |
321 | ||
0381e170 | 322 | public function first() |
0337d704 | 323 | { |
324 | return $this->_pos == 1; | |
325 | } | |
326 | ||
0381e170 | 327 | public function last() |
0337d704 | 328 | { |
0380bf85 | 329 | return $this->_pos == $this->_total; |
0337d704 | 330 | } |
331 | ||
0381e170 | 332 | public function total() |
0337d704 | 333 | { |
334 | return $this->_total; | |
335 | } | |
0380bf85 | 336 | |
0381e170 | 337 | public function nextField() |
0380bf85 | 338 | { |
339 | $this->_fpos++; | |
340 | if ($this->_fpos > $this->_fields) { | |
341 | return null; | |
342 | } | |
0381e170 | 343 | return $this->fetchOneField(); |
0380bf85 | 344 | } |
345 | ||
0381e170 | 346 | public function firstField() |
0380bf85 | 347 | { |
348 | return $this->_fpos == 1; | |
349 | } | |
350 | ||
0381e170 | 351 | public function lastField() |
0380bf85 | 352 | { |
353 | return $this->_fpos == $this->_fields; | |
354 | } | |
355 | ||
0381e170 | 356 | public function totalFields() |
0380bf85 | 357 | { |
358 | return $this->_fields; | |
359 | } | |
0337d704 | 360 | } |
361 | ||
a7de4ef7 | 362 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 363 | ?> |