今天再切換第四個數據庫是怎么都切換不了,于是發現是DB.class.php 文件有個bug,現在已經修復
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]// +----------------------------------------------------------------------// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <liu21st@Gmail.com>// +----------------------------------------------------------------------/** * ThinkPHP 數據庫中間層實現類 * @category Think * @package Think * @subpackage Core * @author liu21st <liu21st@gmail.com> */class Db { // 數據庫類型 PRotected $dbType = null; // 是否自動釋放查詢結果 protected $autoFree = false; // 當前操作所屬的模型名 protected $model = '_think_'; // 是否使用永久連接 protected $pconnect = false; // 當前SQL指令 protected $queryStr = ''; protected $modelSql = array(); // 最后插入ID protected $lastInsID = null; // 返回或者影響記錄數 protected $numRows = 0; // 返回字段數 protected $numCols = 0; // 事務指令數 protected $transTimes = 0; // 錯誤信息 protected $error = ''; // 數據庫連接ID 支持多個連接 protected $linkID = array(); // 當前連接ID protected $_linkID = null; // 當前查詢ID protected $queryID = null; // 是否已經連接數據庫 protected $connected = false; // 數據庫連接參數配置 protected $config = ''; protected $multiConfig = []; // 數據庫表達式 protected $comparison = array('eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN'); // 查詢表達式 protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%COMMENT%'; // 參數綁定 protected $bind = array(); /** * 取得數據庫類實例 * @static * @access public * @return mixed 返回數據庫驅動類 */ public static function getInstance() { $args = func_get_args(); return get_instance_of(__CLASS__,'factory',$args); } /** * 加載數據庫 支持配置文件或者 DSN * @access public * @param mixed $db_config 數據庫配置信息 * @return string */ public function factory($db_config='') { // 讀取數據庫配置 $db_config = $this->parseConfig($db_config); if(empty($db_config['dbms'])) throw_exception(L('_NO_DB_CONFIG_')); // 數據庫類型 $this->dbType = ucWords(strtolower($db_config['dbms'])); $class = 'Db'. $this->dbType; // 檢查驅動類 if(class_exists($class)) { $db = new $class($db_config); // 獲取當前的數據庫類型 if( 'pdo' != strtolower($db_config['dbms']) ) $db->dbType = strtoupper($this->dbType); else $db->dbType = $this->_getDsnType($db_config['dsn']); }else { // 類沒有定義 throw_exception(L('_NO_DB_DRIVER_').': ' . $class); } return $db; } /** * 根據DSN獲取數據庫類型 返回大寫 * @access protected * @param string $dsn dsn字符串 * @return string */ protected function _getDsnType($dsn) { $match = explode(':',$dsn); $dbType = strtoupper(trim($match[0])); return $dbType; } /** * 分析數據庫配置信息,支持數組和DSN * @access private * @param mixed $db_config 數據庫配置信息 * @return string */ private function parseConfig($db_config='') { if ( !empty($db_config) && is_string($db_config)) { // 如果DSN字符串則進行解析 $db_config = $this->parseDSN($db_config); }elseif(is_array($db_config)) { // 數組配置 $db_config = array_change_key_case($db_config); $db_config = array( 'dbms' => $db_config['db_type'], 'username' => $db_config['db_user'], 'password' => $db_config['db_pwd'], 'hostname' => $db_config['db_host'], 'hostport' => $db_config['db_port'], 'database' => $db_config['db_name'], 'dsn' => $db_config['db_dsn'], 'params' => $db_config['db_params'], ); }elseif(empty($db_config)) { // 如果配置為空,讀取配置文件設置 if( C('DB_DSN') && 'pdo' != strtolower(C('DB_TYPE')) ) { // 如果設置了DB_DSN 則優先 $db_config = $this->parseDSN(C('DB_DSN')); }else{ $db_config = array ( 'dbms' => C('DB_TYPE'), 'username' => C('DB_USER'), 'password' => C('DB_PWD'), 'hostname' => C('DB_HOST'), 'hostport' => C('DB_PORT'), 'database' => C('DB_NAME'), 'dsn' => C('DB_DSN'), 'params' => C('DB_PARAMS'), ); } } return $db_config; } /** * 初始化數據庫連接 * @access protected * @param boolean $master 主服務器 * @return void */ protected function initConnect($master=true) { if(1 == C('DB_DEPLOY_TYPE')) // 采用分布式數據庫 $this->_linkID = $this->multiConnect($master); else // 默認單數據庫 if ( !$this->connected ) $this->_linkID = $this->connect(); } /** * 連接分布式服務器 * @access protected * @param boolean $master 主服務器 * @return void */ protected function multiConnect($master=false) { if(empty($this->multiConfig)) { // 緩存分布式數據庫配置解析 foreach ($this->config as $key=>$val){ $this->multiConfig[$key] = explode(',',$val); } } $_config = $this->multiConfig; // 數據庫讀寫是否分離 if(C('DB_RW_SEPARATE')){ // 主從式采用讀寫分離 if($master) // 主服務器寫入 $r = floor(mt_rand(0,C('DB_MASTER_NUM')-1)); else{ if(is_numeric(C('DB_SLAVE_NO'))) {// 指定服務器讀 $r = C('DB_SLAVE_NO'); }else{ // 讀操作連接從服務器 $r = floor(mt_rand(C('DB_MASTER_NUM'),count($_config['hostname'])-1)); // 每次隨機連接的數據庫 } } }else{ // 讀寫操作不區分服務器 $r = floor(mt_rand(0,count($_config['hostname'])-1)); // 每次隨機連接的數據庫 } $db_config = array( 'username' => isset($_config['username'][$r])?$_config['username'][$r]:$_config['username'][0], 'password' => isset($_config['password'][$r])?$_config['password'][$r]:$_config['password'][0], 'hostname' => isset($_config['hostname'][$r])?$_config['hostname'][$r]:$_config['hostname'][0], 'hostport' => isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0], 'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0], 'dsn' => isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0], 'params' => isset($_config['params'][$r])?$_config['params'][$r]:$_config['params'][0], ); return $this->connect($db_config,$r); } /** * DSN解析 * 格式: MySQL://username:passwd@localhost:3306/DbName * @static * @access public * @param string $dsnStr * @return array */ public function parseDSN($dsnStr) { if( empty($dsnStr) ){return false;} $info = parse_url($dsnStr); if($info['scheme']){ $dsn = array( 'dbms' => $info['scheme'], 'username' => isset($info['user']) ? $info['user'] : '', 'password' => isset($info['pass']) ? $info['pass'] : '', 'hostname' => isset($info['host']) ? $info['host'] : '', 'hostport' => isset($info['port']) ? $info['port'] : '', 'database' => isset($info['path']) ? substr($info['path'],1) : '' ); }else { preg_match('/^(.*?)/:////(.*?)/:(.*?)/@(.*?)/:([0-9]{1, 6})//(.*?)$/',trim($dsnStr),$matches); $dsn = array ( 'dbms' => $matches[1], 'username' => $matches[2], 'password' => $matches[3], 'hostname' => $matches[4], 'hostport' => $matches[5], 'database' => $matches[6] ); } $dsn['dsn'] = ''; // 兼容配置信息數組 return $dsn; } /** * 數據庫調試 記錄當前SQL * @access protected */ protected function debug() { $this->modelSql[$this->model] = $this->queryStr; $this->model = '_think_'; // 記錄操作結束時間 if (C('DB_SQL_LOG')) { G('queryEndTime'); trace($this->queryStr.' [ RunTime:'.G('queryStartTime','queryEndTime',6).'s ]','','SQL'); } } /** * 設置鎖機制 * @access protected * @return string */ protected function parseLock($lock=false) { if(!$lock) return ''; if('Oracle' == $this->dbType) { return ' FOR UPDATE NOWAIT '; } return ' FOR UPDATE '; } /** * set分析 * @access protected * @param array $data * @return string */ protected function parseSet($data) { foreach ($data as $key=>$val){ if(is_array($val) && 'exp' == $val[0]){ $set[] = $this->parseKey($key).'='.$val[1]; }elseif(is_scalar($val) || is_null(($val))) { // 過濾非標量數據 if(C('DB_BIND_PARAM') && 0 !== strpos($val,':')){ $name = md5($key); $set[] = $this->parseKey($key).'=:'.$name; $this->bindParam($name,$val); }else{ $set[] = $this->parseKey($key).'='.$this->parseValue($val); } } } return ' SET '.implode(',',$set); } /** * 參數綁定 * @access protected * @param string $name 綁定參數名 * @param mixed $value 綁定值 * @return void */ protected function bindParam($name,$value){ $this->bind[':'.$name] = $value; } /** * 參數綁定分析 * @access protected * @param array $bind * @return array */ protected function parseBind($bind){ $bind = array_merge($this->bind,$bind); $this->bind = array(); return $bind; } /** * 字段名分析 * @access protected * @param string $key * @return string */ protected function parseKey(&$key) { return $key; } /** * value分析 * @access protected * @param mixed $value * @return string */ protected function parseValue($value) { if(is_string($value)) { $value = '/''.$this->escapeString($value).'/''; }elseif(isset($value[0]) && is_string($value[0]) && strtolower($value[0]) == 'exp'){ $value = $this->escapeString($value[1]); }elseif(is_array($value)) { $value = array_map(array($this, 'parseValue'),$value); }elseif(is_bool($value)){ $value = $value ? '1' : '0'; }elseif(is_null($value)){ $value = 'null'; } return $value; } /** * field分析 * @access protected * @param mixed $fields * @return string */ protected function parseField($fields) { if(is_string($fields) && strpos($fields,',')) { $fields = explode(',',$fields); } if(is_array($fields)) { // 完善數組方式傳字段名的支持 // 支持 'field1'=>'field2' 這樣的字段別名定義 $array = array(); foreach ($fields as $key=>$field){ if(!is_numeric($key)) $array[] = $this->parseKey($key).' AS '.$this->parseKey($field); else $array[] = $this->parseKey($field); } $fieldsStr = implode(',', $array); }elseif(is_string($fields) && !empty($fields)) { $fieldsStr = $this->parseKey($fields); }else{ $fieldsStr = '*'; } //TODO 如果是查詢全部字段,并且是join的方式,那么就把要查的表加個別名,以免字段被覆蓋 return $fieldsStr; } /** * table分析 * @access protected * @param mixed $table * @return string */ protected function parseTable($tables) { if(is_array($tables)) {// 支持別名定義 $array = array(); foreach ($tables as $table=>$alias){ if(!is_numeric($table)) $array[] = $this->parseKey($table).' '.$this->parseKey($alias); else $array[] = $this->parseKey($table); } $tables = $array; }elseif(is_string($tables)){ $tables = explode(',',$tables); array_walk($tables, array(&$this, 'parseKey')); } return implode(',',$tables); } /** * where分析 * @access protected * @param mixed $where * @return string */ protected function parseWhere($where) { $whereStr = ''; if(is_string($where)) { // 直接使用字符串條件 $whereStr = $where; }else{ // 使用數組表達式 $Operate = isset($where['_logic'])?strtoupper($where['_logic']):''; if(in_array($operate,array('AND','OR','XOR'))){ // 定義邏輯運算規則 例如 OR XOR AND NOT $operate = ' '.$operate.' '; unset($where['_logic']); }else{ // 默認進行 AND 運算 $operate = ' AND '; } foreach ($where as $key=>$val){ $whereStr .= '( '; if(is_numeric($key)){ $key = '_complex'; } if(0===strpos($key,'_')) { // 解析特殊條件表達式 $whereStr .= $this->parseThinkWhere($key,$val); }else{ // 查詢字段的安全過濾 if(!preg_match('/^[A-Z_/|/&/-.a-z0-9/(/)/,]+$/',trim($key))){ throw_exception(L('_EXPRESS_ERROR_').':'.$key); } // 多條件支持 $multi = is_array($val) && isset($val['_multi']); $key = trim($key); if(strpos($key,'|')) { // 支持 name|title|nickname 方式定義查詢字段 $array = explode('|',$key); $str = array(); foreach ($array as $m=>$k){ $v = $multi?$val[$m]:$val; $str[] = '('.$this->parseWhereItem($this->parseKey($k),$v).')'; } $whereStr .= implode(' OR ',$str); }elseif(strpos($key,'&')){ $array = explode('&',$key); $str = array(); foreach ($array as $m=>$k){ $v = $multi?$val[$m]:$val; $str[] = '('.$this->parseWhereItem($this->parseKey($k),$v).')'; } $whereStr .= implode(' AND ',$str); }else{ $whereStr .= $this->parseWhereItem($this->parseKey($key),$val); } } $whereStr .= ' )'.$operate; } $whereStr = substr($whereStr,0,-strlen($operate)); } return empty($whereStr)?'':' WHERE '.$whereStr; } // where子單元分析 protected function parseWhereItem($key,$val) { $whereStr = ''; if(is_array($val)) { if(is_string($val[0])) { if(preg_match('/^(EQ|NEQ|GT|EGT|LT|ELT)$/i',$val[0])) { // 比較運算 $whereStr .= $key.' '.$this->comparison[strtolower($val[0])].' '.$this->parseValue($val[1]); }elseif(preg_match('/^(NOTLIKE|LIKE)$/i',$val[0])){// 模糊查找 if(is_array($val[1])) { $likeLogic = isset($val[2])?strtoupper($val[2]):'OR'; if(in_array($likeLogic,array('AND','OR','XOR'))){ $likeStr = $this->comparison[strtolower($val[0])]; $like = array(); foreach ($val[1] as $item){ $like[] = $key.' '.$likeStr.' '.$this->parseValue($item); } $whereStr .= '('.implode(' '.$likeLogic.' ',$like).')'; } }else{ $whereStr .= $key.' '.$this->comparison[strtolower($val[0])].' '.$this->parseValue($val[1]); } }elseif('exp'==strtolower($val[0])){ // 使用表達式 $whereStr .= ' ('.$key.' '.$val[1].') '; }elseif(preg_match('/IN/i',$val[0])){ // IN 運算 if(isset($val[2]) && 'exp'==$val[2]) { $whereStr .= $key.' '.strtoupper($val[0]).' '.$val[1]; }else{ if(is_string($val[1])) { $val[1] = explode(',',$val[1]); } $zone = implode(',',$this->parseValue($val[1])); $whereStr .= $key.' '.strtoupper($val[0]).' ('.$zone.')'; } }elseif(preg_match('/BETWEEN/i',$val[0])){ // BETWEEN運算 $data = is_string($val[1])? explode(',',$val[1]):$val[1]; $whereStr .= ' ('.$key.' '.strtoupper($val[0]).' '.$this->parseValue($data[0]).' AND '.$this->parseValue($data[1]).' )'; }else{ throw_exception(L('_EXPRESS_ERROR_').':'.$val[0]); } }else { $count = count($val); $rule = isset($val[$count-1])?strtoupper($val[$count-1]):''; if(in_array($rule,array('AND','OR','XOR'))) { $count = $count -1; }else{ $rule = 'AND'; } for($i=0;$i<$count;$i++) { $data = is_array($val[$i])?$val[$i][1]:$val[$i]; if('exp'==strtolower($val[$i][0])) { $whereStr .= '('.$key.' '.$data.') '.$rule.' '; }else{ $op = is_array($val[$i])?$this->comparison[strtolower($val[$i][0])]:'='; $whereStr .= '('.$key.' '.$op.' '.$this->parseValue($data).') '.$rule.' '; } } $whereStr = substr($whereStr,0,-4); } }else { //對字符串類型字段采用模糊匹配 if(C('DB_LIKE_FIELDS') && preg_match('/('.C('DB_LIKE_FIELDS').')/i',$key)) { $val = '%'.$val.'%'; $whereStr .= $key.' LIKE '.$this->parseValue($val); }else { $whereStr .= $key.' = '.$this->parseValue($val); } } return $whereStr; } /** * 特殊條件分析 * @access protected * @param string $key * @param mixed $val * @return string */ protected function parseThinkWhere($key,$val) { $whereStr = ''; switch($key) { case '_string': // 字符串模式查詢條件 $whereStr = $val; break; case '_complex': // 復合查詢條件 $whereStr = is_string($val)? $val : substr($this->parseWhere($val),6); break; case '_query': // 字符串模式查詢條件 parse_str($val,$where); if(isset($where['_logic'])) { $op = ' '.strtoupper($where['_logic']).' '; unset($where['_logic']); }else{ $op = ' AND '; } $array = array(); foreach ($where as $field=>$data) $array[] = $this->parseKey($field).' = '.$this->parseValue($data); $whereStr = implode($op,$array); break; } return $whereStr; } /** * limit分析 * @access protected * @param mixed $lmit * @return string */ protected function parseLimit($limit) { return !empty($limit)? ' LIMIT '.$limit.' ':''; } /** * join分析 * @access protected * @param mixed $join * @return string */ protected function parseJoin($join) { $joinStr = ''; if(!empty($join)) { if(is_array($join)) { foreach ($join as $key=>$_join){ if(false !== stripos($_join,'JOIN')) $joinStr .= ' '.$_join; else $joinStr .= ' LEFT JOIN ' .$_join; } }else{ $joinStr .= ' LEFT JOIN ' .$join; } } //將__TABLE_NAME__這樣的字符串替換成正規的表名,并且帶上前綴和后綴 $joinStr = preg_replace_callback("/__([A-Z_-]+)__/sU",function($matches){ return C("DB_PREFIX").".".strtolower($matches[1]); },$joinStr); return $joinStr; } /** * order分析 * @access protected * @param mixed $order * @return string */ protected function parSEOrder($order) { if(is_array($order)) { $array = array(); foreach ($order as $key=>$val){ if(is_numeric($key)) { $array[] = $this->parseKey($val); }else{ $array[] = $this->parseKey($key).' '.$val; } } $order = implode(',',$array); } return !empty($order)? ' ORDER BY '.$order:''; } /** * group分析 * @access protected * @param mixed $group * @return string */ protected function parseGroup($group) { return !empty($group)? ' GROUP BY '.$group:''; } /** * having分析 * @access protected * @param string $having * @return string */ protected function parseHaving($having) { return !empty($having)? ' HAVING '.$having:''; } /** * comment分析 * @access protected * @param string $comment * @return string */ protected function parseComment($comment) { return !empty($comment)? ' /* '.$comment.' */':''; } /** * distinct分析 * @access protected * @param mixed $distinct * @return string */ protected function parseDistinct($distinct) { return !empty($distinct)? ' DISTINCT ' :''; } /** * union分析 * @access protected * @param mixed $union * @return string */ protected function parseUnion($union) { if(empty($union)) return ''; if(isset($union['_all'])) { $str = 'UNION ALL '; unset($union['_all']); }else{ $str = 'UNION '; } foreach ($union as $u){ $sql[] = $str.(is_array($u)?$this->buildSelectSql($u):$u); } return implode(' ',$sql); } /** * 插入記錄 * @access public * @param mixed $data 數據 * @param array $options 參數表達式 * @param boolean $replace 是否replace * @return false | integer */ public function insert($data,$options=array(),$replace=false) { $values = $fields = array(); $this->model = $options['model']; foreach ($data as $key=>$val){ if(is_array($val) && 'exp' == $val[0]){ $fields[] = $this->parseKey($key); $values[] = $val[1]; }elseif(is_scalar($val) || is_null(($val))) { // 過濾非標量數據 $fields[] = $this->parseKey($key); if(C('DB_BIND_PARAM') && 0 !== strpos($val,':')){ $name = md5($key); $values[] = ':'.$name; $this->bindParam($name,$val); }else{ $values[] = $this->parseValue($val); } } } $sql = ($replace?'REPLACE':'INSERT').' INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES ('.implode(',', $values).')'; $sql .= $this->parseLock(isset($options['lock'])?$options['lock']:false); $sql .= $this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,$this->parseBind(!empty($options['bind'])?$options['bind']:array())); } /** * 通過Select方式插入記錄 * @access public * @param string $fields 要插入的數據表字段名 * @param string $table 要插入的數據表名 * @param array $option 查詢數據參數 * @return false | integer */ public function selectInsert($fields,$table,$options=array()) { $this->model = $options['model']; if(is_string($fields)) $fields = explode(',',$fields); array_walk($fields, array($this, 'parseKey')); $sql = 'INSERT INTO '.$this->parseTable($table).' ('.implode(',', $fields).') '; $sql .= $this->buildSelectSql($options); return $this->execute($sql,$this->parseBind(!empty($options['bind'])?$options['bind']:array())); } /** * 更新記錄 * @access public * @param mixed $data 數據 * @param array $options 表達式 * @return false | integer */ public function update($data,$options) { $this->model = $options['model']; $sql = 'UPDATE ' .$this->parseTable($options['table']) .$this->parseSet($data) .$this->parseWhere(!empty($options['where'])?$options['where']:'') .$this->parseOrder(!empty($options['order'])?$options['order']:'') .$this->parseLimit(!empty($options['limit'])?$options['limit']:'') .$this->parseLock(isset($options['lock'])?$options['lock']:false) .$this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,$this->parseBind(!empty($options['bind'])?$options['bind']:array())); } /** * 刪除記錄 * @access public * @param array $options 表達式 * @return false | integer */ public function delete($options=array()) { $this->model = $options['model']; $sql = 'DELETE FROM ' .$this->parseTable($options['table']) .$this->parseWhere(!empty($options['where'])?$options['where']:'') .$this->parseOrder(!empty($options['order'])?$options['order']:'') .$this->parseLimit(!empty($options['limit'])?$options['limit']:'') .$this->parseLock(isset($options['lock'])?$options['lock']:false) .$this->parseComment(!empty($options['comment'])?$options['comment']:''); return $this->execute($sql,$this->parseBind(!empty($options['bind'])?$options['bind']:array())); } /** * 查找記錄 * @access public * @param array $options 表達式 * @return mixed */ public function select($options=array()) { $this->model = $options['model']; $sql = $this->buildSelectSql($options); $cache = isset($options['cache'])?$options['cache']:false; if($cache) { // 查詢緩存檢測 $key = is_string($cache['key'])?$cache['key']:md5($sql); $value = S($key,'',$cache); if(false !== $value) { return $value; } } $result = $this->query($sql,$this->parseBind(!empty($options['bind'])?$options['bind']:array())); if($cache && false !== $result ) { // 查詢緩存寫入 S($key,$result,$cache); } return $result; } /** * 生成查詢SQL * @access public * @param array $options 表達式 * @return string */ public function buildSelectSql($options=array()) { if(isset($options['page'])) { // 根據頁數計算limit if(strpos($options['page'],',')) { list($page,$listRows) = explode(',',$options['page']); }else{ $page = $options['page']; } $page = $page?$page:1; $listRows= isset($listRows)?$listRows:(is_numeric($options['limit'])?$options['limit']:20); $offset = $listRows*((int)$page-1); $options['limit'] = $offset.','.$listRows; } if(C('DB_SQL_BUILD_CACHE')) { // SQL創建緩存 $key = md5(serialize($options)); $value = S($key); if(false !== $value) { return $value; } } $sql = $this->parseSql($this->selectSql,$options); $sql .= $this->parseLock(isset($options['lock'])?$options['lock']:false); if(isset($key)) { // 寫入SQL創建緩存 S($key,$sql,array('expire'=>0,'length'=>C('DB_SQL_BUILD_LENGTH'),'queue'=>C('DB_SQL_BUILD_QUEUE'))); } return $sql; } /** * 替換SQL語句中表達式 * @access public * @param array $options 表達式 * @return string */ public function parseSql($sql,$options=array()){ $sql = str_replace( array('%TABLE%','%DISTINCT%','%FIELD%','%JOIN%','%WHERE%','%GROUP%','%HAVING%','%ORDER%','%LIMIT%','%UNION%','%COMMENT%'), array( $this->parseTable($options['table']), $this->parseDistinct(isset($options['distinct'])?$options['distinct']:false), $this->parseField(!empty($options['field'])?$options['field']:'*'), $this->parseJoin(!empty($options['join'])?$options['join']:''), $this->parseWhere(!empty($options['where'])?$options['where']:''), $this->parseGroup(!empty($options['group'])?$options['group']:''), $this->parseHaving(!empty($options['having'])?$options['having']:''), $this->parseOrder(!empty($options['order'])?$options['order']:''), $this->parseLimit(!empty($options['limit'])?$options['limit']:''), $this->parseUnion(!empty($options['union'])?$options['union']:''), $this->parseComment(!empty($options['comment'])?$options['comment']:'') ),$sql); return $sql; } /** * 獲取最近一次查詢的sql語句 * @param string $model 模型名 * @access public * @return string */ public function getLastSql($model='') { return $model?$this->modelSql[$model]:$this->queryStr; } /** * 獲取最近插入的ID * @access public * @return string */ public function getLastInsID() { return $this->lastInsID; } /** * 獲取最近的錯誤信息 * @access public * @return string */ public function getError() { return $this->error; } /** * SQL指令安全過濾 * @access public * @param string $str SQL字符串 * @return string */ public function escapeString($str) { return addslashes($str); } /** * 設置當前操作模型 * @access public * @param string $model 模型名 * @return void */ public function setModel($model){ $this->model = $model; } /** * 析構方法 * @access public */ public function __destruct() { // 釋放查詢 if ($this->queryID){ $this->free(); } // 關閉連接 $this->close(); } // 關閉數據庫 由驅動類定義 public function close(){}}新聞熱點
疑難解答