[ 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: firebird.php,v 1.43 2006/11/10 14:56:18 davidmj 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 * Firebird/Interbase Database Abstraction Layer 23 * Minimum Requirement is Firebird 2.0 24 * @package dbal 25 */ 26 class dbal_firebird extends dbal 27 { 28 var $last_query_text = ''; 29 var $service_handle = false; 30 31 /** 32 * Connect to server 33 */ 34 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) 35 { 36 $this->persistency = $persistency; 37 $this->user = $sqluser; 38 $this->server = $sqlserver . (($port) ? ':' . $port : ''); 39 $this->dbname = $database; 40 41 $this->db_connect_id = ($this->persistency) ? @ibase_pconnect($this->server . ':' . $this->dbname, $this->user, $sqlpassword, false, false, 3) : @ibase_connect($this->server . ':' . $this->dbname, $this->user, $sqlpassword, false, false, 3); 42 43 $this->service_handle = (function_exists('ibase_service_attach')) ? @ibase_service_attach($this->server, $this->user, $sqlpassword) : false; 44 45 return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); 46 } 47 48 /** 49 * Version information about used database 50 */ 51 function sql_server_info() 52 { 53 if ($this->service_handle !== false && function_exists('ibase_server_info')) 54 { 55 return @ibase_server_info($this->service_handle, IBASE_SVC_SERVER_VERSION); 56 } 57 58 return 'Firebird/Interbase'; 59 } 60 61 /** 62 * SQL Transaction 63 * @access private 64 */ 65 function _sql_transaction($status = 'begin') 66 { 67 switch ($status) 68 { 69 case 'begin': 70 return true; 71 break; 72 73 case 'commit': 74 return @ibase_commit(); 75 break; 76 77 case 'rollback': 78 return @ibase_rollback(); 79 break; 80 } 81 82 return true; 83 } 84 85 /** 86 * Base query method 87 * 88 * @param string $query Contains the SQL query which shall be executed 89 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache 90 * @return mixed When casted to bool the returned value returns true on success and false on failure 91 * 92 * @access public 93 */ 94 function sql_query($query = '', $cache_ttl = 0) 95 { 96 if ($query != '') 97 { 98 global $cache; 99 100 $this->last_query_text = $query; 101 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; 102 $this->sql_add_num_queries($this->query_result); 103 104 if ($this->query_result === false) 105 { 106 if (($this->query_result = @ibase_query($this->db_connect_id, $query)) === false) 107 { 108 $this->sql_error($query); 109 } 110 111 if (!$this->transaction) 112 { 113 if (function_exists('ibase_commit_ret')) 114 { 115 @ibase_commit_ret(); 116 } 117 else 118 { 119 // way cooler than ibase_commit_ret :D 120 @ibase_query('COMMIT RETAIN;'); 121 } 122 } 123 124 if ($cache_ttl && method_exists($cache, 'sql_save')) 125 { 126 $this->open_queries[(int) $this->query_result] = $this->query_result; 127 $cache->sql_save($query, $this->query_result, $cache_ttl); 128 } 129 else if (strpos($query, 'SELECT') === 0 && $this->query_result) 130 { 131 $this->open_queries[(int) $this->query_result] = $this->query_result; 132 } 133 } 134 } 135 else 136 { 137 return false; 138 } 139 140 return ($this->query_result) ? $this->query_result : false; 141 } 142 143 /** 144 * Build LIMIT query 145 */ 146 function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 147 { 148 if ($query != '') 149 { 150 $this->query_result = false; 151 152 $query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6); 153 154 return $this->sql_query($query, $cache_ttl); 155 } 156 else 157 { 158 return false; 159 } 160 } 161 162 /** 163 * Return number of affected rows 164 */ 165 function sql_affectedrows() 166 { 167 // PHP 5+ function 168 if (function_exists('ibase_affected_rows')) 169 { 170 return ($this->db_connect_id) ? @ibase_affected_rows($this->db_connect_id) : false; 171 } 172 else 173 { 174 return ($this->query_result) ? true : false; 175 } 176 } 177 178 /** 179 * Fetch current row 180 */ 181 function sql_fetchrow($query_id = false) 182 { 183 global $cache; 184 185 if ($query_id === false) 186 { 187 $query_id = $this->query_result; 188 } 189 190 if (isset($cache->sql_rowset[$query_id])) 191 { 192 return $cache->sql_fetchrow($query_id); 193 } 194 195 if ($query_id === false) 196 { 197 return false; 198 } 199 200 $row = array(); 201 $cur_row = @ibase_fetch_object($query_id, IBASE_TEXT); 202 203 if (!$cur_row) 204 { 205 return false; 206 } 207 208 foreach (get_object_vars($cur_row) as $key => $value) 209 { 210 $row[strtolower($key)] = trim(str_replace(array("\\0", "\\n"), array("\0", "\n"), $value)); 211 } 212 213 return (sizeof($row)) ? $row : false; 214 } 215 216 /** 217 * Seek to given row number 218 * rownum is zero-based 219 */ 220 function sql_rowseek($rownum, $query_id = false) 221 { 222 global $cache; 223 224 if ($query_id === false) 225 { 226 $query_id = $this->query_result; 227 } 228 229 if (isset($cache->sql_rowset[$query_id])) 230 { 231 return $cache->sql_rowseek($rownum, $query_id); 232 } 233 234 if ($query_id === false) 235 { 236 return; 237 } 238 239 $this->sql_freeresult($query_id); 240 $query_id = $this->sql_query($this->last_query_text); 241 242 if ($query_id === false) 243 { 244 return false; 245 } 246 247 // We do not fetch the row for rownum == 0 because then the next resultset would be the second row 248 for ($i = 0; $i < $rownum; $i++) 249 { 250 if (!$this->sql_fetchrow($query_id)) 251 { 252 return false; 253 } 254 } 255 256 return true; 257 } 258 259 /** 260 * Get last inserted id after insert statement 261 */ 262 function sql_nextid() 263 { 264 $query_id = $this->query_result; 265 266 if ($query_id !== false && $this->last_query_text != '') 267 { 268 if ($this->query_result && preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text, $tablename)) 269 { 270 $sql = "SELECT GEN_ID(" . $tablename[1] . "_gen, 0) AS new_id FROM RDB\$DATABASE"; 271 272 if (!($temp_q_id = @ibase_query($this->db_connect_id, $sql))) 273 { 274 return false; 275 } 276 277 $temp_result = @ibase_fetch_object($temp_q_id); 278 @ibase_free_result($temp_q_id); 279 280 return ($temp_result) ? $temp_result->NEW_ID : false; 281 } 282 } 283 284 return false; 285 } 286 287 /** 288 * Free sql result 289 */ 290 function sql_freeresult($query_id = false) 291 { 292 global $cache; 293 294 if ($query_id === false) 295 { 296 $query_id = $this->query_result; 297 } 298 299 if (isset($cache->sql_rowset[$query_id])) 300 { 301 return $cache->sql_freeresult($query_id); 302 } 303 304 if (isset($this->open_queries[(int) $query_id])) 305 { 306 unset($this->open_queries[(int) $query_id]); 307 return @ibase_free_result($query_id); 308 } 309 310 return false; 311 } 312 313 /** 314 * Escape string used in sql query 315 */ 316 function sql_escape($msg) 317 { 318 return (@ini_get('magic_quotes_sybase') == 1 || strtolower(@ini_get('magic_quotes_sybase')) == 'on') ? str_replace('\\\'', '\'', addslashes($msg)) : str_replace('\'', '\'\'', stripslashes($msg)); 319 } 320 321 /** 322 * Build db-specific query data 323 * @access private 324 */ 325 function _sql_custom_build($stage, $data) 326 { 327 return $data; 328 } 329 330 /** 331 * return sql error array 332 * @access private 333 */ 334 function _sql_error() 335 { 336 return array( 337 'message' => @ibase_errmsg(), 338 'code' => (@function_exists('ibase_errcode') ? @ibase_errcode() : '') 339 ); 340 } 341 342 /** 343 * Close sql connection 344 * @access private 345 */ 346 function _sql_close() 347 { 348 if ($this->service_handle !== false) 349 { 350 @ibase_service_detach($this->service_handle); 351 } 352 353 return @ibase_close($this->db_connect_id); 354 } 355 356 /** 357 * Build db-specific report 358 * @access private 359 */ 360 function _sql_report($mode, $query = '') 361 { 362 switch ($mode) 363 { 364 case 'start': 365 break; 366 367 case 'fromcache': 368 $endtime = explode(' ', microtime()); 369 $endtime = $endtime[0] + $endtime[1]; 370 371 $result = @ibase_query($this->db_connect_id, $query); 372 while ($void = @ibase_fetch_object($result, IBASE_TEXT)) 373 { 374 // Take the time spent on parsing rows into account 375 } 376 @ibase_free_result($result); 377 378 $splittime = explode(' ', microtime()); 379 $splittime = $splittime[0] + $splittime[1]; 380 381 $this->sql_report('record_fromcache', $query, $endtime, $splittime); 382 383 break; 384 } 385 } 386 } 387 388 ?>
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 |