【php开源代码栏目提醒】:网学会员,鉴于大家对php开源代码十分关注,论文会员在此为大家搜集整理了“DbPdo.class.php”一文,供大家参考学习!
<?php
// +----------------------------------------------------------------------
// | Think
PHP // +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* PDO鏁版嵁搴撻┍鍔ㄧ被
+------------------------------------------------------------------------------
* @category Think
* @package Think
* @subpackage Db
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
Class DbPdo extends Db{
protected $PDOStatement = null;
/**
+----------------------------------------------------------
* 鏋舵瀯鍑芥暟 璇诲彇鏁版嵁搴撻厤缃俊鎭?
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param array $config 鏁版嵁搴撻厤缃暟缁?
+----------------------------------------------------------
*/
public function __construct($config=''){
if ( !class_exists('PDO') ) {
throw_exception(L('_NOT_SUPPERT_').':PDO');
}
if(!empty($config)) {
$this->config = $config;
if(empty($this->config['params'])) {
$this->config['params'] = array();
}
}
}
/**
+----------------------------------------------------------
* 杩炴帴鏁版嵁搴撴柟娉?
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function connect($config='',$linkNum=0) {
if ( !isset($this->linkID[$linkNum]) ) {
if(empty($config)) $config = $this->config;
if($this->pconnect) {
$config['params'][constant('PDO::ATTR_PERSISTENT')] = true;
}
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['pass
word'],$config['params']);
if ( !$this->linkID[$linkNum]) {
throw_exception('PDO CONNECT ERROR');
return false;
}
$this->linkID[$linkNum]->exec('SET NAMES '.C('DB_CHARSET'));
// 鍥犱釜鍒┍鍔ㄤ笉鏀寔getAttribute鏂规硶 鏆傛椂娉ㄩ噴
//$this->dbVersion = $this->linkID[$linkNum]->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
// 鏍囪杩炴帴鎴愬姛
$this->connected = true;
// 娉ㄩ攢鏁版嵁搴撹繛鎺ラ厤缃俊鎭?
if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
}
return $this->linkID[$linkNum];
}
/**
+----------------------------------------------------------
* 閲婃斁鏌ヨ缁撴灉
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function free() {
$this->PDOStatement = null;
}
/**
+----------------------------------------------------------
* 鎵ц鏌ヨ 涓昏閽堝 SELECT, SHOW 绛夋寚浠?
* 杩斿洖鏁版嵁闆?
+----------------------------------------------------------
* @access protected
+----------------------------------------------------------
* @param string $str sql鎸囦护
+----------------------------------------------------------
* @return ArrayObject
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
protected function _query($str='') {
$this->initConnect(false);
if ( !$this->_linkID ) return false;
if ( $str != '' ) $this->queryStr = $str;
if (!$this->autoCommit && $this->isMainIps($this->queryStr)) {
//$this->startTrans();
}else {
//閲婃斁鍓嶆鐨勬煡璇㈢粨鏋?
if ( !empty($this->PDOStatement) ) { $this->free(); }
}
$this->queryTimes++;
$this->Q(1);
$this->PDOStatement = $this->_linkID->prepare($this->queryStr);
$result = $this->PDOStatement->execute();
$this->debug();
if ( !$result ) {
//if ( $this->de