[ Index ] |
PHP Cross Reference of phpBB 3.0 Beta 3 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * 4 * @package dbal 5 * @version $Id: mysql.php,v 1.54 2006/11/03 17:49:08 acydburn Exp $ 6 * @copyright (c) 2005 phpBB Group 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 8 * 9 */ 10 11 /** 12 * @ignore 13 */ 14 if (!defined('IN_PHPBB')) 15 { 16 exit; 17 } 18 19 include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx); 20 21 /** 22 * MySQL4 Database Abstraction Layer 23 * Compatible with: 24 * MySQL 3.23+ 25 * MySQL 4.0+ 26 * MySQL 4.1+ 27 * MySQL 5.0+ 28 * @package dbal 29 */ 30 class dbal_mysql extends dbal 31 { 32 var $mysql_version; 33 34 /** 35 * Connect to server 36 * @access public 37 */ 38 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) 39 { 40 $this->persistency = $persistency; 41 $this->user = $sqluser; 42 $this->server = $sqlserver . (($port) ? ':' . $port : ''); 43 $this->dbname = $database; 44 45 $this->sql_layer = 'mysql4'; 46 47 $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $sqlpassword) : @mysql_connect($this->server, $this->user, $sqlpassword); 48 49 if ($this->db_connect_id && $this->dbname != '') 50 { 51 if (@mysql_select_db($this->dbname)) 52 { 53 // Determine what version we are using and if it natively supports UNICODE 54 $this->mysql_version = mysql_get_server_info($this->db_connect_id); 55 56 if (version_compare($this->mysql_version, '4.1.3', '>=')) 57 { 58 @mysql_query("SET NAMES 'utf8'", $this->db_connect_id); 59 } 60 else if (version_compare($this->mysql_version, '4.0.0', '<')) 61 { 62 $this->sql_layer = 'mysql'; 63 } 64 65 return $this->db_connect_id; 66 } 67 } 68 69 return $this->sql_error(''); 70 } 71 72 /** 73 * Version information about used database 74 */ 75 function sql_server_info() 76 { 77 return 'MySQL ' . $this->mysql_version; 78 } 79 80 /** 81 * SQL Transaction 82 * @access private 83 */ 84 function _sql_transaction($status = 'begin') 85 { 86 switch ($status) 87 { 88 case 'begin': 89 return @mysql_query('BEGIN', $this->db_connect_id); 90 break; 91 92 case 'commit': 93 return @mysql_query('COMMIT', $this->db_connect_id); 94 break; 95 96 case 'rollback': 97 return @mysql_query('ROLLBACK', $this->db_connect_id); 98 break; 99 } 100 101 return true; 102 } 103 104 /** 105 * Base query method 106 * 107 * @param string $query Contains the SQL query which shall be executed 108 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache 109 * @return mixed When casted to bool the returned value returns true on success and false on failure 110 * 111 * @access public 112 */ 113 function sql_query($query = '', $cache_ttl = 0) 114 { 115 if ($query != '') 116 { 117 global $cache; 118 119 // EXPLAIN only in extra debug mode 120 if (defined('DEBUG_EXTRA')) 121 { 122 $this->sql_report('start', $query); 123 } 124 125 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; 126 $this->sql_add_num_queries($this->query_result); 127 128 if ($this->query_result === false) 129 { 130 if (($this->query_result = @mysql_query($query, $this->db_connect_id)) === false) 131 { 132 $this->sql_error($query); 133 } 134 135 if (defined('DEBUG_EXTRA')) 136 { 137 $this->sql_report('stop', $query); 138 } 139 140 if ($cache_ttl && method_exists($cache, 'sql_save')) 141 { 142 $this->open_queries[(int) $this->query_result] = $this->query_result; 143 $cache->sql_save($query, $this->query_result, $cache_ttl); 144 } 145 else if (strpos($query, 'SELECT') === 0 && $this->query_result) 146 { 147 $this->open_queries[(int) $this->query_result] = $this->query_result; 148 } 149 } 150 else if (defined('DEBUG_EXTRA')) 151 { 152 $this->sql_report('fromcache', $query); 153 } 154 } 155 else 156 { 157 return false; 158 } 159 160 return ($this->query_result) ? $this->query_result : false; 161 } 162 163 /** 164 * Build LIMIT query 165 */ 166 function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 167 { 168 if ($query != '') 169 { 170 $this->query_result = false; 171 172 // if $total is set to 0 we do not want to limit the number of rows 173 if ($total == 0) 174 { 175 // Having a value of -1 was always a bug 176 $total = '18446744073709551615'; 177 } 178 179 $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total); 180 181 return $this->sql_query($query, $cache_ttl); 182 } 183 else 184 { 185 return false; 186 } 187 } 188 189 /** 190 * Return number of affected rows 191 */ 192 function sql_affectedrows() 193 { 194 return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false; 195 } 196 197 /** 198 * Fetch current row 199 */ 200 function sql_fetchrow($query_id = false) 201 { 202 global $cache; 203 204 if ($query_id === false) 205 { 206 $query_id = $this->query_result; 207 } 208 209 if (isset($cache->sql_rowset[$query_id])) 210 { 211 return $cache->sql_fetchrow($query_id); 212 } 213 214 return ($query_id !== false) ? @mysql_fetch_assoc($query_id) : false; 215 } 216 217 /** 218 * Seek to given row number 219 * rownum is zero-based 220 */ 221 function sql_rowseek($rownum, $query_id = false) 222 { 223 global $cache; 224 225 if ($query_id === false) 226 { 227 $query_id = $this->query_result; 228 } 229 230 if (isset($cache->sql_rowset[$query_id])) 231 { 232 return $cache->sql_rowseek($rownum, $query_id); 233 } 234 235 return ($query_id !== false) ? @mysql_data_seek($query_id, $rownum) : false; 236 } 237 238 /** 239 * Get last inserted id after insert statement 240 */ 241 function sql_nextid() 242 { 243 return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false; 244 } 245 246 /** 247 * Free sql result 248 */ 249 function sql_freeresult($query_id = false) 250 { 251 global $cache; 252 253 if ($query_id === false) 254 { 255 $query_id = $this->query_result; 256 } 257 258 if (isset($cache->sql_rowset[$query_id])) 259 { 260 return $cache->sql_freeresult($query_id); 261 } 262 263 if (isset($this->open_queries[(int) $query_id])) 264 { 265 unset($this->open_queries[(int) $query_id]); 266 return @mysql_free_result($query_id); 267 } 268 269 return false; 270 } 271 272 /** 273 * Escape string used in sql query 274 */ 275 function sql_escape($msg) 276 { 277 if (!$this->db_connect_id) 278 { 279 return @mysql_real_escape_string($msg); 280 } 281 282 return @mysql_real_escape_string($msg, $this->db_connect_id); 283 } 284 285 /** 286 * Build db-specific query data 287 * @access private 288 */ 289 function _sql_custom_build($stage, $data) 290 { 291 switch ($stage) 292 { 293 case 'FROM': 294 $data = '(' . $data . ')'; 295 break; 296 } 297 298 return $data; 299 } 300 301 /** 302 * return sql error array 303 * @access private 304 */ 305 function _sql_error() 306 { 307 if (!$this->db_connect_id) 308 { 309 return array( 310 'message' => @mysql_error(), 311 'code' => @mysql_errno() 312 ); 313 } 314 315 return array( 316 'message' => @mysql_error($this->db_connect_id), 317 'code' => @mysql_errno($this->db_connect_id) 318 ); 319 } 320 321 /** 322 * Close sql connection 323 * @access private 324 */ 325 function _sql_close() 326 { 327 return @mysql_close($this->db_connect_id); 328 } 329 330 /** 331 * Build db-specific report 332 * @access private 333 */ 334 function _sql_report($mode, $query = '') 335 { 336 switch ($mode) 337 { 338 case 'start': 339 340 $explain_query = $query; 341 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) 342 { 343 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; 344 } 345 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) 346 { 347 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; 348 } 349 350 if (preg_match('/^SELECT/', $explain_query)) 351 { 352 $html_table = false; 353 354 if ($result = @mysql_query("EXPLAIN $explain_query", $this->db_connect_id)) 355 { 356 while ($row = @mysql_fetch_assoc($result)) 357 { 358 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row); 359 } 360 } 361 @mysql_free_result($result); 362 363 if ($html_table) 364 { 365 $this->html_hold .= '</table>'; 366 } 367 } 368 369 break; 370 371 case 'fromcache': 372 $endtime = explode(' ', microtime()); 373 $endtime = $endtime[0] + $endtime[1]; 374 375 $result = @mysql_query($query, $this->db_connect_id); 376 while ($void = @mysql_fetch_assoc($result)) 377 { 378 // Take the time spent on parsing rows into account 379 } 380 @mysql_free_result($result); 381 382 $splittime = explode(' ', microtime()); 383 $splittime = $splittime[0] + $splittime[1]; 384 385 $this->sql_report('record_fromcache', $query, $endtime, $splittime); 386 387 break; 388 } 389 } 390 } 391 392 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 22 00:35:05 2006 | Cross-referenced by PHPXref 0.6 |