- interbase/firebird 1.5+ support (still needs some tweaking)

git-svn-id: file:///svn/phpbb/trunk@5051 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2005-01-09 20:01:20 +00:00
parent fdac7fa800
commit e5ae182a38
5 changed files with 1793 additions and 1045 deletions

View file

@ -1,23 +1,15 @@
<?php <?php
/*************************************************************************** // -------------------------------------------------------------
* firebird.php //
* ------------------- // $Id$
* begin : Saturday, Feb 13, 2001 //
* copyright :(C) 2001 The phpBB Group // FILENAME : firebird.php
* email : support@phpbb.com // STARTED : Sat Feb 13, 2001
* // COPYRIGHT : © 2001, 2003 phpBB Group
* $Id$ // WWW : http://www.phpbb.com/
* // LICENCE : GPL vs2.0 [ see /docs/COPYING ]
***************************************************************************/ //
// -------------------------------------------------------------
/***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/
if (!defined('SQL_LAYER')) if (!defined('SQL_LAYER'))
{ {
@ -30,25 +22,28 @@ class sql_db
var $query_result; var $query_result;
var $return_on_error = false; var $return_on_error = false;
var $transaction = false; var $transaction = false;
var $sql_report = '';
var $sql_time = 0; var $sql_time = 0;
var $num_queries = 0;
var $open_queries = array();
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database = '', $port = '', $persistency = false) var $last_query_text = '';
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false)
{ {
$this->open_queries = array();
$this->num_queries = 0;
$this->persistency = $persistency; $this->persistency = $persistency;
$this->user = $sqluser; $this->user = $sqluser;
$this->password = $sqlpassword; $this->password = $sqlpassword;
$this->server = $sqlserver; $this->server = $sqlserver . (($port) ? ':' . $port : '');
$this->dbname = $database;
$this->db_connect_id =($this->persistency) ? @ibase_pconnect($this->server, $this->user, $this->password, false, false, 3) : @ibase_connect($this->server, $this->user, $this->password, false, false, 3); $this->db_connect_id = ($this->persistency) ? @ibase_pconnect($this->server . ':' . $this->dbname, $this->user, $this->password, false, false, 3) : @ibase_connect($this->server . ':' . $this->dbname, $this->user, $this->password, false, false, 3);
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
} }
//
// Other base methods // Other base methods
//
function sql_close() function sql_close()
{ {
if (!$this->db_connect_id) if (!$this->db_connect_id)
@ -56,9 +51,14 @@ class sql_db
return false; return false;
} }
if (count($this->open_queries)) if ($this->transaction)
{ {
foreach($this->open_queries as $query_id) @ibase_commit($this->db_connect_id);
}
if (sizeof($this->open_queries))
{
foreach ($this->open_queries as $i_query_id => $query_id)
{ {
@ibase_free_query($query_id); @ibase_free_query($query_id);
} }
@ -79,19 +79,19 @@ class sql_db
function sql_transaction($status = 'begin') function sql_transaction($status = 'begin')
{ {
switch($status) switch ($status)
{ {
case 'begin': case 'begin':
$this->transaction = true; $this->transaction = true;
break; break;
case 'commit': case 'commit':
$result = ibase_commit(); $result = @ibase_commit();
$this->transaction = false; $this->transaction = false;
break; break;
case 'rollback': case 'rollback':
$result = ibase_rollback(); $result = @ibase_rollback();
$this->transaction = false; $this->transaction = false;
break; break;
@ -103,95 +103,34 @@ class sql_db
} }
// Base query method // Base query method
function sql_query($query = '', $expire_time = 0) function sql_query($query = '', $cache_ttl = 0)
{ {
if ($query != '') if ($query != '')
{ {
global $cache; global $cache;
if (!$expire_time || !$cache->sql_load($query, $expire_time)) $this->last_query_text = $query;
{ $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
if ($expire_time)
{
$cache_result = true;
}
$this->query_result = false; if (!$this->query_result)
{
$this->num_queries++; $this->num_queries++;
if (!empty($_GET['explain'])) if (($this->query_result = @ibase_query($this->db_connect_id, $query)) === false)
{
global $starttime;
$curtime = explode(' ', microtime());
$curtime = $curtime[0] + $curtime[1] - $starttime;
}
if (($this->query_result = ibase_query($query, $this->db_connect_id)) === FALSE)
{ {
$this->sql_error($query); $this->sql_error($query);
} }
if (!$this->transaction && (strpos($query, 'INSERT') === 0 || strpos($query, 'UPDATE') === 0)) // TODO: have to debug the commit states in firebird
if (!$this->transaction)
{ {
echo $query; @ibase_commit_ret();
ibase_commit();
} }
if (!empty($_GET['explain'])) if ($cache_ttl && method_exists($cache, 'sql_save'))
{ {
$endtime = explode(' ', microtime()); $cache->sql_save($query, $this->query_result, $cache_ttl);
$endtime = $endtime[0] + $endtime[1] - $starttime;
$this->sql_report .= "<pre>Query:\t" . htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n\t", $query)) . "\n\n";
if ($this->query_result)
{
$this->sql_report .= "Time before: $curtime\nTime after: $endtime\nElapsed time: <b>" .($endtime - $curtime) . "</b>\n</pre>";
} }
else
{
$error = $this->sql_error();
$this->sql_report .= '<b>FAILED</b> - SQL Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']) . '<br><br><pre>';
}
$this->sql_time += $endtime - $curtime;
/*
if (preg_match('/^SELECT/', $query))
{
$html_table = FALSE;
if ($result = mysql_query("EXPLAIN $query", $this->db_connect_id))
{
while($row = mysql_fetch_assoc($result))
{
if (!$html_table && count($row))
{
$html_table = TRUE;
$this->sql_report .= "<table width=100% border=1 cellpadding=2 cellspacing=1>\n";
$this->sql_report .= "<tr>\n<td><b>" . implode("</b></td>\n<td><b>", array_keys($row)) . "</b></td>\n</tr>\n";
}
$this->sql_report .= "<tr>\n<td>" . implode("&nbsp;</td>\n<td>", array_values($row)) . "&nbsp;</td>\n</tr>\n";
}
}
if ($html_table)
{
$this->sql_report .= '</table><br>';
}
}
*/
$this->sql_report .= "<hr>\n";
}
$this->open_queries[] = $this->query_result;
}
$this->last_query_text[$this->query_result] = $query;
if (!empty($cache_result))
{
$cache->sql_save($query, $this->query_result);
@ibase_free_result(array_pop($this->open_queries));
} }
} }
else else
@ -202,16 +141,15 @@ class sql_db
return ($this->query_result) ? $this->query_result : false; return ($this->query_result) ? $this->query_result : false;
} }
function sql_query_limit($query, $total, $offset = 0, $expire_time = 0) function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
{ {
if ($query != '') if ($query != '')
{ {
$this->query_result = false; $this->query_result = false;
$this->num_queries++;
$query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6); $query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6);
return $this->sql_query($query, $expire_time); return $this->sql_query($query, $cache_ttl);
} }
else else
{ {
@ -231,7 +169,7 @@ class sql_db
$values = array(); $values = array();
if ($query == 'INSERT') if ($query == 'INSERT')
{ {
foreach($assoc_ary as $key => $var) foreach ($assoc_ary as $key => $var)
{ {
$fields[] = $key; $fields[] = $key;
@ -245,16 +183,16 @@ class sql_db
} }
else else
{ {
$values[] =(is_bool($var)) ? intval($var) : $var; $values[] = (is_bool($var)) ? intval($var) : $var;
} }
} }
$query = '(' . implode(', ', $fields) . ') VALUES(' . implode(', ', $values) . ')'; $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')';
} }
else if ($query == 'UPDATE') else if ($query == 'UPDATE' || $query == 'SELECT')
{ {
$values = array(); $values = array();
foreach($assoc_ary as $key => $var) foreach ($assoc_ary as $key => $var)
{ {
if (is_null($var)) if (is_null($var))
{ {
@ -266,16 +204,19 @@ class sql_db
} }
else else
{ {
$values[] =(is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var";
} }
} }
$query = implode(', ', $values); $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values);
} }
return $query; return $query;
} }
// Other query methods // Other query methods
//
// NOTE :: Want to remove _ALL_ reliance on sql_numrows from core code ...
// don't want this here by a middle Milestone
function sql_numrows($query_id = false) function sql_numrows($query_id = false)
{ {
return FALSE; return FALSE;
@ -283,10 +224,11 @@ class sql_db
function sql_affectedrows() function sql_affectedrows()
{ {
return ($this->query_result !== FALSE) ? TRUE : FALSE; // Does this work? // hmm, maybe doing something similar as in mssql-odbc.php?
return ($this->query_result) ? true : false;
} }
function sql_fetchrow($query_id = 0) function sql_fetchrow($query_id = false)
{ {
global $cache; global $cache;
@ -295,20 +237,27 @@ class sql_db
$query_id = $this->query_result; $query_id = $this->query_result;
} }
if ($cache->sql_exists($query_id)) if (isset($cache->sql_rowset[$query_id]))
{ {
return $cache->sql_fetchrow($query_id); return $cache->sql_fetchrow($query_id);
} }
$row = array(); $row = array();
foreach (get_object_vars(ibase_fetch_object($query_id, IBASE_TEXT)) as $key => $value) $cur_row = @ibase_fetch_object($query_id, IBASE_TEXT);
if (!$cur_row)
{
return false;
}
foreach (get_object_vars($cur_row) as $key => $value)
{ {
$row[strtolower($key)] = trim(str_replace("\\0", "\0", str_replace("\\n", "\n", $value))); $row[strtolower($key)] = trim(str_replace("\\0", "\0", str_replace("\\n", "\n", $value)));
} }
return ($query_id) ? $row : false; return ($query_id) ? $row : false;
} }
function sql_fetchrowset($query_id = 0) function sql_fetchrowset($query_id = false)
{ {
if (!$query_id) if (!$query_id)
{ {
@ -319,10 +268,12 @@ class sql_db
{ {
unset($this->rowset[$query_id]); unset($this->rowset[$query_id]);
unset($this->row[$query_id]); unset($this->row[$query_id]);
while($this->rowset[$query_id] = get_object_vars(@ibase_fetch_object($query_id, IBASE_TEXT )))
while ($this->rowset[$query_id] = get_object_vars(@ibase_fetch_object($query_id, IBASE_TEXT)))
{ {
$result[] = $this->rowset[$query_id]; $result[] = $this->rowset[$query_id];
} }
return $result; return $result;
} }
else else
@ -342,7 +293,9 @@ class sql_db
{ {
if ($rownum > -1) if ($rownum > -1)
{ {
$result = @mysql_result($query_id, $rownum, $field); // NOTE: Let's see how often we use this one and how fast we can produce a working query. ;D
// At the moment we are not taking advantage of this feature.
trigger_error('ROWNUM > 0 in sql_fetchfield not supported, please file a bug report.');
} }
else else
{ {
@ -393,7 +346,7 @@ class sql_db
function sql_nextid() function sql_nextid()
{ {
if ($this->query_result && preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text[$query_id], $tablename)) if ($this->query_result && preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text, $tablename))
{ {
$query = "SELECT GEN_ID('" . $tablename[1] . "_gen', 0) AS new_id $query = "SELECT GEN_ID('" . $tablename[1] . "_gen', 0) AS new_id
FROM RDB\$DATABASE"; FROM RDB\$DATABASE";
@ -416,6 +369,11 @@ class sql_db
$query_id = $this->query_result; $query_id = $this->query_result;
} }
if (!$this->transaction && $query_id)
{
@ibase_commit();
}
return ($query_id) ? @ibase_free_result($query_id) : false; return ($query_id) ? @ibase_free_result($query_id) : false;
} }

View file

@ -256,7 +256,7 @@ class sql_db
$row_offset = ($total) ? $offset : ''; $row_offset = ($total) ? $offset : '';
$num_rows = ($total) ? $total : $offset; $num_rows = ($total) ? $total : $offset;
$query = 'SELECT TOP ' . ($row_offset + $num_rows) . ' ' . preg_replace('/^SELECT/', '', $query); $query = 'SELECT TOP ' . ($row_offset + $num_rows) . ' ' . substr($query, 6);
return $this->sql_query($query, $cache_ttl); return $this->sql_query($query, $cache_ttl);
} }

View file

@ -1501,7 +1501,7 @@ function split_sql_file($sql, $delimiter)
for ($i = 0; $i < $token_count; $i++) for ($i = 0; $i < $token_count; $i++)
{ {
// Don't wanna add an empty string as the last thing in the array. // Don't wanna add an empty string as the last thing in the array.
if ($i != $token_count - 1 || strlen($tokens[$i] > 0)) if ($i != $token_count - 1)
{ {
// This is the total number of single quotes in the token. // This is the total number of single quotes in the token.
$total_quotes = preg_match_all("#'#", $tokens[$i], $matches); $total_quotes = preg_match_all("#'#", $tokens[$i], $matches);
@ -1639,12 +1639,12 @@ function cache_moderators()
switch (SQL_LAYER) switch (SQL_LAYER)
{ {
case 'mysql': case 'mysql':
case 'mysql4':
$sql = 'INSERT INTO ' . MODERATOR_TABLE . ' (forum_id, user_id, username, group_id, groupname) $sql = 'INSERT INTO ' . MODERATOR_TABLE . ' (forum_id, user_id, username, group_id, groupname)
VALUES ' . implode(', ', preg_replace('#^(.*)$#', '(\1)', $m_sql)); VALUES ' . implode(', ', preg_replace('#^(.*)$#', '(\1)', $m_sql));
$db->sql_query($sql); $db->sql_query($sql);
break; break;
case 'mysql4':
case 'mssql': case 'mssql':
case 'sqlite': case 'sqlite':
$sql = 'INSERT INTO ' . MODERATOR_TABLE . ' (forum_id, user_id, username, group_id, groupname) $sql = 'INSERT INTO ' . MODERATOR_TABLE . ' (forum_id, user_id, username, group_id, groupname)
@ -1949,10 +1949,10 @@ if (class_exists('auth'))
switch (SQL_LAYER) switch (SQL_LAYER)
{ {
case 'mysql': case 'mysql':
case 'mysql4':
$sql = 'VALUES ' . implode(', ', preg_replace('#^(.*?)$#', '(\1)', $sql_subary)); $sql = 'VALUES ' . implode(', ', preg_replace('#^(.*?)$#', '(\1)', $sql_subary));
break; break;
case 'mysql4':
case 'mssql': case 'mssql':
case 'sqlite': case 'sqlite':
$sql = implode(' UNION ALL ', preg_replace('#^(.*?)$#', 'SELECT \1', $sql_subary)); $sql = implode(' UNION ALL ', preg_replace('#^(.*?)$#', 'SELECT \1', $sql_subary));
@ -2091,12 +2091,15 @@ if (class_exists('auth'))
switch (SQL_LAYER) switch (SQL_LAYER)
{ {
case 'mysql': case 'mysql':
case 'mysql4':
$sql .= (($sql != '') ? ', ' : '') . "('$option', " . $type_sql[$type] . ")"; $sql .= (($sql != '') ? ', ' : '') . "('$option', " . $type_sql[$type] . ")";
break; break;
case 'mysql4':
case 'mssql': case 'mssql':
case 'sqlite':
$sql .= (($sql != '') ? ' UNION ALL ' : '') . " SELECT '$option', " . $type_sql[$type]; $sql .= (($sql != '') ? ' UNION ALL ' : '') . " SELECT '$option', " . $type_sql[$type];
break; break;
default: default:
$sql = 'INSERT INTO ' . ACL_OPTIONS_TABLE . " (auth_option, is_global, is_local) $sql = 'INSERT INTO ' . ACL_OPTIONS_TABLE . " (auth_option, is_global, is_local)
VALUES ($option, " . $type_sql[$type] . ")"; VALUES ($option, " . $type_sql[$type] . ")";

View file

@ -83,7 +83,7 @@ $available_dbms = array(
'LABEL' => 'FireBird', 'LABEL' => 'FireBird',
'SCHEMA' => 'firebird', 'SCHEMA' => 'firebird',
'MODULE' => 'interbase', 'MODULE' => 'interbase',
'DELIM' => ';', 'DELIM' => ';;',
'COMMENTS' => 'remove_remarks' 'COMMENTS' => 'remove_remarks'
), ),
'mysql' => array( 'mysql' => array(

File diff suppressed because it is too large Load diff