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