Skip to content

Commit 8a9beee

Browse files
authored
feat: add support for BackedEnums in query params (#171)
1 parent 77922e4 commit 8a9beee

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/Sql/ValueFormatter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SimPod\ClickHouseClient\Sql;
66

7+
use BackedEnum;
78
use DateTimeImmutable;
89
use DateTimeZone;
910
use SimPod\ClickHouseClient\Exception\UnsupportedValue;
@@ -57,6 +58,12 @@ public function format(mixed $value, string|null $paramName = null, string|null
5758
return 'NULL';
5859
}
5960

61+
if ($value instanceof BackedEnum) {
62+
return is_string($value->value)
63+
? "'" . Escaper::escape($value->value) . "'"
64+
: (string) $value->value;
65+
}
66+
6067
if ($value instanceof DateTimeImmutable) {
6168
if ($this->dateTimeZone !== null) {
6269
$value = $value->setTimezone($this->dateTimeZone);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimPod\ClickHouseClient\Tests\Sql\Fixture;
6+
7+
enum BackedIntEnum: int
8+
{
9+
case A = 0;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimPod\ClickHouseClient\Tests\Sql\Fixture;
6+
7+
enum BackedStringEnum: string
8+
{
9+
case A = 'a';
10+
}

tests/Sql/ValueFormatterTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use SimPod\ClickHouseClient\Exception\UnsupportedValue;
1010
use SimPod\ClickHouseClient\Sql\Expression;
1111
use SimPod\ClickHouseClient\Sql\ValueFormatter;
12+
use SimPod\ClickHouseClient\Tests\Sql\Fixture\BackedIntEnum;
13+
use SimPod\ClickHouseClient\Tests\Sql\Fixture\BackedStringEnum;
1214
use SimPod\ClickHouseClient\Tests\TestCaseBase;
1315
use stdClass;
1416

@@ -94,6 +96,16 @@ public function __toString(): string
9496
}
9597
},
9698
];
99+
100+
yield 'String backed enum' => [
101+
"'a'",
102+
BackedStringEnum::A,
103+
];
104+
105+
yield 'Int backed enum' => [
106+
'0',
107+
BackedIntEnum::A,
108+
];
97109
}
98110

99111
/**

0 commit comments

Comments
 (0)