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
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @param $row |
|
|
|
|
57
|
|
|
* @param bool $skipEncode |
|
|
|
|
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
12 |
|
public function quoteRow($row,bool $skipEncode=false ) |
|
|
|
|
61
|
|
|
{ |
62
|
12 |
|
return implode($this->settings['Delimiter'], $this->quoteValue($row,$skipEncode)); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @param $row |
|
|
|
|
67
|
|
|
* @param bool $skipEncode |
|
|
|
|
68
|
|
|
* @return array |
|
|
|
|
69
|
|
|
*/ |
70
|
12 |
|
public function quoteValue($row,bool $skipEncode=false) |
|
|
|
|
71
|
|
|
{ |
72
|
12 |
|
$enclosure = $this->settings['Enclosure']; |
|
|
|
|
73
|
12 |
|
$delimiter = $this->settings['Delimiter']; |
|
|
|
|
74
|
12 |
|
$encodeEnclosure = $this->settings['EncodeEnclosure']; |
75
|
12 |
|
$encodeArray = $this->settings['EnclosureArray']; |
|
|
|
|
76
|
12 |
|
$null = $this->settings['Null']; |
|
|
|
|
77
|
12 |
|
$tabEncode = $this->settings['TabEncode']; |
|
|
|
|
78
|
|
|
|
79
|
|
|
$quote = function($value) use ($enclosure, $delimiter, $encodeEnclosure, $encodeArray, $null, $tabEncode, $skipEncode) { |
|
|
|
|
80
|
12 |
|
$delimiter_esc = preg_quote($delimiter, '/'); |
|
|
|
|
81
|
|
|
|
82
|
12 |
|
$enclosure_esc = preg_quote($enclosure, '/'); |
|
|
|
|
83
|
|
|
|
84
|
12 |
|
$encode_esc = preg_quote($encodeEnclosure, '/'); |
|
|
|
|
85
|
|
|
|
86
|
12 |
|
$encode = true; |
87
|
12 |
|
if ($value instanceof NumericType) { |
88
|
1 |
|
$encode = false; |
89
|
|
|
} |
|
|
|
|
90
|
12 |
|
if ($value instanceof Expression) { |
91
|
1 |
|
$encode = $value->needsEncoding(); |
92
|
|
|
} |
93
|
|
|
|
94
|
12 |
|
if (is_array($value)) { |
95
|
|
|
// Arrays are formatted as a list of values separated by commas in square brackets. |
96
|
|
|
// Elements of the array - the numbers are formatted as usual, and the dates, dates-with-time, and lines are in |
97
|
|
|
// single quotation marks with the same screening rules as above. |
98
|
|
|
// as in the TabSeparated format, and then the resulting string is output in InsertRow in double quotes. |
99
|
|
|
|
100
|
5 |
|
$value = array_map( |
101
|
|
|
function ($v) use ($enclosure_esc, $encode_esc) { |
102
|
5 |
|
return is_string($v) ? $this->encodeString($v, $enclosure_esc, $encode_esc) : $v; |
103
|
5 |
|
}, |
104
|
5 |
|
$value |
105
|
|
|
); |
106
|
5 |
|
$resultArray = FormatLine::Insert($value,($encodeEnclosure==='\\'?true:false)); |
107
|
5 |
|
return $encodeArray . '[' . $resultArray . ']' . $encodeArray; |
108
|
|
|
} |
109
|
|
|
|
110
|
12 |
|
$value = ValueFormatter::formatValue($value, false); |
111
|
|
|
|
112
|
12 |
|
if (is_float($value) || is_int($value)) { |
113
|
10 |
|
return (string) $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
12 |
|
if (is_string($value) && $encode) { |
117
|
10 |
|
if ($tabEncode) { |
118
|
1 |
|
return str_replace(["\t", "\n"], ['\\t', '\\n'], $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
10 |
|
if (!$skipEncode) |
|
|
|
|
122
|
10 |
|
$value = $this->encodeString($value, $enclosure_esc, $encode_esc); |
123
|
|
|
|
124
|
10 |
|
return $enclosure . $value . $enclosure; |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
if ($value === null) { |
128
|
1 |
|
return $null; |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
return $value; |
132
|
12 |
|
}; |
133
|
|
|
|
134
|
12 |
|
return array_map($quote, $row); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
10 |
|
public function encodeString(string $value, string $enclosureEsc, string $encodeEsc) |
141
|
|
|
{ |
142
|
10 |
|
return preg_replace('/(' . $enclosureEsc . '|' . $encodeEsc . ')/', $encodeEsc . '\1', $value); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|