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