From cd023d112e6760d6b91ab1217c1ec410b2c5ce7a Mon Sep 17 00:00:00 2001 From: Charlie Waddell Date: Wed, 23 Jun 2021 16:02:07 +0100 Subject: [PATCH 1/2] Add having clauses to the generated Cache Key --- .gitignore | 1 + src/CacheKey.php | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 48b6166..c2d4570 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock .phpunit.result.cache phpunit.xml +.idea diff --git a/src/CacheKey.php b/src/CacheKey.php index c4b985f..a8e2ee8 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -40,6 +40,7 @@ public function make( $key .= $this->getIdColumn($idColumn ?: ""); $key .= $this->getQueryColumns($columns); $key .= $this->getWhereClauses(); + $key .= $this->getHavingClauses(); $key .= $this->getWithModels(); $key .= $this->getOrderByClauses(); $key .= $this->getOffsetClause(); @@ -47,10 +48,31 @@ public function make( $key .= $this->getBindingsSlug(); $key .= $keyDifferentiator; $key .= $this->macroKey; -// dump($key); + return $key; } + protected function getHavingClauses() + { + return Collection::make($this->query->havings)->reduce(function ($carry, $having) { + $value = $carry; + $value .= $this->getHavingClause($having); + + return $value; + }); + } + + protected function getHavingClause(array $having): string + { + $return = '-having'; + + foreach ($having as $key => $value) { + $return .= '_' . $key . '_' . str_replace(' ', '_', $value); + } + + return $return; + } + protected function getIdColumn(string $idColumn) : string { return $idColumn ? "_{$idColumn}" : ""; From 5c0294f3abba5e80339b80f97844f219e1b0efaf Mon Sep 17 00:00:00 2001 From: Charlie Waddell Date: Fri, 27 May 2022 16:14:53 +0100 Subject: [PATCH 2/2] Conflicts --- .gitignore | 1 + src/CacheKey.php | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 48b6166..c2d4570 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock .phpunit.result.cache phpunit.xml +.idea diff --git a/src/CacheKey.php b/src/CacheKey.php index 37ec4a4..8057d48 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -46,6 +46,7 @@ public function make( $key .= $this->getIdColumn($idColumn ?: ""); $key .= $this->getQueryColumns($columns); $key .= $this->getWhereClauses(); + $key .= $this->getHavingClauses(); $key .= $this->getWithModels(); $key .= $this->getOrderByClauses(); $key .= $this->getOffsetClause(); @@ -53,7 +54,7 @@ public function make( $key .= $this->getBindingsSlug(); $key .= $keyDifferentiator; $key .= $this->macroKey; -// dump($key); + return $key; } @@ -88,6 +89,27 @@ protected function getCurrentBinding(string $type, $bindingFallback = null) return data_get($this->query->bindings, "{$type}.{$this->currentBinding}", $bindingFallback); } + protected function getHavingClauses() + { + return Collection::make($this->query->havings)->reduce(function ($carry, $having) { + $value = $carry; + $value .= $this->getHavingClause($having); + + return $value; + }); + } + + protected function getHavingClause(array $having): string + { + $return = '-having'; + + foreach ($having as $key => $value) { + $return .= '_' . $key . '_' . str_replace(' ', '_', $value); + } + + return $return; + } + protected function getIdColumn(string $idColumn) : string { return $idColumn ? "_{$idColumn}" : ""; @@ -332,7 +354,7 @@ protected function getWhereClauses(array $wheres = []) : string return $value; }); } - + protected function getWheres(array $wheres) : Collection { $wheres = collect($wheres); @@ -366,7 +388,7 @@ protected function getWithModels() : string return "{$carry}-{$relatedConnection}:{$relatedDatabase}:{$related}"; }); } - + protected function recursiveImplode(array $items, string $glue = ",") : string { $result = "";