Skip to content

Commit efaeb5f

Browse files
authored
Merge pull request #409 from dmason30/false-query-binding
[bug] Fix current binding count when previous binding is false
2 parents 9881f1c + 66296ac commit efaeb5f

File tree

4 files changed

+194
-127
lines changed

4 files changed

+194
-127
lines changed

src/CacheKey.php

+132-124
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,66 @@ public function make(
5151
return $key;
5252
}
5353

54+
protected function getBindingsSlug() : string
55+
{
56+
if (! method_exists($this->model, 'query')) {
57+
return '';
58+
}
59+
60+
return Arr::query($this->model->query()->getBindings());
61+
}
62+
63+
protected function getColumnClauses(array $where) : string
64+
{
65+
if ($where["type"] !== "Column") {
66+
return "";
67+
}
68+
69+
return "-{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}";
70+
}
71+
72+
protected function getCurrentBinding(string $type, $bindingFallback = null)
73+
{
74+
return data_get($this->query->bindings, "{$type}.{$this->currentBinding}", $bindingFallback);
75+
}
76+
5477
protected function getIdColumn(string $idColumn) : string
5578
{
5679
return $idColumn ? "_{$idColumn}" : "";
5780
}
5881

82+
protected function getInAndNotInClauses(array $where) : string
83+
{
84+
if (! in_array($where["type"], ["In", "NotIn", "InRaw"])) {
85+
return "";
86+
}
87+
88+
$type = strtolower($where["type"]);
89+
$subquery = $this->getValuesFromWhere($where);
90+
$values = collect($this->getCurrentBinding('where', []));
91+
92+
if (Str::startsWith($subquery, $values->first())) {
93+
$this->currentBinding += count($where["values"]);
94+
}
95+
96+
if (! is_numeric($subquery) && ! is_numeric(str_replace("_", "", $subquery))) {
97+
try {
98+
$subquery = Uuid::fromBytes($subquery);
99+
$values = $this->recursiveImplode([$subquery], "_");
100+
101+
return "-{$where["column"]}_{$type}{$values}";
102+
} catch (Exception $exception) {
103+
// do nothing
104+
}
105+
}
106+
107+
$subquery = preg_replace('/\?(?=(?:[^"]*"[^"]*")*[^"]*\Z)/m', "_??_", $subquery);
108+
$subquery = collect(vsprintf(str_replace("_??_", "%s", $subquery), $values->toArray()));
109+
$values = $this->recursiveImplode($subquery->toArray(), "_");
110+
111+
return "-{$where["column"]}_{$type}{$values}";
112+
}
113+
59114
protected function getLimitClause() : string
60115
{
61116
if (! property_exists($this->query, "limit")
@@ -67,15 +122,18 @@ protected function getLimitClause() : string
67122
return "-limit_{$this->query->limit}";
68123
}
69124

70-
protected function getTableSlug() : string
125+
protected function getModelSlug() : string
71126
{
72-
return (new Str)->slug($this->query->from)
73-
. ":";
127+
return (new Str)->slug(get_class($this->model));
74128
}
75129

76-
protected function getModelSlug() : string
130+
protected function getNestedClauses(array $where) : string
77131
{
78-
return (new Str)->slug(get_class($this->model));
132+
if (! in_array($where["type"], ["Exists", "Nested", "NotExists"])) {
133+
return "";
134+
}
135+
136+
return "-" . strtolower($where["type"]) . $this->getWhereClauses($where["query"]->wheres);
79137
}
80138

81139
protected function getOffsetClause() : string
@@ -110,6 +168,18 @@ protected function getOrderByClauses() : string
110168
?: "";
111169
}
112170

171+
protected function getOtherClauses(array $where) : string
172+
{
173+
if (in_array($where["type"], ["Exists", "Nested", "NotExists", "Column", "raw", "In", "NotIn", "InRaw"])) {
174+
return "";
175+
}
176+
177+
$value = $this->getTypeClause($where);
178+
$value .= $this->getValuesClause($where);
179+
180+
return "-{$where["column"]}_{$value}";
181+
}
182+
113183
protected function getQueryColumns(array $columns) : string
114184
{
115185
if (($columns === ["*"]
@@ -129,6 +199,36 @@ protected function getQueryColumns(array $columns) : string
129199
return "_" . implode("_", $columns);
130200
}
131201

202+
protected function getRawClauses(array $where) : string
203+
{
204+
if (! in_array($where["type"], ["raw"])) {
205+
return "";
206+
}
207+
208+
$queryParts = explode("?", $where["sql"]);
209+
$clause = "_{$where["boolean"]}";
210+
211+
while (count($queryParts) > 1) {
212+
$clause .= "_" . array_shift($queryParts);
213+
$clause .= $this->getCurrentBinding("where");
214+
$this->currentBinding++;
215+
}
216+
217+
$lastPart = array_shift($queryParts);
218+
219+
if ($lastPart) {
220+
$clause .= "_" . $lastPart;
221+
}
222+
223+
return "-" . str_replace(" ", "_", $clause);
224+
}
225+
226+
protected function getTableSlug() : string
227+
{
228+
return (new Str)->slug($this->query->from)
229+
. ":";
230+
}
231+
132232
protected function getTypeClause($where) : string
133233
{
134234
$type = in_array($where["type"], ["InRaw", "In", "NotIn", "Null", "NotNull", "between", "NotInSub", "InSub", "JsonContains"])
@@ -174,12 +274,15 @@ protected function getValuesFromWhere(array $where) : string
174274

175275
protected function getValuesFromBindings(array $where, string $values) : string
176276
{
177-
if (($this->query->bindings["where"][$this->currentBinding] ?? false) !== false) {
178-
$values = $this->query->bindings["where"][$this->currentBinding];
277+
$bindingFallback = __CLASS__ . ':UNKNOWN_BINDING';
278+
$currentBinding = $this->getCurrentBinding("where", $bindingFallback);
279+
280+
if ($currentBinding !== $bindingFallback) {
281+
$values = $currentBinding;
179282
$this->currentBinding++;
180283

181284
if ($where["type"] === "between") {
182-
$values .= "_" . $this->query->bindings["where"][$this->currentBinding];
285+
$values .= "_" . $this->getCurrentBinding("where");
183286
$this->currentBinding++;
184287
}
185288
}
@@ -207,57 +310,41 @@ protected function getWhereClauses(array $wheres = []) : string
207310
return $value;
208311
});
209312
}
210-
211-
protected function getNestedClauses(array $where) : string
313+
314+
protected function getWheres(array $wheres) : Collection
212315
{
213-
if (! in_array($where["type"], ["Exists", "Nested", "NotExists"])) {
214-
return "";
215-
}
216-
217-
return "-" . strtolower($where["type"]) . $this->getWhereClauses($where["query"]->wheres);
218-
}
316+
$wheres = collect($wheres);
219317

220-
protected function getColumnClauses(array $where) : string
221-
{
222-
if ($where["type"] !== "Column") {
223-
return "";
318+
if ($wheres->isEmpty()
319+
&& property_exists($this->query, "wheres")
320+
) {
321+
$wheres = collect($this->query->wheres);
224322
}
225323

226-
return "-{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}";
324+
return $wheres;
227325
}
228326

229-
protected function getInAndNotInClauses(array $where) : string
327+
protected function getWithModels() : string
230328
{
231-
if (! in_array($where["type"], ["In", "NotIn", "InRaw"])) {
232-
return "";
233-
}
234-
235-
$type = strtolower($where["type"]);
236-
$subquery = $this->getValuesFromWhere($where);
237-
$values = collect($this->query->bindings["where"][$this->currentBinding] ?? []);
329+
$eagerLoads = collect($this->eagerLoad);
238330

239-
if (Str::startsWith($subquery, $values->first())) {
240-
$this->currentBinding += count($where["values"]);
331+
if ($eagerLoads->isEmpty()) {
332+
return "";
241333
}
242334

243-
if (! is_numeric($subquery) && ! is_numeric(str_replace("_", "", $subquery))) {
244-
try {
245-
$subquery = Uuid::fromBytes($subquery);
246-
$values = $this->recursiveImplode([$subquery], "_");
247-
248-
return "-{$where["column"]}_{$type}{$values}";
249-
} catch (Exception $exception) {
250-
// do nothing
335+
return $eagerLoads->keys()->reduce(function ($carry, $related) {
336+
if (! method_exists($this->model, $related)) {
337+
return "{$carry}-{$related}";
251338
}
252-
}
253339

254-
$subquery = preg_replace('/\?(?=(?:[^"]*"[^"]*")*[^"]*\Z)/m', "_??_", $subquery);
255-
$subquery = collect(vsprintf(str_replace("_??_", "%s", $subquery), $values->toArray()));
256-
$values = $this->recursiveImplode($subquery->toArray(), "_");
340+
$relatedModel = $this->model->$related()->getRelated();
341+
$relatedConnection = $relatedModel->getConnection()->getName();
342+
$relatedDatabase = $relatedModel->getConnection()->getDatabaseName();
257343

258-
return "-{$where["column"]}_{$type}{$values}";
344+
return "{$carry}-{$relatedConnection}:{$relatedDatabase}:{$related}";
345+
});
259346
}
260-
347+
261348
protected function recursiveImplode(array $items, string $glue = ",") : string
262349
{
263350
$result = "";
@@ -283,83 +370,4 @@ protected function recursiveImplode(array $items, string $glue = ",") : string
283370

284371
return $result;
285372
}
286-
287-
protected function getRawClauses(array $where) : string
288-
{
289-
if (! in_array($where["type"], ["raw"])) {
290-
return "";
291-
}
292-
293-
$queryParts = explode("?", $where["sql"]);
294-
$clause = "_{$where["boolean"]}";
295-
296-
while (count($queryParts) > 1) {
297-
$clause .= "_" . array_shift($queryParts);
298-
$clause .= $this->query->bindings["where"][$this->currentBinding];
299-
$this->currentBinding++;
300-
}
301-
302-
$lastPart = array_shift($queryParts);
303-
304-
if ($lastPart) {
305-
$clause .= "_" . $lastPart;
306-
}
307-
308-
return "-" . str_replace(" ", "_", $clause);
309-
}
310-
311-
protected function getOtherClauses(array $where) : string
312-
{
313-
if (in_array($where["type"], ["Exists", "Nested", "NotExists", "Column", "raw", "In", "NotIn", "InRaw"])) {
314-
return "";
315-
}
316-
317-
$value = $this->getTypeClause($where);
318-
$value .= $this->getValuesClause($where);
319-
320-
return "-{$where["column"]}_{$value}";
321-
}
322-
323-
protected function getWheres(array $wheres) : Collection
324-
{
325-
$wheres = collect($wheres);
326-
327-
if ($wheres->isEmpty()
328-
&& property_exists($this->query, "wheres")
329-
) {
330-
$wheres = collect($this->query->wheres);
331-
}
332-
333-
return $wheres;
334-
}
335-
336-
protected function getWithModels() : string
337-
{
338-
$eagerLoads = collect($this->eagerLoad);
339-
340-
if ($eagerLoads->isEmpty()) {
341-
return "";
342-
}
343-
344-
return $eagerLoads->keys()->reduce(function ($carry, $related) {
345-
if (! method_exists($this->model, $related)) {
346-
return "{$carry}-{$related}";
347-
}
348-
349-
$relatedModel = $this->model->$related()->getRelated();
350-
$relatedConnection = $relatedModel->getConnection()->getName();
351-
$relatedDatabase = $relatedModel->getConnection()->getDatabaseName();
352-
353-
return "{$carry}-{$relatedConnection}:{$relatedDatabase}:{$related}";
354-
});
355-
}
356-
357-
protected function getBindingsSlug() : string
358-
{
359-
if (! method_exists($this->model, 'query')) {
360-
return '';
361-
}
362-
363-
return Arr::query($this->model->query()->getBindings());
364-
}
365373
}

0 commit comments

Comments
 (0)