Skip to content

Commit 5f4fe72

Browse files
committed
UInt64 value wrapper
Resolves #80
1 parent 809b5fc commit 5f4fe72

File tree

7 files changed

+172
-2
lines changed

7 files changed

+172
-2
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ $stat = $db->insert('summing_url_views',
102102
);
103103
```
104104

105+
If you need to insert UInt64 value, you can wrap the value in `ClickHouseDB\Type\UInt64` DTO.
106+
107+
```php
108+
$statement = $db->insert('table_name',
109+
[
110+
[time(), UInt64::fromString('18446744073709551615')],
111+
],
112+
['event_time', 'uint64_type_column']
113+
);
114+
UInt64::fromString('18446744073709551615')
115+
```
116+
105117
Select:
106118
```php
107119
$statement = $db->select('SELECT * FROM summing_url_views LIMIT 2');
@@ -915,4 +927,4 @@ MIT
915927
ChangeLog
916928
---------
917929

918-
See [changeLog.md](CHANGELOG.md)
930+
See [changeLog.md](CHANGELOG.md)

src/Quote/StrictQuoteLine.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace ClickHouseDB\Quote;
33

44
use ClickHouseDB\Exception\QueryException;
5+
use ClickHouseDB\Type\NumericType;
56
use function array_map;
67
use function is_array;
78
use function is_float;
@@ -69,6 +70,11 @@ public function quoteValue($row)
6970

7071
$encode_esc = preg_quote($encode, '/');
7172

73+
$encode = true;
74+
if ($value instanceof NumericType) {
75+
$encode = false;
76+
}
77+
7278
if (is_array($value)) {
7379
// Arrays are formatted as a list of values separated by commas in square brackets.
7480
// Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in
@@ -91,7 +97,7 @@ function ($v) use ($enclosure_esc, $encode_esc) {
9197
return (string) $value;
9298
}
9399

94-
if (is_string($value)) {
100+
if (is_string($value) && $encode) {
95101
if ($tabEncode) {
96102
return str_replace(["\t", "\n"], ['\\t', '\\n'], $value);
97103
}

src/Quote/ValueFormatter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace ClickHouseDB\Quote;
66

77
use ClickHouseDB\Exception\UnsupportedValueType;
8+
use ClickHouseDB\Type\Type;
89
use DateTimeInterface;
910
use function addslashes;
1011
use function is_bool;
@@ -31,6 +32,10 @@ public static function formatValue($value, bool $addQuotes = true)
3132
return $value;
3233
}
3334

35+
if ($value instanceof Type) {
36+
return $value->getValue();
37+
}
38+
3439
if (is_object($value) && is_callable([$value, '__toString'])) {
3540
$value = (string) $value;
3641
}

src/Type/NumericType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClickHouseDB\Type;
6+
7+
interface NumericType extends Type
8+
{
9+
}

src/Type/Type.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClickHouseDB\Type;
6+
7+
interface Type
8+
{
9+
/**
10+
* @return mixed
11+
*/
12+
public function getValue();
13+
}

src/Type/UInt64.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClickHouseDB\Type;
6+
7+
final class UInt64 implements NumericType
8+
{
9+
/** @var string */
10+
public $value;
11+
12+
private function __construct(string $uint64Value)
13+
{
14+
$this->value = $uint64Value;
15+
}
16+
17+
/**
18+
* @return self
19+
*/
20+
public static function fromString(string $uint64Value)
21+
{
22+
return new self($uint64Value);
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getValue()
29+
{
30+
return $this->value;
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function __toString()
37+
{
38+
return $this->value;
39+
}
40+
}

tests/Type/UInt64Test.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClickHouseDB\Tests\Type;
6+
7+
use ClickHouseDB\Tests\WithClient;
8+
use ClickHouseDB\Type\UInt64;
9+
use DateTimeImmutable;
10+
use PHPUnit\Framework\TestCase;
11+
use function array_column;
12+
use function implode;
13+
use function sprintf;
14+
15+
/**
16+
* @group integration
17+
*/
18+
final class UInt64Test extends TestCase
19+
{
20+
use WithClient;
21+
22+
/**
23+
* @return void
24+
*/
25+
public function setUp()
26+
{
27+
$this->client->write('DROP TABLE IF EXISTS uint64_data');
28+
$this->client->write('
29+
CREATE TABLE IF NOT EXISTS uint64_data (
30+
date Date MATERIALIZED toDate(datetime),
31+
datetime DateTime,
32+
number UInt64
33+
)
34+
ENGINE = MergeTree
35+
PARTITION BY date
36+
ORDER BY (datetime);
37+
');
38+
39+
parent::setUp();
40+
}
41+
42+
/**
43+
* @return void
44+
*/
45+
public function testWriteInsert()
46+
{
47+
$this->client->write(sprintf(
48+
'INSERT INTO uint64_data VALUES %s',
49+
implode(
50+
',',
51+
[
52+
sprintf('(now(), %s)', UInt64::fromString('0')),
53+
sprintf('(now(), %s)', UInt64::fromString('1')),
54+
sprintf('(now(), %s)', UInt64::fromString('18446744073709551615')),
55+
]
56+
)
57+
));
58+
59+
$statement = $this->client->select('SELECT number FROM uint64_data ORDER BY number ASC');
60+
61+
self::assertSame(3, $statement->count());
62+
self::assertSame(['0', '1', '18446744073709551615'], array_column($statement->rows(), 'number'));
63+
}
64+
65+
/**
66+
* @return void
67+
*/
68+
public function testInsert()
69+
{
70+
$now = new DateTimeImmutable();
71+
$this->client->insert(
72+
'uint64_data',
73+
[
74+
[$now, UInt64::fromString('0')],
75+
[$now, UInt64::fromString('1')],
76+
[$now, UInt64::fromString('18446744073709551615')],
77+
]
78+
);
79+
80+
$statement = $this->client->select('SELECT number FROM uint64_data ORDER BY number ASC');
81+
82+
self::assertSame(3, $statement->count());
83+
self::assertSame(['0', '1', '18446744073709551615'], array_column($statement->rows(), 'number'));
84+
}
85+
}

0 commit comments

Comments
 (0)