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