// To compile the query, we'll spin through each component of the query and
// see if that component exists. If it does we'll just call the compiler
// function for the component which is responsible for making the SQL.
if (! is_null($query->$component)) {
$method = 'compile'.ucfirst($component);
//var_dump($component,$method,$query->$component,'-------'); //将这些条件打印出来看一下
$sql[$component] = $this->$method($query, $query->$component);
}
}
//dd('over');
return $sql;
}
这个方法内部,将selectComponents属性,也就是查询语句模板,进行了遍历,并判断出了,在$query对象中所存在的那一部分。通过这些语句,来构建sql语句片段。这个模板如下所示。
protected $selectComponents = [
'aggregate',
'columns',
'from',
'joins',
'wheres',
'groups',
'havings',
'orders',
'limit',
'offset',
'unions',
'lock',
];
而$query对象中所存在的部分,将它们打印后,结果如下所示。通过我上面代码段中被注释的部分,将其打印了出来,我在下图中对三个属性做了注释。
总结来讲,这个方法会根据builder对象中所存储的属性,运行模板方法,将其构建成sql字符串部件。而builder对象中的属性则是我们自己通过DB或Model方法添加进去的。
那么我们刚刚那句简单的sql查询则是运行了compileColumns、compileFrom、compileWheres。这三个方法。
protected function compileColumns(Builder $query, $columns)
{
// If the query is actually performing an aggregating select, we will let that
// compiler handle the building of the select clauses, as it will need some
// more syntax that is best handled by that function to keep things neat.
if (! is_null($query->aggregate)) {
return;
}
$select = $query->distinct ? 'select distinct ' : 'select ';
return $select.$this->columnize($columns);
}
public function columnize(array $columns)
{
return implode(', ', array_map([$this, 'wrap'], $columns));
}
先来看compileColumns,这个方法看上去很简单,判断aggregate不为空后,根据distinct 属性来得出sql语句头,然后将这个字符串与$this->columnize()方法的返回值进行拼接。就得出了上面'select *'这句字符串。而关键在于columnize方法中的array_map的[$this, 'wrap']。
array_map这个函数会传入两个参数,第一个参数为函数名,第二个参数为数组。将第二个数组参数中的每个值当成参数,传入第一个参数所代表的函数中循环执行。
那么现在我们要找到wrap这个方法了。
public function wrap($value, $prefixAlias = false)
{
if ($this->isExpression($value)) {
return $this->getValue($value);
}
// If the value being wrapped has a column alias we will need to separate out
// the pieces so we can wrap each of the segments of the expression on it
// own, and then joins them both back together with the "as" connector.
if (strpos(strtolower($value), ' as ') !== false) {
return $this->wrapAliasedValue($value, $prefixAlias);
}
return $this->wrapSegments(explode('.', $value));
}
这个方法,首先判断了传入参数不是一个表达式,而是一个确定的值。然后strpos(strtolower($value), ' as ') !== false这一句将$value转为小写,并判断了sql语句中没有as字段。然后便返回了$this->wrapSegments的值。
protected function wrapSegments($segments)
{
return collect($segments)->map(function ($segment, $key) use ($segments) {
return $key == 0 && count($segments) > 1
? $this->wrapTable($segment)
: $this->wrapValue($segment);
})->implode('.');
}
到这里,我们会发现这个方法,只是传入了一个闭包函数,就给返回了,laravel框架实在是难以跟踪。
事实上collect()方法代表了\vendor\laravel\framework\src\Illuminate\Support\Collection.php对象。
可以看到在collection类的构造方法中,我们将参数存入了它的属性,而在map方法中,通过array_keys对这些属性做了处理过后,又通过array_map对其进了加工。看下刚刚wrapSegments中的闭包函数是怎么写的,他们调用了wrapTable()和wrapValue这两个方法。根据传入参数的不同,来分别调用。
public function __construct($i