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 | { |
0337d704 |
24 | // {{{ function _prepare |
25 | |
26 | 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 | |
0337d704 |
32 | // }}} |
33 | // {{{ function query |
34 | |
35 | function &query() |
36 | { |
08cce2ff |
37 | return new XOrgDBResult(XDB::_prepare(func_get_args())); |
0337d704 |
38 | } |
39 | |
40 | // }}} |
41 | // {{{ function execute() |
42 | |
43 | function execute() { |
44 | global $globals; |
08cce2ff |
45 | return $globals->db->query(XDB::_prepare(func_get_args())); |
0337d704 |
46 | } |
13a25546 |
47 | |
0337d704 |
48 | // }}} |
49 | // {{{ function iterator() |
50 | |
51 | function &iterator() |
52 | { |
08cce2ff |
53 | return new XOrgDBIterator(XDB::_prepare(func_get_args())); |
0337d704 |
54 | } |
13a25546 |
55 | |
0337d704 |
56 | // }}} |
57 | // {{{ function iterRow() |
58 | |
59 | function &iterRow() |
60 | { |
08cce2ff |
61 | return new XOrgDBIterator(XDB::_prepare(func_get_args()), MYSQL_NUM); |
0337d704 |
62 | } |
13a25546 |
63 | |
64 | // }}} |
65 | // {{{ function insertId() |
66 | |
67 | function insertId() |
68 | { |
69 | return mysql_insert_id(); |
70 | } |
71 | |
0337d704 |
72 | // }}} |
73 | // {{{ function _db_escape |
74 | |
75 | function _db_escape($var) |
76 | { |
77 | switch (gettype($var)) { |
13a25546 |
78 | case 'boolean': |
79 | return $var ? 1 : 0; |
80 | |
81 | case 'integer': |
82 | case 'double': |
83 | case 'float': |
84 | return $var; |
85 | |
86 | case 'string': |
87 | return "'".addslashes($var)."'"; |
88 | |
89 | case 'NULL': |
90 | return 'NULL'; |
91 | |
92 | case 'object': |
93 | case 'array': |
94 | return "'".addslashes(serialize($var))."'"; |
95 | |
96 | default: |
97 | die(var_export($var, true).' is not a valid for a database entry'); |
0337d704 |
98 | } |
99 | } |
100 | |
101 | // }}} |
102 | } |
103 | |
0337d704 |
104 | class XOrgDBResult |
105 | { |
106 | // {{{ properties |
107 | |
108 | var $_res; |
109 | |
110 | // }}} |
111 | // {{{ constructor |
112 | |
113 | function XOrgDBResult($query) |
114 | { |
115 | global $globals; |
116 | if (strpos($query, 'SQL_CALC_FOUND_ROWS') === false) { |
117 | $this->_res = $globals->db->query($query); |
118 | } else { |
119 | $this->_res = mysql_query($query); |
120 | } |
121 | } |
122 | |
123 | // }}} |
124 | // {{{ destructor |
125 | |
126 | function free() |
127 | { |
128 | mysql_free_result($this->_res); |
129 | unset($this); |
130 | } |
131 | |
132 | // }}} |
133 | // {{{ function fetchRow |
134 | |
135 | function _fetchRow() |
136 | { |
137 | return mysql_fetch_row($this->_res); |
138 | } |
139 | |
140 | // }}} |
141 | // {{{ function fetchAssoc |
142 | |
143 | function _fetchAssoc() |
144 | { |
145 | return mysql_fetch_assoc($this->_res); |
146 | } |
147 | |
148 | // }}} |
149 | // {{{ function fetchAllRow |
150 | |
151 | function fetchAllRow() |
152 | { |
153 | $result = Array(); |
154 | while ($result[] = mysql_fetch_row($this->_res)) { } |
155 | array_pop($result); |
156 | $this->free(); |
157 | return $result; |
158 | } |
159 | |
160 | // }}} |
161 | // {{{ function fetchAllAssoc |
162 | |
163 | function fetchAllAssoc() |
164 | { |
165 | $result = Array(); |
166 | while ($result[] = mysql_fetch_assoc($this->_res)) { } |
167 | array_pop($result); |
168 | $this->free(); |
169 | return $result; |
170 | } |
171 | |
172 | // }}} |
173 | // {{{ function fetchOneAssoc() |
174 | |
175 | function fetchOneAssoc() |
176 | { |
177 | $tmp = $this->_fetchAssoc(); |
178 | $this->free(); |
179 | return $tmp; |
180 | } |
181 | |
182 | // }}} |
183 | // {{{ function fetchOneRow() |
184 | |
185 | function fetchOneRow() |
186 | { |
187 | $tmp = $this->_fetchRow(); |
188 | $this->free(); |
189 | return $tmp; |
190 | } |
191 | |
192 | // }}} |
193 | // {{{ function fetchOneCell() |
194 | |
195 | function fetchOneCell() |
196 | { |
197 | $tmp = $this->_fetchRow(); |
198 | $this->free(); |
199 | return $tmp[0]; |
200 | } |
201 | |
202 | // }}} |
203 | // {{{ function fetchColumn() |
204 | |
205 | function fetchColumn($key = 0) |
206 | { |
207 | $res = Array(); |
208 | if (is_numeric($key)) { |
209 | while($tmp = $this->_fetchRow()) { |
210 | $res[] = $tmp[$key]; |
211 | } |
212 | } else { |
213 | while($tmp = $this->_fetchAssoc()) { |
214 | $res[] = $tmp[$key]; |
215 | } |
216 | } |
217 | $this->free(); |
218 | return $res; |
219 | } |
220 | |
221 | // }}} |
222 | // {{{ function numRows |
13a25546 |
223 | |
0337d704 |
224 | function numRows() |
225 | { |
226 | return mysql_num_rows($this->_res); |
227 | } |
228 | |
229 | // }}} |
230 | } |
231 | |
da88cfee |
232 | class XOrgDBIterator |
0337d704 |
233 | { |
234 | // {{{ properties |
235 | |
236 | var $_result; |
237 | var $_pos; |
238 | var $_total; |
239 | var $_mode = MYSQL_ASSOC; |
240 | |
241 | // }}} |
242 | // {{{ constructor |
13a25546 |
243 | |
0337d704 |
244 | function XOrgDBIterator($query, $mode = MYSQL_ASSOC) |
245 | { |
246 | $this->_result =& new XOrgDBResult($query); |
247 | $this->_pos = 0; |
248 | $this->_total = $this->_result->numRows(); |
249 | $this->_mode = $mode; |
250 | } |
251 | |
252 | // }}} |
253 | // {{{ function next () |
13a25546 |
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 | |
266 | // }}} |
267 | // {{{ function first |
268 | |
269 | function first() |
270 | { |
271 | return $this->_pos == 1; |
272 | } |
273 | |
274 | // }}} |
275 | // {{{ function last |
276 | |
277 | function last() |
278 | { |
279 | return $this->_last == $this->_total; |
280 | } |
281 | |
282 | // }}} |
283 | // {{{ function total() |
284 | |
285 | function total() |
286 | { |
287 | return $this->_total; |
288 | } |
289 | |
290 | // }}} |
291 | } |
292 | |
0337d704 |
293 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
294 | ?> |