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