From 1d688c6b8de0073e637be7601e0efbd5947bce00 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sun, 2 Oct 2005 18:18:52 +0000 Subject: [PATCH] - add building multi insert to dbal - fix sql explain in mysql4 layer git-svn-id: file:///svn/phpbb/trunk@5242 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/db/dbal.php | 117 ++++++++++++++++++++++++++++------- phpBB/includes/db/mysql4.php | 42 ++++++------- 2 files changed, 117 insertions(+), 42 deletions(-) diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index 70089aa11f..f91f7ef469 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -22,17 +22,34 @@ class dbal var $num_queries = 0; var $open_queries = array(); + var $curtime = 0; + var $query_hold = ''; + var $html_hold = ''; + var $sql_report = ''; + var $cache_num_queries = 0; + + + /** + * return on error or display error message + */ function sql_return_on_error($fail = false) { $this->return_on_error = $fail; } + /** + * Return number of sql queries used (cached and real queries are counted the same) + */ function sql_num_queries() { return $this->num_queries; } - // Idea for this from Ikonboard + /** + * Build sql statement from array for insert/update/select statements + * + * Idea for this from Ikonboard + */ function sql_build_array($query, $assoc_ary = false) { if (!is_array($assoc_ary)) @@ -64,6 +81,32 @@ class dbal $query = ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'; } + else if ($query == 'MULTI_INSERT') + { + $ary = array(); + foreach ($assoc_ary as $id => $sql_ary) + { + $values = array(); + foreach ($sql_ary as $key => $var) + { + if (is_null($var)) + { + $values[] = 'NULL'; + } + elseif (is_string($var)) + { + $values[] = "'" . $this->sql_escape($var) . "'"; + } + else + { + $values[] = (is_bool($var)) ? intval($var) : $var; + } + } + $ary[] = '(' . implode(', ', $values) . ')'; + } + + $query = ' (' . implode(', ', array_keys($assoc_ary[0])) . ') VALUES ' . implode(', ', $ary); + } else if ($query == 'UPDATE' || $query == 'SELECT') { $values = array(); @@ -88,6 +131,9 @@ class dbal return $query; } + /** + * display sql error page + */ function sql_error($sql = '') { $error = $this->db_sql_error(); @@ -110,21 +156,22 @@ class dbal return $error; } + /** + * Explain queries + * @child _sql_report + */ function sql_report($mode, $query = '') { + global $cache, $starttime, $phpbb_root_path; + 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)) + if (!$query && $this->query_hold != '') { - $query = $query_hold; + $query = $this->query_hold; } switch ($mode) @@ -139,13 +186,27 @@ class dbal $mtime = explode(' ', microtime()); $totaltime = $mtime[0] + $mtime[1] - $starttime; - echo '' . $msg_title . ''; - echo '
phpBB LogoSQL Report      

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' : '') . '
Time spent on MySQL queries: ' . round($this->sql_time, 5) . 's | Time spent on PHP: ' . round($totaltime - $this->sql_time, 5) . 's
'; - echo $sql_report; - echo '

'; + echo ' + + + Explain + + + +
phpBB LogoSQL Report      
+
+ + + + +
Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries} queries" . (($this->cache_num_queries) ? " + {$this->cache_num_queries} " . (($this->cache_num_queries == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '
Time spent on MySQL queries: ' . round($this->sql_time, 5) . 's | Time spent on PHP: ' . round($totaltime - $this->sql_time, 5) . 's
+ + +
+ ' . $this->sql_report . '
+
+ + '; exit; break; @@ -153,25 +214,37 @@ class dbal $endtime = explode(' ', microtime()); $endtime = $endtime[0] + $endtime[1]; - $sql_report .= '

Query #' . $this->num_queries . '
' . $html_hold . '

'; + $this->sql_report .= ' +



+ + + + + + + + +
Query #' . $this->num_queries . '
' . $this->html_hold . ' +

+ '; if ($this->query_result) { if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query)) { - $sql_report .= "Affected rows: " . $this->sql_affectedrows($this->query_result) . ' | '; + $this->sql_report .= 'Affected rows: ' . $this->sql_affectedrows($this->query_result) . ' | '; } - $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: ' . sprintf('%.5f', $endtime - $curtime) . 's'; + $this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: ' . sprintf('%.5f', $endtime - $this->curtime) . 's'; } else { $error = $this->sql_error(); - $sql_report .= 'FAILED - ' . SQL_LAYER . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); + $this->sql_report .= 'FAILED - ' . SQL_LAYER . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']); } - $sql_report .= '

'; + $this->sql_report .= '

'; - $this->sql_time += $endtime - $curtime; + $this->sql_time += $endtime - $this->curtime; break; default: @@ -183,6 +256,8 @@ class dbal } } +/** +*/ if (!defined('IN_PHPBB')) { exit; diff --git a/phpBB/includes/db/mysql4.php b/phpBB/includes/db/mysql4.php index df176db329..6d3438006f 100644 --- a/phpBB/includes/db/mysql4.php +++ b/phpBB/includes/db/mysql4.php @@ -20,11 +20,14 @@ if (!defined('SQL_LAYER')) /** * @package dbal * MySQL4 Database Abstraction Layer -* Minimum Requirement is 4.0+/4.1+ +* Minimum Requirement is 4.0+ (4.1+ compatible) */ class dbal_mysql4 extends dbal { + /** + * Connect to sql server + */ function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) { $this->persistency = $persistency; @@ -319,15 +322,12 @@ class dbal_mysql4 extends dbal function _sql_report($mode, $query = '') { global $cache, $starttime, $phpbb_root_path; - static $curtime, $query_hold, $html_hold; - static $sql_report = ''; - static $cache_num_queries = 0; switch ($mode) { case 'start': - $query_hold = $query; - $html_hold = ''; + $this->query_hold = $query; + $this->html_hold = ''; $explain_query = $query; if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m)) @@ -341,7 +341,7 @@ class dbal_mysql4 extends dbal if (preg_match('/^SELECT/', $explain_query)) { - $html_table = FALSE; + $html_table = false; if ($result = mysql_query("EXPLAIN $explain_query", $this->db_connect_id)) { @@ -349,35 +349,35 @@ class dbal_mysql4 extends dbal { if (!$html_table && sizeof($row)) { - $html_table = TRUE; - $html_hold .= ''; + $html_table = true; + $this->html_hold .= '
'; foreach (array_keys($row) as $val) { - $html_hold .= ''; + $this->html_hold .= ''; } - $html_hold .= ''; + $this->html_hold .= ''; } - $html_hold .= ''; + $this->html_hold .= ''; $class = 'row1'; foreach (array_values($row) as $val) { $class = ($class == 'row1') ? 'row2' : 'row1'; - $html_hold .= ''; + $this->html_hold .= ''; } - $html_hold .= ''; + $this->html_hold .= ''; } } if ($html_table) { - $html_hold .= '
' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '
' . (($val) ? $val : ' ') . '' . (($val) ? $val : ' ') . '
'; + $this->html_hold .= ''; } } - $curtime = explode(' ', microtime()); - $curtime = $curtime[0] + $curtime[1]; + $this->curtime = explode(' ', microtime()); + $this->curtime = $this->curtime[0] + $this->curtime[1]; break; case 'fromcache': @@ -392,19 +392,19 @@ class dbal_mysql4 extends dbal $splittime = explode(' ', microtime()); $splittime = $splittime[0] + $splittime[1]; - $time_cache = $endtime - $curtime; + $time_cache = $endtime - $this->curtime; $time_db = $splittime - $endtime; $color = ($time_db > $time_cache) ? 'green' : 'red'; - $sql_report .= '

Query results obtained from the cache

'; + $this->sql_report .= '



Query results obtained from the cache

'; - $sql_report .= 'Before: ' . sprintf('%.5f', $curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: ' . sprintf('%.5f', ($time_cache)) . 's | Elapsed [db]: ' . sprintf('%.5f', $time_db) . 's

'; + $this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: ' . sprintf('%.5f', ($time_cache)) . 's | Elapsed [db]: ' . sprintf('%.5f', $time_db) . 's

'; // Pad the start time to not interfere with page timing $starttime += $time_db; mysql_free_result($result); - $cache_num_queries++; + $this->cache_num_queries++; break; } }