Many UTF-8 fixes:
[platal.git] / classes / xdb.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
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
22 class XDB
23 {
24 public static $connec = null;
25 var $_trace_data = array();
26
27 public static function _prepare($args)
28 {
29 $query = array_map(Array('XDB', '_db_escape'), $args);
30 $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0]));
31 return call_user_func_array('sprintf', $query);
32 }
33
34 public static function _reformatQuery($query)
35 {
36 $query = preg_split("/\n\\s*/", trim($query));
37 $length = 0;
38 foreach ($query as $key=>$line) {
39 $local = -2;
40 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/u', $line, $matches)
41 && $matches[1] != 'AND' && $matches[1] != 'OR')
42 {
43 $local = strlen($matches[1]);
44 $line = $matches[1] . ' ' . $matches[2];
45 $length = max($length, $local);
46 }
47 $query[$key] = array($line, $local);
48 }
49 $res = '';
50 foreach ($query as $array) {
51 list($line, $local) = $array;
52 $local = max(0, $length - $local);
53 $res .= str_repeat(' ', $local) . $line . "\n";
54 $length += 2 * (substr_count($line, '(') - substr_count($line, ')'));
55 }
56 return $res;
57 }
58
59 public static function _query($query)
60 {
61 global $globals;
62
63 if ($globals->debug & 1 && strpos($query, 'FOUND_ROWS()') === false) {
64 $_res = mysqli_query(XDB::$connec, "EXPLAIN $query");
65 $explain = array();
66 while ($row = mysqli_fetch_assoc($_res)) {
67 $explain[] = $row;
68 }
69 $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain);
70 @mysqli_free_result($_res);
71 $time_start = microtime();
72 } elseif ($globals->debug & 1) {
73 $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => array());
74 $time_start = microtime();
75 }
76
77 $res = mysqli_query(XDB::$connec, $query);
78
79 if ($globals->debug & 1) {
80 list($ue, $se) = explode(" ", microtime());
81 list($us, $ss) = explode(" ", $time_start);
82 $time = intval((($ue - $us) + ($se - $ss)) * 1000);
83 $trace_data['error'] = mysqli_error(XDB::$connec);
84 $trace_data['exectime'] = $time;
85 $trace_data['rows'] = @mysqli_num_rows($res) ? mysqli_num_rows($res) : mysqli_affected_rows(XDB::$connec);
86 $GLOBALS['XDB::trace_data'][] = $trace_data;
87 if (mysqli_errno(XDB::$connec)) {
88 $GLOBALS['XDB::error'] = true;
89 }
90 }
91
92 return $res;
93 }
94
95 public static function query()
96 {
97 return new XOrgDBResult(XDB::_prepare(func_get_args()));
98 }
99
100 public static function execute()
101 {
102 return XDB::_query(XDB::_prepare(func_get_args()));
103 }
104
105 public static function iterator()
106 {
107 return new XOrgDBIterator(XDB::_prepare(func_get_args()));
108 }
109
110 public static function iterRow()
111 {
112 return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM);
113 }
114
115 public static function insertId()
116 {
117 return mysqli_insert_id(XDB::$connec);
118 }
119
120 public static function _db_escape($var)
121 {
122 switch (gettype($var)) {
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');
143 }
144 }
145
146 public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
147 $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
148 $page->assign('db_error', @$GLOBALS['XDB::error']);
149 return $page->fetch($template);
150 }
151 }
152
153 class XOrgDBResult
154 {
155
156 var $_res;
157
158 function XOrgDBResult($query)
159 {
160 $this->_res = XDB::_query($query);
161 }
162
163 function free()
164 {
165 mysqli_free_result($this->_res);
166 unset($this);
167 }
168
169 function _fetchRow()
170 {
171 return mysqli_fetch_row($this->_res);
172 }
173
174 function _fetchAssoc()
175 {
176 return mysqli_fetch_assoc($this->_res);
177 }
178
179 function fetchAllRow()
180 {
181 $result = Array();
182 while ($result[] = mysqli_fetch_row($this->_res)) { }
183 array_pop($result);
184 $this->free();
185 return $result;
186 }
187
188 function fetchAllAssoc()
189 {
190 $result = Array();
191 while ($result[] = mysqli_fetch_assoc($this->_res)) { }
192 array_pop($result);
193 $this->free();
194 return $result;
195 }
196
197 function fetchOneAssoc()
198 {
199 $tmp = $this->_fetchAssoc();
200 $this->free();
201 return $tmp;
202 }
203
204 function fetchOneRow()
205 {
206 $tmp = $this->_fetchRow();
207 $this->free();
208 return $tmp;
209 }
210
211 function fetchOneCell()
212 {
213 $tmp = $this->_fetchRow();
214 $this->free();
215 return $tmp[0];
216 }
217
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
234 function numRows()
235 {
236 return mysqli_num_rows($this->_res);
237 }
238 }
239
240 class XOrgDBIterator
241 {
242 private $_result;
243 private $_pos;
244 private $_total;
245 private $_mode = MYSQL_ASSOC;
246
247 function __construct($query, $mode = MYSQL_ASSOC)
248 {
249 $this->_result = new XOrgDBResult($query);
250 $this->_pos = 0;
251 $this->_total = $this->_result->numRows();
252 $this->_mode = $mode;
253 }
254
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
266 function first()
267 {
268 return $this->_pos == 1;
269 }
270
271 function last()
272 {
273 return $this->_last == $this->_total;
274 }
275
276 function total()
277 {
278 return $this->_total;
279 }
280 }
281
282 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
283 ?>