## 创建core/database/query/QueryBuilder.php
```
<?php
namespace core\database\query;
use core\database\connection\Connection;
class QueryBuilder
{
protected $connection;
protected $grammar;
public $binds;
public $columns;
public $distinct;
public $form;
public $union;
public $bindings = [
'select' => [],
'from' => [],
'join' => [],
'where' => [],
'groupBy' => [],
'having' => [],
'order' => [],
'union' => [],
'unionOrder' => [],
];
protected $operators = [
'=','<','>','<=','>=','<>','!=','<=>','like','like binary','not like','ilike','&','|','^',
'<<','>>','rlike','not rlike','regexp','not regexp','~','~*','!~','!~*','similar to','not similar to',
'not ilike','~~*','!~~*'
];
public function __construct(Connection $connection, $grammar)
{
$this->connection = $connection; // 数据库连接
$this->grammar = $grammar; // 编译成sql的类
}
public function table(string $table,$as = null)
{
return $this->from($table,$as);
// return (clone $this)->from($table,$as);
}
public function from($table,$as)
{
$this->from = $as ? "{$table} as {$as}" : $table;
return $this;
}
public function get($columns = ['*'])
{
if(! is_array($columns))
$columns = func_get_args();
$this->columns = $columns;
$sql = $this->toSql();
return $this->runSql($sql);
}
// 运行sql
public function runSql($sql)
{
return $this->connection->select(
$sql,$this->getBinds()
);
}
public function where($column, $operator = null, $value = null, $joiner = 'and')
{
if ( is_array($column)) // 如果是 where(['id' => '2','name' => 'xxh']) 这种
foreach ($column as $col => $value)
$this->where($col,'=',$value);
if(! in_array($operator,$this->operators)){ // 操作符不存在
$value = $operator;
$operator = '=';
}
$type = 'Basic';
$this->wheres[] = compact(
'type', 'column', 'operator', 'value', 'joiner'
); // 存到wheres变量
$this->binds[] = $value;
return $this;
}
public function orWhere($column, $operator = null, $value = null)
{
return $this->where($column, $operator, $value ,'or');
}
public function find($id,$columns = ['*'],$key = 'id')
{
return $this->where($key,$id)->get($columns);
}
public function whereLike($column, $operator = null, $value = null)
{
return $this->where($column, $operator, $value, 'like');
}
public function toSql() // 编译成sql
{
return $this->grammar->compileSql($this);
}
public function getBinds() // 绑定
{
return $this->binds;
}
}
```
## 创建core/database/query/MysqlGrammar
```
<?php
namespace core\database\query;
class MysqlGrammar
{
protected $selectComponents = [
'columns',
'from',
'joins',
'wheres',
'groups',
'havings',
'orders',
'limit',
'offset',
'lock',
];
public function compileSql(QueryBuilder $query)
{
$sql = [];
foreach ($this->selectComponents as $component)
if( isset( $query->{$component}))
$sql[$component] = $this->$component($query, $query->$component);
return implode($sql);
}
protected function columns(QueryBuilder $query,$columns)
{
if(! $columns)
$columns = ['*'];
$select = 'select ';
if ($query->distinct)
$select = 'select distinct ';
return $select . implode(',',$columns);
}
protected function from(QueryBuilder $query,$form)
{
return ' from '.$form;
}
protected function joins()
{
}
protected function wheres(QueryBuilder $queryBuilder,$wheres)
{
if(! $wheres)
return '';
$where_arrs = [];
foreach ($wheres as $index => $where){
if(! $index)
$where['joiner'] = ' where';
$where_arrs[] = sprintf(' %s `%s` %s ?',$where['joiner'], $where['column'], $where['operator']);
}
return implode($where_arrs);
}
protected function groups()
{
}
protected function havings()
{
}
protected function orders()
{
}
protected function limit()
{
}
protected function offset()
{
}
protected function lock()
{
}
}
```
## 编辑core\database\connection\MysqlConnection.php
添加以下代码
```
// 调用不存在的方法 调用一个新的查询构造器
public function __call($method, $parameters)
{
// 返回QueryBuilder类
return $this->newBuilder()->$method(...$parameters);
}
// 创建新的查询器
public function newBuilder()
{
return new QueryBuilder($this, new MysqlGrammar());
}
````
![](https://img.kancloud.cn/bb/cb/bbcbebc939ce6fba81180d9abdfa9347_1308x1011.png)
## 运行
![](https://img.kancloud.cn/39/a1/39a1a9afaeafa608ac0c9b496eace473_758x238.png)
![](https://img.kancloud.cn/b7/38/b7382b992b23c9d0618d02583fcb25f2_804x477.png)
- 前言
- 基础篇
- 1. 第一步 创建框架目录结构
- 2. 引入composer自动加载
- 3. php自动加载 (解释篇)
- 4. 创建容器 注册树模式
- 5. 关于psr规范解释
- 6. 关于"容器" "契约" "依赖注入" (解释篇)
- 7. 添加函数文件helpers.php
- 8. 初始化请求(Request)
- 9. 响应 (Response)
- 10. 路由一 (路由组实现)
- 11. 路由二 (加入中间件)
- 12. 配置信息 (类似laravel)
- 13. 数据库连接 (多例模式)
- 14. 查询构造器 (query builder)
- MVC实现
- M 模型实现 (数据映射 + 原型 模式)
- C 控制器实现 + 控制器中间件
- V 视图实现 (Laravel Blade 引擎)
- V 视图切换成 ThinkPhp 模板 引擎)
- 其他轮子
- 日志
- 自定义异常 (异常托管)
- 单元测试 (phpunit)
- 替换成swoole的http服务器
- 协程上下文解决request问题
- qps测试
- 发布到packagist.org