diff options
author | mensonge <mensonge@b3834d28-1941-0410-a4f8-b48e95affb8f> | 2007-12-12 16:29:16 +0000 |
---|---|---|
committer | mensonge <mensonge@b3834d28-1941-0410-a4f8-b48e95affb8f> | 2007-12-12 16:29:16 +0000 |
commit | d582054c77b22daeb08d2bff17794b9a69a20dd4 (patch) | |
tree | d6e7cef8639da6f573cd0b21a5316abf5af24fac /includes/db | |
download | semanticscuttle-d582054c77b22daeb08d2bff17794b9a69a20dd4.tar.gz semanticscuttle-d582054c77b22daeb08d2bff17794b9a69a20dd4.tar.bz2 |
import of scuttle 0.7.2
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@1 b3834d28-1941-0410-a4f8-b48e95affb8f
Diffstat (limited to 'includes/db')
-rw-r--r-- | includes/db/db2.php | 417 | ||||
-rw-r--r-- | includes/db/firebird.php | 527 | ||||
-rw-r--r-- | includes/db/index.htm | 10 | ||||
-rw-r--r-- | includes/db/mssql-odbc.php | 576 | ||||
-rw-r--r-- | includes/db/mssql.php | 551 | ||||
-rw-r--r-- | includes/db/mysql.php | 552 | ||||
-rw-r--r-- | includes/db/mysql4.php | 552 | ||||
-rw-r--r-- | includes/db/mysqli.php | 566 | ||||
-rw-r--r-- | includes/db/oracle.php | 468 | ||||
-rw-r--r-- | includes/db/postgres.php | 597 | ||||
-rw-r--r-- | includes/db/sqlite.php | 387 |
11 files changed, 5203 insertions, 0 deletions
diff --git a/includes/db/db2.php b/includes/db/db2.php new file mode 100644 index 0000000..b1abf1a --- /dev/null +++ b/includes/db/db2.php @@ -0,0 +1,417 @@ +<?php +/** +* +* @package dbal_db2 +* @version $Id: db2.php,v 1.2 2005/06/10 08:52:03 devalley Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if(!defined("SQL_LAYER")) +{ + +define("SQL_LAYER","db2"); + +/** +* @package dbal_db2 +* DB2 Database Abstraction Layer +*/ +class sql_db +{ + + var $db_connect_id; + var $query_result; + var $query_resultset; + var $query_numrows; + var $next_id; + var $row = array(); + var $rowset = array(); + var $row_index; + var $num_queries = 0; + + // + // Constructor + // + function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true) + { + $this->persistency = $persistency; + $this->user = $sqluser; + $this->password = $sqlpassword; + $this->dbname = $database; + + $this->server = $sqlserver; + + if($this->persistency) + { + $this->db_connect_id = odbc_pconnect($this->server, "", ""); + } + else + { + $this->db_connect_id = odbc_connect($this->server, "", ""); + } + + if($this->db_connect_id) + { + @odbc_autocommit($this->db_connect_id, off); + + return $this->db_connect_id; + } + else + { + return false; + } + } + // + // Other base methods + // + function sql_close() + { + if($this->db_connect_id) + { + if($this->query_result) + { + @odbc_free_result($this->query_result); + } + $result = @odbc_close($this->db_connect_id); + return $result; + } + else + { + return false; + } + } + + + // + // Query method + // + function sql_query($query = "", $transaction = FALSE) + { + // + // Remove any pre-existing queries + // + unset($this->query_result); + unset($this->row); + if($query != "") + { + $this->num_queries++; + + if(!eregi("^INSERT ",$query)) + { + if(eregi("LIMIT", $query)) + { + preg_match("/^(.*)LIMIT ([0-9]+)[, ]*([0-9]+)*/s", $query, $limits); + + $query = $limits[1]; + if($limits[3]) + { + $row_offset = $limits[2]; + $num_rows = $limits[3]; + } + else + { + $row_offset = 0; + $num_rows = $limits[2]; + } + + $query .= " FETCH FIRST ".($row_offset+$num_rows)." ROWS ONLY OPTIMIZE FOR ".($row_offset+$num_rows)." ROWS"; + + $this->query_result = odbc_exec($this->db_connect_id, $query); + + $query_limit_offset = $row_offset; + $this->result_numrows[$this->query_result] = $num_rows; + } + else + { + $this->query_result = odbc_exec($this->db_connect_id, $query); + + $row_offset = 0; + $this->result_numrows[$this->query_result] = 5E6; + } + + $result_id = $this->query_result; + if($this->query_result && eregi("^SELECT", $query)) + { + + for($i = 1; $i < odbc_num_fields($result_id)+1; $i++) + { + $this->result_field_names[$result_id][] = odbc_field_name($result_id, $i); + } + + $i = $row_offset + 1; + $k = 0; + while(odbc_fetch_row($result_id, $i) && $k < $this->result_numrows[$result_id]) + { + + for($j = 1; $j < count($this->result_field_names[$result_id])+1; $j++) + { + $this->result_rowset[$result_id][$k][$this->result_field_names[$result_id][$j-1]] = odbc_result($result_id, $j); + } + $i++; + $k++; + } + + $this->result_numrows[$result_id] = $k; + $this->row_index[$result_id] = 0; + } + else + { + $this->result_numrows[$result_id] = @odbc_num_rows($result_id); + $this->row_index[$result_id] = 0; + } + } + else + { + if(eregi("^(INSERT|UPDATE) ", $query)) + { + $query = preg_replace("/\\\'/s", "''", $query); + } + + $this->query_result = odbc_exec($this->db_connect_id, $query); + + if($this->query_result) + { + $sql_id = "VALUES(IDENTITY_VAL_LOCAL())"; + + $id_result = odbc_exec($this->db_connect_id, $sql_id); + if($id_result) + { + $row_result = odbc_fetch_row($id_result); + if($row_result) + { + $this->next_id[$this->query_result] = odbc_result($id_result, 1); + } + } + } + + odbc_commit($this->db_connect_id); + + $this->query_limit_offset[$this->query_result] = 0; + $this->result_numrows[$this->query_result] = 0; + } + + return $this->query_result; + } + else + { + return false; + } + } + + // + // Other query methods + // + function sql_numrows($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + return $this->result_numrows[$query_id]; + } + else + { + return false; + } + } + function sql_affectedrows($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + return $this->result_numrows[$query_id]; + } + else + { + return false; + } + } + function sql_numfields($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = count($this->result_field_names[$query_id]); + return $result; + } + else + { + return false; + } + } + function sql_fieldname($offset, $query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = $this->result_field_names[$query_id][$offset]; + return $result; + } + else + { + return false; + } + } + function sql_fieldtype($offset, $query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = @odbc_field_type($query_id, $offset); + return $result; + } + else + { + return false; + } + } + function sql_fetchrow($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + if($this->row_index[$query_id] < $this->result_numrows[$query_id]) + { + $result = $this->result_rowset[$query_id][$this->row_index[$query_id]]; + $this->row_index[$query_id]++; + return $result; + } + else + { + return false; + } + } + else + { + return false; + } + } + function sql_fetchrowset($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $this->row_index[$query_id] = $this->result_numrows[$query_id]; + return $this->result_rowset[$query_id]; + } + else + { + return false; + } + } + function sql_fetchfield($field, $row = -1, $query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + if($row < $this->result_numrows[$query_id]) + { + if($row == -1) + { + $getrow = $this->row_index[$query_id]-1; + } + else + { + $getrow = $row; + } + + return $this->result_rowset[$query_id][$getrow][$this->result_field_names[$query_id][$field]]; + + } + else + { + return false; + } + } + else + { + return false; + } + } + function sql_rowseek($offset, $query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $this->row_index[$query_id] = 0; + return true; + } + else + { + return false; + } + } + function sql_nextid($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + return $this->next_id[$query_id]; + } + else + { + return false; + } + } + function sql_freeresult($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = @odbc_free_result($query_id); + return $result; + } + else + { + return false; + } + } + function sql_error($query_id = 0) + { +// $result['code'] = @odbc_error($this->db_connect_id); +// $result['message'] = @odbc_errormsg($this->db_connect_id); + + return ""; + } + +} // class sql_db + +} // if ... define + +?>
\ No newline at end of file diff --git a/includes/db/firebird.php b/includes/db/firebird.php new file mode 100644 index 0000000..58a7d07 --- /dev/null +++ b/includes/db/firebird.php @@ -0,0 +1,527 @@ +<?php +/** +* +* @package dbal_firebird +* @version $Id: firebird.php,v 1.2 2005/06/10 08:52:03 devalley Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('SQL_LAYER')) +{ + +define('SQL_LAYER', 'firebird'); + +/** +* @package dbal_firebird +* Firebird/Interbase Database Abstraction Layer +* Minimum Requirement is Firebird 1.5+/Interbase 7.1+ +*/ +class sql_db +{ + var $db_connect_id; + var $query_result; + var $return_on_error = false; + var $transaction = false; + var $sql_time = 0; + var $num_queries = 0; + var $open_queries = array(); + + var $last_query_text = ''; + + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) + { + $this->persistency = $persistency; + $this->user = $sqluser; + $this->server = $sqlserver . (($port) ? ':' . $port : ''); + $this->dbname = $database; + + $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); + + return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); + } + + // + // Other base methods + // + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + if ($this->transaction) + { + @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); + } + } + + return @ibase_close($this->db_connect_id); + } + + function sql_return_on_error($fail = false) + { + $this->return_on_error = $fail; + } + + function sql_num_queries() + { + return $this->num_queries; + } + + function sql_transaction($status = 'begin') + { + switch ($status) + { + case 'begin': + $this->transaction = true; + break; + + case 'commit': + $result = @ibase_commit(); + $this->transaction = false; + + if (!$result) + { + @ibase_rollback(); + } + break; + + case 'rollback': + $result = @ibase_rollback(); + $this->transaction = false; + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $cache_ttl = 0) + { + if ($query != '') + { + global $cache; + + $this->last_query_text = $query; + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; + + if (!$this->query_result) + { + $this->num_queries++; + + if (($this->query_result = @ibase_query($this->db_connect_id, $query)) === false) + { + $this->sql_error($query); + } + + // TODO: have to debug the commit states in firebird + if (!$this->transaction) + { + @ibase_commit_ret(); + } + + if ($cache_ttl && method_exists($cache, 'sql_save')) + { + $cache->sql_save($query, $this->query_result, $cache_ttl); + } + } + } + else + { + return false; + } + + return ($this->query_result) ? $this->query_result : false; + } + + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) + { + if ($query != '') + { + $this->query_result = false; + + $query = 'SELECT FIRST ' . $total . ((!empty($offset)) ? ' SKIP ' . $offset : '') . substr($query, 6); + + return $this->sql_query($query, $cache_ttl); + } + else + { + return false; + } + } + + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) + { + if (!is_array($assoc_ary)) + { + return false; + } + + $fields = array(); + $values = array(); + if ($query == 'INSERT') + { + foreach ($assoc_ary as $key => $var) + { + $fields[] = $key; + + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE' || $query == 'SELECT') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); + } + + return $query; + } + + // 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) + { + return FALSE; + } + + function sql_affectedrows() + { + // TODO: hmm, maybe doing something similar as in mssql-odbc.php? + return ($this->query_result) ? true : false; + } + + function sql_fetchrow($query_id = false) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($cache->sql_rowset[$query_id])) + { + return $cache->sql_fetchrow($query_id); + } + + $row = array(); + $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))); + } + return ($query_id) ? $row : false; + } + + function sql_fetchrowset($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + unset($this->rowset[$query_id]); + unset($this->row[$query_id]); + + $result = array(); + while ($this->rowset[$query_id] = get_object_vars(@ibase_fetch_object($query_id, IBASE_TEXT))) + { + $result[] = $this->rowset[$query_id]; + } + + return $result; + } + else + { + return false; + } + } + + function sql_fetchfield($field, $rownum = -1, $query_id = 0) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($rownum > -1) + { + // erm... ok, my bad, we always use zero. :/ + for ($i = 0; $i <= $rownum; $i++) + { + $row = $this->sql_fetchrow($query_id); + } + + return $row[$field]; + } + else + { + if (empty($this->row[$query_id]) && empty($this->rowset[$query_id])) + { + if ($this->sql_fetchrow($query_id)) + { + $result = $this->row[$query_id][$field]; + } + } + else + { + if ($this->rowset[$query_id]) + { + $result = $this->rowset[$query_id][$field]; + } + else if ($this->row[$query_id]) + { + $result = $this->row[$query_id][$field]; + } + } + } + return $result; + } + else + { + return false; + } + } + + function sql_rowseek($rownum, $query_id = 0) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + for($i = 1; $i < $rownum; $i++) + { + if (!$this->sql_fetchrow($query_id)) + { + return false; + } + } + + return true; + } + + function sql_nextid() + { + 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 + FROM RDB\$DATABASE"; + if (!($temp_q_id = @ibase_query($this->db_connect_id, $query))) + { + return false; + } + + $temp_result = @ibase_fetch_object($temp_q_id); + $this->sql_freeresult($temp_q_id); + + return ($temp_result) ? $temp_result->last_value : false; + } + } + + function sql_freeresult($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (!$this->transaction && $query_id) + { + @ibase_commit(); + } + + return ($query_id) ? @ibase_free_result($query_id) : false; + } + + function sql_escape($msg) + { + return (@ini_get('magic_quotes_sybase') || strtolower(@ini_get('magic_quotes_sybase')) == 'on') ? str_replace('\\\'', '\'', addslashes($msg)) : str_replace('\'', '\'\'', stripslashes($msg)); + } + + function sql_error($sql = '') + { + if (!$this->return_on_error) + { + $this_page =(!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' .((!empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : $_ENV['QUERY_STRING']); + + $message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . @ibase_errmsg() . '<br /><br /><u>CALLING PAGE</u><br /><br />' . $this_page .(($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />'; + + if ($this->transaction) + { + $this->sql_transaction('rollback'); + } + + trigger_error($message, E_USER_ERROR); + } + + $result['message'] = @ibase_errmsg(); + $result['code'] = ''; + + return $result; + } + + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $this->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8869-1"><meta http-equiv="Content-Style-Type" content="text/css"><link rel="stylesheet" href="' . $phpbb_root_path . 'adm/subSilver.css" type="text/css"><style type="text/css">' . "\n"; + echo 'th { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic3.gif\') }' . "\n"; + echo 'td.cat { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic1.gif\') }' . "\n"; + echo '</style><title>' . $msg_title . '</title></head><body>'; + echo '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td><a href="' . htmlspecialchars(preg_replace('/&explain=([^&]*)/', '', $_SERVER['REQUEST_URI'])) . '"><img src="' . $phpbb_root_path . 'adm/images/header_left.jpg" width="200" height="60" alt="phpBB Logo" title="phpBB Logo" border="0"/></a></td><td width="100%" background="' . $phpbb_root_path . 'adm/images/header_bg.jpg" height="60" align="right" nowrap="nowrap"><span class="maintitle">SQL Report</span> </td></tr></table><br clear="all"/><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td height="40" align="center" valign="middle"><b>Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '</b></td></tr><tr><td align="center" nowrap="nowrap">Time spent on MySQL queries: <b>' . round($this->sql_time, 5) . 's</b> | Time spent on PHP: <b>' . round($totaltime - $this->sql_time, 5) . 's</b></td></tr></table><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td>'; + echo $sql_report; + echo '</td></tr></table><br /></body></html>'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = @ibase_query($this->db_connect_id, $query); + while ($void = @ibase_fetch_object($result, IBASE_TEXT)) + { + // Take the time spent on parsing rows into account + } + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query results obtained from the cache</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table><p align="center">'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p>'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + @ibase_freeresult($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query #' . $this->num_queries . '</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table> ' . $html_hold . '<p align="center">'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: <b>" . $this->sql_affectedrows($this->query_result) . '</b> | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: <b>' . sprintf('%.5f', $endtime - $curtime) . 's</b>'; + } + else + { + $error = $this->sql_error(); + $sql_report .= '<b style="color: red">FAILED</b> - ' . SQL_LAYER . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '</p>'; + + $this->sql_time += $endtime - $curtime; + break; + } + } + +} // class sql_db + +} // if ... define + +?>
\ No newline at end of file diff --git a/includes/db/index.htm b/includes/db/index.htm new file mode 100644 index 0000000..ee1f723 --- /dev/null +++ b/includes/db/index.htm @@ -0,0 +1,10 @@ +<html> +<head> +<title></title> +<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> +</head> + +<body bgcolor="#FFFFFF" text="#000000"> + +</body> +</html> diff --git a/includes/db/mssql-odbc.php b/includes/db/mssql-odbc.php new file mode 100644 index 0000000..a2d3d02 --- /dev/null +++ b/includes/db/mssql-odbc.php @@ -0,0 +1,576 @@ +<?php +/** +* +* @package dbal_odbc_mssql +* @version $Id: mssql-odbc.php,v 1.2 2005/06/10 08:52:03 devalley Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('SQL_LAYER')) +{ + +define('SQL_LAYER', 'mssql-odbc'); + +/** +* @package dbal_odbc_mssql +* MSSQL ODBC Database Abstraction Layer for MSSQL +* Minimum Requirement is Version 2000+ +*/ +class sql_db +{ + var $db_connect_id; + var $query_result; + var $return_on_error = false; + var $transaction = false; + var $sql_time = 0; + var $num_queries = 0; + var $open_queries = array(); + + var $result_rowset = array(); + var $field_names = array(); + var $field_types = array(); + var $num_rows = array(); + var $current_row = array(); + + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) + { + $this->persistency = $persistency; + $this->user = $sqluser; + $this->server = $sqlserver . (($port) ? ':' . $port : ''); + $this->dbname = $database; + + $this->db_connect_id = ($this->persistency) ? @odbc_pconnect($this->server, $this->user, $sqlpassword) : @odbc_connect($this->server, $this->user, $sqlpassword); + + return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); + } + + // + // Other base methods + // + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + if ($this->transaction) + { + @odbc_commit($this->db_connect_id); + } + + if (sizeof($this->result_rowset)) + { + unset($this->result_rowset); + unset($this->field_names); + unset($this->field_types); + unset($this->num_rows); + unset($this->current_row); + } + + if (sizeof($this->open_queries)) + { + foreach ($this->open_queries as $i_query_id => $query_id) + { + @odbc_free_result($query_id); + } + } + + return @odbc_close($this->db_connect_id); + } + + function sql_return_on_error($fail = false) + { + $this->return_on_error = $fail; + } + + function sql_num_queries() + { + return $this->num_queries; + } + + function sql_transaction($status = 'begin') + { + switch ($status) + { + case 'begin': + $result = @odbc_autocommit($this->db_connect_id, false); + $this->transaction = true; + break; + + case 'commit': + $result = @odbc_commit($this->db_connect_id); + @odbc_autocommit($this->db_connect_id, true); + $this->transaction = false; + + if (!$result) + { + @odbc_rollback($this->db_connect_id); + @odbc_autocommit($this->db_connect_id, true); + } + break; + + case 'rollback': + $result = @odbc_rollback($this->db_connect_id); + @odbc_autocommit($this->db_connect_id, true); + $this->transaction = false; + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $cache_ttl = 0) + { + if ($query != '') + { + global $cache; + + // EXPLAIN only in extra debug mode + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('start', $query); + } + + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; + + if (!$this->query_result) + { + $this->num_queries++; + + if (($this->query_result = $this->_odbc_execute_query($query)) === false) + { + $this->sql_error($query); + } + + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('stop', $query); + } + + if ($cache_ttl && method_exists($cache, 'sql_save')) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + $cache->sql_save($query, $this->query_result, $cache_ttl); + // odbc_free_result called within sql_save() + } + else if (strpos($query, 'SELECT') !== false && $this->query_result) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + } + } + else if (defined('DEBUG_EXTRA')) + { + $this->sql_report('fromcache', $query); + } + } + else + { + return false; + } + + return ($this->query_result) ? $this->query_result : false; + } + + function _odbc_execute_query($query) + { + $result = false; + + if (eregi("^SELECT ", $query)) + { + $result = @odbc_exec($this->db_connect_id, $query); + + if ($result) + { + if (empty($this->field_names[$result])) + { + for ($i = 1, $j = @odbc_num_fields($result) + 1; $i < $j; $i++) + { + $this->field_names[$result][] = @odbc_field_name($result, $i); + $this->field_types[$result][] = @odbc_field_type($result, $i); + } + } + + $this->current_row[$result] = 0; + $this->result_rowset[$result] = array(); + + $row_outer = (isset($row_offset)) ? $row_offset + 1 : 1; + $row_outer_max = (isset($num_rows)) ? $row_offset + $num_rows + 1 : 1E9; + $row_inner = 0; + + while (@odbc_fetch_row($result, $row_outer) && $row_outer < $row_outer_max) + { + for ($i = 0, $j = sizeof($this->field_names[$result]); $i < $j; $i++) + { + $this->result_rowset[$result][$row_inner][$this->field_names[$result][$i]] = stripslashes(@odbc_result($result, $i + 1)); + } + + $row_outer++; + $row_inner++; + } + + $this->num_rows[$result] = sizeof($this->result_rowset[$result]); + } + } + else if (eregi("^INSERT ", $query)) + { + $result = @odbc_exec($this->db_connect_id, $query); + + if ($result) + { + $result_id = @odbc_exec($this->db_connect_id, 'SELECT @@IDENTITY'); + if ($result_id) + { + if (@odbc_fetch_row($result_id)) + { + $this->next_id[$this->db_connect_id] = @odbc_result($result_id, 1); + $this->affected_rows[$this->db_connect_id] = @odbc_num_rows($result); + } + } + } + } + else + { + $result = @odbc_exec($this->db_connect_id, $query); + + if ($result) + { + $this->affected_rows[$this->db_connect_id] = @odbc_num_rows($result); + } + } + + return $result; + } + + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) + { + if ($query != '') + { + $this->query_result = false; + + // if $total is set to 0 we do not want to limit the number of rows + if ($total == 0) + { + $total = -1; + } + + $row_offset = ($total) ? $offset : ''; + $num_rows = ($total) ? $total : $offset; + + $query = 'SELECT TOP ' . ($row_offset + $num_rows) . ' ' . substr($query, 6); + + return $this->sql_query($query, $cache_ttl); + } + else + { + return false; + } + } + + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) + { + if (!is_array($assoc_ary)) + { + return false; + } + + $fields = array(); + $values = array(); + if ($query == 'INSERT') + { + foreach ($assoc_ary as $key => $var) + { + $fields[] = $key; + + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE' || $query == 'SELECT') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); + } + + return $query; + } + + // 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) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @$this->num_rows($query_id) : false; + } + + function sql_affectedrows() + { + return ($this->affected_rows[$this->db_connect_id]) ? $this->affected_rows[$this->db_connect_id] : false; + } + + function sql_fetchrow($query_id = false) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($cache->sql_rowset[$query_id])) + { + return $cache->sql_fetchrow($query_id); + } + + return ($this->num_rows[$query_id] && $this->current_row[$query_id] < $this->num_rows[$query_id]) ? $this->result_rowset[$query_id][$this->current_row[$query_id]++] : false; + } + + function sql_fetchrowset($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($this->num_rows[$query_id]) ? $this->result_rowset[$query_id] : false; + } + + function sql_fetchfield($field, $rownum = -1, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($rownum < $this->num_rows[$query_id]) + { + $getrow = ($rownum == -1) ? $this->current_row[$query_id] - 1 : $rownum; + + return $this->result_rowset[$query_id][$getrow][$this->field_names[$query_id][$field]]; + } + } + + return false; + } + + function sql_rowseek($rownum, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($this->current_row[$query_id])) + { + $this->current_row[$query_id] = $rownum; + return true; + } + + return false; + } + + function sql_nextid() + { + return ($this->next_id[$this->db_connect_id]) ? $this->next_id[$this->db_connect_id] : false; + } + + function sql_freeresult($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($this->open_queries[(int) $query_id])) + { + unset($this->open_queries[(int) $query_id]); + unset($this->num_rows[$query_id]); + unset($this->current_row[$query_id]); + unset($this->result_rowset[$query_id]); + unset($this->field_names[$query_id]); + unset($this->field_types[$query_id]); + + return @odbc_free_result($query_id); + } + + return false; + } + + function sql_escape($msg) + { + return str_replace("'", "''", str_replace('\\', '\\\\', $msg)); + } + + function sql_error($sql = '') + { + if (!$this->return_on_error) + { + $this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : '')); + + $message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . @odbc_errormsg() . '<br /><br /><u>CALLING PAGE</u><br /><br />' . htmlspecialchars($this_page) . (($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />'; + + if ($this->transaction) + { + $this->sql_transaction('rollback'); + } + + trigger_error($message, E_USER_ERROR); + } + + $result = array( + 'message' => @odbc_errormsg(), + 'code' => @odbc_error() + ); + + return $result; + } + + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $this->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8869-1"><meta http-equiv="Content-Style-Type" content="text/css"><link rel="stylesheet" href="' . $phpbb_root_path . 'adm/subSilver.css" type="text/css"><style type="text/css">' . "\n"; + echo 'th { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic3.gif\') }' . "\n"; + echo 'td.cat { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic1.gif\') }' . "\n"; + echo '</style><title>' . $msg_title . '</title></head><body>'; + echo '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td><a href="' . htmlspecialchars(preg_replace('/&explain=([^&]*)/', '', $_SERVER['REQUEST_URI'])) . '"><img src="' . $phpbb_root_path . 'adm/images/header_left.jpg" width="200" height="60" alt="phpBB Logo" title="phpBB Logo" border="0"/></a></td><td width="100%" background="' . $phpbb_root_path . 'adm/images/header_bg.jpg" height="60" align="right" nowrap="nowrap"><span class="maintitle">SQL Report</span> </td></tr></table><br clear="all"/><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td height="40" align="center" valign="middle"><b>Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '</b></td></tr><tr><td align="center" nowrap="nowrap">Time spent on MySQL queries: <b>' . round($this->sql_time, 5) . 's</b> | Time spent on PHP: <b>' . round($totaltime - $this->sql_time, 5) . 's</b></td></tr></table><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td>'; + echo $sql_report; + echo '</td></tr></table><br /></body></html>'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = $this->_odbc_execute_query($query); + + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query results obtained from the cache</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table><p align="center">'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p>'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + @odbc_free_result($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query #' . $this->num_queries . '</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table> ' . $html_hold . '<p align="center">'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: <b>" . $this->sql_affectedrows($this->query_result) . '</b> | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: <b>' . sprintf('%.5f', $endtime - $curtime) . 's</b>'; + } + else + { + $error = $this->sql_error(); + $sql_report .= '<b style="color: red">FAILED</b> - ' . SQL_LAYER . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '</p>'; + + $this->sql_time += $endtime - $curtime; + break; + } + } + +} // class sql_db + +} // if ... define + +?>
\ No newline at end of file diff --git a/includes/db/mssql.php b/includes/db/mssql.php new file mode 100644 index 0000000..2b17b9e --- /dev/null +++ b/includes/db/mssql.php @@ -0,0 +1,551 @@ +<?php +/** +* +* @package dbal_mssql +* @version $Id: mssql.php,v 1.2 2005/06/10 08:52:03 devalley Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('SQL_LAYER')) +{ + +define('SQL_LAYER', 'mssql'); + +/** +* @package dbal_mssql +* MSSQL Database Abstraction Layer +* Minimum Requirement is MSSQL 2000+ +*/ +class sql_db +{ + var $db_connect_id; + var $query_result; + var $return_on_error = false; + var $transaction = false; + var $sql_time = 0; + var $num_queries = 0; + var $open_queries = array(); + + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) + { + $this->persistency = $persistency; + $this->user = $sqluser; + $this->server = $sqlserver . (($port) ? ':' . $port : ''); + $this->dbname = $database; + + $this->db_connect_id = ($this->persistency) ? @mssql_pconnect($this->server, $this->user, $sqlpassword) : @mssql_connect($this->server, $this->user, $sqlpassword); + + if ($this->db_connect_id && $this->dbname != '') + { + if (!@mssql_select_db($this->dbname, $this->db_connect_id)) + { + @mssql_close($this->db_connect_id); + return false; + } + } + + return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); + } + + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + if ($this->transaction) + { + @mssql_query('COMMIT', $this->db_connect_id); + } + + if (sizeof($this->open_queries)) + { + foreach ($this->open_queries as $i_query_id => $query_id) + { + @mssql_free_result($query_id); + } + } + + return @mssql_close($this->db_connect_id); + } + + function sql_return_on_error($fail = false) + { + $this->return_on_error = $fail; + } + + function sql_num_queries() + { + return $this->num_queries; + } + + function sql_transaction($status = 'begin') + { + switch ($status) + { + case 'begin': + $result = @mssql_query('BEGIN TRANSACTION', $this->db_connect_id); + $this->transaction = true; + break; + + case 'commit': + $result = @mssql_query('commit', $this->db_connect_id); + $this->transaction = false; + + if (!$result) + { + @mssql_query('ROLLBACK', $this->db_connect_id); + } + break; + + case 'rollback': + $result = @mssql_query('ROLLBACK', $this->db_connect_id); + $this->transaction = false; + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $cache_ttl = 0) + { + if ($query != '') + { + global $cache; + + // EXPLAIN only in extra debug mode + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('start', $query); + } + + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; + + if (!$this->query_result) + { + $this->num_queries++; + + if (($this->query_result = @mssql_query($query, $this->db_connect_id)) === false) + { + $this->sql_error($query); + } + + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('stop', $query); + } + + if ($cache_ttl && method_exists($cache, 'sql_save')) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + $cache->sql_save($query, $this->query_result, $cache_ttl); + // sql_freeresult called within sql_save() + } + else if (strpos($query, 'SELECT') !== false && $this->query_result) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + } + } + else if (defined('DEBUG_EXTRA')) + { + $this->sql_report('fromcache', $query); + } + } + else + { + return false; + } + + return ($this->query_result) ? $this->query_result : false; + } + + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) + { + if ($query != '') + { + $this->query_result = false; + + // if $total is set to 0 we do not want to limit the number of rows + if ($total == 0) + { + $total = -1; + } + + $row_offset = ($total) ? $offset : ''; + $num_rows = ($total) ? $total : $offset; + + $query = 'SELECT TOP ' . ($row_offset + $num_rows) . ' ' . substr($query, 6); + + return $this->sql_query($query, $cache_ttl); + } + else + { + return false; + } + } + + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) + { + if (!is_array($assoc_ary)) + { + return false; + } + + $fields = array(); + $values = array(); + if ($query == 'INSERT') + { + foreach ($assoc_ary as $key => $var) + { + $fields[] = $key; + + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE' || $query == 'SELECT') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); + } + + return $query; + } + + // 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) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + +// return (isset($this->limit_offset[$query_id])) ? @mssql_num_rows($query_id) - $this->limit_offset[$query_id] : @mssql_num_rows($query_id); + return ($query_id) ? @mssql_num_rows($query_id) : false; + } + + function sql_affectedrows() + { + return ($this->db_connect_id) ? @mssql_rows_affected($this->db_connect_id) : false; + } + + function sql_fetchrow($query_id = false) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($cache->sql_rowset[$query_id])) + { + return $cache->sql_fetchrow($query_id); + } + + $row = @mssql_fetch_array($query_id, MSSQL_ASSOC); + + if ($row) + { + foreach ($row as $key => $value) + { + $row[$key] = ($value === ' ') ? trim($value) : $value; + } + } + + return $row; + } + + function sql_fetchrowset($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + unset($this->rowset[$query_id]); + unset($this->row[$query_id]); + + $result = array(); + while ($this->rowset[$query_id] = $this->sql_fetchrow($query_id)) + { + $result[] = $this->rowset[$query_id]; + } + return $result; + } + + return false; + } + + function sql_fetchfield($field, $rownum = -1, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($rownum > -1) + { +// (!empty($this->limit_offset[$query_id])) ? @mssql_data_seek($query_id, ($this->limit_offset[$query_id] + $rownum)) : @mssql_data_seek($query_id, $rownum); + @mssql_data_seek($query_id, $rownum); + $row = @mssql_fetch_array($query_id, MSSQL_ASSOC); + $result = isset($row[$field]) ? $row[$field] : false; + } + else + { + if (empty($this->row[$query_id]) && empty($this->rowset[$query_id])) + { + if ($this->sql_fetchrow($query_id)) + { + $result = $this->row[$query_id][$field]; + } + } + else + { + if ($this->rowset[$query_id]) + { + $result = $this->rowset[$query_id][$field]; + } + elseif ($this->row[$query_id]) + { + $result = $this->row[$query_id][$field]; + } + } + } + + return $result; + } + + return false; + } + + function sql_rowseek($rownum, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($this->current_row[$query_id])) + { +// (!empty($this->limit_offset[$query_id])) ? @mssql_data_seek($query_id, ($this->limit_offset[$query_id] + $rownum)) : @mssql_data_seek($query_id, $rownum); + @mssql_data_seek($query_id, $rownum); + return true; + } + + return false; + } + + function sql_nextid() + { + $result_id = @mssql_query('SELECT @@IDENTITY', $this->db_connect_id); + if ($result_id) + { + if (@mssql_fetch_array($result_id, MSSQL_ASSOC)) + { + return @mssql_result($result_id, 1); + } + } + + return false; + } + + function sql_freeresult($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($this->open_queries[$query_id])) + { + unset($this->open_queries[$query_id]); + unset($this->result_rowset[$query_id]); + + return @mssql_free_result($query_id); + } + + return false; + } + + function sql_escape($msg) + { + return str_replace("'", "''", str_replace('\\', '\\\\', $msg)); + } + + function sql_error($sql = '') + { + if (!$this->return_on_error) + { + $this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : '')); + + $message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . @mssql_get_last_message() . '<br /><br /><u>CALLING PAGE</u><br /><br />' . htmlspecialchars($this_page) . (($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />'; + + if ($this->transaction) + { + $this->sql_transaction('rollback'); + } + + trigger_error($message, E_USER_ERROR); + } + + $result = array( + 'message' => @mssql_get_last_message($this->db_connect_id), + 'code' => '' + ); + + return $result; + } + + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $this->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8869-1"><meta http-equiv="Content-Style-Type" content="text/css"><link rel="stylesheet" href="' . $phpbb_root_path . 'adm/subSilver.css" type="text/css"><style type="text/css">' . "\n"; + echo 'th { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic3.gif\') }' . "\n"; + echo 'td.cat { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic1.gif\') }' . "\n"; + echo '</style><title>' . $msg_title . '</title></head><body>'; + echo '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td><a href="' . htmlspecialchars(preg_replace('/&explain=([^&]*)/', '', $_SERVER['REQUEST_URI'])) . '"><img src="' . $phpbb_root_path . 'adm/images/header_left.jpg" width="200" height="60" alt="phpBB Logo" title="phpBB Logo" border="0"/></a></td><td width="100%" background="' . $phpbb_root_path . 'adm/images/header_bg.jpg" height="60" align="right" nowrap="nowrap"><span class="maintitle">SQL Report</span> </td></tr></table><br clear="all"/><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td height="40" align="center" valign="middle"><b>Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '</b></td></tr><tr><td align="center" nowrap="nowrap">Time spent on MySQL queries: <b>' . round($this->sql_time, 5) . 's</b> | Time spent on PHP: <b>' . round($totaltime - $this->sql_time, 5) . 's</b></td></tr></table><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td>'; + echo $sql_report; + echo '</td></tr></table><br /></body></html>'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = @mssql_query($query, $this->db_connect_id); + while ($void = @mssql_fetch_array($result, MSSQL_ASSOC)) + { + // Take the time spent on parsing rows into account + } + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query results obtained from the cache</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table><p align="center">'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p>'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + @mssql_free_result($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query #' . $this->num_queries . '</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table> ' . $html_hold . '<p align="center">'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: <b>" . $this->sql_affectedrows($this->query_result) . '</b> | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: <b>' . sprintf('%.5f', $endtime - $curtime) . 's</b>'; + } + else + { + $error = $this->sql_error(); + $sql_report .= '<b style="color: red">FAILED</b> - ' . SQL_LAYER . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '</p>'; + + $this->sql_time += $endtime - $curtime; + break; + } + } + +} // class sql_db + +} // if ... define + +?>
\ No newline at end of file diff --git a/includes/db/mysql.php b/includes/db/mysql.php new file mode 100644 index 0000000..a646e0d --- /dev/null +++ b/includes/db/mysql.php @@ -0,0 +1,552 @@ +<?php +/** +* +* @package dbal_mysql +* @version $Id: mysql.php,v 1.5 2006/02/10 01:30:19 scronide Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('SQL_LAYER')) +{ + +define('SQL_LAYER', 'mysql'); + +/** +* @package dbal_mysql +* MySQL Database Abstraction Layer +* Minimum Requirement is 3.23+/4.0+/4.1+ +*/ +class sql_db +{ + var $db_connect_id; + var $query_result; + var $return_on_error = false; + var $transaction = false; + var $sql_time = 0; + var $num_queries = 0; + var $open_queries = array(); + + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) + { + $this->persistency = $persistency; + $this->user = $sqluser; + $this->server = $sqlserver . (($port) ? ':' . $port : ''); + $this->dbname = $database; + + $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $sqlpassword) : @mysql_connect($this->server, $this->user, $sqlpassword); + + if ($this->db_connect_id && $this->dbname != '') + { + if (@mysql_select_db($this->dbname)) + { + return $this->db_connect_id; + } + } + + return $this->sql_error(''); + } + + // + // Other base methods + // + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + if (sizeof($this->open_queries)) + { + foreach ($this->open_queries as $i_query_id => $query_id) + { + @mysql_free_result($query_id); + } + } + + return @mysql_close($this->db_connect_id); + } + + function sql_return_on_error($fail = false) + { + $this->return_on_error = $fail; + } + + function sql_num_queries() + { + return $this->num_queries; + } + + function sql_transaction($status = 'begin') + { + switch ($status) + { + case 'begin': + $result = @mysql_query('BEGIN', $this->db_connect_id); + $this->transaction = true; + break; + + case 'commit': + $result = @mysql_query('COMMIT', $this->db_connect_id); + $this->transaction = false; + + if (!$result) + { + @mysql_query('ROLLBACK', $this->db_connect_id); + } + break; + + case 'rollback': + $result = @mysql_query('ROLLBACK', $this->db_connect_id); + $this->transaction = false; + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $cache_ttl = 0) + { + if ($query != '') + { + global $cache; + + // EXPLAIN only in extra debug mode + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('start', $query); + } + + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; + + if (!$this->query_result) + { + $this->num_queries++; + + if (($this->query_result = @mysql_query($query, $this->db_connect_id)) === false) + { + $this->sql_error($query); + } + + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('stop', $query); + } + + if ($cache_ttl && method_exists($cache, 'sql_save')) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + $cache->sql_save($query, $this->query_result, $cache_ttl); + // mysql_free_result called within sql_save() + } + else if (strpos($query, 'SELECT') !== false && $this->query_result) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + } + } + else if (defined('DEBUG_EXTRA')) + { + $this->sql_report('fromcache', $query); + } + } + else + { + return false; + } + + return ($this->query_result) ? $this->query_result : false; + } + + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) { + if ($query != '') { + $this->query_result = false; + + // only limit the number of rows if $total is greater than 0 + if ($total > 0) + $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total); + + return $this->sql_query($query, $cache_ttl); + } else { + return false; + } + } + + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) + { + if (!is_array($assoc_ary)) + { + return false; + } + + $fields = array(); + $values = array(); + if ($query == 'INSERT') + { + foreach ($assoc_ary as $key => $var) + { + $fields[] = $key; + + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE' || $query == 'SELECT') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); + } + + return $query; + } + + // 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) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @mysql_num_rows($query_id) : false; + } + + function sql_affectedrows() + { + return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false; + } + + function sql_fetchrow($query_id = false) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($cache->sql_rowset[$query_id])) + { + return $cache->sql_fetchrow($query_id); + } + + return ($query_id) ? @mysql_fetch_assoc($query_id) : false; + } + + function sql_fetchrowset($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + unset($this->rowset[$query_id]); + unset($this->row[$query_id]); + + $result = array(); + while ($this->rowset[$query_id] = $this->sql_fetchrow($query_id)) + { + $result[] = $this->rowset[$query_id]; + } + return $result; + } + + return false; + } + + function sql_fetchfield($field, $rownum = -1, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($rownum > -1) + { + $result = @mysql_result($query_id, $rownum, $field); + } + else + { + if (empty($this->row[$query_id]) && empty($this->rowset[$query_id])) + { + if ($this->sql_fetchrow($query_id)) + { + $result = $this->row[$query_id][$field]; + } + } + else + { + if ($this->rowset[$query_id]) + { + $result = $this->rowset[$query_id][$field]; + } + elseif ($this->row[$query_id]) + { + $result = $this->row[$query_id][$field]; + } + } + } + return $result; + } + return false; + } + + function sql_rowseek($rownum, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @mysql_data_seek($query_id, $rownum) : false; + } + + function sql_nextid() + { + return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false; + } + + function sql_freeresult($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($this->open_queries[(int) $query_id])) + { + unset($this->open_queries[(int) $query_id]); + return @mysql_free_result($query_id); + } + + return false; + } + + function sql_escape($msg) { + if (function_exists('mysql_real_escape_string')) { + return @mysql_real_escape_string($msg, $this->db_connect_id); + } else { + return mysql_escape_string($msg); + } + } + + function sql_error($sql = '') + { + if (!$this->return_on_error) + { + $this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : '')); + + $message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . @mysql_error() . '<br /><br /><u>CALLING PAGE</u><br /><br />' . htmlspecialchars($this_page) . (($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />'; + + if ($this->transaction) + { + $this->sql_transaction('rollback'); + } + + trigger_error($message, E_USER_ERROR); + } + + $result = array( + 'message' => @mysql_error(), + 'code' => @mysql_errno() + ); + + return $result; + } + + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $db, $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $db->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8869-1"><meta http-equiv="Content-Style-Type" content="text/css"><link rel="stylesheet" href="' . $phpbb_root_path . 'adm/subSilver.css" type="text/css"><style type="text/css">' . "\n"; + echo 'th { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic3.gif\') }' . "\n"; + echo 'td.cat { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic1.gif\') }' . "\n"; + echo '</style><title>' . $msg_title . '</title></head><body>'; + echo '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td><a href="' . htmlspecialchars(preg_replace('/&explain=([^&]*)/', '', $_SERVER['REQUEST_URI'])) . '"><img src="' . $phpbb_root_path . 'adm/images/header_left.jpg" width="200" height="60" alt="phpBB Logo" title="phpBB Logo" border="0"/></a></td><td width="100%" background="' . $phpbb_root_path . 'adm/images/header_bg.jpg" height="60" align="right" nowrap="nowrap"><span class="maintitle">SQL Report</span> </td></tr></table><br clear="all"/><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td height="40" align="center" valign="middle"><b>Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '</b></td></tr><tr><td align="center" nowrap="nowrap">Time spent on MySQL queries: <b>' . round($this->sql_time, 5) . 's</b> | Time spent on PHP: <b>' . round($totaltime - $this->sql_time, 5) . 's</b></td></tr></table><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td>'; + echo $sql_report; + echo '</td></tr></table><br /></body></html>'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $explain_query = $query; + if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) + { + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; + } + elseif (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) + { + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; + } + + if (preg_match('/^SELECT/', $explain_query)) + { + $html_table = FALSE; + + if ($result = mysql_query("EXPLAIN $explain_query", $this->db_connect_id)) + { + while ($row = mysql_fetch_assoc($result)) + { + if (!$html_table && sizeof($row)) + { + $html_table = TRUE; + $html_hold .= '<table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0" align="center"><tr>'; + + foreach (array_keys($row) as $val) + { + $html_hold .= '<th nowrap="nowrap">' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '</th>'; + } + $html_hold .= '</tr>'; + } + $html_hold .= '<tr>'; + + $class = 'row1'; + foreach (array_values($row) as $val) + { + $class = ($class == 'row1') ? 'row2' : 'row1'; + $html_hold .= '<td class="' . $class . '">' . (($val) ? $val : ' ') . '</td>'; + } + $html_hold .= '</tr>'; + } + } + + if ($html_table) + { + $html_hold .= '</table>'; + } + } + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = mysql_query($query, $this->db_connect_id); + while ($void = mysql_fetch_assoc($result)) + { + // Take the time spent on parsing rows into account + } + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query results obtained from the cache</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table><p align="center">'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p>'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + mysql_free_result($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query #' . $this->num_queries . '</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table> ' . $html_hold . '<p align="center">'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: <b>" . $this->sql_affectedrows($this->query_result) . '</b> | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: <b>' . sprintf('%.5f', $endtime - $curtime) . 's</b>'; + } + else + { + $error = $this->sql_error(); + $sql_report .= '<b style="color: red">FAILED</b> - MySQL Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '</p>'; + + $this->sql_time += $endtime - $curtime; + break; + } + } +} // class sql_db + +} // if ... define + +?>
\ No newline at end of file diff --git a/includes/db/mysql4.php b/includes/db/mysql4.php new file mode 100644 index 0000000..0639518 --- /dev/null +++ b/includes/db/mysql4.php @@ -0,0 +1,552 @@ +<?php +/** +* +* @package dbal_mysql4 +* @version $Id: mysql4.php,v 1.4 2006/02/10 01:30:19 scronide Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('SQL_LAYER')) +{ + +define('SQL_LAYER', 'mysql4'); + +/** +* @package dbal_mysql4 +* MySQL4 Database Abstraction Layer +* Minimum Requirement is 4.0+/4.1+ +*/ +class sql_db +{ + var $db_connect_id; + var $query_result; + var $return_on_error = false; + var $transaction = false; + var $sql_time = 0; + var $num_queries = 0; + var $open_queries = array(); + + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) + { + $this->persistency = $persistency; + $this->user = $sqluser; + $this->server = $sqlserver . (($port) ? ':' . $port : ''); + $this->dbname = $database; + + $this->db_connect_id = ($this->persistency) ? @mysql_pconnect($this->server, $this->user, $sqlpassword) : @mysql_connect($this->server, $this->user, $sqlpassword); + + if ($this->db_connect_id && $this->dbname != '') + { + if (@mysql_select_db($this->dbname)) + { + return $this->db_connect_id; + } + } + + return $this->sql_error(''); + } + + // + // Other base methods + // + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + if (sizeof($this->open_queries)) + { + foreach ($this->open_queries as $i_query_id => $query_id) + { + @mysql_free_result($query_id); + } + } + + return @mysql_close($this->db_connect_id); + } + + function sql_return_on_error($fail = false) + { + $this->return_on_error = $fail; + } + + function sql_num_queries() + { + return $this->num_queries; + } + + function sql_transaction($status = 'begin') + { + switch ($status) + { + case 'begin': + $result = @mysql_query('BEGIN', $this->db_connect_id); + $this->transaction = true; + break; + + case 'commit': + $result = @mysql_query('COMMIT', $this->db_connect_id); + $this->transaction = false; + + if (!$result) + { + @mysql_query('ROLLBACK', $this->db_connect_id); + } + break; + + case 'rollback': + $result = @mysql_query('ROLLBACK', $this->db_connect_id); + $this->transaction = false; + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $cache_ttl = 0) + { + if ($query != '') + { + global $cache; + + // EXPLAIN only in extra debug mode + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('start', $query); + } + + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; + + if (!$this->query_result) + { + $this->num_queries++; + + if (($this->query_result = @mysql_query($query, $this->db_connect_id)) === false) + { + $this->sql_error($query); + } + + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('stop', $query); + } + + if ($cache_ttl && method_exists($cache, 'sql_save')) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + $cache->sql_save($query, $this->query_result, $cache_ttl); + // mysql_free_result called within sql_save() + } + else if (strpos($query, 'SELECT') !== false && $this->query_result) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + } + } + else if (defined('DEBUG_EXTRA')) + { + $this->sql_report('fromcache', $query); + } + } + else + { + return false; + } + + return ($this->query_result) ? $this->query_result : false; + } + + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) { + if ($query != '') { + $this->query_result = false; + + // only limit the number of rows if $total is greater than 0 + if ($total > 0) + $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total); + + return $this->sql_query($query, $cache_ttl); + } else { + return false; + } + } + + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) + { + if (!is_array($assoc_ary)) + { + return false; + } + + $fields = array(); + $values = array(); + if ($query == 'INSERT') + { + foreach ($assoc_ary as $key => $var) + { + $fields[] = $key; + + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE' || $query == 'SELECT') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); + } + + return $query; + } + + // 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) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @mysql_num_rows($query_id) : false; + } + + function sql_affectedrows() + { + return ($this->db_connect_id) ? @mysql_affected_rows($this->db_connect_id) : false; + } + + function sql_fetchrow($query_id = false) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($cache->sql_rowset[$query_id])) + { + return $cache->sql_fetchrow($query_id); + } + + return ($query_id) ? @mysql_fetch_assoc($query_id) : false; + } + + function sql_fetchrowset($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + unset($this->rowset[$query_id]); + unset($this->row[$query_id]); + + $result = array(); + while ($this->rowset[$query_id] = $this->sql_fetchrow($query_id)) + { + $result[] = $this->rowset[$query_id]; + } + return $result; + } + + return false; + } + + function sql_fetchfield($field, $rownum = -1, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($rownum > -1) + { + $result = @mysql_result($query_id, $rownum, $field); + } + else + { + if (empty($this->row[$query_id]) && empty($this->rowset[$query_id])) + { + if ($this->sql_fetchrow($query_id)) + { + $result = $this->row[$query_id][$field]; + } + } + else + { + if ($this->rowset[$query_id]) + { + $result = $this->rowset[$query_id][$field]; + } + elseif ($this->row[$query_id]) + { + $result = $this->row[$query_id][$field]; + } + } + } + return $result; + } + return false; + } + + function sql_rowseek($rownum, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @mysql_data_seek($query_id, $rownum) : false; + } + + function sql_nextid() + { + return ($this->db_connect_id) ? @mysql_insert_id($this->db_connect_id) : false; + } + + function sql_freeresult($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (isset($this->open_queries[(int) $query_id])) + { + unset($this->open_queries[(int) $query_id]); + return @mysql_free_result($query_id); + } + + return false; + } + + function sql_escape($msg) { + if (function_exists('mysql_real_escape_string')) { + return @mysql_real_escape_string($msg, $this->db_connect_id); + } else { + return mysql_escape_string($msg); + } + } + + function sql_error($sql = '') + { + if (!$this->return_on_error) + { + $this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : '')); + + $message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . @mysql_error() . '<br /><br /><u>CALLING PAGE</u><br /><br />' . htmlspecialchars($this_page) . (($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />'; + + if ($this->transaction) + { + $this->sql_transaction('rollback'); + } + + trigger_error($message, E_USER_ERROR); + } + + $result = array( + 'message' => @mysql_error(), + 'code' => @mysql_errno() + ); + + return $result; + } + + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $db, $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $db->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8869-1"><meta http-equiv="Content-Style-Type" content="text/css"><link rel="stylesheet" href="' . $phpbb_root_path . 'adm/subSilver.css" type="text/css"><style type="text/css">' . "\n"; + echo 'th { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic3.gif\') }' . "\n"; + echo 'td.cat { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic1.gif\') }' . "\n"; + echo '</style><title>' . $msg_title . '</title></head><body>'; + echo '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td><a href="' . htmlspecialchars(preg_replace('/&explain=([^&]*)/', '', $_SERVER['REQUEST_URI'])) . '"><img src="' . $phpbb_root_path . 'adm/images/header_left.jpg" width="200" height="60" alt="phpBB Logo" title="phpBB Logo" border="0"/></a></td><td width="100%" background="' . $phpbb_root_path . 'adm/images/header_bg.jpg" height="60" align="right" nowrap="nowrap"><span class="maintitle">SQL Report</span> </td></tr></table><br clear="all"/><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td height="40" align="center" valign="middle"><b>Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '</b></td></tr><tr><td align="center" nowrap="nowrap">Time spent on MySQL queries: <b>' . round($this->sql_time, 5) . 's</b> | Time spent on PHP: <b>' . round($totaltime - $this->sql_time, 5) . 's</b></td></tr></table><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td>'; + echo $sql_report; + echo '</td></tr></table><br /></body></html>'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $explain_query = $query; + if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) + { + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; + } + elseif (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) + { + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; + } + + if (preg_match('/^SELECT/', $explain_query)) + { + $html_table = FALSE; + + if ($result = mysql_query("EXPLAIN $explain_query", $this->db_connect_id)) + { + while ($row = mysql_fetch_assoc($result)) + { + if (!$html_table && sizeof($row)) + { + $html_table = TRUE; + $html_hold .= '<table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0" align="center"><tr>'; + + foreach (array_keys($row) as $val) + { + $html_hold .= '<th nowrap="nowrap">' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '</th>'; + } + $html_hold .= '</tr>'; + } + $html_hold .= '<tr>'; + + $class = 'row1'; + foreach (array_values($row) as $val) + { + $class = ($class == 'row1') ? 'row2' : 'row1'; + $html_hold .= '<td class="' . $class . '">' . (($val) ? $val : ' ') . '</td>'; + } + $html_hold .= '</tr>'; + } + } + + if ($html_table) + { + $html_hold .= '</table>'; + } + } + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = mysql_query($query, $this->db_connect_id); + while ($void = mysql_fetch_assoc($result)) + { + // Take the time spent on parsing rows into account + } + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query results obtained from the cache</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table><p align="center">'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p>'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + mysql_free_result($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query #' . $this->num_queries . '</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table> ' . $html_hold . '<p align="center">'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: <b>" . $this->sql_affectedrows($this->query_result) . '</b> | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: <b>' . sprintf('%.5f', $endtime - $curtime) . 's</b>'; + } + else + { + $error = $this->sql_error(); + $sql_report .= '<b style="color: red">FAILED</b> - MySQL Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '</p>'; + + $this->sql_time += $endtime - $curtime; + break; + } + } +} // class sql_db + +} // if ... define + +?>
\ No newline at end of file diff --git a/includes/db/mysqli.php b/includes/db/mysqli.php new file mode 100644 index 0000000..fa643c2 --- /dev/null +++ b/includes/db/mysqli.php @@ -0,0 +1,566 @@ +<?php +/** +* +* @package dbal_mysqli +* @version $Id: mysqli.php,v 1.4 2006/02/10 01:30:19 scronide Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('SQL_LAYER')) +{ + +define('SQL_LAYER', 'mysqli'); + +/** +* @package dbal_mysqli +* MySQLi Database Abstraction Layer +* Minimum Requirement is MySQL 4.1+ and the mysqli-extension +*/ +class sql_db +{ + var $db_connect_id; + var $query_result; + var $return_on_error = false; + var $transaction = false; + var $sql_time = 0; + var $num_queries = 0; + var $open_queries = array(); + + var $indexed = 0; + + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) + { + $this->persistency = $persistency; + $this->user = $sqluser; + $this->server = $sqlserver . (($port) ? ':' . $port : ''); + $this->dbname = $database; + + $this->db_connect_id = ($this->persistency) ? @mysqli_pconnect($this->server, $this->user, $sqlpassword) : @mysqli_connect($this->server, $this->user, $sqlpassword); + + if ($this->db_connect_id && $this->dbname != '') + { + if (@mysqli_select_db($this->db_connect_id, $this->dbname)) + { + return $this->db_connect_id; + } + } + + return $this->sql_error(''); + } + + // + // Other base methods + // + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + if ($this->transaction) + { + @mysqli_commit($this->db_connect_id); + } + + return @mysqli_close($this->db_connect_id); + } + + function sql_return_on_error($fail = false) + { + $this->return_on_error = $fail; + } + + function sql_num_queries() + { + return $this->num_queries; + } + + function sql_transaction($status = 'begin') + { + switch ($status) + { + case 'begin': + $result = @mysqli_autocommit($this->db_connect_id, false); + $this->transaction = true; + break; + + case 'commit': + $result = @mysqli_commit($this->db_connect_id); + @mysqli_autocommit($this->db_connect_id, true); + $this->transaction = false; + + if (!$result) + { + @mysqli_rollback($this->db_connect_id); + @mysqli_autocommit($this->db_connect_id, true); + } + break; + + case 'rollback': + $result = @mysqli_rollback($this->db_connect_id); + @mysqli_autocommit($this->db_connect_id, true); + $this->transaction = false; + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $cache_ttl = 0) + { + if ($query != '') + { + global $cache; + + // EXPLAIN only in extra debug mode + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('start', $query); + } + + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; + + if (!$this->query_result) + { + $this->num_queries++; + + if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false) + { + $this->sql_error($query); + } + + if (is_object($this->query_result)) + { + $this->query_result->cur_index = $this->indexed++; + } + + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('stop', $query); + } + + if ($cache_ttl && method_exists($cache, 'sql_save')) + { + $cache->sql_save($query, $this->query_result, $cache_ttl); + } + } + else if (defined('DEBUG_EXTRA')) + { + $this->sql_report('fromcache', $query); + } + } + else + { + return false; + } + + return ($this->query_result) ? $this->query_result : false; + } + + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) { + if ($query != '') { + $this->query_result = false; + + // only limit the number of rows if $total is greater than 0 + if ($total > 0) + $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total); + + return $this->sql_query($query, $cache_ttl); + } else { + return false; + } + } + + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) + { + if (!is_array($assoc_ary)) + { + return false; + } + + $fields = array(); + $values = array(); + if ($query == 'INSERT') + { + foreach ($assoc_ary as $key => $var) + { + $fields[] = $key; + + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE' || $query == 'SELECT') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); + } + + return $query; + } + + // 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) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @mysqli_num_rows($query_id) : false; + } + + function sql_affectedrows() + { + return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false; + } + + function sql_fetchrow($query_id = false) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (!is_object($query_id) && isset($cache->sql_rowset[$query_id])) + { + return $cache->sql_fetchrow($query_id); + } + + return ($query_id) ? @mysqli_fetch_assoc($query_id) : false; + } + + function sql_fetchrowset($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + $cur_index = (is_object($query_id)) ? $query_id->cur_index : $query_id; + + unset($this->rowset[$cur_index]); + unset($this->row[$cur_index]); + + $result = array(); + while ($this->rowset[$cur_index] = $this->sql_fetchrow($query_id)) + { + $result[] = $this->rowset[$cur_index]; + } + return $result; + } + + return false; + } + + function sql_fetchfield($field, $rownum = -1, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($rownum > -1) + { + @mysqli_data_seek($query_id, $rownum); + $row = @mysqli_fetch_assoc($query_id); + $result = isset($row[$field]) ? $row[$field] : false; + } + else + { + $cur_index = (is_object($query_id)) ? $query_id->cur_index : $query_id; + + if (empty($this->row[$cur_index]) && empty($this->rowset[$cur_index])) + { + if ($this->row[$cur_index] = $this->sql_fetchrow($query_id)) + { + $result = $this->row[$cur_index][$field]; + } + } + else + { + if ($this->rowset[$cur_index]) + { + $result = $this->rowset[$cur_index][$field]; + } + elseif ($this->row[$cur_index]) + { + $result = $this->row[$cur_index][$field]; + } + } + } + return $result; + } + return false; + } + + function sql_rowseek($rownum, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false; + } + + function sql_nextid() + { + return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false; + } + + function sql_freeresult($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + $cur_index = (is_object($query_id)) ? $query_id->cur_index : $query_id; + + unset($this->rowset[$cur_index]); + unset($this->row[$cur_index]); + + if (is_object($query_id)) + { + $this->indexed--; + return @mysqli_free_result($query_id); + } + else + { + return false; + } + } + + function sql_escape($msg) { + if (function_exists('mysql_real_escape_string')) { + return @mysql_real_escape_string($msg, $this->db_connect_id); + } else { + return mysql_escape_string($msg); + } + } + + function sql_error($sql = '') + { + if (!$this->return_on_error) + { + $this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : '')); + + $message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . @mysqli_error($this->db_connect_id) . '<br /><br /><u>CALLING PAGE</u><br /><br />' . htmlspecialchars($this_page) . (($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />'; + + if ($this->transaction) + { + $this->sql_transaction('rollback'); + } + + trigger_error($message, E_USER_ERROR); + } + + $result = array( + 'message' => @mysqli_error($this->db_connect_id), + 'code' => @mysqli_errno($this->db_connect_id) + ); + + return $result; + } + + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $db, $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $db->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8869-1"><meta http-equiv="Content-Style-Type" content="text/css"><link rel="stylesheet" href="' . $phpbb_root_path . 'adm/subSilver.css" type="text/css"><style type="text/css">' . "\n"; + echo 'th { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic3.gif\') }' . "\n"; + echo 'td.cat { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic1.gif\') }' . "\n"; + echo '</style><title>' . $msg_title . '</title></head><body>'; + echo '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td><a href="' . htmlspecialchars(preg_replace('/&explain=([^&]*)/', '', $_SERVER['REQUEST_URI'])) . '"><img src="' . $phpbb_root_path . 'adm/images/header_left.jpg" width="200" height="60" alt="phpBB Logo" title="phpBB Logo" border="0"/></a></td><td width="100%" background="' . $phpbb_root_path . 'adm/images/header_bg.jpg" height="60" align="right" nowrap="nowrap"><span class="maintitle">SQL Report</span> </td></tr></table><br clear="all"/><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td height="40" align="center" valign="middle"><b>Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '</b></td></tr><tr><td align="center" nowrap="nowrap">Time spent on MySQL queries: <b>' . round($this->sql_time, 5) . 's</b> | Time spent on PHP: <b>' . round($totaltime - $this->sql_time, 5) . 's</b></td></tr></table><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td>'; + echo $sql_report; + echo '</td></tr></table><br /></body></html>'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $explain_query = $query; + if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) + { + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; + } + elseif (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) + { + $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2]; + } + + if (preg_match('/^SELECT/', $explain_query)) + { + $html_table = FALSE; + + if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query")) + { + while ($row = @mysqli_fetch_assoc($result)) + { + if (!$html_table && sizeof($row)) + { + $html_table = TRUE; + $html_hold .= '<table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0" align="center"><tr>'; + + foreach (array_keys($row) as $val) + { + $html_hold .= '<th nowrap="nowrap">' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '</th>'; + } + $html_hold .= '</tr>'; + } + $html_hold .= '<tr>'; + + $class = 'row1'; + foreach (array_values($row) as $val) + { + $class = ($class == 'row1') ? 'row2' : 'row1'; + $html_hold .= '<td class="' . $class . '">' . (($val) ? $val : ' ') . '</td>'; + } + $html_hold .= '</tr>'; + } + } + + if ($html_table) + { + $html_hold .= '</table>'; + } + } + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = @mysqli_query($this->db_connect_id, $query); + while ($void = @mysqli_fetch_assoc($result)) + { + // Take the time spent on parsing rows into account + } + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query results obtained from the cache</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table><p align="center">'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p>'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + @mysqli_free_result($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query #' . $this->num_queries . '</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table> ' . $html_hold . '<p align="center">'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: <b>" . $this->sql_affectedrows($this->query_result) . '</b> | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: <b>' . sprintf('%.5f', $endtime - $curtime) . 's</b>'; + } + else + { + $error = $this->sql_error(); + $sql_report .= '<b style="color: red">FAILED</b> - MySQL Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '</p>'; + + $this->sql_time += $endtime - $curtime; + break; + } + } +} // class sql_db + +} // if ... define + +?>
\ No newline at end of file diff --git a/includes/db/oracle.php b/includes/db/oracle.php new file mode 100644 index 0000000..7ef10e5 --- /dev/null +++ b/includes/db/oracle.php @@ -0,0 +1,468 @@ +<?php +/** +* +* @package dbal_oracle +* @version $Id: oracle.php,v 1.2 2005/06/10 08:52:03 devalley Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if(!defined("SQL_LAYER")) +{ + +define("SQL_LAYER","oracle"); + +/** +* @package dbal_oracle +* Oracle Database Abstraction Layer +*/ +class sql_db +{ + + var $db_connect_id; + var $query_result; + var $in_transaction = 0; + var $row = array(); + var $rowset = array(); + var $num_queries = 0; + var $last_query_text = ""; + + // + // Constructor + // + function sql_db($sqlserver, $sqluser, $sqlpassword, $database="", $persistency = true) + { + $this->persistency = $persistency; + $this->user = $sqluser; + $this->password = $sqlpassword; + $this->server = $sqlserver; + $this->dbname = $database; + + if($this->persistency) + { + $this->db_connect_id = @OCIPLogon($this->user, $this->password, $this->server); + } + else + { + $this->db_connect_id = @OCINLogon($this->user, $this->password, $this->server); + } + if($this->db_connect_id) + { + return $this->db_connect_id; + } + else + { + return false; + } + } + + // + // Other base methods + // + function sql_close() + { + if($this->db_connect_id) + { + // Commit outstanding transactions + if($this->in_transaction) + { + OCICommit($this->db_connect_id); + } + + if($this->query_result) + { + @OCIFreeStatement($this->query_result); + } + $result = @OCILogoff($this->db_connect_id); + return $result; + } + else + { + return false; + } + } + + // + // Base query method + // + function sql_query($query = "", $transaction = FALSE) + { + // Remove any pre-existing queries + unset($this->query_result); + + // Put us in transaction mode because with Oracle as soon as you make a query you're in a transaction + $this->in_transaction = TRUE; + + if($query != "") + { + $this->last_query = $query; + $this->num_queries++; + + if(eregi("LIMIT", $query)) + { + preg_match("/^(.*)LIMIT ([0-9]+)[, ]*([0-9]+)*/s", $query, $limits); + + $query = $limits[1]; + if($limits[3]) + { + $row_offset = $limits[2]; + $num_rows = $limits[3]; + } + else + { + $row_offset = 0; + $num_rows = $limits[2]; + } + } + + if(eregi("^(INSERT|UPDATE) ", $query)) + { + $query = preg_replace("/\\\'/s", "''", $query); + } + + $this->query_result = @OCIParse($this->db_connect_id, $query); + $success = @OCIExecute($this->query_result, OCI_DEFAULT); + } + if($success) + { + if($transaction == END_TRANSACTION) + { + OCICommit($this->db_connect_id); + $this->in_transaction = FALSE; + } + + unset($this->row[$this->query_result]); + unset($this->rowset[$this->query_result]); + $this->last_query_text[$this->query_result] = $query; + + return $this->query_result; + } + else + { + if($this->in_transaction) + { + OCIRollback($this->db_connect_id); + } + return false; + } + } + + // + // Other query methods + // + function sql_numrows($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = @OCIFetchStatement($query_id, $this->rowset); + // OCIFetchStatment kills our query result so we have to execute the statment again + // if we ever want to use the query_id again. + @OCIExecute($query_id, OCI_DEFAULT); + return $result; + } + else + { + return false; + } + } + function sql_affectedrows($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = @OCIRowCount($query_id); + return $result; + } + else + { + return false; + } + } + function sql_numfields($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = @OCINumCols($query_id); + return $result; + } + else + { + return false; + } + } + function sql_fieldname($offset, $query_id = 0) + { + // OCIColumnName uses a 1 based array so we have to up the offset by 1 in here to maintain + // full abstraction compatibitly + $offset += 1; + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = strtolower(@OCIColumnName($query_id, $offset)); + return $result; + } + else + { + return false; + } + } + function sql_fieldtype($offset, $query_id = 0) + { + // This situation is the same as fieldname + $offset += 1; + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = @OCIColumnType($query_id, $offset); + return $result; + } + else + { + return false; + } + } + function sql_fetchrow($query_id = 0, $debug = FALSE) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result_row = ""; + $result = @OCIFetchInto($query_id, $result_row, OCI_ASSOC+OCI_RETURN_NULLS); + if($debug) + { + echo "Query was: ".$this->last_query . "<br>"; + echo "Result: $result<br>"; + echo "Query ID: $query_id<br>"; + echo "<pre>"; + var_dump($result_row); + echo "</pre>"; + } + if($result_row == "") + { + return false; + } + + for($i = 0; $i < count($result_row); $i++) + { + list($key, $val) = each($result_row); + $return_arr[strtolower($key)] = $val; + } + $this->row[$query_id] = $return_arr; + + return $this->row[$query_id]; + } + else + { + return false; + } + } + // This function probably isn't as efficant is it could be but any other way I do it + // I end up losing 1 row... + function sql_fetchrowset($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $rows = @OCIFetchStatement($query_id, $results); + @OCIExecute($query_id, OCI_DEFAULT); + for($i = 0; $i <= $rows; $i++) + { + @OCIFetchInto($query_id, $tmp_result, OCI_ASSOC+OCI_RETURN_NULLS); + + for($j = 0; $j < count($tmp_result); $j++) + { + list($key, $val) = each($tmp_result); + $return_arr[strtolower($key)] = $val; + } + $result[] = $return_arr; + } + return $result; + } + else + { + return false; + } + } + function sql_fetchfield($field, $rownum = -1, $query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + if($rownum > -1) + { + // Reset the internal rownum pointer. + @OCIExecute($query_id, OCI_DEFAULT); + for($i = 0; $i < $rownum; $i++) + { + // Move the interal pointer to the row we want + @OCIFetch($query_id); + } + // Get the field data. + $result = @OCIResult($query_id, strtoupper($field)); + } + else + { + // The internal pointer should be where we want it + // so we just grab the field out of the current row. + $result = @OCIResult($query_id, strtoupper($field)); + } + return $result; + } + else + { + return false; + } + } + function sql_rowseek($rownum, $query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + @OCIExecute($query_id, OCI_DEFAULT); + for($i = 0; $i < $rownum; $i++) + { + @OCIFetch($query_id); + } + $result = @OCIFetch($query_id); + return $result; + } + else + { + return false; + } + } + function sql_nextid($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id && $this->last_query_text[$query_id] != "") + { + if( eregi("^(INSERT{1}|^INSERT INTO{1})[[:space:]][\"]?([a-zA-Z0-9\_\-]+)[\"]?", $this->last_query_text[$query_id], $tablename)) + { + $query = "SELECT ".$tablename[2]."_id_seq.currval FROM DUAL"; + $stmt = @OCIParse($this->db_connect_id, $query); + @OCIExecute($stmt,OCI_DEFAULT ); + $temp_result = @OCIFetchInto($stmt, $temp_result, OCI_ASSOC+OCI_RETURN_NULLS); + if($temp_result) + { + return $temp_result['CURRVAL']; + } + else + { + return false; + } + } + else + { + return false; + } + } + else + { + return false; + } + } + + function sql_nextid($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id && $this->last_query_text[$query_id] != "") + { + if( eregi("^(INSERT{1}|^INSERT INTO{1})[[:space:]][\"]?([a-zA-Z0-9\_\-]+)[\"]?", $this->last_query_text[$query_id], $tablename)) + { + $query = "SELECT ".$tablename[2]."_id_seq.CURRVAL FROM DUAL"; + $temp_q_id = @OCIParse($this->db_connect_id, $query); + @OCIExecute($temp_q_id, OCI_DEFAULT); + @OCIFetchInto($temp_q_id, $temp_result, OCI_ASSOC+OCI_RETURN_NULLS); + + if($temp_result) + { + return $temp_result['CURRVAL']; + } + else + { + return false; + } + } + else + { + return false; + } + } + else + { + return false; + } + } + + + + function sql_freeresult($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = @OCIFreeStatement($query_id); + return $result; + } + else + { + return false; + } + } + function sql_error($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + $result = @OCIError($query_id); + return $result; + } + +} // class sql_db + +} // if ... define + +?>
\ No newline at end of file diff --git a/includes/db/postgres.php b/includes/db/postgres.php new file mode 100644 index 0000000..b5bad20 --- /dev/null +++ b/includes/db/postgres.php @@ -0,0 +1,597 @@ +<?php +/** +* +* @package dbal_postgres +* @version $Id: postgres.php,v 1.2 2005/06/10 08:52:03 devalley Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('SQL_LAYER')) +{ + +define('SQL_LAYER', 'postgresql'); + +/** +* @package dbal_postgres +* PostgreSQL Database Abstraction Layer +* Minimum Requirement is Version 7.3+ +*/ +class sql_db +{ + var $db_connect_id; + var $query_result; + var $return_on_error = false; + var $transaction = false; + var $sql_time = 0; + var $num_queries = 0; + var $open_queries = array(); + + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) + { + $this->connect_string = ''; + + if ($sqluser) + { + $this->connect_string .= "user=$sqluser "; + } + + if ($sqlpassword) + { + $this->connect_string .= "password=$sqlpassword "; + } + + if ($sqlserver) + { + if (ereg(":", $sqlserver)) + { + list($sqlserver, $sqlport) = split(":", $sqlserver); + $this->connect_string .= "host=$sqlserver port=$sqlport "; + } + else + { + if ($sqlserver != "localhost") + { + $this->connect_string .= "host=$sqlserver "; + } + + if ($port) + { + $this->connect_string .= "port=$port "; + } + } + } + + if ($database) + { + $this->dbname = $database; + $this->connect_string .= "dbname=$database"; + } + + $this->persistency = $persistency; + + $this->db_connect_id = ($this->persistency) ? @pg_pconnect($this->connect_string) : @pg_connect($this->connect_string); + + return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error(''); + } + + // + // Other base methods + // + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + if ($this->transaction) + { + @pg_exec($this->db_connect_id, 'COMMIT'); + } + + return @pg_close($this->db_connect_id); + } + + function sql_return_on_error($fail = false) + { + $this->return_on_error = $fail; + } + + function sql_num_queries() + { + return $this->num_queries; + } + + function sql_transaction($status = 'begin') + { + switch ($status) + { + case 'begin': + $result = @pg_exec($this->db_connect_id, 'BEGIN'); + $this->transaction = true; + break; + + case 'commit': + $result = @pg_exec($this->db_connect_id, 'COMMIT'); + $this->transaction = false; + + if (!$result) + { + @pg_exec($this->db_connect_id, 'ROLLBACK'); + } + break; + + case 'rollback': + $result = @pg_exec($this->db_connect_id, 'ROLLBACK'); + $this->transaction = false; + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $cache_ttl = 0) + { + if ($query != '') + { + global $cache; + + // EXPLAIN only in extra debug mode + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('start', $query); + } + + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false; + + if (!$this->query_result) + { + $this->num_queries++; + $this->last_query_text = $query; + + if (($this->query_result = @pg_exec($this->db_connect_id, $query)) === false) + { + $this->sql_error($query); + } + + if (defined('DEBUG_EXTRA')) + { + $this->sql_report('stop', $query); + } + + if ($cache_ttl && method_exists($cache, 'sql_save')) + { + $cache->sql_save($query, $this->query_result, $cache_ttl); + } + } + else if (defined('DEBUG_EXTRA')) + { + $this->sql_report('fromcache', $query); + } + } + else + { + return false; + } + + return ($this->query_result) ? $this->query_result : false; + } + + function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) + { + if ($query != '') + { + $this->query_result = false; + + // if $total is set to 0 we do not want to limit the number of rows + if ($total == 0) + { + $total = -1; + } + + $query .= "\n LIMIT $total OFFSET $offset"; + + return $this->sql_query($query, $cache_ttl); + } + else + { + return false; + } + } + + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) + { + if (!is_array($assoc_ary)) + { + return false; + } + + $fields = array(); + $values = array(); + if ($query == 'INSERT') + { + foreach ($assoc_ary as $key => $var) + { + $fields[] = $key; + + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE' || $query == 'SELECT') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); + } + + return $query; + } + + // 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) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @pg_numrows($query_id) : false; + } + + function sql_affectedrows($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @pg_cmdtuples($query_id) : false; + } + + function sql_fetchrow($query_id = false) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if (!isset($this->rownum[$query_id])) + { + $this->rownum[$query_id] = 0; + } + + if (isset($cache->sql_rowset[$query_id])) + { + return $cache->sql_fetchrow($query_id); + } + + $result = @pg_fetch_array($query_id, NULL, PGSQL_ASSOC); + + if ($result) + { + $this->rownum[$query_id]++; + } + + return $result; + } + + function sql_fetchrowset($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + $result = array(); + + if ($query_id) + { + unset($this->rowset[$query_id]); + unset($this->row[$query_id]); + + $result = array(); + while ($this->rowset[$query_id] = $this->sql_fetchrow($query_id)) + { + $result[] = $this->rowset[$query_id]; + } + return $result; + } + + return false; + } + + function sql_fetchfield($field, $rownum = -1, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($rownum > -1) + { + if (@function_exists('pg_result_seek')) + { + @pg_result_seek($query_id, $rownum); + $row = @pg_fetch_assoc($query_id); + $result = isset($row[$field]) ? $row[$field] : false; + } + else + { + $this->sql_rowseek($offset, $query_id); + $row = $this->sql_fetchrow($query_id); + $result = isset($row[$field]) ? $row[$field] : false; + } + } + else + { + if (empty($this->row[$query_id]) && empty($this->rowset[$query_id])) + { + if ($this->sql_fetchrow($query_id)) + { + $result = $this->row[$query_id][$field]; + } + } + else + { + if ($this->rowset[$query_id]) + { + $result = $this->rowset[$query_id][$field]; + } + elseif ($this->row[$query_id]) + { + $result = $this->row[$query_id][$field]; + } + } + } + return $result; + } + return false; + } + + function sql_rowseek($offset, $query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + if ($offset > -1) + { + if (@function_exists('pg_result_seek')) + { + @pg_result_seek($query_id, $rownum); + } + else + { + for ($i = $this->rownum[$query_id]; $i < $offset; $i++) + { + $this->sql_fetchrow($query_id); + } + } + return true; + } + else + { + return false; + } + } + + return false; + } + + function sql_nextid() + { + $query_id = $this->query_result; + + if ($query_id && $this->last_query_text != '') + { + if (preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text, $tablename)) + { + $query = "SELECT currval('" . $tablename[1] . "_id_seq') AS last_value"; + $temp_q_id = @pg_exec($this->db_connect_id, $query); + if (!$temp_q_id) + { + return false; + } + + $temp_result = @pg_fetch_array($temp_q_id, NULL, PGSQL_ASSOC); + + return ($temp_result) ? $temp_result['last_value'] : false; + } + } + + return false; + } + + function sql_freeresult($query_id = false) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return (is_resource($query_id)) ? @pg_freeresult($query_id) : false; + } + + function sql_escape($msg) + { + return str_replace("'", "''", str_replace('\\', '\\\\', $msg)); + } + + function sql_error($sql = '') + { + if (!$this->return_on_error) + { + $this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : '')); + + $message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . @pg_errormessage() . '<br /><br /><u>CALLING PAGE</u><br /><br />' . htmlspecialchars($this_page) . (($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />'; + + if ($this->transaction) + { + $this->sql_transaction('rollback'); + } + + trigger_error($message, E_USER_ERROR); + } + + $result = array( + 'message' => @pg_errormessage(), + 'code' => '' + ); + + return $result; + } + + function sql_report($mode, $query = '') + { + if (empty($_GET['explain'])) + { + return; + } + + global $cache, $starttime, $phpbb_root_path; + static $curtime, $query_hold, $html_hold; + static $sql_report = ''; + static $cache_num_queries = 0; + + if (!$query && !empty($query_hold)) + { + $query = $query_hold; + } + + switch ($mode) + { + case 'display': + if (!empty($cache)) + { + $cache->unload(); + } + $this->sql_close(); + + $mtime = explode(' ', microtime()); + $totaltime = $mtime[0] + $mtime[1] - $starttime; + + echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8869-1"><meta http-equiv="Content-Style-Type" content="text/css"><link rel="stylesheet" href="' . $phpbb_root_path . 'adm/subSilver.css" type="text/css"><style type="text/css">' . "\n"; + echo 'th { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic3.gif\') }' . "\n"; + echo 'td.cat { background-image: url(\'' . $phpbb_root_path . 'adm/images/cellpic1.gif\') }' . "\n"; + echo '</style><title>' . $msg_title . '</title></head><body>'; + echo '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td><a href="' . htmlspecialchars(preg_replace('/&explain=([^&]*)/', '', $_SERVER['REQUEST_URI'])) . '"><img src="' . $phpbb_root_path . 'adm/images/header_left.jpg" width="200" height="60" alt="phpBB Logo" title="phpBB Logo" border="0"/></a></td><td width="100%" background="' . $phpbb_root_path . 'adm/images/header_bg.jpg" height="60" align="right" nowrap="nowrap"><span class="maintitle">SQL Report</span> </td></tr></table><br clear="all"/><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td height="40" align="center" valign="middle"><b>Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($cache_num_queries) ? " + $cache_num_queries " . (($cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '</b></td></tr><tr><td align="center" nowrap="nowrap">Time spent on MySQL queries: <b>' . round($this->sql_time, 5) . 's</b> | Time spent on PHP: <b>' . round($totaltime - $this->sql_time, 5) . 's</b></td></tr></table><table width="95%" cellspacing="1" cellpadding="4" border="0" align="center"><tr><td>'; + echo $sql_report; + echo '</td></tr></table><br /></body></html>'; + exit; + break; + + case 'start': + $query_hold = $query; + $html_hold = ''; + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1]; + break; + + case 'fromcache': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $result = @pg_exec($this->db_connect_id, $query); + while ($void = @pg_fetch_array($result, NULL, PGSQL_ASSOC)) + { + // Take the time spent on parsing rows into account + } + $splittime = explode(' ', microtime()); + $splittime = $splittime[0] + $splittime[1]; + + $time_cache = $endtime - $curtime; + $time_db = $splittime - $endtime; + $color = ($time_db > $time_cache) ? 'green' : 'red'; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query results obtained from the cache</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table><p align="center">'; + + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: <b style="color: ' . $color . '">' . sprintf('%.5f', ($time_cache)) . 's</b> | Elapsed [db]: <b>' . sprintf('%.5f', $time_db) . 's</b></p>'; + + // Pad the start time to not interfere with page timing + $starttime += $time_db; + + @pg_freeresult($result); + $cache_num_queries++; + break; + + case 'stop': + $endtime = explode(' ', microtime()); + $endtime = $endtime[0] + $endtime[1]; + + $sql_report .= '<hr width="100%"/><br /><table class="bg" width="100%" cellspacing="1" cellpadding="4" border="0"><tr><th>Query #' . $this->num_queries . '</th></tr><tr><td class="row1"><textarea style="font-family:\'Courier New\',monospace;width:100%" rows="5">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea></td></tr></table> ' . $html_hold . '<p align="center">'; + + if ($this->query_result) + { + if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) + { + $sql_report .= "Affected rows: <b>" . $this->sql_affectedrows($this->query_result) . '</b> | '; + } + $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: <b>' . sprintf('%.5f', $endtime - $curtime) . 's</b>'; + } + else + { + $error = $this->sql_error(); + $sql_report .= '<b style="color: red">FAILED</b> - ' . SQL_LAYER . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + } + + $sql_report .= '</p>'; + + $this->sql_time += $endtime - $curtime; + break; + } + } + +} // class ... db_sql + +} // if ... defined + +?>
\ No newline at end of file diff --git a/includes/db/sqlite.php b/includes/db/sqlite.php new file mode 100644 index 0000000..1591396 --- /dev/null +++ b/includes/db/sqlite.php @@ -0,0 +1,387 @@ +<?php +/** +* +* @package dbal_sqlite +* @version $Id: sqlite.php,v 1.2 2005/06/10 08:52:03 devalley Exp $ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined("SQL_LAYER")) +{ + +define("SQL_LAYER","sqlite"); + +/** +* @package dbal_sqlite +* Sqlite Database Abstraction Layer +*/ +class sql_db +{ + var $db_connect_id; + var $query_result; + var $return_on_error = false; + var $transaction = false; + var $sql_report = ''; + var $sql_time = 0; + var $num_queries = 0; + var $open_queries = array(); + + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port, $persistency = false) + { + $this->persistency = $persistency; + $this->user = $sqluser; + $this->server = $sqlserver . (($port) ? ':' . $port : ''); + $this->dbname = $database; + + $this->db_connect_id = ($this->persistency) ? @sqlite_popen($this->server, 0, $error) : @sqlite_open($this->server, 0, $error); + + return ($this->db_connect_id) ? true : $error; + } + + // Other base methods + function sql_close() + { + if (!$this->db_connect_id) + { + return false; + } + + return @sqlite_close($this->db_connect_id); + } + + function sql_return_on_error($fail = false) + { + $this->return_on_error = $fail; + } + + function sql_num_queries() + { + return $this->num_queries; + } + + function sql_transaction($status = 'begin') + { + switch ($status) + { + case 'begin': + $this->transaction = true; + $result = @sqlite_query('BEGIN', $this->db_connect_id); + break; + + case 'commit': + $this->transaction = false; + $result = @sqlite_query('COMMIT', $this->db_connect_id); + break; + + case 'rollback': + $this->transaction = false; + $result = @sqlite_query('ROLLBACK', $this->db_connect_id); + break; + + default: + $result = true; + } + + return $result; + } + + // Base query method + function sql_query($query = '', $expire_time = 0) + { + if ($query != '') + { + global $cache; + + $query = preg_replace('#FROM \((.*?)\)(,|[\n\t ]+?WHERE) #s', 'FROM \1\2 ', $query); + + if (!$expire_time || !$cache->sql_load($query, $expire_time)) + { + if ($expire_time) + { + $cache_result = true; + } + + $this->query_result = false; + $this->num_queries++; + + if (!empty($_GET['explain'])) + { + global $starttime; + + $curtime = explode(' ', microtime()); + $curtime = $curtime[0] + $curtime[1] - $starttime; + } + + if (!($this->query_result = @sqlite_query($query, $this->db_connect_id))) + { + $this->sql_error($query); + } + + if (!empty($_GET['explain'])) + { + $endtime = explode(' ', microtime()); + $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> - SQLite ' . $error['code'] . ': ' . htmlspecialchars($error['message']) . '<br><br><pre>'; + } + + $this->sql_time += $endtime - $curtime; + + if (preg_match('#^SELECT#', $query)) + { + $html_table = FALSE; + if ($result = @sqlite_query("EXPLAIN $query", $this->db_connect_id)) + { + while ($row = @sqlite_fetch_array($result, @sqlite_ASSOC)) + { + if (!$html_table && sizeof($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(" </td>\n<td>", array_values($row)) . " </td>\n</tr>\n"; + } + } + + if ($html_table) + { + $this->sql_report .= '</table><br>'; + } + } + + $this->sql_report .= "<hr>\n"; + } + + if (preg_match('#^SELECT#', $query)) + { + $this->open_queries[] = $this->query_result; + } + } + + if (!empty($cache_result)) + { + $cache->sql_save($query, $this->query_result); + } + } + else + { + return false; + } + + return ($this->query_result) ? $this->query_result : false; + } + + function sql_query_limit($query, $total, $offset = 0, $expire_time = 0) + { + if ($query != '') + { + $this->query_result = false; + + $query .= ' LIMIT ' . ((!empty($offset)) ? $total . ' OFFSET ' . $offset : $total); + + return $this->sql_query($query, $expire_time); + } + else + { + return false; + } + } + + // Idea for this from Ikonboard + function sql_build_array($query, $assoc_ary = false) + { + if (!is_array($assoc_ary)) + { + return false; + } + + $fields = array(); + $values = array(); + if ($query == 'INSERT') + { + foreach ($assoc_ary as $key => $var) + { + $fields[] = $key; + + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + + $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; + } + else if ($query == 'UPDATE') + { + $values = array(); + foreach ($assoc_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = "$key = NULL"; + } + elseif (is_string($var)) + { + $values[] = "$key = '" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var"; + } + } + $query = implode(', ', $values); + } + + return $query; + } + + // 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) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @sqlite_num_rows($query_id) : false; + } + + function sql_affectedrows() + { + return ($this->db_connect_id) ? @sqlite_changes($this->db_connect_id) : false; + } + + function sql_fetchrow($query_id = 0) + { + global $cache; + + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($cache->sql_exists($query_id)) + { + return $cache->sql_fetchrow($query_id); + } + + return ($query_id) ? @sqlite_fetch_array($query_id, @sqlite_ASSOC) : false; + } + + function sql_fetchrowset($query_id = 0) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + unset($this->rowset[$query_id]); + unset($this->row[$query_id]); + while ($this->rowset[$query_id] = @sqlite_fetch_array($query_id, @sqlite_ASSOC)) + { + $result[] = $this->rowset[$query_id]; + } + return $result; + } + else + { + return false; + } + } + + function sql_fetchfield($field, $rownum = -1, $query_id = 0) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + if ($query_id) + { + return ($rownum > -1) ? ((@sqlite_seek($query_id, $rownum)) ? @sqlite_column($query_id, $field) : false) : @sqlite_column($query_id, $field); + } + } + + function sql_rowseek($rownum, $query_id = 0) + { + if (!$query_id) + { + $query_id = $this->query_result; + } + + return ($query_id) ? @sqlite_seek($query_id, $rownum) : false; + } + + function sql_nextid() + { + return ($this->db_connect_id) ? @sqlite_last_insert_rowid($this->db_connect_id) : false; + } + + function sql_freeresult($query_id = false) + { + return true; + } + + function sql_escape($msg) + { + return @sqlite_escape_string(stripslashes($msg)); + } + + function sql_error($sql = '') + { + if (!$this->return_on_error) + { + $this_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF']; + $this_page .= '&' . ((!empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : $_ENV['QUERY_STRING']); + + $message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . @sqlite_error_string(@sqlite_last_error($this->db_connect_id)) . '<br /><br /><u>CALLING PAGE</u><br /><br />' . htmlspecialchars($this_page) . (($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />'; + + if ($this->transaction) + { + $this->sql_transaction('rollback'); + } + + trigger_error($message, E_USER_ERROR); + } + + $result = array( + 'message' => @sqlite_error_string(@sqlite_last_error($this->db_connect_id)), + 'code' => @sqlite_last_error($this->db_connect_id) + ); + + return $result; + } + +} // class sql_db + +} // if ... define + +?>
\ No newline at end of file |