FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge branch 'cleanup' of https://github.com/acasar/framework into ac… · JavaDevl/framework@ccb1fef · GitHub

Commit ccb1fef

Browse files
committed
Merge branch 'cleanup' of https://github.com/acasar/framework into acasar-cleanup
2 parents 96d6a4c + c041e5d commit ccb1fef

3 files changed

Lines changed: 53 additions & 30 deletions

File tree

‎src/Illuminate/Database/Eloquent/Builder.php‎

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ public function onDelete(Closure $callback)
463463
*/
464464
public function getModels($columns = ['*'])
465465
{
466-
$results = $this->applyScopes()->getQuery()->get($columns);
466+
$results = $this->toBase()->get($columns);
467467

468468
$connection = $this->model->getConnectionName();
469469

@@ -892,8 +892,8 @@ protected function callScope($scope, $parameters)
892892

893893
$result = call_user_func_array([$this->model, $scope], $parameters) ?: $this;
894894

895-
if ($this->shouldNestWheresForScope($originalWhereCount, $query)) {
896-
$this->nestWheresForScope($query, [0, $originalWhereCount, count($query->wheres)]);
895+
if ($this->shouldNestWheresForScope($query, $originalWhereCount)) {
896+
$this->nestWheresForScope($query, $originalWhereCount);
897897
}
898898

899899
return $result;
@@ -919,7 +919,7 @@ public function applyScopes()
919919
// query as their own isolated nested where statement and avoid issues.
920920
$originalWhereCount = count($query->wheres);
921921

922-
$whereCounts = [0, $originalWhereCount];
922+
$whereCounts = [$originalWhereCount];
923923

924924
foreach ($this->scopes as $scope) {
925925
$this->applyScope($scope, $builder);
@@ -930,8 +930,8 @@ public function applyScopes()
930930
$whereCounts[] = count($query->wheres);
931931
}
932932

933-
if ($this->shouldNestWheresForScope($originalWhereCount, $query)) {
934-
$this->nestWheresForScope($query, array_unique($whereCounts));
933+
if ($this->shouldNestWheresForScope($query, $originalWhereCount)) {
934+
$this->nestWheresForScope($query, $whereCounts);
935935
}
936936

937937
return $builder;
@@ -956,23 +956,23 @@ protected function applyScope($scope, $builder)
956956
/**
957957
* Determine if the scope added after the given offset should be nested.
958958
*
959-
* @param int $originalWhereCount
960959
* @param \Illuminate\Database\Query\Builder $query
960+
* @param int $originalWhereCount
961961
* @return bool
962962
*/
963-
protected function shouldNestWheresForScope($originalWhereCount, QueryBuilder $query)
963+
protected function shouldNestWheresForScope(QueryBuilder $query, $originalWhereCount)
964964
{
965965
return $originalWhereCount && count($query->wheres) > $originalWhereCount;
966966
}
967967

968968
/**
969-
* Nest where conditions of the builder and each global scope.
969+
* Nest where conditions by slicing them at the given where count.
970970
*
971971
* @param \Illuminate\Database\Query\Builder $query
972-
* @param array $offsets
972+
* @param int|array $whereCounts
973973
* @return void
974974
*/
975-
protected function nestWheresForScope(QueryBuilder $query, array $whereCounts)
975+
protected function nestWheresForScope(QueryBuilder $query, $whereCounts)
976976
{
977977
// Here, we totally remove all of the where clauses since we are going to
978978
// rebuild them as nested queries by slicing the groups of wheres into
@@ -981,33 +981,56 @@ protected function nestWheresForScope(QueryBuilder $query, array $whereCounts)
981981

982982
$query->wheres = [];
983983

984-
// We will take the first offset (typically 0) of where clauses and start
985-
// slicing out every scope's where clauses into their own nested where
986-
// groups for improved isolation of every scope's added constraints.
987-
$previousCount = array_shift($whereCounts);
984+
// We will construct where offsets by adding the outer most offsets to the
985+
// collection (0 and total where count) while also flattening the array
986+
// and extracting unique values, ensuring that all wheres are sliced.
987+
$whereOffsets = collect([0, $whereCounts, count($allWheres)])->flatten()->unique();
988+
989+
$sliceFrom = $whereOffsets->shift();
990+
991+
foreach ($whereOffsets as $sliceTo) {
992+
$this->sliceWhereConditions($query, $allWheres, $sliceFrom, $sliceTo);
988993

989-
foreach ($whereCounts as $whereCount) {
990-
$query->wheres[] = $this->sliceWhereConditions(
991-
$allWheres, $previousCount, $whereCount - $previousCount
992-
);
994+
$sliceFrom = $sliceTo;
995+
}
996+
}
993997

994-
$previousCount = $whereCount;
998+
/**
999+
* Create a slice of where conditions at the given offsets and nest them if needed.
1000+
*
1001+
* @param \Illuminate\Database\Query\Builder $query
1002+
* @param array $wheres
1003+
* @param int $sliceFrom
1004+
* @param int $sliceTo
1005+
* @return void
1006+
*/
1007+
protected function sliceWhereConditions(QueryBuilder $query, array $wheres, $sliceFrom, $sliceTo)
1008+
{
1009+
$whereSlice = array_slice($wheres, $sliceFrom, $sliceTo - $sliceFrom);
1010+
1011+
$whereBooleans = collect($whereSlice)->pluck('boolean');
1012+
1013+
// Here we'll check if the given subset of where clauses contains any "or"
1014+
// booleans and in this case create a nested where expression. That way
1015+
// we don't add any unnecessary nesting thus keeping the query clean.
1016+
if ($whereBooleans->contains('or')) {
1017+
$query->wheres[] = $this->nestWhereSlice($whereSlice);
1018+
} else {
1019+
$query->wheres = array_merge($query->wheres, $whereSlice);
9951020
}
9961021
}
9971022

9981023
/**
999-
* Create a where array with sliced where conditions.
1024+
* Create a where array with nested where conditions.
10001025
*
1001-
* @param array $allWheres
1002-
* @param int $offset
1003-
* @param int $length
1026+
* @param array $whereSubset
10041027
* @return array
10051028
*/
1006-
protected function sliceWhereConditions($allWheres, $offset, $length)
1029+
protected function nestWhereSlice($whereSubset)
10071030
{
10081031
$whereGroup = $this->getQuery()->forNestedWhere();
10091032

1010-
$whereGroup->wheres = array_slice($allWheres, $offset, $length);
1033+
$whereGroup->wheres = $whereSubset;
10111034

10121035
return ['type' => 'Nested', 'query' => $whereGroup, 'boolean' => 'and'];
10131036
}
@@ -1136,7 +1159,7 @@ public function __call($method, $parameters)
11361159
}
11371160

11381161
if (in_array($method, $this->passthru)) {
1139-
return call_user_func_array([$this->applyScopes()->getQuery(), $method], $parameters);
1162+
return call_user_func_array([$this->toBase(), $method], $parameters);
11401163
}
11411164

11421165
call_user_func_array([$this->query, $method], $parameters);

‎tests/Database/DatabaseEloquentBuilderTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public function testRealNestedWhereWithScopes()
399399
$model = new EloquentBuilderTestNestedStub;
400400
$this->mockConnectionForModel($model, 'SQLite');
401401
$query = $model->newQuery()->where('foo', '=', 'bar')->where(function ($query) { $query->where('baz', '>', 9000); });
402-
$this->assertEquals('select * from "table" where ("foo" = ? and ("baz" > ?)) and ("table"."deleted_at" is null)', $query->toSql());
402+
$this->assertEquals('select * from "table" where "foo" = ? and ("baz" > ?) and "table"."deleted_at" is null', $query->toSql());
403403
$this->assertEquals(['bar', 9000], $query->getBindings());
404404
}
405405

@@ -408,7 +408,7 @@ public function testRealNestedWhereWithMultipleScopesAndOneDeadScope()
408408
$model = new EloquentBuilderTestNestedStub;
409409
$this->mockConnectionForModel($model, 'SQLite');
410410
$query = $model->newQuery()->empty()->where('foo', '=', 'bar')->empty()->where(function ($query) { $query->empty()->where('baz', '>', 9000); });
411-
$this->assertEquals('select * from "table" where ("foo" = ? and ("baz" > ?)) and ("table"."deleted_at" is null)', $query->toSql());
411+
$this->assertEquals('select * from "table" where "foo" = ? and ("baz" > ?) and "table"."deleted_at" is null', $query->toSql());
412412
$this->assertEquals(['bar', 9000], $query->getBindings());
413413
}
414414

‎tests/Database/DatabaseEloquentGlobalScopesTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testGlobalScopesWithOrWhereConditionsAreNested()
7070
$model = new EloquentClosureGlobalScopesWithOrTestModel();
7171

7272
$query = $model->newQuery()->where('col1', 'val1')->orWhere('col2', 'val2');
73-
$this->assertEquals('select "email", "password" from "table" where ("col1" = ? or "col2" = ?) and ("email" = ? or "email" = ?) and ("active" = ?) order by "name" asc', $query->toSql());
73+
$this->assertEquals('select "email", "password" from "table" where ("col1" = ? or "col2" = ?) and ("email" = ? or "email" = ?) and "active" = ? order by "name" asc', $query->toSql());
7474
$this->assertEquals(['val1', 'val2', 'taylor@gmail.com', 'someone@else.com', 1], $query->getBindings());
7575
}
7676

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL