5
* @version $Id: firebird.php,v 1.57 2007/10/05 14:36:32 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
19
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
22
* Firebird/Interbase Database Abstraction Layer
23
* Minimum Requirement is Firebird 2.0
26
class dbal_firebird extends dbal
28
var $last_query_text = '';
29
var $service_handle = false;
30
var $affected_rows = 0;
35
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
37
$this->persistency = $persistency;
38
$this->user = $sqluser;
39
$this->server = $sqlserver . (($port) ? ':' . $port : '');
40
$this->dbname = $database;
42
$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);
44
$this->service_handle = (function_exists('ibase_service_attach')) ? @ibase_service_attach($this->server, $this->user, $sqlpassword) : false;
46
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
50
* Version information about used database
52
function sql_server_info()
54
if ($this->service_handle !== false && function_exists('ibase_server_info'))
56
return @ibase_server_info($this->service_handle, IBASE_SVC_SERVER_VERSION);
59
return 'Firebird/Interbase';
66
function _sql_transaction($status = 'begin')
75
return @ibase_commit();
79
return @ibase_rollback();
89
* @param string $query Contains the SQL query which shall be executed
90
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
91
* @return mixed When casted to bool the returned value returns true on success and false on failure
95
function sql_query($query = '', $cache_ttl = 0)
101
// EXPLAIN only in extra debug mode
102
if (defined('DEBUG_EXTRA'))
104
$this->sql_report('start', $query);
107
$this->last_query_text = $query;
108
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
109
$this->sql_add_num_queries($this->query_result);
111
if ($this->query_result === false)
114
// We overcome Firebird's 32767 char limit by binding vars
115
if (strlen($query) > 32767)
117
if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs))
119
if (strlen($regs[3]) > 32767)
121
preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
126
foreach ($inserts as $key => $value)
128
if (!empty($value) && $value[0] === "'" && strlen($value) > 32769) // check to see if this thing is greater than the max + 'x2
130
$inserts[$key] = '?';
131
$array[] = str_replace("''", "'", substr($value, 1, -1));
135
$query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
138
else if (preg_match('/^(UPDATE ([\\w_]++)\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|\\d+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data))
140
if (strlen($data[3]) > 32767)
144
preg_match_all('/(\\w++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[3], $temp, PREG_SET_ORDER);
148
foreach ($temp as $value)
150
if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 32769) // check to see if this thing is greater than the max + 'x2
152
$array[] = str_replace("''", "'", substr($value[2], 1, -1));
153
$cols[] = $value[1] . '=?';
157
$cols[] = $value[1] . '=' . $value[2];
161
$query = $update . implode(', ', $cols) . ' ' . $where;
167
if (!function_exists('ibase_affected_rows') && (preg_match('/^UPDATE ([\w_]++)\s+SET [\w_]++\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+)(?:,\s*[\w_]++\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+\s+(WHERE.*)?$/s', $query, $regs) || preg_match('/^DELETE FROM ([\w_]++)\s*(WHERE\s*.*)?$/s', $query, $regs)))
169
$affected_sql = 'SELECT COUNT(*) as num_rows_affected FROM ' . $regs[1];
170
if (!empty($regs[2]))
172
$affected_sql .= ' ' . $regs[2];
175
if (!($temp_q_id = @ibase_query($this->db_connect_id, $affected_sql)))
180
$temp_result = @ibase_fetch_assoc($temp_q_id);
181
@ibase_free_result($temp_q_id);
183
$this->affected_rows = ($temp_result) ? $temp_result['NUM_ROWS_AFFECTED'] : false;
188
$p_query = @ibase_prepare($this->db_connect_id, $query);
189
array_unshift($array, $p_query);
190
$this->query_result = call_user_func_array('ibase_execute', $array);
193
if ($this->query_result === false)
195
$this->sql_error($query);
198
else if (($this->query_result = @ibase_query($this->db_connect_id, $query)) === false)
200
$this->sql_error($query);
203
if (defined('DEBUG_EXTRA'))
205
$this->sql_report('stop', $query);
208
if (!$this->transaction)
210
if (function_exists('ibase_commit_ret'))
216
// way cooler than ibase_commit_ret :D
217
@ibase_query('COMMIT RETAIN;');
221
if ($cache_ttl && method_exists($cache, 'sql_save'))
223
$this->open_queries[(int) $this->query_result] = $this->query_result;
224
$cache->sql_save($query, $this->query_result, $cache_ttl);
226
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
228
$this->open_queries[(int) $this->query_result] = $this->query_result;
231
else if (defined('DEBUG_EXTRA'))
233
$this->sql_report('fromcache', $query);
241
return ($this->query_result) ? $this->query_result : false;
247
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
249
$this->query_result = false;
251
$query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6);
253
return $this->sql_query($query, $cache_ttl);
257
* Return number of affected rows
259
function sql_affectedrows()
262
if (function_exists('ibase_affected_rows'))
264
return ($this->db_connect_id) ? @ibase_affected_rows($this->db_connect_id) : false;
268
return $this->affected_rows;
275
function sql_fetchrow($query_id = false)
279
if ($query_id === false)
281
$query_id = $this->query_result;
284
if (isset($cache->sql_rowset[$query_id]))
286
return $cache->sql_fetchrow($query_id);
289
if ($query_id === false)
295
$cur_row = @ibase_fetch_object($query_id, IBASE_TEXT);
302
foreach (get_object_vars($cur_row) as $key => $value)
304
$row[strtolower($key)] = (is_string($value)) ? trim(str_replace(array("\\0", "\\n"), array("\0", "\n"), $value)) : $value;
307
return (sizeof($row)) ? $row : false;
311
* Seek to given row number
312
* rownum is zero-based
314
function sql_rowseek($rownum, &$query_id)
318
if ($query_id === false)
320
$query_id = $this->query_result;
323
if (isset($cache->sql_rowset[$query_id]))
325
return $cache->sql_rowseek($rownum, $query_id);
328
if ($query_id === false)
333
$this->sql_freeresult($query_id);
334
$query_id = $this->sql_query($this->last_query_text);
336
if ($query_id === false)
341
// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
342
for ($i = 0; $i < $rownum; $i++)
344
if (!$this->sql_fetchrow($query_id))
354
* Get last inserted id after insert statement
356
function sql_nextid()
358
$query_id = $this->query_result;
360
if ($query_id !== false && $this->last_query_text != '')
362
if ($this->query_result && preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#i', $this->last_query_text, $tablename))
364
$sql = 'SELECT GEN_ID(' . $tablename[1] . '_gen, 0) AS new_id FROM RDB$DATABASE';
366
if (!($temp_q_id = @ibase_query($this->db_connect_id, $sql)))
371
$temp_result = @ibase_fetch_assoc($temp_q_id);
372
@ibase_free_result($temp_q_id);
374
return ($temp_result) ? $temp_result['NEW_ID'] : false;
384
function sql_freeresult($query_id = false)
388
if ($query_id === false)
390
$query_id = $this->query_result;
393
if (isset($cache->sql_rowset[$query_id]))
395
return $cache->sql_freeresult($query_id);
398
if (isset($this->open_queries[(int) $query_id]))
400
unset($this->open_queries[(int) $query_id]);
401
return @ibase_free_result($query_id);
408
* Escape string used in sql query
410
function sql_escape($msg)
412
return str_replace("'", "''", $msg);
416
* Build LIKE expression
419
function _sql_like_expression($expression)
421
return $expression . " ESCAPE '\\'";
425
* Build db-specific query data
428
function _sql_custom_build($stage, $data)
434
* return sql error array
437
function _sql_error()
440
'message' => @ibase_errmsg(),
441
'code' => (@function_exists('ibase_errcode') ? @ibase_errcode() : '')
446
* Close sql connection
449
function _sql_close()
451
if ($this->service_handle !== false)
453
@ibase_service_detach($this->service_handle);
456
return @ibase_close($this->db_connect_id);
460
* Build db-specific report
463
function _sql_report($mode, $query = '')
471
$endtime = explode(' ', microtime());
472
$endtime = $endtime[0] + $endtime[1];
474
$result = @ibase_query($this->db_connect_id, $query);
475
while ($void = @ibase_fetch_object($result, IBASE_TEXT))
477
// Take the time spent on parsing rows into account
479
@ibase_free_result($result);
481
$splittime = explode(' ', microtime());
482
$splittime = $splittime[0] + $splittime[1];
484
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
b'\\ No newline at end of file'