📄 dbal.php
字号:
<?php/** ** @package dbal* @version $Id: dbal.php,v 1.48 2006/11/25 20:00:56 naderman Exp $* @copyright (c) 2005 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License **//*** Database Abstraction Layer* @package dbal*/class dbal{ var $db_connect_id; var $query_result; var $return_on_error = false; var $transaction = false; var $sql_time = 0; var $num_queries = array(); var $open_queries = array(); var $curtime = 0; var $query_hold = ''; var $html_hold = ''; var $sql_report = ''; var $persistency = false; var $user = ''; var $server = ''; var $dbname = ''; // Set to true if error triggered var $sql_error_triggered = false; // Holding the last sql query on sql error var $sql_error_sql = ''; /** * Current sql layer */ var $sql_layer = ''; /** * Constructor */ function dbal() { $this->num_queries = array( 'cached' => 0, 'normal' => 0, 'total' => 0, ); // Fill default sql layer based on the class being called. // This can be changed by the specified layer itself later if needed. $this->sql_layer = substr(get_class($this), 5); } /** * return on error or display error message */ function sql_return_on_error($fail = false) { $this->sql_error_triggered = false; $this->sql_error_sql = ''; $this->return_on_error = $fail; } /** * Return number of sql queries and cached sql queries used */ function sql_num_queries($cached = false) { return ($cached) ? $this->num_queries['cached'] : $this->num_queries['normal']; } /** * Add to query count */ function sql_add_num_queries($cached = false) { $this->num_queries['cached'] += ($cached) ? 1 : 0; $this->num_queries['normal'] += ($cached) ? 0 : 1; $this->num_queries['total'] += 1; } /** * DBAL garbage collection, close sql connection */ function sql_close() { if (!$this->db_connect_id) { return false; } if ($this->transaction) { $this->sql_transaction('commit'); } if (sizeof($this->open_queries)) { foreach ($this->open_queries as $i_query_id => $query_id) { $this->sql_freeresult($query_id); } } return $this->_sql_close(); } /** * Fetch all rows */ function sql_fetchrowset($query_id = false) { if ($query_id === false) { $query_id = $this->query_result; } if ($query_id !== false) { $result = array(); while ($row = $this->sql_fetchrow($query_id)) { $result[] = $row; } return $result; } return 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) { global $cache; if ($query_id === false) { $query_id = $this->query_result; } if ($query_id !== false) { if ($rownum !== false) { $this->sql_rowseek($rownum, $query_id); } if (!is_object($query_id) && isset($cache->sql_rowset[$query_id])) { return $cache->sql_fetchfield($query_id, $field); } $row = $this->sql_fetchrow($query_id); return (isset($row[$field])) ? $row[$field] : false; } return false; } /** * SQL Transaction * @access private */ function sql_transaction($status = 'begin') { switch ($status) { case 'begin': // Commit previously opened transaction before opening another transaction if ($this->transaction) { $this->_sql_transaction('commit'); } $result = $this->_sql_transaction('begin'); $this->transaction = true; break; case 'commit': $result = $this->_sql_transaction('commit'); $this->transaction = false; if (!$result) { $this->_sql_transaction('rollback'); } break; case 'rollback': $result = $this->_sql_transaction('rollback'); $this->transaction = false; break; default: $result = $this->_sql_transaction($status); break; } return $result; } /** * Build sql statement from array for insert/update/select statements * * Idea for this from Ikonboard * Possible query values: INSERT, INSERT_SELECT, MULTI_INSERT, UPDATE, SELECT * */ function sql_build_array($query, $assoc_ary = false) { if (!is_array($assoc_ary)) { return false; } $fields = $values = array(); if ($query == 'INSERT' || $query == 'INSERT_SELECT') { foreach ($assoc_ary as $key => $var) { $fields[] = $key; if (is_array($var) && is_string($var[0])) { // This is used for INSERT_SELECT(s) $values[] = $var[0]; } else { $values[] = $this->_sql_validate_value($var); } } $query = ($query == 'INSERT') ? ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')' : ' (' . implode(', ', $fields) . ') SELECT ' . implode(', ', $values) . ' '; } else if ($query == 'MULTI_INSERT') { $ary = array(); foreach ($assoc_ary as $id => $sql_ary) { // If by accident the sql array is only one-dimensional we build a normal insert statement if (!is_array($sql_ary)) { return $this->sql_build_array('INSERT', $assoc_ary); } $values = array(); foreach ($sql_ary as $key => $var) { $values[] = $this->_sql_validate_value($var); } $ary[] = '(' . implode(', ', $values) . ')'; } $query = ' (' . implode(', ', array_keys($assoc_ary[0])) . ') VALUES ' . implode(', ', $ary); } else if ($query == 'UPDATE' || $query == 'SELECT') { $values = array(); foreach ($assoc_ary as $key => $var) { $values[] = "$key = " . $this->_sql_validate_value($var); } $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values); } return $query; } /** * Build IN, NOT IN, = and <> sql comparison string. * @access public */ function sql_in_set($field, $array, $negate = false) { if (!sizeof($array)) { // Not optimal, but at least the backtrace should help in identifying where the problem lies. $this->sql_error('No values specified for SQL IN comparison'); } if (!is_array($array)) { $array = array($array); } if (sizeof($array) == 1) { @reset($array); $var = current($array); return $field . ($negate ? ' <> ' : ' = ') . $this->_sql_validate_value($var); } else { return $field . ($negate ? ' NOT IN ' : ' IN ') . '(' . implode(', ', array_map(array($this, '_sql_validate_value'), $array)) . ')'; } } /** * Run more than one insert statement. * * @param string $table table name to run the statements on * @param array &$sql_ary multi-dimensional array holding the statement data. * * @return bool false if no statements were executed. * @access public */ function sql_multi_insert($table, &$sql_ary) { if (!sizeof($sql_ary)) { return false; } switch ($this->sql_layer) { case 'mysql': case 'mysql4': case 'mysqli': $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('MULTI_INSERT', $sql_ary)); break; default: foreach ($sql_ary as $ary) { if (!is_array($ary)) { return false; } $this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $ary)); } break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -