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, 感谢原作者分享。