958b3e2ce36339945e3e502a8d02eec3d18d2f52
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 **************************************************************************/
24 var $_trace_data = array();
26 // {{{ public static function _prepare
28 public static function _prepare($args) {
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);
35 // {{{ public static function _reformatQuery
37 public static function _reformatQuery($query)
39 $query = preg_split("/\n\\s*/", $query);
41 foreach ($query as $key=>$line) {
43 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/', $line, $matches)
44 && $matches[1] != 'AND' && $matches[1] != 'OR') {
45 $local = strlen($matches[1]);
46 $line = $matches[1] . ' ' . $matches[2];
47 $length = max($length, $local);
49 $query[$key] = array($line, $local);
52 foreach ($query as $array) {
53 list($line, $local) = $array;
54 $local = max(0, $length - $local);
55 $res .= str_repeat(' ', $local) . $line . "\n";
56 $length +
= 2 * (substr_count($line, '(') - substr_count($line, ')'));
62 // {{{ public static function _query
64 public static function _query($query) {
67 if ($globals->debug
& 1) {
68 $_res = mysql_query("EXPLAIN $query");
70 while ($row = @mysql_fetch_assoc
($_res)) {
73 $trace_data = array('query' => XDB
::_reformatQuery($query), 'explain' => $explain);
74 @mysql_free_result
($_res);
77 $res = mysql_query($query);
79 if ($globals->debug
& 1) {
80 $trace_data['error'] = mysql_error();
81 $GLOBALS['XDB::trace_data'][] = $trace_data;
83 $GLOBALS['XDB::error'] = true
;
91 // {{{ public static function query
93 public static function query()
95 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
99 // {{{ public static function execute()
101 public static function execute()
103 return XDB
::_query(XDB
::_prepare(func_get_args()));
107 // {{{ public static function iterator()
109 public static function iterator()
111 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
115 // {{{ public static function iterRow()
117 public static function iterRow()
119 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
123 // {{{ public static function insertId()
125 public static function insertId()
127 return mysql_insert_id();
131 // {{{ public static function _db_escape
133 public static function _db_escape($var)
135 switch (gettype($var)) {
145 return "'".addslashes($var)."'";
152 return "'".addslashes(serialize($var))."'";
155 die(var_export($var, true
).' is not a valid for a database entry');
161 public static function trace_format(&$page, $template = 'database-debug.tpl') {
162 $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
163 $page->assign('db_error', @$GLOBALS['XDB::error']);
164 return $page->fetch($template);
177 function XOrgDBResult($query)
179 $this->_res
= XDB
::_query($query);
187 mysql_free_result($this->_res
);
192 // {{{ function fetchRow
196 return mysql_fetch_row($this->_res
);
200 // {{{ function fetchAssoc
202 function _fetchAssoc()
204 return mysql_fetch_assoc($this->_res
);
208 // {{{ function fetchAllRow
210 function fetchAllRow()
213 while ($result[] = mysql_fetch_row($this->_res
)) { }
220 // {{{ function fetchAllAssoc
222 function fetchAllAssoc()
225 while ($result[] = mysql_fetch_assoc($this->_res
)) { }
232 // {{{ function fetchOneAssoc()
234 function fetchOneAssoc()
236 $tmp = $this->_fetchAssoc();
242 // {{{ function fetchOneRow()
244 function fetchOneRow()
246 $tmp = $this->_fetchRow();
252 // {{{ function fetchOneCell()
254 function fetchOneCell()
256 $tmp = $this->_fetchRow();
262 // {{{ function fetchColumn()
264 function fetchColumn($key = 0)
267 if (is_numeric($key)) {
268 while($tmp = $this->_fetchRow()) {
272 while($tmp = $this->_fetchAssoc()) {
281 // {{{ function numRows
285 return mysql_num_rows($this->_res
);
298 var $_mode = MYSQL_ASSOC
;
303 function XOrgDBIterator($query, $mode = MYSQL_ASSOC
)
305 $this->_result
=& new XOrgDBResult($query);
307 $this->_total
= $this->_result
->numRows();
308 $this->_mode
= $mode;
312 // {{{ function next ()
317 if ($this->_pos
> $this->_total
) {
318 $this->_result
->free();
322 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
326 // {{{ function first
330 return $this->_pos
== 1;
338 return $this->_last
== $this->_total
;
342 // {{{ function total()
346 return $this->_total
;
352 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: