Skip to content

Commit b6c260d

Browse files
committed
fix: Update Database adapter to support lessThanEqual and greaterThanEqual query types
1 parent a89db31 commit b6c260d

2 files changed

Lines changed: 47 additions & 21 deletions

File tree

src/Usage/Adapter/ClickHouse.php

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,6 @@ private function formatDateTime($dateTime): string
523523
}
524524
}
525525

526-
// This is unreachable code but kept for completeness - all valid types are handled above
527-
// @phpstan-ignore-next-line
528-
throw new Exception('DateTime must be a DateTime object or string');
529526
}
530527

531528
/**
@@ -1003,11 +1000,7 @@ private function parseQueries(array $queries): array
10031000
$paramCounter = 0;
10041001

10051002
foreach ($queries as $query) {
1006-
if (!$query instanceof Query) {
1007-
/** @phpstan-ignore-next-line ternary.alwaysTrue - runtime validation despite type hint */
1008-
$type = is_object($query) ? get_class($query) : gettype($query);
1009-
throw new \InvalidArgumentException("Invalid query item: expected instance of Query, got {$type}");
1010-
}
1003+
10111004

10121005
$method = $query->getMethod();
10131006
$attribute = $query->getAttribute();
@@ -1019,33 +1012,44 @@ private function parseQueries(array $queries): array
10191012
$escapedAttr = $this->escapeIdentifier($attribute);
10201013

10211014
// Support arrays of values (produce IN (...) ) or single value equality
1022-
if (is_array($values)) {
1015+
if (count($values) > 1) {
1016+
/** @var array<mixed> $arrayValues */
1017+
$arrayValues = $values;
10231018
$inParams = [];
1024-
foreach ($values as $value) {
1019+
foreach ($arrayValues as $value) {
10251020
$paramName = 'param_' . $paramCounter++;
10261021
if ($attribute === 'time') {
10271022
$inParams[] = "{{$paramName}:DateTime64(3)}";
1028-
$params[$paramName] = $this->formatDateTime($value);
1023+
/** @var \DateTime|string|null $timeValue */
1024+
$timeValue = $value;
1025+
$params[$paramName] = $this->formatDateTime($timeValue);
10291026
} else {
10301027
$inParams[] = "{{$paramName}:String}";
1031-
$params[$paramName] = $this->formatParamValue($value);
1028+
/** @var bool|float|int|string $scalarValue */
1029+
$scalarValue = $value;
1030+
$params[$paramName] = $this->formatParamValue($scalarValue);
10321031
}
10331032
}
10341033

1035-
if (count($inParams) === 1) {
1034+
/** @var int $inParamCount */
1035+
$inParamCount = count($inParams);
1036+
if ($inParamCount === 1) {
10361037
$filters[] = "{$escapedAttr} = " . $inParams[0];
10371038
} else {
10381039
$filters[] = "{$escapedAttr} IN (" . implode(', ', $inParams) . ")";
10391040
}
10401041
} else {
10411042
$paramName = 'param_' . $paramCounter++;
10421043
if ($attribute === 'time') {
1044+
/** @var array<\DateTime|string|null> $values */
1045+
$formattedValue = $this->formatDateTime($values[0]);
10431046
$filters[] = "{$escapedAttr} = {{$paramName}:DateTime64(3)}";
1044-
$params[$paramName] = $this->formatDateTime($values);
10451047
} else {
1048+
/** @var bool|float|int|string $formattedValue */
1049+
$formattedValue = $this->formatParamValue($values[0]);
10461050
$filters[] = "{$escapedAttr} = {{$paramName}:String}";
1047-
$params[$paramName] = $this->formatParamValue($values);
10481051
}
1052+
$params[$paramName] = $formattedValue;
10491053
}
10501054
break;
10511055

@@ -1119,10 +1123,14 @@ private function parseQueries(array $queries): array
11191123
$paramName = 'param_' . $paramCounter++;
11201124
if ($attribute === 'time') {
11211125
$inParams[] = "{{$paramName}:DateTime64(3)}";
1122-
$params[$paramName] = $this->formatDateTime($value);
1126+
/** @var \DateTime|string|null $singleValue */
1127+
$singleValue = $value;
1128+
$params[$paramName] = $this->formatDateTime($singleValue);
11231129
} else {
11241130
$inParams[] = "{{$paramName}:String}";
1125-
$params[$paramName] = $this->formatParamValue($value);
1131+
/** @var bool|float|int|string $singleValue */
1132+
$singleValue = $value;
1133+
$params[$paramName] = $this->formatParamValue($singleValue);
11261134
}
11271135
}
11281136
if (!empty($inParams)) {
@@ -1135,11 +1143,19 @@ private function parseQueries(array $queries): array
11351143
$escapedAttr = $this->escapeIdentifier($attribute);
11361144
$paramName = 'param_' . $paramCounter++;
11371145
if ($attribute === 'time') {
1146+
if (is_array($values)) {
1147+
/** @var \DateTime|string|null $singleValue */
1148+
$singleValue = $values[0] ?? null;
1149+
}
11381150
$filters[] = "{$escapedAttr} <= {{$paramName}:DateTime64(3)}";
1139-
$params[$paramName] = $this->formatDateTime(is_array($values) ? ($values[0] ?? null) : $values);
1151+
$params[$paramName] = $this->formatDateTime($singleValue);
11401152
} else {
1153+
if (is_array($values)) {
1154+
/** @var bool|float|int|string $singleValue */
1155+
$singleValue = $values[0] ?? null;
1156+
}
11411157
$filters[] = "{$escapedAttr} <= {{$paramName}:String}";
1142-
$params[$paramName] = $this->formatParamValue(is_array($values) ? ($values[0] ?? null) : $values);
1158+
$params[$paramName] = $this->formatParamValue($singleValue);
11431159
}
11441160
break;
11451161

@@ -1148,11 +1164,19 @@ private function parseQueries(array $queries): array
11481164
$escapedAttr = $this->escapeIdentifier($attribute);
11491165
$paramName = 'param_' . $paramCounter++;
11501166
if ($attribute === 'time') {
1167+
if (is_array($values)) {
1168+
/** @var \DateTime|string|null $singleValue */
1169+
$singleValue = $values[0] ?? null;
1170+
}
11511171
$filters[] = "{$escapedAttr} >= {{$paramName}:DateTime64(3)}";
1152-
$params[$paramName] = $this->formatDateTime(is_array($values) ? ($values[0] ?? null) : $values);
1172+
$params[$paramName] = $this->formatDateTime($singleValue);
11531173
} else {
1174+
if (is_array($values)) {
1175+
/** @var bool|float|int|string $singleValue */
1176+
$singleValue = $values[0] ?? null;
1177+
}
11541178
$filters[] = "{$escapedAttr} >= {{$paramName}:String}";
1155-
$params[$paramName] = $this->formatParamValue(is_array($values) ? ($values[0] ?? null) : $values);
1179+
$params[$paramName] = $this->formatParamValue($singleValue);
11561180
}
11571181
break;
11581182

src/Usage/Adapter/Database.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ private function convertQueriesToDatabase(array $queries): array
188188
break;
189189
case Query::TYPE_LESSER_EQUAL:
190190
if (!empty($values)) {
191+
/** @var bool|float|int|string $value */
191192
$value = $values[0];
192193
$dbQueries[] = DatabaseQuery::lessThanEqual($attribute, $value);
193194
}
194195
break;
195196
case Query::TYPE_GREATER_EQUAL:
196197
if (!empty($values)) {
198+
/** @var bool|float|int|string $value */
197199
$value = $values[0];
198200
$dbQueries[] = DatabaseQuery::greaterThanEqual($attribute, $value);
199201
}

0 commit comments

Comments
 (0)