1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace ClickHouseDB\Quote; |
3
|
|
|
|
4
|
|
|
use ClickHouseDB\Exception\QueryException; |
5
|
|
|
use ClickHouseDB\Type\NumericType; |
6
|
|
|
use function array_map; |
7
|
|
|
use function is_array; |
8
|
|
|
use function is_float; |
9
|
|
|
use function is_int; |
10
|
|
|
use function is_string; |
11
|
|
|
use function preg_replace; |
12
|
|
|
use function str_replace; |
13
|
|
|
|
14
|
|
|
class StrictQuoteLine |
15
|
|
|
{ |
16
|
|
|
private $preset = [ |
|
|
|
|
17
|
|
|
'CSV'=>[ |
18
|
|
|
'EnclosureArray'=>'"', |
19
|
|
|
'EncodeEnclosure'=>'"', |
20
|
|
|
'Enclosure'=>'"', |
21
|
|
|
'Null'=>"\\N", |
|
|
|
|
22
|
|
|
'Delimiter'=>",", |
|
|
|
|
23
|
|
|
'TabEncode'=>false, |
24
|
|
|
], |
25
|
|
|
'Insert'=>[ |
26
|
|
|
'EnclosureArray'=>'', |
27
|
|
|
'EncodeEnclosure'=>'\\', |
28
|
|
|
'Enclosure'=>'\'', |
29
|
|
|
'Null'=>"NULL", |
|
|
|
|
30
|
|
|
'Delimiter'=>",", |
|
|
|
|
31
|
|
|
'TabEncode'=>false, |
32
|
|
|
], |
33
|
|
|
'TSV'=>[ |
34
|
|
|
'EnclosureArray'=>'', |
35
|
|
|
'EncodeEnclosure'=>'', |
36
|
|
|
'Enclosure'=>'\\', |
37
|
|
|
'Null'=>" ", |
|
|
|
|
38
|
|
|
'Delimiter'=>"\t", |
39
|
|
|
'TabEncode'=>true, |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
private $settings = []; |
|
|
|
|
43
|
|
|
|
44
|
3 |
|
public function __construct($format) |
|
|
|
|
45
|
|
|
{ |
46
|
3 |
|
if (empty($this->preset[$format])) |
47
|
|
|
{ |
48
|
|
|
throw new QueryException("Unsupport format encode line:" . $format); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$this->settings = $this->preset[$format]; |
52
|
3 |
|
} |
53
|
10 |
|
public function quoteRow($row) |
|
|
|
|
54
|
|
|
{ |
55
|
10 |
|
return implode($this->settings['Delimiter'], $this->quoteValue($row)); |
|
|
|
|
56
|
|
|
} |
57
|
10 |
|
public function quoteValue($row) |
|
|
|
|
58
|
|
|
{ |
59
|
10 |
|
$enclosure = $this->settings['Enclosure']; |
|
|
|
|
60
|
10 |
|
$delimiter = $this->settings['Delimiter']; |
|
|
|
|
61
|
10 |
|
$encode = $this->settings['EncodeEnclosure']; |
|
|
|
|
62
|
10 |
|
$encodeArray = $this->settings['EnclosureArray']; |
63
|
10 |
|
$null = $this->settings['Null']; |
|
|
|
|
64
|
10 |
|
$tabEncode = $this->settings['TabEncode']; |
|
|
|
|
65
|
|
|
|
66
|
|
|
$quote = function($value) use ($enclosure, $delimiter, $encode, $encodeArray, $null, $tabEncode) { |
|
|
|
|
67
|
10 |
|
$delimiter_esc = preg_quote($delimiter, '/'); |
|
|
|
|
68
|
|
|
|
69
|
10 |
|
$enclosure_esc = preg_quote($enclosure, '/'); |
|
|
|
|
70
|
|
|
|
71
|
10 |
|
$encode_esc = preg_quote($encode, '/'); |
|
|
|
|
72
|
|
|
|
73
|
10 |
|
$encode = true; |
74
|
10 |
|
if ($value instanceof NumericType) { |
75
|
1 |
|
$encode = false; |
76
|
|
|
} |
77
|
|
|
|
78
|
10 |
|
if (is_array($value)) { |
79
|
|
|
// Arrays are formatted as a list of values separated by commas in square brackets. |
80
|
|
|
// Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in |
81
|
|
|
// single quotation marks with the same screening rules as above. |
82
|
|
|
// as in the TabSeparated format, and then the resulting string is output in InsertRow in double quotes. |
83
|
4 |
|
$value = array_map( |
|
|
|
|
84
|
|
|
function ($v) use ($enclosure_esc, $encode_esc) { |
85
|
4 |
|
return is_string($v) ? $this->encodeString($v, $enclosure_esc, $encode_esc) : $v; |
86
|
4 |
|
}, |
87
|
4 |
|
$value |
88
|
|
|
); |
89
|
4 |
|
$resultArray = FormatLine::Insert($value); |
90
|
|
|
|
91
|
4 |
|
return $encodeArray . '[' . $resultArray . ']' . $encodeArray; |
92
|
|
|
} |
93
|
|
|
|
94
|
10 |
|
$value = ValueFormatter::formatValue($value, false); |
95
|
|
|
|
96
|
10 |
|
if (is_float($value) || is_int($value)) { |
97
|
9 |
|
return (string) $value; |
98
|
|
|
} |
99
|
|
|
|
100
|
10 |
|
if (is_string($value) && $encode) { |
101
|
9 |
|
if ($tabEncode) { |
102
|
1 |
|
return str_replace(["\t", "\n"], ['\\t', '\\n'], $value); |
103
|
|
|
} |
104
|
|
|
|
105
|
9 |
|
$value = $this->encodeString($value, $enclosure_esc, $encode_esc); |
106
|
|
|
|
107
|
9 |
|
return $enclosure . $value . $enclosure; |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
if ($value === null) { |
111
|
1 |
|
return $null; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $value; |
115
|
10 |
|
}; |
116
|
|
|
|
117
|
10 |
|
return array_map($quote, $row); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
9 |
|
public function encodeString(string $value, string $enclosureEsc, string $encodeEsc) |
|
|
|
|
124
|
|
|
{ |
125
|
9 |
|
return preg_replace('/(' . $enclosureEsc . '|' . $encodeEsc . ')/', $encodeEsc . '\1', $value); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|