Skip to content

Commit

Permalink
UInt64 value wrapper
Browse files Browse the repository at this point in the history
Resolves #80
  • Loading branch information
simPod committed Sep 25, 2018
1 parent 809b5fc commit 5f4fe72
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 2 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ $stat = $db->insert('summing_url_views',
);
```

If you need to insert UInt64 value, you can wrap the value in `ClickHouseDB\Type\UInt64` DTO.

```php
$statement = $db->insert('table_name',
[
[time(), UInt64::fromString('18446744073709551615')],
],
['event_time', 'uint64_type_column']
);
UInt64::fromString('18446744073709551615')
```

Select:
```php
$statement = $db->select('SELECT * FROM summing_url_views LIMIT 2');
Expand Down Expand Up @@ -915,4 +927,4 @@ MIT
ChangeLog
---------

See [changeLog.md](CHANGELOG.md)
See [changeLog.md](CHANGELOG.md)
8 changes: 7 additions & 1 deletion src/Quote/StrictQuoteLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace ClickHouseDB\Quote;

use ClickHouseDB\Exception\QueryException;
use ClickHouseDB\Type\NumericType;
use function array_map;
use function is_array;
use function is_float;
Expand Down Expand Up @@ -69,6 +70,11 @@ public function quoteValue($row)

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

$encode = true;
if ($value instanceof NumericType) {
$encode = false;
}

if (is_array($value)) {
// Arrays are formatted as a list of values separated by commas in square brackets.
// Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in
Expand All @@ -91,7 +97,7 @@ function ($v) use ($enclosure_esc, $encode_esc) {
return (string) $value;
}

if (is_string($value)) {
if (is_string($value) && $encode) {
if ($tabEncode) {
return str_replace(["\t", "\n"], ['\\t', '\\n'], $value);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Quote/ValueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ClickHouseDB\Quote;

use ClickHouseDB\Exception\UnsupportedValueType;
use ClickHouseDB\Type\Type;
use DateTimeInterface;
use function addslashes;
use function is_bool;
Expand All @@ -31,6 +32,10 @@ public static function formatValue($value, bool $addQuotes = true)
return $value;
}

if ($value instanceof Type) {
return $value->getValue();
}

if (is_object($value) && is_callable([$value, '__toString'])) {
$value = (string) $value;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Type/NumericType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace ClickHouseDB\Type;

interface NumericType extends Type
{
}
13 changes: 13 additions & 0 deletions src/Type/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace ClickHouseDB\Type;

interface Type
{
/**
* @return mixed
*/
public function getValue();
}
40 changes: 40 additions & 0 deletions src/Type/UInt64.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace ClickHouseDB\Type;

final class UInt64 implements NumericType
{
/** @var string */
public $value;

private function __construct(string $uint64Value)
{
$this->value = $uint64Value;
}

/**
* @return self
*/
public static function fromString(string $uint64Value)
{
return new self($uint64Value);
}

/**
* @return string
*/
public function getValue()
{
return $this->value;
}

/**
* @return string
*/
public function __toString()
{
return $this->value;
}
}
85 changes: 85 additions & 0 deletions tests/Type/UInt64Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace ClickHouseDB\Tests\Type;

use ClickHouseDB\Tests\WithClient;
use ClickHouseDB\Type\UInt64;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use function array_column;
use function implode;
use function sprintf;

/**
* @group integration
*/
final class UInt64Test extends TestCase
{
use WithClient;

/**
* @return void
*/
public function setUp()
{
$this->client->write('DROP TABLE IF EXISTS uint64_data');
$this->client->write('
CREATE TABLE IF NOT EXISTS uint64_data (
date Date MATERIALIZED toDate(datetime),
datetime DateTime,
number UInt64
)
ENGINE = MergeTree
PARTITION BY date
ORDER BY (datetime);
');

parent::setUp();
}

/**
* @return void
*/
public function testWriteInsert()
{
$this->client->write(sprintf(
'INSERT INTO uint64_data VALUES %s',
implode(
',',
[
sprintf('(now(), %s)', UInt64::fromString('0')),
sprintf('(now(), %s)', UInt64::fromString('1')),
sprintf('(now(), %s)', UInt64::fromString('18446744073709551615')),
]
)
));

$statement = $this->client->select('SELECT number FROM uint64_data ORDER BY number ASC');

self::assertSame(3, $statement->count());
self::assertSame(['0', '1', '18446744073709551615'], array_column($statement->rows(), 'number'));
}

/**
* @return void
*/
public function testInsert()
{
$now = new DateTimeImmutable();
$this->client->insert(
'uint64_data',
[
[$now, UInt64::fromString('0')],
[$now, UInt64::fromString('1')],
[$now, UInt64::fromString('18446744073709551615')],
]
);

$statement = $this->client->select('SELECT number FROM uint64_data ORDER BY number ASC');

self::assertSame(3, $statement->count());
self::assertSame(['0', '1', '18446744073709551615'], array_column($statement->rows(), 'number'));
}
}

0 comments on commit 5f4fe72

Please sign in to comment.