Thinkphp對Oracle的支持簡直弱爆,只做到了基本的操作,就連事務都不支持。今天來手動改一改DbOracle.class.php,讓它稍微好用一些吧。 首先是insert。原來的insert應該沒有什么問題,但實際項目中更多的是需要在插入的時候遇到已存在的記錄則進行更新。于是
Thinkphp對Oracle的支持簡直弱爆,只做到了基本的操作,就連事務都不支持。今天來手動改一改DbOracle.class.php,讓它稍微好用一些吧。
首先是insert。原來的insert應該沒有什么問題,但實際項目中更多的是需要在插入的時候遇到已存在的記錄則進行更新。于是,利用Oracle中的MERGE INTO來實現這一點。
public function insert($data, $options = array(), $replace = false) { if (!$replace) { return parent::insert($data, $options, $replace); } $values = $fields = array(); $this->model = $options['model']; $sql_merge = 'MERGE INTO ' . $this->parseTable($options['table']) . ' using (select 1 from dual) ' . ' ON (' . $this->parseValue($data[$options['marge_key']]) . ' is not null and ' . $this->parseValue($data[$options['marge_key']]) . ' = ' . $options['marge_key'] . ')'; //insert foreach ($data as $key => $val) { //主鍵值為空時,不插入主鍵 if ($this->parseKey($key) == $this->parseKey($options['marge_key']) && $val == null ) { } elseif (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_insert = 'INSERT (' . implode(',', $fields) . ') VALUES (' . implode(',', $values) . ')'; //update if (isset($data[$this->parseKey($options['marge_key'])]) || $data[$this->parseKey($options['marge_key'])] == null ) { unset($data[$this->parseKey($options['marge_key'])]); } $sql_update = 'UPDATE ' . $this->parseSet($data) . $this->parseWhere(!empty($options['where']) ? $options['where'] : '') . $this->parseOrder(!empty($options['order']) ? $options['order'] : '') . $this->parseLimit(!empty($options['limit']) ? $options['limit'] : ''); $sql = $sql_merge . ' WHEN MATCHED THEN ' . $sql_update . ' WHEN NOT MATCHED THEN ' . $sql_insert; return $this->execute($sql, $this->parseBind(!empty($options['bind']) ? $options['bind'] : array())); }
不支持事務是Thinkphp連接Oracle時的另一個問題,框架作者似乎已經做過適配,但是應該是沒有測試,留下一堆bug。DbOracle.class.php中已經有了startTrans,commit,rollback等,稍作修改即可。
Thinkphp對數據庫的所有操作最終都是匯集到query或execute上來執行,但這兩個函數里放了一句$this->mode = OCI_COMMIT_ON_SUCCESS;活生生的把事務扼殺了,所以,這里先把兩個函數里的這句注釋掉。
然后,驚人得發現execute()中調用oci_execute時根本沒有傳入mode!前面辛辛苦苦改mode又是何苦,果斷加上oci_execute($stmt, $this->mode)。
接下來才是讓事務生效的重頭戲。在事務的幾個開關函數中加入對mode的修改,在startTrans()中,將mode設為OCI_DEFAULT,commit和rollback中將將mode設為改回OCI_COMMIT_ON_SUCCESS。這三個函數大概就是這樣子的:
/** * 啟動事務 * @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->mode = OCI_COMMIT_ON_SUCCESS;//陳宣亦 2014.11.09 14:07 $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->mode = OCI_COMMIT_ON_SUCCESS;//陳宣亦 2014.11.09 14:07 $this->transTimes = 0; } return true; }
還有一個頭疼的問題就是日期類型轉換,我還在尋找一種便捷的方法來解決這個問題。以后再補充上來吧。
本文出自:http://www.chenxuanyi.cn, 原文地址:http://www.chenxuanyi.cn/thinkphp-oracle.html, 感謝原作者分享。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com