From 4d95ba78fb65f9a579b233cfd7738c2d2f283056 Mon Sep 17 00:00:00 2001 From: ThinkPHP Date: Sat, 27 Apr 2013 21:18:28 +0800 Subject: [PATCH] =?UTF-8?q?Think/Model/Lite=E7=B1=BB=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Think/Model/Lite.php | 474 ++++++++++++++++++----------------- 1 file changed, 240 insertions(+), 234 deletions(-) diff --git a/Library/Think/Model/Lite.php b/Library/Think/Model/Lite.php index a1e0178a..d6041092 100644 --- a/Library/Think/Model/Lite.php +++ b/Library/Think/Model/Lite.php @@ -1,234 +1,240 @@ - -// +---------------------------------------------------------------------- - -namespace Think\Model; - -class Lite { - // 当前数据库操作对象 - protected $db = null; - // 数据库名称 - protected $dbName = ''; - //数据库配置 - protected $connection = ''; - // 数据表前缀 - protected $tablePrefix = ''; - // 数据表名(不包含表前缀) - protected $tableName = ''; - // 实际数据表名(包含表前缀) - protected $trueTableName = ''; - // 最近错误信息 - protected $error = ''; - // 配置参数 - protected $config = []; - - /** - * 架构函数 - * 取得DB类的实例对象 字段检查 - * @access public - * @param string $name 模型名称 - * @param string $tablePrefix 表前缀 - * @param mixed $connection 数据库连接信息 - */ - public function __construct($name='',$tablePrefix='',$connection='') { - // 模型初始化 - $this->_initialize(); - // 读取配置参数 - $this->config = Config::get(); - - // 获取模型名称 - if(!empty($name)) { - if(strpos($name,'.')) { // 支持 数据库名.模型名的 定义 - list($this->dbName,$this->name) = explode('.',$name); - }else{ - $this->name = $name; - } - }elseif(empty($this->name)){ - $this->name = $this->getModelName(); - } - // 设置表前缀 - if(is_null($tablePrefix)) {// 前缀为Null表示没有前缀 - $this->tablePrefix = ''; - }elseif('' != $tablePrefix) { - $this->tablePrefix = $tablePrefix; - }else{ - $this->tablePrefix = $this->tablePrefix?$this->tablePrefix:$this->config['db_prefix']; - } - - // 数据库初始化操作 - // 获取数据库操作对象 - // 当前模型有独立的数据库连接信息 - $this->db(0,empty($this->connection)?$connection:$this->connection); - } - - /** - * 得到当前的数据对象名称 - * @access public - * @return string - */ - public function getModelName() { - if(empty($this->name)) - $this->name = substr(get_class($this),0,-5); - return $this->name; - } - - /** - * 得到完整的数据表名 - * @access public - * @return string - */ - public function getTableName() { - if(empty($this->trueTableName)) { - $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : ''; - if(!empty($this->tableName)) { - $tableName .= $this->tableName; - }else{ - $tableName .= parse_name($this->name); - } - $this->trueTableName = strtolower($tableName); - } - return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName; - } - - - /** - * SQL查询 - * @access public - * @param string $sql SQL指令 - * @param array $binding 参数绑定 - * @return mixed - */ - public function query($sql,$binding=[]) { - $sql = $this->parseSql($sql); - return $this->db->query($sql,$binding); - } - - /** - * 执行SQL语句 - * @access public - * @param string $sql SQL指令 - * @param array $binding 参数绑定 - * @return false | integer - */ - public function execute($sql,$binding=[]) { - $sql = $this->parseSql($sql); - return $this->db->execute($sql,$binding); - } - - /** - * 解析SQL语句 - * @access public - * @param string $sql SQL指令 - * @return string - */ - protected function parseSql($sql) { - // 分析表达式 - $sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->config['DB_PREFIX'])); - $this->db->setModel($this->name); - return $sql; - } - - /** - * 切换当前的数据库连接 - * @access public - * @param integer $linkNum 连接序号 - * @param mixed $config 数据库连接信息 - * @return Model - */ - public function db($linkNum='',$config=''){ - if(''===$linkNum && $this->db) { - return $this->db; - } - static $_linkNum = []; - static $_db = []; - if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $config && $_linkNum[$linkNum]!=$config) ) { - // 创建一个新的实例 - if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数 - $config = Config::get($config); - } - $_db[$linkNum] = Db::Lite($config); - }elseif(NULL === $config){ - $_db[$linkNum]->close(); // 关闭数据库连接 - unset($_db[$linkNum]); - return ; - } - // 记录连接信息 - $_linkNum[$linkNum] = $config; - // 切换数据库连接 - $this->db = $_db[$linkNum]; - return $this; - } - - /** - * 启动事务 - * @access public - * @return void - */ - public function startTrans() { - $this->commit(); - $this->db->startTrans(); - return ; - } - - /** - * 提交事务 - * @access public - * @return boolean - */ - public function commit() { - return $this->db->commit(); - } - - /** - * 事务回滚 - * @access public - * @return boolean - */ - public function rollback() { - return $this->db->rollback(); - } - - /** - * 返回模型的错误信息 - * @access public - * @return string - */ - public function getError(){ - return $this->error; - } - - /** - * 返回数据库的错误信息 - * @access public - * @return string - */ - public function getDbError() { - return $this->db->getError(); - } - - /** - * 返回最后插入的ID - * @access public - * @return string - */ - public function getLastInsID() { - return $this->db->getLastInsID(); - } - - /** - * 返回最后执行的sql语句 - * @access public - * @return string - */ - public function getLastSql() { - return $this->db->getLastSql($this->name); - } - -} + +// +---------------------------------------------------------------------- + +namespace Think\Model; + +class Lite { + // 当前数据库操作对象 + protected $db = null; + // 数据库名称 + protected $dbName = ''; + //数据库配置 + protected $connection = ''; + // 数据表前缀 + protected $tablePrefix = ''; + // 数据表名(不包含表前缀) + protected $tableName = ''; + // 实际数据表名(包含表前缀) + protected $trueTableName = ''; + // 最近错误信息 + protected $error = ''; + // 配置参数 + protected $config = []; + + /** + * 架构函数 + * 取得DB类的实例对象 字段检查 + * @access public + * @param string $name 模型名称 + * @param array $config 模型配置 + */ + public function __construct($name='',$config=[]) { + // 传入模型参数 + if(!empty($name)){ + $this->name = $name; + }elseif(empty($this->name)){ + $this->name = $this->getModelName(); + } + if(strpos($this->name,'.')) { // 支持 数据库名.模型名的 定义 + list($this->dbName,$this->name) = explode('.',$this->name); + } + + if(isset($config['table_prefix'])) { + $this->tablePrefix = $config['table_prefix']; + } + if(isset($config['connection'])) { + $this->connection = $config['connection']; + } + if(isset($config['table_name'])) { + $this->tableName = $config['table_name']; + } + if(isset($config['true_table_name'])) { + $this->trueTableName = $config['true_table_name']; + } + if(isset($config['db_name'])) { + $this->dbName = $config['db_name']; + } + + // 设置表前缀 + if(empty($this->tablePrefix)) { + $this->tablePrefix = is_null($this->tablePrefix)?'':C('database.prefix'); + } + + // 数据库初始化操作 + // 获取数据库操作对象 + // 当前模型有独立的数据库连接信息 + $this->db(0,$this->connection); + } + + /** + * 得到当前的数据对象名称 + * @access public + * @return string + */ + public function getModelName() { + if(empty($this->name)) + $this->name = substr(get_class($this),0,-5); + return $this->name; + } + + /** + * 得到完整的数据表名 + * @access public + * @return string + */ + public function getTableName() { + if(empty($this->trueTableName)) { + $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : ''; + if(!empty($this->tableName)) { + $tableName .= $this->tableName; + }else{ + $tableName .= parse_name($this->name); + } + $this->trueTableName = strtolower($tableName); + } + return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName; + } + + + /** + * SQL查询 + * @access public + * @param string $sql SQL指令 + * @param array $binding 参数绑定 + * @return mixed + */ + public function query($sql,$binding=[]) { + $sql = $this->parseSql($sql); + return $this->db->query($sql,$binding); + } + + /** + * 执行SQL语句 + * @access public + * @param string $sql SQL指令 + * @param array $binding 参数绑定 + * @return false | integer + */ + public function execute($sql,$binding=[]) { + $sql = $this->parseSql($sql); + return $this->db->execute($sql,$binding); + } + + /** + * 解析SQL语句 + * @access public + * @param string $sql SQL指令 + * @return string + */ + protected function parseSql($sql) { + // 分析表达式 + $sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->config['DB_PREFIX'])); + $this->db->setModel($this->name); + return $sql; + } + + /** + * 切换当前的数据库连接 + * @access public + * @param integer $linkNum 连接序号 + * @param mixed $config 数据库连接信息 + * @return Model + */ + public function db($linkNum='',$config=''){ + if(''===$linkNum && $this->db) { + return $this->db; + } + static $_linkNum = []; + static $_db = []; + if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $config && $_linkNum[$linkNum]!=$config) ) { + // 创建一个新的实例 + if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数 + $config = Config::get($config); + } + $_db[$linkNum] = \Think\Db::instance($config,true); + }elseif(NULL === $config){ + $_db[$linkNum]->close(); // 关闭数据库连接 + unset($_db[$linkNum]); + return ; + } + // 记录连接信息 + $_linkNum[$linkNum] = $config; + // 切换数据库连接 + $this->db = $_db[$linkNum]; + return $this; + } + + /** + * 启动事务 + * @access public + * @return void + */ + public function startTrans() { + $this->commit(); + $this->db->startTrans(); + return ; + } + + /** + * 提交事务 + * @access public + * @return boolean + */ + public function commit() { + return $this->db->commit(); + } + + /** + * 事务回滚 + * @access public + * @return boolean + */ + public function rollback() { + return $this->db->rollback(); + } + + /** + * 返回模型的错误信息 + * @access public + * @return string + */ + public function getError(){ + return $this->error; + } + + /** + * 返回数据库的错误信息 + * @access public + * @return string + */ + public function getDbError() { + return $this->db->getError(); + } + + /** + * 返回最后插入的ID + * @access public + * @return string + */ + public function getLastInsID() { + return $this->db->getLastInsID(); + } + + /** + * 返回最后执行的sql语句 + * @access public + * @return string + */ + public function getLastSql() { + return $this->db->getLastSql($this->name); + } + +}