diff options
author | Meik Sievertsen <acydburn@phpbb.com> | 2005-10-07 23:00:41 +0000 |
---|---|---|
committer | Meik Sievertsen <acydburn@phpbb.com> | 2005-10-07 23:00:41 +0000 |
commit | 913c38768a21901187d2ded9427b3e2e533206f4 (patch) | |
tree | 7480cbd098b54782134f2f166573f56a585b8074 /phpBB/includes/db/mysqli.php | |
parent | 8d72671cc1c983dcafac6948f07ac3cf7447c93c (diff) | |
download | forums-913c38768a21901187d2ded9427b3e2e533206f4.tar forums-913c38768a21901187d2ded9427b3e2e533206f4.tar.gz forums-913c38768a21901187d2ded9427b3e2e533206f4.tar.bz2 forums-913c38768a21901187d2ded9427b3e2e533206f4.tar.xz forums-913c38768a21901187d2ded9427b3e2e533206f4.zip |
- another try to break things. :)
- adjusted the dbal a little bit
- removed db2 support
git-svn-id: file:///svn/phpbb/trunk@5263 89ea8834-ac86-4346-8a33-228a782c2dd0
Diffstat (limited to 'phpBB/includes/db/mysqli.php')
-rw-r--r-- | phpBB/includes/db/mysqli.php | 221 |
1 files changed, 82 insertions, 139 deletions
diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php index 9dcb5ec7c0..0b10d01679 100644 --- a/phpBB/includes/db/mysqli.php +++ b/phpBB/includes/db/mysqli.php @@ -26,6 +26,9 @@ class dbal_mysqli extends dbal { var $indexed = 0; + /** + * Connect to server + */ function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false) { $this->persistency = $persistency; @@ -46,24 +49,9 @@ class dbal_mysqli extends dbal 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); - } - + /** + * sql transaction + */ function sql_transaction($status = 'begin') { switch ($status) @@ -98,7 +86,9 @@ class dbal_mysqli extends dbal return $result; } - // Base query method + /** + * Base query method + */ function sql_query($query = '', $cache_ttl = 0) { if ($query != '') @@ -122,11 +112,6 @@ class dbal_mysqli extends dbal $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); @@ -134,8 +119,13 @@ class dbal_mysqli extends dbal 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); } + else if (strpos($query, 'SELECT') !== false && $this->query_result) + { + $this->open_queries[(int) $this->query_result] = $this->query_result; + } } else if (defined('DEBUG_EXTRA')) { @@ -150,6 +140,9 @@ class dbal_mysqli extends dbal return ($this->query_result) ? $this->query_result : false; } + /** + * Build LIMIT query + */ function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) { if ($query != '') @@ -172,10 +165,10 @@ class dbal_mysqli extends dbal } } - // Other query methods - // - // NOTE :: Want to remove _ALL_ reliance on sql_numrows from core code ... - // don't want this here by a middle Milestone + /** + * Return number of rows + * Not used within core code + */ function sql_numrows($query_id = false) { if (!$query_id) @@ -186,11 +179,17 @@ class dbal_mysqli extends dbal return ($query_id) ? @mysqli_num_rows($query_id) : false; } + /** + * Return number of affected rows + */ function sql_affectedrows() { return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false; } + /** + * Fetch current row + */ function sql_fetchrow($query_id = false) { global $cache; @@ -208,7 +207,11 @@ class dbal_mysqli extends dbal return ($query_id) ? @mysqli_fetch_assoc($query_id) : false; } - function sql_fetchrowset($query_id = false) + /** + * Fetch field + * if rownum is false, the current row is used, else it is pointing to the row (zero-based) + */ + function sql_fetchfield($field, $rownum = false, $query_id = false) { if (!$query_id) { @@ -217,65 +220,22 @@ class dbal_mysqli extends dbal 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)) + if ($rownum !== false) { - $result[] = $this->rowset[$cur_index]; + $this->sql_rowseek($rownum, $query_id); } - return $result; + + $row = $this->sql_fetchrow($query_id); + return isset($row[$field]) ? $row[$field] : false; } 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; - } - + /** + * Seek to given row number + * rownum is zero-based + */ function sql_rowseek($rownum, $query_id = false) { if (!$query_id) @@ -286,11 +246,17 @@ class dbal_mysqli extends dbal return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false; } + /** + * Get last inserted id after insert statement + */ function sql_nextid() { return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false; } + /** + * Free sql result + */ function sql_freeresult($query_id = false) { if (!$query_id) @@ -298,28 +264,28 @@ class dbal_mysqli extends dbal $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)) + if (isset($this->open_queries[(int) $query_id])) { - $this->indexed--; + unset($this->open_queries[(int) $query_id]); return @mysqli_free_result($query_id); } - else - { - return false; - } + + return false; } + /** + * Escape string used in sql query + */ function sql_escape($msg) { return @mysqli_real_escape_string($this->db_connect_id, $msg); } - function db_sql_error() + /** + * return sql error array + * @private + */ + function _sql_error() { return array( 'message' => @mysqli_error($this->db_connect_id), @@ -327,69 +293,55 @@ class dbal_mysqli extends dbal ); } - function _sql_report($mode, $query = '') + /** + * Close sql connection + * @private + */ + function _sql_close() { - global $cache, $starttime, $phpbb_root_path; - static $curtime, $query_hold, $html_hold; - static $sql_report = ''; - static $cache_num_queries = 0; + return @mysqli_close($this->db_connect_id); + } + /** + * Build db-specific report + * @private + */ + function _sql_report($mode, $query = '') + { switch ($mode) { 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)) + else if (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; + $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>'; + $html_table = $this->sql_report('add_select_row', $query, $html_table, $row); } } + @mysqli_free_result($result); if ($html_table) { - $html_hold .= '</table>'; + $this->html_hold .= '</table>'; } } - $curtime = explode(' ', microtime()); - $curtime = $curtime[0] + $curtime[1]; - break; + break; case 'fromcache': $endtime = explode(' ', microtime()); @@ -400,23 +352,14 @@ class dbal_mysqli extends dbal { // Take the time spent on parsing rows into account } + @mysqli_free_result($result); + $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">'; + $this->sql_report('record_fromcache', $query, $endtime, $splittime); - $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; + break; } } } |