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