为了账号安全,请及时绑定邮箱和手机立即绑定

thinkphp 和 laravel使用sql语句操作db和源码浅析

标签:
ThinkPHP

前言

对于一个PHP应用,可能最多的就是操作数据,以致于初学者有时只把php当做数据库增删查改的工具(这也无可厚非)。而基于框架的语言,在框架中自然不能少了对数据库操作的封装,总想打开源码,看看到底是怎么工作的,趁着在期末考试前有时间~~

thinkphp

首先是这个中国人用的最多的框架说起。
ps:我是基于thinkphp3.2来说,tp5.x党见谅~

thinkphp支持对原生的sql语句执行,如:

        $db=M();        $condition="XXX";        $sql="select student.id from student where `s_name`= '$condition'";        $result=$db->query($sql);

既是使用M()方法实例一个空值,然后->query($sql),我们来看下tp的db类的源码(ThinkPHP\Library\Think\Db):

  public function query($str,$fetchSql=false) {        $this->initConnect(false);        if ( !$this->_linkID ) return false;        $this->queryStr     =   $str;        if(!empty($this->bind)){
            $that   =   $this;            $this->queryStr =   strtr($this->queryStr,array_map(function($val) use($that)
                                            {                                                return '\''.$that->escapeString($val).'\'';
                                            }
                                                 ,$this->bind));
        }        if($fetchSql){            return $this->queryStr;
        }        //释放前次的查询结果
        if ( !empty($this->PDOStatement) ) $this->free();        $this->queryTimes++;
        N('db_query',1); // 兼容代码
        // 调试开始
        $this->debug(true);        $this->PDOStatement = $this->_linkID->prepare($str);        if(false === $this->PDOStatement){            $this->error();            return false;
        }  
print_r($this->bind);//test
        foreach ($this->bind as $key => $val) {            if(is_array($val)){                $this->PDOStatement->bindValue($key, $val[0], $val[1]);
            }else{                $this->PDOStatement->bindValue($key, $val);
            }
        }        $this->bind =   array();
        $result =   $this->PDOStatement->execute();        // 调试结束
        $this->debug(false);        if ( false === $result ) {            $this->error();            return false;
        } else {            return $this->getResult();
        }
    }

可以看到,这个function要是没有绑定参数就先是把$str参数给到一个闭包函数里进行替换,$that->escapeString($val);

 public function escapeString($str) {        return addslashes($str);
    }

而这个函数也只是做了个简单addslashes($str),连个mysql_real_escape_string()都没有~~~;还是有sql注入可能性,而我们要是以直接建立sql语句查询的话,是没有使用参数绑定的操作的.然后我们看到$this->PDOStatement->execute(),就是说$this->query();还是进行了pdo 的prepare和execute的操作(虽然只是对其addslashes一下),而对于不时select的sql呢,有个$db->execute(),我们接着看源码。不时很清楚addslashes的点开看看

////////其他都和query()一样
 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();
            }            return $this->numRows;
        }
    }

看到了吧,没有加任何容错机制,只是对insert语句返回了lastInsertId(),,然后这个还要我们自己来$db->getLastInsID()来获取,不过单纯的insert也还好啦,,想加入事务机制呢,我们接着往下看源码:

    public function startTrans() {        $this->initConnect(true);        if ( !$this->_linkID ) return false;        //数据rollback 支持
        if ($this->transTimes == 0) {            $this->_linkID->beginTransaction();
        }        $this->transTimes++;        return ;
    }    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;
    }

看到了都是基于PDO的,调用方法也和pdo类似

$m->startTrans();
$result=$m->where('删除条件')->delete();
$result2=m2->where('删除条件')->delete();if($result && $result2){
$m->commit();//成功则提交}else{
$m->rollback();//不成功,则回滚}

可我们用框架不能还是老写sql呀,要用人家的方法呀~~

        $con['s_name']=$condition;
        $con['id']=3;
        $con['_logic'] = 'OR';
        $field=array('id','s_name');
        $db2=M('student');
        $result2=$db2->where($con)->field($field)->select();
        print_r($result2);
        $result3=$db2->getFieldBys_name('gbw2','id');
        print_r($result3);

我们先关注下第一个result的源码,不过在这之前我们先看thinkphp中的返回结果模式

  protected $options = array(
        PDO::ATTR_CASE              =>  PDO::CASE_LOWER,
        PDO::ATTR_ERRMODE           =>  PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_ORACLE_NULLS      =>  PDO::NULL_NATURAL,
        PDO::ATTR_STRINGIFY_FETCHES =>  false,
    );

其他的咱们都不怎么care,这里<b>PDO::ATTR_STRINGIFY_FETCHES =>  false ,</b>是不把取出来的数字结果转为字符串还有就是innodb(MyISAM 则不然)把insert默认当做事务来处理,<b>PDO::ATTR_AUTOCOMMIT</b>默认是1,也就是insert自动执行,所以insert语句的时候记得最好加上commit

好的,言归正传

 public function select($options=array()) {        $this->model  =   $options['model'];        $this->parseBind(!empty($options['bind'])?$options['bind']:array());
        $sql    = $this->buildSelectSql($options);
        $result   = $this->query($sql,!empty($options['fetch_sql']) ? true : false);        return $result;
    }    /**
     * 生成查询SQL
     * @access public
     * @param array $options 表达式
     * @return string
     */
    public function buildSelectSql($options=array()) {        if(isset($options['page'])) {            // 根据页数计算limit
            list($page,$listRows)   =   $options['page'];
            $page    =  $page>0 ? $page : 1;
            $listRows=  $listRows>0 ? $listRows : (is_numeric($options['limit'])?$options['limit']:20);
            $offset  =  $listRows*($page-1);
            $options['limit'] =  $offset.','.$listRows;
        }
        $sql  =   $this->parseSql($this->selectSql,$options);        return $sql;
    }/*
*
*中间省略
*/
 public function parseSql($sql,$options=array()){
        $sql   = str_replace(        array('%TABLE%','%DISTINCT%','%FIELD%','%JOIN%','%WHERE%','%GROUP%','%HAVING%','%ORDER%','%LIMIT%','%UNION%','%LOCK%','%COMMENT%','%FORCE%'),            array(                $this->parseTable($options['table']),                $this->parseDistinct(isset($options['distinct'])?$options['distinct']:false),                $this->parseField(!empty($options['field'])?$options['field']:'*'),                $this->parseJoin(!empty($options['join'])?$options['join']:''),                $this->parseWhere(!empty($options['where'])?$options['where']:''),                $this->parseGroup(!empty($options['group'])?$options['group']:''),                $this->parseHaving(!empty($options['having'])?$options['having']:''),                $this->parseOrder(!empty($options['order'])?$options['order']:''),                $this->parseLimit(!empty($options['limit'])?$options['limit']:''),                $this->parseUnion(!empty($options['union'])?$options['union']:''),                $this->parseLock(isset($options['lock'])?$options['lock']:false),                $this->parseComment(!empty($options['comment'])?$options['comment']:''),                $this->parseForce(!empty($options['force'])?$options['force']:'')
            ),$sql);        return $sql;
    }

select()中先是parseBind();其余的也是通过固定格式来构造sql语句,最后还是用query执行,换汤不换药。

  protected function parseBind($bind){        $this->bind   =   array_merge($this->bind,$bind);
    }
        //我们手动加上bind函数
    $con['id']=':id';
        $con['_logic'] = 'OR';
        $field=array('id','s_name');
        $db2=M('student');

        $result2=$db2->where($con)->bind(':id',3)->field($field)->select();        //再在类中打印这个绑定的参数发现有了
        print_r($this->bind);

可是奇怪的是在drive类中,我并没有找到bind() 这个函数,而且  <b>'DB_BIND_PARAM' => true</b>这个设置后,在insert/update/delete中会自动绑定参数还是相对安全的,,不过还是手动bind下更放心,踏实~

leveral

我们第二个主角登场了,thinkphp要是中国用的最多的框架,那么leveral则是世界上用的最多的框架了,哈哈~

首先我们先要找到DB类在哪,是<b>vendor\laravel\framework\src\Illuminate\Support\Facades</b> 吗?
我们打开发现里面就这个protected static function getFacadeAccessor() { return 'db'; }

好像不对呀,我们再找,真正起作用的是<b>\Illuminate\Database\DatabaseManager</b>打开看看吧好像只有一些reconnect(),disconnect()之类的函数,我们常见的select呢,,我们再找找,<b>\Illuminate\Database\Connection.php</b>这回是了。
我们先简单写几个demo吧

$users = DB::table('users')->where('votes', '>', 100)->get();
$result=DB::table('users')->select('name', 'email')->get();
$goodsShow = DB::table('goods')->where([product_id'=>$id,'name'=>$name])->first();
//同样可以使用原生的
$db->select('select * from user where id in (?,?)', [$id,2]);
$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();
DB::table('users')->where('id', 1)->update(array('votes' => 1));

写了几个发现好像和sql还蛮像的,而且好接受,这可能正是lereval的魔力吧,我们来看看源码~

//先看看Illuminate\Database\Connectors\Connector.php
    protected $options = array(
        PDO::ATTR_CASE => PDO::CASE_NATURAL,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
        PDO::ATTR_STRINGIFY_FETCHES => false,
        PDO::ATTR_EMULATE_PREPARES => false,
    );//初始化public function __construct(PDO $pdo, $database = '', $tablePrefix = '', array $config = array())
    {        $this->pdo = $pdo;
....//先是DB::tablepublic function table($table)
    {
        $processor = $this->getPostProcessor();

        $query = new Query\Builder($this, $this->getQueryGrammar(), $processor);        return $query->from($table);
    }//selectpublic function select($query, $bindings = array(), $useReadPdo = true)
    {        return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
        {            if ($me->pretending()) return array();            // For select statements, we'll simply execute the query and return an array
            // of the database result set. Each element in the array will be a single
            // row from the database table, and will either be an array or objects.
            $statement = $this->getPdoForSelect($useReadPdo)->prepare($query);

            $statement->execute($me->prepareBindings($bindings));            return $statement->fetchAll($me->getFetchMode());
        });
    }//commitpublic function commit()
    {        if ($this->transactions == 1) $this->pdo->commit();

        --$this->transactions;        $this->fireConnectionEvent('committed');
    }

可以看出同样支持commit,callback等,选择table既是from,这都好理解,和thinkphp大同小异。
而着重提出的是select(),也是基于pretend和execute的,并且在参数绑定上做的显式表示了,从使用起来即可看到,

对比

sql操作不仅限于select/update/delete/insert/,还加入join,union等更复杂的关联操作,以及在case when短句等相关操作。一旦非要用这些,我建议还是原生的sql,然后直接query()来的痛快,要不容易出错不说还容易产生性能问题。

thinkphp是国产的,可能更适用于大多数国人的思维吧,我在看源码上也比较清晰。但其中对于参数的绑定,和灵活性以及怎么说,对原生sql用户的体验感就差点(还有那个令牌系统,画蛇添足有没有。。。)。
而laravel的原则就是优雅(就是一个方法打死不超过50行,然后各种return ,use别的方法,也是醉的不行),其中对安全性和用户操作及学习的体验性则更高一筹

总结

本文仅对thinkphp 和 laravel中的db操作一小部分做了探究,发现我们在特别方便地使用框架时,同时不考虑安全问题的时候,大多数情况人家都为我们考虑好了,所以饮水思源,提升代码能力最好的办法就是看源码了。
对于该用哪个框架,我并不做出回应,看大家个人喜好,要是有时间自己写个最适合自己的框架(其实是从别人的框架上改)也是不错的~~

参考文献



作者:michaelgbw
链接:https://www.jianshu.com/p/40a17b7508d2

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消