[李景山php]thinkphp核心源码注释|Lite.class.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace Think\Db;
use Think\Config;
use Think\Debug;
use Think\Log;
use PDO;
// 各种空间的集成 引入
class Lite {
    // 获取 数据量
    // PDO操作实例
    protected $PDOStatement = null; // 获取 PDO 状态
    // 当前操作所属的模型名
    protected $model      = '_think_';// 模型 开始
    // 当前SQL指令
    protected $queryStr   = ''; // 处理完成的 sql 语句
    protected $modelSql   = array(); // 数组记录
    // 最后插入ID
    protected $lastInsID  = null; // 最后插入的 数据
    // 返回或者影响记录数
    protected $numRows    = 0; // 影响行数
    // 事务指令数
    protected $transTimes = 0;// 指定条数
    // 错误信息
    protected $error      = ''; // 获取对应的 错误信息
    // 数据库连接ID 支持多个连接
    protected $linkID     = array();// 支持分布式连接 ID
    // 当前连接ID
    protected $_linkID    = null; // 当前 执行  数据连接
    // 数据库连接参数配置
    protected $config     = array(
        'type'              =>  '',     // 数据库类型
        'hostname'          =>  '127.0.0.1', // 服务器地址
        'database'          =>  '',          // 数据库名
        'username'          =>  '',      // 用户名
        'password'          =>  '',          // 密码
        'hostport'          =>  '',        // 端口     
        'dsn'               =>  '', //   另外一种 数据格式 进行 数据库连接的解析的
        'params'            =>  array(), // 数据库连接参数   其它参数
        'charset'           =>  'utf8',      // 数据库编码默认采用utf8  
        'prefix'            =>  '',    // 数据库表前缀  第一次接触到表
        'debug'             =>  false, // 数据库调试模式  默认关闭
        'deploy'            =>  0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)  是否可以
        'rw_separate'       =>  false,       // 数据库读写是否分离 主从式有效
        'master_num'        =>  1, // 读写分离后 主服务器数量
        'slave_no'          =>  '', // 指定从服务器序号 主从备份
    );
    // 数据库表达式
    protected $comparison = array('eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN');// 简单的数组翻译功能
    // 查询表达式
    protected $selectSql  = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%COMMENT%'; // 一个基础的表达式查询语句
    // 查询次数
    protected $queryTimes   =   0;//查询的 次数,非数据库执行次数
    // 执行次数
    protected $executeTimes =   0;// 执行次数
    // PDO连接参数
    protected $options = array( //  PDO 的连接参数
        PDO::ATTR_CASE              =>  PDO::CASE_LOWER, // 在没有看 应该是转化为小写
        PDO::ATTR_ERRMODE           =>  PDO::ERRMODE_EXCEPTION,// 错误异常方式
        PDO::ATTR_ORACLE_NULLS      =>  PDO::NULL_NATURAL,//
        PDO::ATTR_STRINGIFY_FETCHES =>  false,
    );
    // 因为不同数据库对返回的字段名称大小写处理不同,所以PDO提供了PDO::ATTR_CASE设置项(包括PDO::CASE_LOWER,PDO::CASE_NATURAL,PDO::CASE_UPPER),来确定返回的字段名称的大小写。
    // 通过设置PDO::ATTR_ORACLE_NULLS类型(包括PDO::NULL_NATURAL,PDO::NULL_EmpTY_STRING,PDO::NULL_TO_STRING)来指定数据库返回的NULL值在php中对应的数值。


    /**
     * 架构函数 读取数据库配置信息
     * @access public
     * @param array $config 数据库配置数组
     * 初始化配置选项而已
     */
    public function __construct($config=''){
        if(!empty($config)) {
            $this->config           =   array_merge($this->config,$config);
            if(is_array($this->config['params'])){
                $this->options  +=   $this->config['params']; // 这个操作基本上跟array_merge 差不多的
            }
        }
    }

    /**
     * 连接数据库方法
     * @access public
     */
    public function connect($config='',$linkNum=0) { // 连接数据库 是可以 指定连接 参数的  后面的 0 是为了 主从数据库设置的
        // 说实话,这个我不是很喜欢,但是 他的指定参数 对于临时调整的数据连接 很有用的
        if ( !isset($this->linkID[$linkNum]) ) { // 单例模式,
            if(empty($config))  $config =   $this->config; // 跟三元运算符差不多
            try{
                if(empty($config['dsn'])) {
                    $config['dsn']  =   $this->parseDsn($config);// 获取dsn 解析
                }
                if(version_compare(PHP_VERSION,'5.3.6','<=')){ //禁用模拟预处理语句
                    $this->options[PDO::ATTR_EMULATE_PREPARES]  =   false; // 版本控制
                }
                $this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$this->options);// 普通的PDO  连接参数

            }catch (\PDOException $e) {
                E($e->getMessage());// 报错这个 是靠谱的
            }
        }
        return $this->linkID[$linkNum]; // 返回连接资源
    }

    /**
     * 解析pdo连接的dsn信息
     * @access public
     * @param array $config 连接信息
     * @return string
     */
    protected function parseDsn($config){}// 这个不是骗人吗,嘿嘿,么有了

    /**
     * 释放查询结果
     * @access public
     */
    public function free() {
        $this->PDOStatement = null; // 清空结果,也就意味着释放掉了内存
    }
    // 明日继续




















    /**
     * 执行查询 返回数据集
     * @access public
     * @param string $str  sql指令
     * @param array $bind  参数绑定
     * @return mixed
     * 一个普通的查询
     */
    public function query($str,$bind=array()) {
        $this->initConnect(false); // 一个多连接配置
        if ( !$this->_linkID ) return false; // 如果默认没有找到当前的正常的连接,那么,这个,就失效了,嘿嘿
        $this->queryStr     =   $str;// 获取其对应的数据
        if(!empty($bind)){// 如果数据不为空 含有绑定的参数  相当于 特殊处理一下参数 了
            $that   =   $this;
//          $arr = array("Hello" => "Hi", "world" => "earth");
//          echo strtr("Hello world",$arr);
            // 居然还可以批量替换,嘿嘿  把字符串 "Hello world" 替换成 "Hi earth":
            //strtr() 函数转换字符串中特定的字符。strtr(string,from,to)  应该是跟那个 str_replace 差不多的一个档次了
            $this->queryStr =   strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$bind));
            // 进行一个数组遍历  其中的escapeString 号称是对绑定参数的一个过滤,但是,其实就是加了 斜线而已了
        }
        //释放前次的查询结果
        if ( !empty($this->PDOStatement) ) $this->free();// 这个注释很恰当
        $this->queryTimes++;// 次数++ 为什么不这么写 ++$this->queryTimes;嘿嘿,估计怕看起来不方便吧
        N('db_query',1); // 兼容代码        // 这不是记录次数 吗
        // 调试开始
        $this->debug(true);// 居然还开启调试 好吧,这个我醉了,默认开始调试不也就是可以了吗
        $this->PDOStatement = $this->_linkID->prepare($str); //从翻译的角度来说,这个就是个准备,prepare
        if(false === $this->PDOStatement)// 如果啥都没有 ,那么 怎么弄呢
            E($this->error());// 报错啊,瞅啥呢?
        foreach ($bind as $key => $val) { // 有参数,则进行循环 绑定数值
            if(is_array($val)){
                $this->PDOStatement->bindValue($key, $val[0], $val[1]);// 你的地盘你做主吧两个方式
            }else{
                $this->PDOStatement->bindValue($key, $val);// 还是单独的 key value 靠谱一些
            }
        }
        $result =   $this->PDOStatement->execute();// 这里进行执行,执行后
        // 调试结束
        $this->debug(false);// 完成了,自动关闭调试,牛叉
        if ( false === $result ) {// 没有结果的话,还要报错
            $this->error();
            return false;
        } else {
            return $this->getResult();// 否则的话,处理结果集 进行返回
        }
    }

    /**
     * 执行语句
     * @access public
     * @param string $str  sql指令
     * @param array $bind  参数绑定
     * @return integer
     * 这个完全有点重写的意味呢?
     */
    public function execute($str,$bind=array()) {
        $this->initConnect(true);// 初始化连接
        if ( !$this->_linkID ) return false; // 判读报错返回
        $this->queryStr = $str;// 查询传入执行,这个我倒是,此刻不怎么赞成
        if(!empty($bind)){// 针对于绑定的 参数 做 查询语句的特殊处理 大致应该就是那个 过滤吧
            $that   =   $this;
            $this->queryStr =   strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$bind));
        }      
        //释放前次的查询结果
        if ( !empty($this->PDOStatement) ) $this->free();// 清空仓库 +1 然后 准备收货
        $this->executeTimes++; // 执行次数加一
        N('db_write',1); // 兼容代码        
        // 记录开始执行时间
        $this->debug(true);// 搞了半天,这个是 个调试记录啊
        $this->PDOStatement =   $this->_linkID->prepare($str);
        if(false === $this->PDOStatement) {
            E($this->error());
        }
        foreach ($bind as $key => $val) {
            if(is_array($val)){
                $this->PDOStatement->bindValue($key, $val[0], $val[1]);
            }else{
                $this->PDOStatement->bindValue($key, $val);
            }
        }
        $result =   $this->PDOStatement->execute();
        $this->debug(false); // 此处跟上面的一个函数,基本上没什么大的区别呢
        if ( false === $result) {
            $this->error();
            return false;
        } else {
            $this->numRows = $this->PDOStatement->rowCount(); // 返回 影响的数据呗
            if(preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
                $this->lastInsID = $this->_linkID->lastInsertId(); // 根据不同的语句 看看是否有ID返回
            }
            return $this->numRows;
        }
    }// 返回数据
    // 总结:
    /**
     * 第一步:基本上先 单例连接 错误返回
     * 第二步:处理特殊绑定的 参数 有则处理,没有继续向下
     * 第三步:清空仓库 等待收货
     * 第四步:记录开始状态,进行数据、语句准备
     * 第五步:对执行结果审查,合适返回数据,否则报错异常
     */


















    /**
     * 启动事务
     * @access public
     * @return void
     * 就是干了一个,关闭自动提交的功能
     */
    public function startTrans() {
        $this->initConnect(true);
        if ( !$this->_linkID ) return false;
        //数据rollback 支持
        if ($this->transTimes == 0) {
            $this->_linkID->beginTransaction();
            /* 开始一个事务,关闭自动提交 */
        }
        $this->transTimes++;
        return ;
    }
    // 总结,其它 没什么了

    /**
     * 用于非自动提交状态下面的查询提交
     * @access public
     * @return boolean
     */
    public function commit() {
        if ($this->transTimes > 0) {
            $result = $this->_linkID->commit(); // 进行个数据提交,仅此而已了
            $this->transTimes = 0;
            if(!$result){
                $this->error();
                return false;
            }
        }
        return true;
    }

    /**
     * 事务回滚
     * @access public
     * @return boolean
     */
    public function rollback() {
        if ($this->transTimes > 0) {
            $result = $this->_linkID->rollback();// 普通的事务回滚
            $this->transTimes = 0;
            if(!$result){
                $this->error();
                return false;
            }
        }
        return true;
    }

    /**
     * 获得所有的查询数据
     * @access private
     * @return array
     */
    private function getResult() {
        //返回数据集
        $result =   $this->PDOStatement->fetchAll(PDO::FETCH_ASSOC);// 获取数据
        $this->numRows = count( $result );
        return $result;
    }

    /**
     * 获得查询次数
     * @access public
     * @param boolean $execute 是否包含所有查询
     * @return integer
     */
    public function getQueryTimes($execute=false){// 仅仅是个获取查询次数
        return $execute?$this->queryTimes+$this->executeTimes:$this->queryTimes;
    }

    /**
     * 获得执行次数
     * @access public
     * @return integer
     */
    public function getExecuteTimes(){ // 获取执行次数
        return $this->executeTimes;
    }

    /**
     * 关闭数据库
     * @access public
     */
    public function close() { // 关闭连接,释放资源
        $this->_linkID = null;
    }

    /**
     * 数据库错误信息
     * 并显示当前的SQL语句
     * @access public
     * @return string
     */
    public function error() { // 返回错误信息
        if($this->PDOStatement) {
            $error = $this->PDOStatement->errorInfo();
            $this->error = $error[1].':'.$error[2];
        }else{
            $this->error = '';
        }
        if('' != $this->queryStr){
            $this->error .= "\n [ SQL语句 ] : ".$this->queryStr;
        }
        // 记录错误日志
        trace($this->error,'','ERR');
        if($this->config['debug']) {// 开启数据库调试模式  记录数据库错误信息日志
            E($this->error);
        }else{
            return $this->error;
        }
    }

    /**
     * 获取最近一次查询的sql语句 
     * @param string $model  模型名
     * @access public
     * @return string
     */
    public function getLastSql($model='') {// 获取最后一条语句
        return $model?$this->modelSql[$model]:$this->queryStr;
    }

    /**
     * 获取最近插入的ID
     * @access public
     * @return string
     */
    public function getLastInsID() {
        return $this->lastInsID;
    }

    /**
     * 获取最近的错误信息
     * @access public
     * @return string
     */
    public function getError() {
        return $this->error;// 这种基本的 错误信息 进行返回
    }

    /**
     * SQL指令安全过滤
     * @access public
     * @param string $str  SQL字符串
     * @return string
     */
    public function escapeString($str) {
        return addslashes($str); //在每个双引号(")前添加反斜杠: 这样干,就预防了 注入了,因为 " 可以构成 这个 注入的语句
    }

    /**
     * 设置当前操作模型
     * @access public
     * @param string $model  模型名
     * @return void
     */
    public function setModel($model){
        $this->model =  $model; // 设置模型 临时设置的这种
    }

    /**
     * 数据库调试 记录当前SQL
     * @access protected
     * @param boolean $start  调试开始标记 true 开始 false 结束
     */
    protected function debug($start) {
        if($this->config['debug']) {// 开启数据库调试模式
            if($start) {
                G('queryStartTime'); // 才看见这个的真面目, 设置时间而已了
            }else{
                $this->modelSql[$this->model]   =  $this->queryStr;//记录语句
                //$this->model  =   '_think_';
                // 记录操作结束时间
                G('queryEndTime'); //记录结束时间
                trace($this->queryStr.' [ RunTime:'.G('queryStartTime','queryEndTime').'s ]','','SQL');// 页面运行记录
            }
        }
    }

    /**
     * 初始化数据库连接
     * @access protected
     * @param boolean $master 主服务器
     * @return void
     */
    protected function initConnect($master=true) {
        if(!empty($this->config['deploy'])) // 是否分布式
            // 采用分布式数据库
            $this->_linkID = $this->multiConnect($master);// 分布式的 高大上 啊,先连接 主库
        else
            // 默认单数据库
            if ( !$this->_linkID ) $this->_linkID = $this->connect(); // 创建单数据库连接
    }

    /**
     * 连接分布式服务器
     * @access protected
     * @param boolean $master 主服务器
     * @return void
     */
    protected function multiConnect($master=false) {
        // 分布式数据库配置解析
        $_config['username']    =   explode(',',$this->config['username']);
        $_config['password']    =   explode(',',$this->config['password']);
        $_config['hostname']    =   explode(',',$this->config['hostname']);
        $_config['hostport']    =   explode(',',$this->config['hostport']);
        $_config['database']    =   explode(',',$this->config['database']);
        $_config['dsn']         =   explode(',',$this->config['dsn']);
        $_config['charset']     =   explode(',',$this->config['charset']);
        // 多数据库 解析

        // 数据库读写是否分离
        if($this->config['rw_separate']){ // 读写分离
            // 主从式采用读写分离
            if($master)
                // 主服务器写入
                $r  =   floor(mt_rand(0,$this->config['master_num']-1));// 随机写入主服务器
            else{
                if(is_numeric($this->config['slave_no'])) {// 指定服务器读 // 如果指定 读取从服务器
                    $r = $this->config['slave_no'];
                }else{
                    // 读操作连接从服务器  随机 连接从 服务器
                    $r = floor(mt_rand($this->config['master_num'],count($_config['hostname'])-1));   // 每次随机连接的数据库
                }
            }
        }else{ // 读写不分离
            // 读写操作不区分服务器
            $r = floor(mt_rand(0,count($_config['hostname'])-1));   // 每次随机连接的数据库
        }
        $db_config = array(
            'username'  =>  isset($_config['username'][$r])?$_config['username'][$r]:$_config['username'][0],
            'password'  =>  isset($_config['password'][$r])?$_config['password'][$r]:$_config['password'][0],
            'hostname'  =>  isset($_config['hostname'][$r])?$_config['hostname'][$r]:$_config['hostname'][0],
            'hostport'  =>  isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0],
            'database'  =>  isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0],
            'dsn'       =>  isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0],
            'charset'   =>  isset($_config['charset'][$r])?$_config['charset'][$r]:$_config['charset'][0],
        );
        return $this->connect($db_config,$r); // 连接指定配置的服务器, 这个主从,看来真的一点技术含量都没有
        // 估计是这想的,会用到主从的人,估计这个事情自己就可以搞定了,哈哈
    }

   /**
     * 析构方法
     * @access public
     */
    public function __destruct() {
        // 释放查询
        if ($this->PDOStatement){
            $this->free(); // 释放 内存空间
        }
        // 关闭连接
        $this->close(); // 释放连接资源
    }
}

// 总结:
/**
 * 感觉这类也没干什么大事,就是各种封装,并且 弄了主从,完善了默认的 mysql 函数而已了。
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值