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($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);
32 public static function _reformatQuery($query)
34 $query = preg_split("/\n\\s*/", $query);
36 foreach ($query as $key=>$line) {
38 if (preg_match('/^([A-Z]+(?:\s+(?:JOIN|BY|FROM|INTO))?)\s+(.*)/', $line, $matches)
39 && $matches[1] != 'AND' && $matches[1] != 'OR')
41 $local = strlen($matches[1]);
42 $line = $matches[1] . ' ' . $matches[2];
43 $length = max($length, $local);
45 $query[$key] = array($line, $local);
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, ')'));
57 public static function _query($query) {
60 if ($globals->debug
& 1) {
61 $_res = mysql_query("EXPLAIN $query");
63 while ($row = @mysql_fetch_assoc
($_res)) {
66 $trace_data = array('query' => XDB
::_reformatQuery($query), 'explain' => $explain);
67 @mysql_free_result
($_res);
70 $res = mysql_query($query);
72 if ($globals->debug
& 1) {
73 $trace_data['error'] = mysql_error();
74 $GLOBALS['XDB::trace_data'][] = $trace_data;
76 $GLOBALS['XDB::error'] = true
;
83 public static function query()
85 return new XOrgDBResult(XDB
::_prepare(func_get_args()));
88 public static function execute()
90 return XDB
::_query(XDB
::_prepare(func_get_args()));
93 public static function iterator()
95 return new XOrgDBIterator(XDB
::_prepare(func_get_args()));
98 public static function iterRow()
100 return new XOrgDBIterator(XDB
::_prepare(func_get_args()), MYSQL_NUM
);
103 public static function insertId()
105 return mysql_insert_id();
108 public static function _db_escape($var)
110 switch (gettype($var)) {
120 return "'".addslashes($var)."'";
127 return "'".addslashes(serialize($var))."'";
130 die(var_export($var, true
).' is not a valid for a database entry');
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);
146 function XOrgDBResult($query)
148 $this->_res
= XDB
::_query($query);
153 mysql_free_result($this->_res
);
159 return mysql_fetch_row($this->_res
);
162 function _fetchAssoc()
164 return mysql_fetch_assoc($this->_res
);
167 function fetchAllRow()
170 while ($result[] = mysql_fetch_row($this->_res
)) { }
176 function fetchAllAssoc()
179 while ($result[] = mysql_fetch_assoc($this->_res
)) { }
185 function fetchOneAssoc()
187 $tmp = $this->_fetchAssoc();
192 function fetchOneRow()
194 $tmp = $this->_fetchRow();
199 function fetchOneCell()
201 $tmp = $this->_fetchRow();
206 function fetchColumn($key = 0)
209 if (is_numeric($key)) {
210 while($tmp = $this->_fetchRow()) {
214 while($tmp = $this->_fetchAssoc()) {
224 return mysql_num_rows($this->_res
);
233 private $_mode = MYSQL_ASSOC
;
235 function __construct($query, $mode = MYSQL_ASSOC
)
237 $this->_result
=& new XOrgDBResult($query);
239 $this->_total
= $this->_result
->numRows();
240 $this->_mode
= $mode;
246 if ($this->_pos
> $this->_total
) {
247 $this->_result
->free();
251 return $this->_mode
!= MYSQL_ASSOC ?
$this->_result
->_fetchRow() : $this->_result
->_fetchAssoc();
256 return $this->_pos
== 1;
261 return $this->_last
== $this->_total
;
266 return $this->_total
;
270 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: