0337d704 |
1 | <?php |
2 | /*************************************************************************** |
50a40a33 |
3 | * Copyright (C) 2003-2006 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 | { |
f1ca33de |
24 | var $_trace_data = array(); |
25 | |
6995a9b9 |
26 | public static function _prepare($args) { |
08cce2ff |
27 | $query = array_map(Array('XDB', '_db_escape'), $args); |
0337d704 |
28 | $query[0] = str_replace('{?}', '%s', str_replace('%', '%%', $args[0])); |
29 | return call_user_func_array('sprintf', $query); |
30 | } |
13a25546 |
31 | |
6995a9b9 |
32 | public static function _reformatQuery($query) |
7c571120 |
33 | { |
34 | $query = preg_split("/\n\\s*/", $query); |
35 | $length = 0; |
d3c52d30 |
36 | foreach ($query as $key=>$line) { |
37 | $local = -2; |
eadff9f9 |
38 | if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/', $line, $matches) |
85d3b330 |
39 | && $matches[1] != 'AND' && $matches[1] != 'OR') |
40 | { |
d3c52d30 |
41 | $local = strlen($matches[1]); |
42 | $line = $matches[1] . ' ' . $matches[2]; |
43 | $length = max($length, $local); |
7c571120 |
44 | } |
d3c52d30 |
45 | $query[$key] = array($line, $local); |
7c571120 |
46 | } |
47 | $res = ''; |
d3c52d30 |
48 | foreach ($query as $array) { |
49 | list($line, $local) = $array; |
9630c649 |
50 | $local = max(0, $length - $local); |
7c571120 |
51 | $res .= str_repeat(' ', $local) . $line . "\n"; |
52 | $length += 2 * (substr_count($line, '(') - substr_count($line, ')')); |
53 | } |
54 | return $res; |
55 | } |
56 | |
6995a9b9 |
57 | public static function _query($query) { |
f1ca33de |
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 | } |
7c571120 |
66 | $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain); |
f1ca33de |
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; |
eadff9f9 |
75 | if (mysql_errno()) { |
76 | $GLOBALS['XDB::error'] = true; |
77 | } |
f1ca33de |
78 | } |
79 | |
80 | return $res; |
81 | } |
82 | |
6995a9b9 |
83 | public static function query() |
0337d704 |
84 | { |
08cce2ff |
85 | return new XOrgDBResult(XDB::_prepare(func_get_args())); |
0337d704 |
86 | } |
87 | |
6995a9b9 |
88 | public static function execute() |
f1ca33de |
89 | { |
90 | return XDB::_query(XDB::_prepare(func_get_args())); |
0337d704 |
91 | } |
13a25546 |
92 | |
6995a9b9 |
93 | public static function iterator() |
0337d704 |
94 | { |
08cce2ff |
95 | return new XOrgDBIterator(XDB::_prepare(func_get_args())); |
0337d704 |
96 | } |
13a25546 |
97 | |
6995a9b9 |
98 | public static function iterRow() |
0337d704 |
99 | { |
08cce2ff |
100 | return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM); |
0337d704 |
101 | } |
13a25546 |
102 | |
6995a9b9 |
103 | public static function insertId() |
13a25546 |
104 | { |
105 | return mysql_insert_id(); |
106 | } |
107 | |
6995a9b9 |
108 | public static function _db_escape($var) |
0337d704 |
109 | { |
110 | switch (gettype($var)) { |
13a25546 |
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'); |
0337d704 |
131 | } |
132 | } |
133 | |
6995a9b9 |
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']); |
f1ca33de |
137 | return $page->fetch($template); |
138 | } |
0337d704 |
139 | } |
140 | |
0337d704 |
141 | class XOrgDBResult |
142 | { |
0337d704 |
143 | |
144 | var $_res; |
145 | |
0337d704 |
146 | function XOrgDBResult($query) |
147 | { |
755abda6 |
148 | $this->_res = XDB::_query($query); |
0337d704 |
149 | } |
150 | |
0337d704 |
151 | function free() |
152 | { |
153 | mysql_free_result($this->_res); |
154 | unset($this); |
155 | } |
156 | |
0337d704 |
157 | function _fetchRow() |
158 | { |
159 | return mysql_fetch_row($this->_res); |
160 | } |
161 | |
0337d704 |
162 | function _fetchAssoc() |
163 | { |
164 | return mysql_fetch_assoc($this->_res); |
165 | } |
166 | |
0337d704 |
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 | |
0337d704 |
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 | |
0337d704 |
185 | function fetchOneAssoc() |
186 | { |
187 | $tmp = $this->_fetchAssoc(); |
188 | $this->free(); |
189 | return $tmp; |
190 | } |
191 | |
0337d704 |
192 | function fetchOneRow() |
193 | { |
194 | $tmp = $this->_fetchRow(); |
195 | $this->free(); |
196 | return $tmp; |
197 | } |
198 | |
0337d704 |
199 | function fetchOneCell() |
200 | { |
201 | $tmp = $this->_fetchRow(); |
202 | $this->free(); |
203 | return $tmp[0]; |
204 | } |
205 | |
0337d704 |
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 | |
0337d704 |
222 | function numRows() |
223 | { |
224 | return mysql_num_rows($this->_res); |
225 | } |
0337d704 |
226 | } |
227 | |
da88cfee |
228 | class XOrgDBIterator |
0337d704 |
229 | { |
85d3b330 |
230 | private $_result; |
231 | private $_pos; |
232 | private $_total; |
233 | private $_mode = MYSQL_ASSOC; |
0337d704 |
234 | |
85d3b330 |
235 | function __construct($query, $mode = MYSQL_ASSOC) |
0337d704 |
236 | { |
237 | $this->_result =& new XOrgDBResult($query); |
238 | $this->_pos = 0; |
239 | $this->_total = $this->_result->numRows(); |
240 | $this->_mode = $mode; |
241 | } |
242 | |
0337d704 |
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 | |
0337d704 |
254 | function first() |
255 | { |
256 | return $this->_pos == 1; |
257 | } |
258 | |
0337d704 |
259 | function last() |
260 | { |
261 | return $this->_last == $this->_total; |
262 | } |
263 | |
0337d704 |
264 | function total() |
265 | { |
266 | return $this->_total; |
267 | } |
0337d704 |
268 | } |
269 | |
0337d704 |
270 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
271 | ?> |