数据库中间层改为PDO

增加Think\Db\Lite 用于操作原生SQL需求
This commit is contained in:
thinkphp
2013-03-17 18:56:15 +08:00
parent dec45af1aa
commit 647d47ccb4
13 changed files with 1440 additions and 4167 deletions

View File

@@ -9,7 +9,7 @@
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think\Db\Driver;
use Think\Db;
use Think\Db\Driver;
/**
* Oracle数据库驱动
* @category Extend
@@ -17,193 +17,49 @@ use Think\Db;
* @subpackage Driver.Db
* @author ZhangXuehun <zhangxuehun@sohu.com>
*/
class Oracle extends Db{
class Oracle extends Driver{
private $mode = OCI_COMMIT_ON_SUCCESS;
private $table = '';
protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';
/**
* 架构函数 读取数据库配置信息
* @access public
* @param array $config 数据库配置数组
*/
public function __construct($config=''){
putenv("NLS_LANG=AMERICAN_AMERICA.UTF8");
if ( !extension_loaded('oci8') ) {
throw_exception(L('_NOT_SUPPERT_').'oracle');
}
if(!empty($config)) {
$this->config = $config;
if(empty($this->config['params'])) {
$this->config['params'] = array();
}
}
}
/**
* 连接数据库方法
* @access public
*/
public function connect($config='',$linkNum=0) {
if ( !isset($this->linkID[$linkNum]) ) {
if(empty($config)) $config = $this->config;
$pconnect = !empty($config['params']['persist'])? $config['params']['persist']:$this->pconnect;
$conn = $pconnect ? 'oci_pconnect':'oci_new_connect';
$this->linkID[$linkNum] = $conn($config['username'], $config['password'],$config['database']);//modify by wyfeng at 2008.12.19
if (!$this->linkID[$linkNum]){
$this->error(false);
}
// 标记连接成功
$this->connected = true;
//注销数据库安全信息
if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
}
return $this->linkID[$linkNum];
}
/**
* 释放查询结果
* @access public
*/
public function free() {
oci_free_statement($this->queryID);
$this->queryID = null;
}
/**
* 执行查询 返回数据集
* @access public
* @param string $str sql指令
* @return mixed
*/
public function query($str) {
$this->initConnect(false);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
//更改事务模式
$this->mode = OCI_COMMIT_ON_SUCCESS;
//释放前次的查询结果
if ( $this->queryID ) $this->free();
N('db_query',1);
// 记录开始执行时间
G('queryStartTime');
$this->queryID = oci_parse($this->_linkID,$str);
$this->debug();
if (false === oci_execute($this->queryID, $this->mode)) {
$this->error();
return false;
} else {
return $this->getAll();
}
}
/**
* 执行语句
* @access public
* @param string $str sql指令
* @return integer
*/
public function execute($str) {
public function execute($str,$bind=[]) {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
// 判断新增操作
$flag = false;
if(preg_match("/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $this->queryStr, $match)) {
$this->table = C("DB_SEQUENCE_PREFIX") .str_ireplace(C("DB_PREFIX"), "", $match[2]);
$this->table = C("DB_SEQUENCE_PREFIX").str_ireplace(C("DB_PREFIX"), "", $match[2]);
$flag = (boolean)$this->query("SELECT * FROM user_sequences WHERE sequence_name='" . strtoupper($this->table) . "'");
}//modify by wyfeng at 2009.08.28
//更改事务模式
$this->mode = OCI_COMMIT_ON_SUCCESS;
}
//释放前次的查询结果
if ( $this->queryID ) $this->free();
N('db_write',1);
if ( !empty($this->PDOStatement) ) $this->free();
$this->executeTimes++;
// 记录开始执行时间
G('queryStartTime');
$stmt = oci_parse($this->_linkID,$str);
Debug::remark('queryStartTime','time');
$this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement) {
throw_exception($this->error());
}
$result = $this->PDOStatement->execute($bind);
$this->debug();
if (false === oci_execute($stmt)) {
if ( false === $result) {
$this->error();
return false;
} else {
$this->numRows = oci_num_rows($stmt);
$this->lastInsID = $flag?$this->insertLastId():0;//modify by wyfeng at 2009.08.28
$this->numRows = $this->PDOStatement->rowCount();
if($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
$this->lastInsID = $this->getLastInsertId();
}
return $this->numRows;
}
}
/**
* 启动事务
* @access public
* @return void
*/
public function startTrans() {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
//数据rollback 支持
if ($this->transTimes == 0) {
$this->mode = OCI_DEFAULT;
}
$this->transTimes++;
return ;
}
/**
* 用于非自动提交状态下面的查询提交
* @access public
* @return boolen
*/
public function commit(){
if ($this->transTimes > 0) {
$result = oci_commit($this->_linkID);
if(!$result){
$this->error();
return false;
}
$this->transTimes = 0;
}
return true;
}
/**
* 事务回滚
* @access public
* @return boolen
*/
public function rollback(){
if ($this->transTimes > 0) {
$result = oci_rollback($this->_linkID);
if(!$result){
$this->error();
return false;
}
$this->transTimes = 0;
}
return true;
}
/**
* 获得所有的查询数据
* @access private
* @return array
*/
private function getAll() {
//返回数据集
$result = array();
$this->numRows = oci_fetch_all($this->queryID, $result, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
//add by wyfeng at 2008-12-23 强制将字段名转换为小写以配合Model类函数如count等
if(C("DB_CASE_LOWER")) {
foreach($result as $k=>$v) {
$result[$k] = array_change_key_case($result[$k], CASE_LOWER);
}
}
return $result;
}
/**
* 取得数据表的字段信息
* @access public
@@ -242,39 +98,6 @@ class Oracle extends Db{
return $info;
}
/**
* 关闭数据库
* @access public
*/
public function close() {
if($this->_linkID){
oci_close($this->_linkID);
}
$this->_linkID = null;
}
/**
* 数据库错误信息
* 并显示当前的SQL语句
* @access public
* @return string
*/
public function error($result = true) {
if($result){
$error = oci_error($this->queryID);
}elseif(!$this->_linkID){
$error = oci_error();
}else{
$error = oci_error($this->_linkID);
}
if('' != $this->queryStr){
$error['message'] .= "\n [ SQL语句 ] : ".$this->queryStr;
}
$result? ThinkLog::record($error['message'],'ERR'):throw_exception($error['message'],'',$error['code']);
$this->error = $error['message'];
return $this->error;
}
/**
* SQL指令安全过滤
* @access public
@@ -309,7 +132,7 @@ class Oracle extends Db{
* @access public
* @return integer
*/
public function insertLastId() {
public function getLastInsertId() {
if(empty($this->table)) {
return 0;
}