基础model类 添加query和execute方法 调整map方法

This commit is contained in:
ThinkPHP
2013-04-30 10:10:53 +08:00
parent 76bcf6259b
commit 7cfdde0ab7
2 changed files with 116 additions and 114 deletions

View File

@@ -690,6 +690,30 @@ class Model {
} }
} }
/**
* SQL查询
* @access public
* @param string $sql SQL指令
* @param array $bind 参数绑定
* @return mixed
*/
public function query($sql,$bind=[]) {
$sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]);
return $this->db->query($sql,$bind);
}
/**
* 执行SQL语句
* @access public
* @param string $sql SQL指令
* @param array $bind 参数绑定
* @return false | integer
*/
public function execute($sql,$bind=[]) {
$sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]);
return $this->db->execute($sql,$bind);
}
/** /**
* 设置数据对象值 * 设置数据对象值
* @access public * @access public
@@ -1001,7 +1025,7 @@ class Model {
* @return Model * @return Model
*/ */
public function map($map){ public function map($map){
$this->map = array_merge($this->map,$map); $this->map = $map;
return $this; return $this;
} }
} }

View File

@@ -1,114 +1,92 @@
<?php <?php
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ] // | TOPThink [ WE CAN DO IT JUST THINK ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Copyright (c) 2011 http://topthink.com All rights reserved. // | Copyright (c) 2011 http://topthink.com All rights reserved.
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace Traits\Think\Model; namespace Traits\Think\Model;
trait Query { trait Query {
/** /**
* 启动事务 * 启动事务
* @access public * @access public
* @return void * @return void
*/ */
public function startTrans() { public function startTrans() {
$this->commit(); $this->commit();
$this->db->startTrans(); $this->db->startTrans();
return ; return ;
} }
/** /**
* 提交事务 * 提交事务
* @access public * @access public
* @return boolean * @return boolean
*/ */
public function commit() { public function commit() {
return $this->db->commit(); return $this->db->commit();
} }
/** /**
* 事务回滚 * 事务回滚
* @access public * @access public
* @return boolean * @return boolean
*/ */
public function rollback() { public function rollback() {
return $this->db->rollback(); return $this->db->rollback();
} }
/** /**
* SQL查询 * 解析SQL语句
* @access public * @access public
* @param string $sql SQL指令 * @param string $sql SQL指令
* @param array $bind 参数绑定 * @param boolean $parse 是否需要解析SQL
* @return mixed * @return string
*/ */
public function query($sql,$bind=[]) { public function parseSql($sql,$parse) {
return $this->db->query($sql,$bind); // 分析表达式
} if(true === $parse) {
$options = $this->_parseOptions();
/** $sql = $this->db->parseSql($sql,$options);
* 执行SQL语句 }elseif(is_array($parse)){ // SQL预处理
* @access public $sql = vsprintf($sql,$parse);
* @param string $sql SQL指令 }else{
* @param array $bind 参数绑定 $sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]);
* @return false | integer }
*/ return $sql;
public function execute($sql,$bind=[]) { }
return $this->db->execute($sql,$bind);
} /**
* 批处理执行SQL语句
/** * 批处理的指令都认为是execute操作
* 解析SQL语句 * @access public
* @access public * @param array $sql SQL批处理指令
* @param string $sql SQL指令 * @return boolean
* @param boolean $parse 是否需要解析SQL */
* @return string public function patchQuery($sql=[]) {
*/ if(!is_array($sql)) return false;
public function parseSql($sql,$parse) { // 自动启动事务支持
// 分析表达式 $this->startTrans();
if(true === $parse) { try{
$options = $this->_parseOptions(); foreach ($sql as $_sql){
$sql = $this->db->parseSql($sql,$options); $result = $this->execute($_sql);
}elseif(is_array($parse)){ // SQL预处理 if(false === $result) {
$sql = vsprintf($sql,$parse); // 发生错误自动回滚事务
}else{ $this->rollback();
$sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]); return false;
} }
return $sql; }
} // 提交事务
$this->commit();
/** } catch (\Think\Exception $e) {
* 批处理执行SQL语句 $this->rollback();
* 批处理的指令都认为是execute操作 }
* @access public return true;
* @param array $sql SQL批处理指令 }
* @return boolean
*/
public function patchQuery($sql=[]) {
if(!is_array($sql)) return false;
// 自动启动事务支持
$this->startTrans();
try{
foreach ($sql as $_sql){
$result = $this->execute($_sql);
if(false === $result) {
// 发生错误自动回滚事务
$this->rollback();
return false;
}
}
// 提交事务
$this->commit();
} catch (\Think\Exception $e) {
$this->rollback();
}
return true;
}
} }