From 8b0f6ead6e4ba7aa8fe24f806b00742a4871f425 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Wed, 4 Oct 2017 13:26:48 -0700 Subject: [PATCH 1/3] Fix nested where clause parsing --- CHANGELOG.md | 4 ++++ src/CachedBuilder.php | 14 ++++++++++++-- tests/Unit/CachedBuilderTest.php | 5 +++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebb05ea..680f659 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.2.5] - 2017-10-04 +### Fixed +- parsing of nested where clauses. + ## [0.2.4] - 2017-10-03 ### Added - .gitignore file to reduce download size for production environment. diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index 07a8e77..02e0b79 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -71,9 +71,19 @@ protected function getQueryColumns(array $columns) : string return '_' . implode('_', $columns); } - protected function getWhereClauses() : string + protected function getWhereClauses(array $wheres = []) : string { - return collect($this->query->wheres)->reduce(function ($carry, $where) { + $wheres = collect($wheres); + + if ($wheres->isEmpty()) { + $wheres = collect($this->query->wheres); + } + + return $wheres->reduce(function ($carry, $where) { + if ($where['type'] === 'Nested') { + return $this->getWhereClauses($where['query']->wheres); + } + $value = array_get($where, 'value'); if (in_array($where['type'], ['In', 'Null', 'NotNull'])) { diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index a3afd12..319d40a 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -463,4 +463,9 @@ public function testLazyLoadingOnResourceIsCached() $this->assertEmpty($books->diffAssoc($cachedResults)); $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } + + public function testNestedRelationshipWhereClauseParsing() + { + $this->markTestIncomplete(); + } } From ba0f9929bef338b223464560d84bee5acc9932c0 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Wed, 4 Oct 2017 13:39:04 -0700 Subject: [PATCH 2/3] Fix where parsing for exists and column clauses --- CHANGELOG.md | 2 +- src/CachedBuilder.php | 7 ++++++- tests/Unit/CachedBuilderTest.php | 13 +++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 680f659..f3f6ea7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [0.2.5] - 2017-10-04 ### Fixed -- parsing of nested where clauses. +- parsing of nested, exists, and column where clauses. ## [0.2.4] - 2017-10-03 ### Added diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index 02e0b79..9c42f0c 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -80,10 +80,14 @@ protected function getWhereClauses(array $wheres = []) : string } return $wheres->reduce(function ($carry, $where) { - if ($where['type'] === 'Nested') { + if (in_array($where['type'], ['Exists', 'Nested'])) { return $this->getWhereClauses($where['query']->wheres); } + if ($where['type'] === 'Column') { + return "{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}"; + } + $value = array_get($where, 'value'); if (in_array($where['type'], ['In', 'Null', 'NotNull'])) { @@ -98,6 +102,7 @@ protected function getWhereClauses(array $wheres = []) : string }) ?: ''; } + protected function getWithModels() : string { $eagerLoads = collect($this->eagerLoad); diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 319d40a..09aec04 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -466,6 +466,19 @@ public function testLazyLoadingOnResourceIsCached() public function testNestedRelationshipWhereClauseParsing() { + // -> with('modelA.modelB') + $this->markTestIncomplete(); + } + + public function testExistsRelationshipWhereClauseParsing() + { + // ->whereHas(...) + $this->markTestIncomplete(); + } + + public function testColumnsRelationshipWhereClauseParsing() + { + // ??? $this->markTestIncomplete(); } } From 54140af88d81f1413f99c3955e45337dd9f10a69 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Wed, 4 Oct 2017 13:55:44 -0700 Subject: [PATCH 3/3] Fix whereRaw parsing --- CHANGELOG.md | 2 +- src/CachedBuilder.php | 7 +++++-- tests/Unit/CachedBuilderTest.php | 6 ++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3f6ea7..21be263 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [0.2.5] - 2017-10-04 ### Fixed -- parsing of nested, exists, and column where clauses. +- parsing of nested, exists, raw, and column where clauses. ## [0.2.4] - 2017-10-03 ### Added diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index 9c42f0c..f01b59c 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -85,7 +85,11 @@ protected function getWhereClauses(array $wheres = []) : string } if ($where['type'] === 'Column') { - return "{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}"; + return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}"; + } + + if ($where['type'] === 'raw') { + return "_{$where['boolean']}_" . str_slug($where['sql']); } $value = array_get($where, 'value'); @@ -102,7 +106,6 @@ protected function getWhereClauses(array $wheres = []) : string }) ?: ''; } - protected function getWithModels() : string { $eagerLoads = collect($this->eagerLoad); diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 09aec04..99a30ff 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -481,4 +481,10 @@ public function testColumnsRelationshipWhereClauseParsing() // ??? $this->markTestIncomplete(); } + + public function testRawWhereClauseParsing() + { + // ->whereRaw(...) + $this->markTestIncomplete(); + } }