Skip to content

Commit e260255

Browse files
github-actions[bot]claude
authored andcommitted
refactor(sql): extract getSQLConditionsForCollection helper
The find/count/sum methods all repeated the same try/finally block to set $currentQueryCollection around getSQLConditions(). Three identical copies of an exception-safety pattern is one mistake away from a leak the next time the pattern is added — extract into a single helper so the set/reset is in one place. No behaviour change. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent dc321de commit e260255

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

src/Database/Adapter/SQL.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ protected function getFloatPrecision(float $value): string
5959
return sprintf('%.'. $this->floatPrecision . 'F', $value);
6060
}
6161

62+
/**
63+
* Build SQL conditions for a query while exposing the active collection
64+
* via $currentQueryCollection. The state is always reset, even when
65+
* getSQLConditions throws — adapter overrides that read it (e.g.
66+
* SQLite's FTS5 routing) rely on it being absent outside this scope.
67+
*
68+
* @param array<Query> $queries
69+
* @param array<string,mixed> $binds
70+
*/
71+
protected function getSQLConditionsForCollection(string $name, array $queries, array &$binds, string $separator = 'AND'): string
72+
{
73+
$this->currentQueryCollection = $name;
74+
try {
75+
return $this->getSQLConditions($queries, $binds, $separator);
76+
} finally {
77+
$this->currentQueryCollection = null;
78+
}
79+
}
80+
6281
/**
6382
* Constructor.
6483
*
@@ -3165,12 +3184,7 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
31653184
$where[] = '(' . implode(' OR ', $cursorWhere) . ')';
31663185
}
31673186

3168-
$this->currentQueryCollection = $name;
3169-
try {
3170-
$conditions = $this->getSQLConditions($queries, $binds);
3171-
} finally {
3172-
$this->currentQueryCollection = null;
3173-
}
3187+
$conditions = $this->getSQLConditionsForCollection($name, $queries, $binds);
31743188
if (!empty($conditions)) {
31753189
$where[] = $conditions;
31763190
}
@@ -3314,12 +3328,7 @@ public function count(Document $collection, array $queries = [], ?int $max = nul
33143328
}
33153329
}
33163330

3317-
$this->currentQueryCollection = $name;
3318-
try {
3319-
$conditions = $this->getSQLConditions($otherQueries, $binds);
3320-
} finally {
3321-
$this->currentQueryCollection = null;
3322-
}
3331+
$conditions = $this->getSQLConditionsForCollection($name, $otherQueries, $binds);
33233332
if (!empty($conditions)) {
33243333
$where[] = $conditions;
33253334
}
@@ -3405,12 +3414,7 @@ public function sum(Document $collection, string $attribute, array $queries = []
34053414
}
34063415
}
34073416

3408-
$this->currentQueryCollection = $name;
3409-
try {
3410-
$conditions = $this->getSQLConditions($otherQueries, $binds);
3411-
} finally {
3412-
$this->currentQueryCollection = null;
3413-
}
3417+
$conditions = $this->getSQLConditionsForCollection($name, $otherQueries, $binds);
34143418
if (!empty($conditions)) {
34153419
$where[] = $conditions;
34163420
}

0 commit comments

Comments
 (0)