<?php<liu21st@gmail.com>
defined('THINK_PATH') or exit();
/**
* Oracle数据库驱动
* @category Extend
* @package Extend
* @subpackage Driver.Db
* @author ZhangXuehun <zhangxuehun@sohu.com>
*/
class DbOracle extends Db{
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%';
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();
}
}
}
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'])
if (!$this->linkID[$linkNum]){
$this->error(false);
}
$this->connected = true;
if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
}
return $this->linkID[$linkNum];
}
public function free() {
oci_free_statement($this->queryID);
$this->queryID = null;
}
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();
}
}
public function execute($str) {
$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]);
$flag = (boolean)$this->query("SELECT * FROM user_sequences WHERE sequence_name='" . strtoupper($this->table) . "'");
$this->mode = OCI_COMMIT_ON_SUCCESS;
if ( $this->queryID ) $this->free();
N('db_write',1);
G('queryStartTime');
$stmt = oci_parse($this->_linkID,$str);
$this->debug();
if (false === oci_execute($stmt)) {
$this->error();
return false;
} else {
$this->numRows = oci_num_rows($stmt);
$this->lastInsID = $flag?$this->insertLastId():0 return $this->numRows;
}
}
public function startTrans() {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
if ($this->transTimes == 0) {
$this->mode = OCI_DEFAULT;
}
$this->transTimes++;
return ;
}
public function commit(){
if ($this->transTimes > 0) {
$result = oci_commit($this->_linkID);
if(!$result){
$this->error();
return false;
}
$this->transTimes = 0;
}
return true;
}
public function rollback(){
if ($this->transTimes > 0) {
$result = oci_rollback($this->_linkID);
if(!$result){
$this->error();
return false;
}
$this->transTimes = 0;
}
return true;
}
private function getAll() {
$result = array();
$this->numRows = oci_fetch_all($this->queryID, $result, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
if(C("DB_CASE_LOWER")) {
foreach($result as $k=>$v) {
$result[$k] = array_change_key_case($result[$k], CASE_LOWER);
}
}
return $result;
}
public function getFields($tableName) {
$result = $this->query("select a.column_name,data_type,decode(nullable,'Y',0,1) notnull,data_default,decode(a.column_name,b.column_name,1,0) pk "
."from user_tab_columns a,(select column_name from user_constraints c,user_cons_columns col "
."where c.constraint_name=col.constraint_name and c.constraint_type='P'and c.table_name='".strtoupper($tableName)
."') b where table_name='".strtoupper($tableName)."' and a.column_name=b.column_name(+)");
$info = array();
if($result) {
foreach ($result as $key => $val) {
$info[strtolower($val['column_name'])] = array(
'name' => strtolower($val['column_name']),
'type' => strtolower($val['data_type']),
'notnull' => $val['notnull'],
'default' => $val['data_default'],
'primary' => $val['pk'],
'autoinc' => $val['pk'],
);
}
}
return $info;
}
public function getTables($dbName='') {
$result = $this->query("select table_name from user_tables");
$info = array();
foreach ($result as $key => $val) {
$info[$key] = current($val);
}
return $info;
}
public function close() {
if($this->_linkID){
oci_close($this->_linkID);
}
$this->_linkID = null;
}
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? trace($error['message'],'','ERR'):throw_exception($error['message'],'',$error['code']);
$this->error = $error['message'];
return $this->error;
}
public function escapeString($str) {
return str_ireplace("'", "''", $str);
}
public function insertLastId() {
if(empty($this->table)) {
return 0;
}
$sequenceName = $this->table;
$vo = $this->query("SELECT {$sequenceName}.currval currval FROM dual");
return $vo?$vo[0]["currval"]:0;
}
public function parseLimit($limit) {
$limitStr = '';
if(!empty($limit)) {
$limit = explode(',',$limit);
if(count($limit)>1)
$limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0]+$limit[1]) . ")";
else
$limitStr = "(numrow>0 AND numrow<=".$limit[0].")";
}
return $limitStr?' WHERE '.$limitStr:'';
}
}