Think/Model/Lite类调整

This commit is contained in:
ThinkPHP
2013-04-27 21:18:28 +08:00
parent b89d2cba76
commit 4d95ba78fb

View File

@@ -1,234 +1,240 @@
<?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 Think\Model; namespace Think\Model;
class Lite { class Lite {
// 当前数据库操作对象 // 当前数据库操作对象
protected $db = null; protected $db = null;
// 数据库名称 // 数据库名称
protected $dbName = ''; protected $dbName = '';
//数据库配置 //数据库配置
protected $connection = ''; protected $connection = '';
// 数据表前缀 // 数据表前缀
protected $tablePrefix = ''; protected $tablePrefix = '';
// 数据表名(不包含表前缀) // 数据表名(不包含表前缀)
protected $tableName = ''; protected $tableName = '';
// 实际数据表名(包含表前缀) // 实际数据表名(包含表前缀)
protected $trueTableName = ''; protected $trueTableName = '';
// 最近错误信息 // 最近错误信息
protected $error = ''; protected $error = '';
// 配置参数 // 配置参数
protected $config = []; protected $config = [];
/** /**
* 架构函数 * 架构函数
* 取得DB类的实例对象 字段检查 * 取得DB类的实例对象 字段检查
* @access public * @access public
* @param string $name 模型名称 * @param string $name 模型名称
* @param string $tablePrefix 表前缀 * @param array $config 模型配置
* @param mixed $connection 数据库连接信息 */
*/ public function __construct($name='',$config=[]) {
public function __construct($name='',$tablePrefix='',$connection='') { // 传入模型参数
// 模型初始化 if(!empty($name)){
$this->_initialize(); $this->name = $name;
// 读取配置参数 }elseif(empty($this->name)){
$this->config = Config::get(); $this->name = $this->getModelName();
}
// 获取模型名称 if(strpos($this->name,'.')) { // 支持 数据库名.模型名的 定义
if(!empty($name)) { list($this->dbName,$this->name) = explode('.',$this->name);
if(strpos($name,'.')) { // 支持 数据库名.模型名的 定义 }
list($this->dbName,$this->name) = explode('.',$name);
}else{ if(isset($config['table_prefix'])) {
$this->name = $name; $this->tablePrefix = $config['table_prefix'];
} }
}elseif(empty($this->name)){ if(isset($config['connection'])) {
$this->name = $this->getModelName(); $this->connection = $config['connection'];
} }
// 设置表前缀 if(isset($config['table_name'])) {
if(is_null($tablePrefix)) {// 前缀为Null表示没有前缀 $this->tableName = $config['table_name'];
$this->tablePrefix = ''; }
}elseif('' != $tablePrefix) { if(isset($config['true_table_name'])) {
$this->tablePrefix = $tablePrefix; $this->trueTableName = $config['true_table_name'];
}else{ }
$this->tablePrefix = $this->tablePrefix?$this->tablePrefix:$this->config['db_prefix']; if(isset($config['db_name'])) {
} $this->dbName = $config['db_name'];
}
// 数据库初始化操作
// 获取数据库操作对象 // 设置表前缀
// 当前模型有独立的数据库连接信息 if(empty($this->tablePrefix)) {
$this->db(0,empty($this->connection)?$connection:$this->connection); $this->tablePrefix = is_null($this->tablePrefix)?'':C('database.prefix');
} }
/** // 数据库初始化操作
* 得到当前的数据对象名称 // 获取数据库操作对象
* @access public // 当前模型有独立的数据库连接信息
* @return string $this->db(0,$this->connection);
*/ }
public function getModelName() {
if(empty($this->name)) /**
$this->name = substr(get_class($this),0,-5); * 得到当前的数据对象名称
return $this->name; * @access public
} * @return string
*/
/** public function getModelName() {
* 得到完整的数据表名 if(empty($this->name))
* @access public $this->name = substr(get_class($this),0,-5);
* @return string return $this->name;
*/ }
public function getTableName() {
if(empty($this->trueTableName)) { /**
$tableName = !empty($this->tablePrefix) ? $this->tablePrefix : ''; * 得到完整的数据表名
if(!empty($this->tableName)) { * @access public
$tableName .= $this->tableName; * @return string
}else{ */
$tableName .= parse_name($this->name); public function getTableName() {
} if(empty($this->trueTableName)) {
$this->trueTableName = strtolower($tableName); $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : '';
} if(!empty($this->tableName)) {
return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName; $tableName .= $this->tableName;
} }else{
$tableName .= parse_name($this->name);
}
/** $this->trueTableName = strtolower($tableName);
* SQL查询 }
* @access public return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName;
* @param string $sql SQL指令 }
* @param array $binding 参数绑定
* @return mixed
*/ /**
public function query($sql,$binding=[]) { * SQL查询
$sql = $this->parseSql($sql); * @access public
return $this->db->query($sql,$binding); * @param string $sql SQL指令
} * @param array $binding 参数绑定
* @return mixed
/** */
* 执行SQL语句 public function query($sql,$binding=[]) {
* @access public $sql = $this->parseSql($sql);
* @param string $sql SQL指令 return $this->db->query($sql,$binding);
* @param array $binding 参数绑定 }
* @return false | integer
*/ /**
public function execute($sql,$binding=[]) { * 执行SQL语句
$sql = $this->parseSql($sql); * @access public
return $this->db->execute($sql,$binding); * @param string $sql SQL指令
} * @param array $binding 参数绑定
* @return false | integer
/** */
* 解析SQL语句 public function execute($sql,$binding=[]) {
* @access public $sql = $this->parseSql($sql);
* @param string $sql SQL指令 return $this->db->execute($sql,$binding);
* @return string }
*/
protected function parseSql($sql) { /**
// 分析表达式 * 解析SQL语句
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->config['DB_PREFIX'])); * @access public
$this->db->setModel($this->name); * @param string $sql SQL指令
return $sql; * @return string
} */
protected function parseSql($sql) {
/** // 分析表达式
* 切换当前的数据库连接 $sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->config['DB_PREFIX']));
* @access public $this->db->setModel($this->name);
* @param integer $linkNum 连接序号 return $sql;
* @param mixed $config 数据库连接信息 }
* @return Model
*/ /**
public function db($linkNum='',$config=''){ * 切换当前的数据库连接
if(''===$linkNum && $this->db) { * @access public
return $this->db; * @param integer $linkNum 连接序号
} * @param mixed $config 数据库连接信息
static $_linkNum = []; * @return Model
static $_db = []; */
if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $config && $_linkNum[$linkNum]!=$config) ) { public function db($linkNum='',$config=''){
// 创建一个新的实例 if(''===$linkNum && $this->db) {
if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数 return $this->db;
$config = Config::get($config); }
} static $_linkNum = [];
$_db[$linkNum] = Db::Lite($config); static $_db = [];
}elseif(NULL === $config){ if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $config && $_linkNum[$linkNum]!=$config) ) {
$_db[$linkNum]->close(); // 关闭数据库连接 // 创建一个新的实例
unset($_db[$linkNum]); if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数
return ; $config = Config::get($config);
} }
// 记录连接信息 $_db[$linkNum] = \Think\Db::instance($config,true);
$_linkNum[$linkNum] = $config; }elseif(NULL === $config){
// 切换数据库连接 $_db[$linkNum]->close(); // 关闭数据库连接
$this->db = $_db[$linkNum]; unset($_db[$linkNum]);
return $this; return ;
} }
// 记录连接信息
/** $_linkNum[$linkNum] = $config;
* 启动事务 // 切换数据库连接
* @access public $this->db = $_db[$linkNum];
* @return void return $this;
*/ }
public function startTrans() {
$this->commit(); /**
$this->db->startTrans(); * 启动事务
return ; * @access public
} * @return void
*/
/** public function startTrans() {
* 提交事务 $this->commit();
* @access public $this->db->startTrans();
* @return boolean return ;
*/ }
public function commit() {
return $this->db->commit(); /**
} * 提交事务
* @access public
/** * @return boolean
* 事务回滚 */
* @access public public function commit() {
* @return boolean return $this->db->commit();
*/ }
public function rollback() {
return $this->db->rollback(); /**
} * 事务回滚
* @access public
/** * @return boolean
* 返回模型的错误信息 */
* @access public public function rollback() {
* @return string return $this->db->rollback();
*/ }
public function getError(){
return $this->error; /**
} * 返回模型的错误信息
* @access public
/** * @return string
* 返回数据库的错误信息 */
* @access public public function getError(){
* @return string return $this->error;
*/ }
public function getDbError() {
return $this->db->getError(); /**
} * 返回数据库的错误信息
* @access public
/** * @return string
* 返回最后插入的ID */
* @access public public function getDbError() {
* @return string return $this->db->getError();
*/ }
public function getLastInsID() {
return $this->db->getLastInsID(); /**
} * 返回最后插入的ID
* @access public
/** * @return string
* 返回最后执行的sql语句 */
* @access public public function getLastInsID() {
* @return string return $this->db->getLastInsID();
*/ }
public function getLastSql() {
return $this->db->getLastSql($this->name); /**
} * 返回最后执行的sql语句
* @access public
} * @return string
*/
public function getLastSql() {
return $this->db->getLastSql($this->name);
}
}