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
|
2 |
|
public function __construct($format) |
38
|
|
|
{ |
39
|
2 |
|
if (empty($this->preset[$format])) |
40
|
|
|
{ |
41
|
|
|
throw new QueryException("Unsupport format encode line:".$format); |
42
|
|
|
} |
43
|
2 |
|
$this->settings=$this->preset[$format]; |
44
|
2 |
|
} |
45
|
6 |
|
public function quoteRow($row) |
46
|
|
|
{ |
47
|
6 |
|
return implode($this->settings['Delimiter'],$this->quoteValue($row)); |
48
|
|
|
} |
49
|
6 |
|
public function quoteValue($row) |
50
|
|
|
{ |
51
|
6 |
|
$enclosure = $this->settings['Enclosure']; |
52
|
6 |
|
$delimiter = $this->settings['Delimiter']; |
53
|
6 |
|
$encode = $this->settings['EncodeEnclosure']; |
54
|
6 |
|
$encodeArray = $this->settings['EnclosureArray']; |
55
|
6 |
|
$null = $this->settings['Null']; |
56
|
6 |
|
$tabEncode=$this->settings['TabEncode']; |
57
|
|
|
|
58
|
|
|
|
59
|
6 |
|
$quote = function ($value) use ($enclosure,$delimiter,$encode,$encodeArray,$null,$tabEncode) { |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
6 |
|
$delimiter_esc = preg_quote($delimiter, '/'); |
|
|
|
|
64
|
|
|
|
65
|
6 |
|
$enclosure_esc = preg_quote($enclosure, '/'); |
66
|
|
|
|
67
|
6 |
|
$encode_esc = preg_quote($encode, '/'); |
68
|
|
|
|
69
|
6 |
|
$type = gettype($value); |
70
|
|
|
|
71
|
6 |
|
if ($type == 'integer' || $type == 'double') { |
72
|
6 |
|
return strval($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
if (is_string($value)) { |
76
|
6 |
|
if ($tabEncode) |
77
|
|
|
{ |
78
|
1 |
|
return str_replace(["\t","\n"],['\\t','\\n'],$value); |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
$value = strval($value); |
82
|
6 |
|
$value = preg_replace('/('.$enclosure_esc.'|'.$encode_esc.')/',$encode_esc.'\1', $value); |
83
|
6 |
|
return $enclosure . $value . $enclosure; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
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
|
3 |
|
$result_array = FormatLine::Insert($value); |
92
|
|
|
|
93
|
3 |
|
return $encodeArray . '[' . $result_array . ']' .$encodeArray; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (null === $value) { |
97
|
|
|
return $null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $value; |
101
|
6 |
|
}; |
102
|
|
|
|
103
|
6 |
|
return array_map($quote, $row); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|