Many UTF-8 fixes:
[platal.git] / classes / xdb.php
CommitLineData
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 22class XDB
0337d704 23{
a14159bf 24 public static $connec = null;
f1ca33de 25 var $_trace_data = array();
26
ed3f4d3e 27 public static function _prepare($args)
28 {
08cce2ff 29 $query = array_map(Array('XDB', '_db_escape'), $args);
0337d704 30 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
31 return call_user_func_array('sprintf', $query);
32 }
13a25546 33
6995a9b9 34 public static function _reformatQuery($query)
7c571120 35 {
a4f89886 36 $query = preg_split("/\n\\s*/", trim($query));
7c571120 37 $length = 0;
d3c52d30 38 foreach ($query as $key=>$line) {
39 $local = -2;
a14159bf 40 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches)
85d3b330 41 && $matches[1] != 'AND' && $matches[1] != 'OR')
42 {
d3c52d30 43 $local = strlen($matches[1]);
44 $line = $matches[1] . ' ' . $matches[2];
45 $length = max($length, $local);
7c571120 46 }
d3c52d30 47 $query[$key] = array($line, $local);
7c571120 48 }
49 $res = '';
d3c52d30 50 foreach ($query as $array) {
51 list($line, $local) = $array;
9630c649 52 $local = max(0, $length - $local);
7c571120 53 $res .= str_repeat(' ', $local) . $line . "\n";
54 $length += 2 * (substr_count($line, '(') - substr_count($line, ')'));
55 }
56 return $res;
57 }
58
ed3f4d3e 59 public static function _query($query)
60 {
f1ca33de 61 global $globals;
62
a14159bf 63 if ($globals->debug & 1 && strpos($query, 'FOUND_ROWS()') === false) {
64 $_res = mysqli_query(XDB::$connec, "EXPLAIN $query");
f1ca33de 65 $explain = array();
a14159bf 66 while ($row = mysqli_fetch_assoc($_res)) {
f1ca33de 67 $explain[] = $row;
68 }
7c571120 69 $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain);
a14159bf 70 @mysqli_free_result($_res);
71 $time_start = microtime();
72 } elseif ($globals->debug & 1) {
73 $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => array());
ed3f4d3e 74 $time_start = microtime();
f1ca33de 75 }
76
a14159bf 77 $res = mysqli_query(XDB::$connec, $query);
78
f1ca33de 79 if ($globals->debug & 1) {
ed3f4d3e 80 list($ue, $se) = explode(" ", microtime());
81 list($us, $ss) = explode(" ", $time_start);
82 $time = intval((($ue - $us) + ($se - $ss)) * 1000);
a14159bf 83 $trace_data['error'] = mysqli_error(XDB::$connec);
ed3f4d3e 84 $trace_data['exectime'] = $time;
a14159bf 85 $trace_data['rows'] = @mysqli_num_rows($res) ? mysqli_num_rows($res) : mysqli_affected_rows(XDB::$connec);
f1ca33de 86 $GLOBALS['XDB::trace_data'][] = $trace_data;
a14159bf 87 if (mysqli_errno(XDB::$connec)) {
eadff9f9 88 $GLOBALS['XDB::error'] = true;
89 }
f1ca33de 90 }
91
92 return $res;
93 }
94
6995a9b9 95 public static function query()
0337d704 96 {
08cce2ff 97 return new XOrgDBResult(XDB::_prepare(func_get_args()));
0337d704 98 }
99
6995a9b9 100 public static function execute()
f1ca33de 101 {
102 return XDB::_query(XDB::_prepare(func_get_args()));
0337d704 103 }
13a25546 104
6995a9b9 105 public static function iterator()
0337d704 106 {
08cce2ff 107 return new XOrgDBIterator(XDB::_prepare(func_get_args()));
0337d704 108 }
13a25546 109
6995a9b9 110 public static function iterRow()
0337d704 111 {
08cce2ff 112 return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM);
0337d704 113 }
13a25546 114
6995a9b9 115 public static function insertId()
13a25546 116 {
a14159bf 117 return mysqli_insert_id(XDB::$connec);
13a25546 118 }
119
6995a9b9 120 public static function _db_escape($var)
0337d704 121 {
122 switch (gettype($var)) {
13a25546 123 case 'boolean':
124 return $var ? 1 : 0;
125
126 case 'integer':
127 case 'double':
128 case 'float':
129 return $var;
130
131 case 'string':
132 return "'".addslashes($var)."'";
133
134 case 'NULL':
135 return 'NULL';
136
137 case 'object':
138 case 'array':
139 return "'".addslashes(serialize($var))."'";
140
141 default:
142 die(var_export($var, true).' is not a valid for a database entry');
0337d704 143 }
144 }
145
8b1f8e12 146 public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
6995a9b9 147 $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
148 $page->assign('db_error', @$GLOBALS['XDB::error']);
f1ca33de 149 return $page->fetch($template);
150 }
0337d704 151}
152
0337d704 153class XOrgDBResult
154{
0337d704 155
156 var $_res;
157
0337d704 158 function XOrgDBResult($query)
159 {
755abda6 160 $this->_res = XDB::_query($query);
0337d704 161 }
162
0337d704 163 function free()
164 {
a14159bf 165 mysqli_free_result($this->_res);
0337d704 166 unset($this);
167 }
168
0337d704 169 function _fetchRow()
170 {
a14159bf 171 return mysqli_fetch_row($this->_res);
0337d704 172 }
173
0337d704 174 function _fetchAssoc()
175 {
a14159bf 176 return mysqli_fetch_assoc($this->_res);
0337d704 177 }
178
0337d704 179 function fetchAllRow()
180 {
181 $result = Array();
a14159bf 182 while ($result[] = mysqli_fetch_row($this->_res)) { }
0337d704 183 array_pop($result);
184 $this->free();
185 return $result;
186 }
187
0337d704 188 function fetchAllAssoc()
189 {
190 $result = Array();
a14159bf 191 while ($result[] = mysqli_fetch_assoc($this->_res)) { }
0337d704 192 array_pop($result);
193 $this->free();
194 return $result;
195 }
196
0337d704 197 function fetchOneAssoc()
198 {
199 $tmp = $this->_fetchAssoc();
200 $this->free();
201 return $tmp;
202 }
203
0337d704 204 function fetchOneRow()
205 {
206 $tmp = $this->_fetchRow();
207 $this->free();
208 return $tmp;
209 }
210
0337d704 211 function fetchOneCell()
212 {
213 $tmp = $this->_fetchRow();
214 $this->free();
215 return $tmp[0];
216 }
217
0337d704 218 function fetchColumn($key = 0)
219 {
220 $res = Array();
221 if (is_numeric($key)) {
222 while($tmp = $this->_fetchRow()) {
223 $res[] = $tmp[$key];
224 }
225 } else {
226 while($tmp = $this->_fetchAssoc()) {
227 $res[] = $tmp[$key];
228 }
229 }
230 $this->free();
231 return $res;
232 }
233
0337d704 234 function numRows()
235 {
a14159bf 236 return mysqli_num_rows($this->_res);
0337d704 237 }
0337d704 238}
239
da88cfee 240class XOrgDBIterator
0337d704 241{
85d3b330 242 private $_result;
243 private $_pos;
244 private $_total;
245 private $_mode = MYSQL_ASSOC;
0337d704 246
85d3b330 247 function __construct($query, $mode = MYSQL_ASSOC)
0337d704 248 {
a0f05027 249 $this->_result = new XOrgDBResult($query);
0337d704 250 $this->_pos = 0;
251 $this->_total = $this->_result->numRows();
252 $this->_mode = $mode;
253 }
254
0337d704 255 function next()
256 {
257 $this->_pos ++;
258 if ($this->_pos > $this->_total) {
259 $this->_result->free();
260 unset($this);
261 return null;
262 }
263 return $this->_mode != MYSQL_ASSOC ? $this->_result->_fetchRow() : $this->_result->_fetchAssoc();
264 }
265
0337d704 266 function first()
267 {
268 return $this->_pos == 1;
269 }
270
0337d704 271 function last()
272 {
273 return $this->_last == $this->_total;
274 }
275
0337d704 276 function total()
277 {
278 return $this->_total;
279 }
0337d704 280}
281
a7de4ef7 282// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 283?>