Skip to content

Commit 2949158

Browse files
authored
Merge pull request #94 from simPod/uint64-values
Uint64 values
2 parents 1870c81 + 5f4fe72 commit 2949158

File tree

12 files changed

+290
-88
lines changed

12 files changed

+290
-88
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/Exception/UnsupportedParameterType.php renamed to src/Exception/UnsupportedValueType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use function gettype;
99
use function sprintf;
1010

11-
final class UnsupportedParameterType extends InvalidArgumentException implements ClickHouseException
11+
final class UnsupportedValueType extends InvalidArgumentException implements ClickHouseException
1212
{
1313
/**
1414
* @param mixed $parameter

src/Query/Degeneration/Bindings.php

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@
44

55
namespace ClickHouseDB\Query\Degeneration;
66

7-
use ClickHouseDB\Exception\UnsupportedParameterType;
87
use ClickHouseDB\Query\Degeneration;
9-
use DateTimeInterface;
8+
use ClickHouseDB\Quote\ValueFormatter;
109
use function array_map;
1110
use function implode;
1211
use function is_array;
13-
use function is_bool;
14-
use function is_callable;
15-
use function is_float;
16-
use function is_int;
17-
use function is_object;
18-
use function is_string;
19-
use function sprintf;
2012

2113
class Bindings implements Degeneration
2214
{
@@ -45,17 +37,6 @@ public function bindParam($column, $value)
4537
$this->bindings[$column] = $value;
4638
}
4739

48-
/**
49-
* Escape an string
50-
*
51-
* @param string $value
52-
* @return string
53-
*/
54-
private function escapeString($value)
55-
{
56-
return addslashes($value);
57-
}
58-
5940
/**
6041
* Binds a list of values to the corresponding parameters.
6142
* This is similar to [[bindValue()]] except that it binds multiple values at a time.
@@ -92,15 +73,15 @@ public function process($sql)
9273

9374
$values = array_map(
9475
function ($value) {
95-
return $this->formatParameter($value);
76+
return ValueFormatter::formatValue($value);
9677
},
9778
$value
9879
);
9980

10081
$formattedParameter = implode(',', $values);
10182
} else {
10283
$valueSet = $value;
103-
$formattedParameter = $this->formatParameter($value);
84+
$formattedParameter = ValueFormatter::formatValue($value);
10485
}
10586

10687
if ($formattedParameter !== null) {
@@ -122,37 +103,4 @@ function ($value) {
122103

123104
return $sql;
124105
}
125-
126-
/**
127-
* @param mixed $value
128-
* @return mixed
129-
*/
130-
private function formatParameter($value)
131-
{
132-
if ($value instanceof DateTimeInterface) {
133-
$value = $value->format('Y-m-d H:i:s');
134-
}
135-
136-
if (is_float($value) || is_int($value) || is_bool($value) || $value === null) {
137-
return $value;
138-
}
139-
140-
if (is_object($value) && is_callable([$value, '__toString'])) {
141-
$value = (string) $value;
142-
}
143-
144-
if (is_string($value)) {
145-
return $this->formatStringParameter($this->escapeString($value));
146-
}
147-
148-
throw UnsupportedParameterType::new($value);
149-
}
150-
151-
/**
152-
* @return string
153-
*/
154-
private function formatStringParameter($value)
155-
{
156-
return sprintf("'%s'", $value);
157-
}
158106
}

src/Quote/FormatLine.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FormatLine
1616
* @param string $format
1717
* @return StrictQuoteLine
1818
*/
19-
private static function strictQuote($format)
19+
public static function strictQuote($format)
2020
{
2121
if (empty(self::$strict[$format]))
2222
{
@@ -28,10 +28,10 @@ private static function strictQuote($format)
2828
/**
2929
* Array in a string for a query Insert
3030
*
31-
* @param array $row
31+
* @param mixed[] $row
3232
* @return string
3333
*/
34-
public static function Insert(Array $row)
34+
public static function Insert(array $row)
3535
{
3636
return self::strictQuote('Insert')->quoteRow($row);
3737
}

src/Quote/StrictQuoteLine.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
namespace ClickHouseDB\Quote;
33

44
use ClickHouseDB\Exception\QueryException;
5+
use ClickHouseDB\Type\NumericType;
56
use function array_map;
7+
use function is_array;
8+
use function is_float;
9+
use function is_int;
610
use function is_string;
711
use function preg_replace;
812
use function str_replace;
913

1014
class StrictQuoteLine
1115
{
12-
1316
private $preset = [
1417
'CSV'=>[
1518
'EnclosureArray'=>'"',
@@ -44,6 +47,7 @@ public function __construct($format)
4447
{
4548
throw new QueryException("Unsupport format encode line:" . $format);
4649
}
50+
4751
$this->settings = $this->preset[$format];
4852
}
4953
public function quoteRow($row)
@@ -59,51 +63,51 @@ public function quoteValue($row)
5963
$null = $this->settings['Null'];
6064
$tabEncode = $this->settings['TabEncode'];
6165

62-
6366
$quote = function($value) use ($enclosure, $delimiter, $encode, $encodeArray, $null, $tabEncode) {
64-
65-
66-
6767
$delimiter_esc = preg_quote($delimiter, '/');
6868

6969
$enclosure_esc = preg_quote($enclosure, '/');
7070

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

73-
$type = gettype($value);
74-
75-
if ($type == 'integer' || $type == 'double') {
76-
return strval($value);
77-
}
78-
79-
if (is_string($value)) {
80-
if ($tabEncode)
81-
{
82-
return str_replace(["\t", "\n"], ['\\t', '\\n'], $value);
83-
}
84-
85-
$value = strval($value);
86-
$value = $this->encodeString($value, $enclosure_esc, $encode_esc);
87-
return $enclosure . $value . $enclosure;
73+
$encode = true;
74+
if ($value instanceof NumericType) {
75+
$encode = false;
8876
}
8977

9078
if (is_array($value)) {
9179
// Arrays are formatted as a list of values separated by commas in square brackets.
9280
// Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in
9381
// single quotation marks with the same screening rules as above.
9482
// as in the TabSeparated format, and then the resulting string is output in InsertRow in double quotes.
95-
$value = array_map(
83+
$value = array_map(
9684
function ($v) use ($enclosure_esc, $encode_esc) {
9785
return is_string($v) ? $this->encodeString($v, $enclosure_esc, $encode_esc) : $v;
9886
},
9987
$value
10088
);
101-
$result_array = FormatLine::Insert($value);
89+
$resultArray = FormatLine::Insert($value);
90+
91+
return $encodeArray . '[' . $resultArray . ']' . $encodeArray;
92+
}
93+
94+
$value = ValueFormatter::formatValue($value, false);
95+
96+
if (is_float($value) || is_int($value)) {
97+
return (string) $value;
98+
}
10299

103-
return $encodeArray . '[' . $result_array . ']' . $encodeArray;
100+
if (is_string($value) && $encode) {
101+
if ($tabEncode) {
102+
return str_replace(["\t", "\n"], ['\\t', '\\n'], $value);
103+
}
104+
105+
$value = $this->encodeString($value, $enclosure_esc, $encode_esc);
106+
107+
return $enclosure . $value . $enclosure;
104108
}
105109

106-
if (null === $value) {
110+
if ($value === null) {
107111
return $null;
108112
}
109113

src/Quote/ValueFormatter.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClickHouseDB\Quote;
6+
7+
use ClickHouseDB\Exception\UnsupportedValueType;
8+
use ClickHouseDB\Type\Type;
9+
use DateTimeInterface;
10+
use function addslashes;
11+
use function is_bool;
12+
use function is_callable;
13+
use function is_float;
14+
use function is_int;
15+
use function is_object;
16+
use function is_string;
17+
use function sprintf;
18+
19+
class ValueFormatter
20+
{
21+
/**
22+
* @param mixed $value
23+
* @return mixed
24+
*/
25+
public static function formatValue($value, bool $addQuotes = true)
26+
{
27+
if ($value instanceof DateTimeInterface) {
28+
$value = $value->format('Y-m-d H:i:s');
29+
}
30+
31+
if (is_float($value) || is_int($value) || is_bool($value) || $value === null) {
32+
return $value;
33+
}
34+
35+
if ($value instanceof Type) {
36+
return $value->getValue();
37+
}
38+
39+
if (is_object($value) && is_callable([$value, '__toString'])) {
40+
$value = (string) $value;
41+
}
42+
43+
if (is_string($value)) {
44+
if ($addQuotes) {
45+
return self::formatStringParameter(self::escapeString($value));
46+
}
47+
48+
return $value;
49+
}
50+
51+
throw UnsupportedValueType::new($value);
52+
}
53+
54+
/**
55+
* Escape an string
56+
*
57+
* @param string $value
58+
* @return string
59+
*/
60+
private static function escapeString($value)
61+
{
62+
return addslashes($value);
63+
}
64+
65+
/**
66+
* @return string
67+
*/
68+
private static function formatStringParameter($value)
69+
{
70+
return sprintf("'%s'", $value);
71+
}
72+
}

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+
}

0 commit comments

Comments
 (0)