1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB\Query; |
4
|
|
|
|
5
|
|
|
use ClickHouseDB\Exception\QueryException; |
6
|
|
|
use function sizeof; |
|
|
|
|
7
|
|
|
|
8
|
|
|
class Query |
9
|
|
|
{ |
10
|
|
|
/** |
|
|
|
|
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $sql; |
14
|
|
|
|
15
|
|
|
/** |
|
|
|
|
16
|
|
|
* @var string|null |
17
|
|
|
*/ |
18
|
|
|
protected $format = null; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @var array |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
private $degenerations = []; |
24
|
|
|
|
25
|
|
|
private $supportFormats=[ |
|
|
|
|
26
|
|
|
"FORMAT\\s+TSVRaw", |
|
|
|
|
27
|
|
|
"FORMAT\\s+TSVWithNamesAndTypes", |
|
|
|
|
28
|
|
|
"FORMAT\\s+TSVWithNames", |
|
|
|
|
29
|
|
|
"FORMAT\\s+TSV", |
|
|
|
|
30
|
|
|
"FORMAT\\s+Vertical", |
|
|
|
|
31
|
|
|
"FORMAT\\s+JSONCompact", |
|
|
|
|
32
|
|
|
"FORMAT\\s+JSONEachRow", |
|
|
|
|
33
|
|
|
"FORMAT\\s+TSKV", |
|
|
|
|
34
|
|
|
"FORMAT\\s+TabSeparatedWithNames", |
|
|
|
|
35
|
|
|
"FORMAT\\s+TabSeparatedWithNamesAndTypes", |
|
|
|
|
36
|
|
|
"FORMAT\\s+TabSeparatedRaw", |
|
|
|
|
37
|
|
|
"FORMAT\\s+BlockTabSeparated", |
|
|
|
|
38
|
|
|
"FORMAT\\s+CSVWithNames", |
|
|
|
|
39
|
|
|
"FORMAT\\s+CSV", |
|
|
|
|
40
|
|
|
"FORMAT\\s+JSON", |
|
|
|
|
41
|
|
|
"FORMAT\\s+TabSeparated" |
|
|
|
|
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Query constructor. |
|
|
|
|
46
|
|
|
* @param string $sql |
|
|
|
|
47
|
|
|
* @param array $degenerations |
|
|
|
|
48
|
|
|
*/ |
49
|
46 |
|
public function __construct($sql, $degenerations = []) |
|
|
|
|
50
|
|
|
{ |
51
|
46 |
|
if (!trim($sql)) |
|
|
|
|
52
|
|
|
{ |
53
|
1 |
|
throw new QueryException('Empty Query'); |
54
|
|
|
} |
55
|
45 |
|
$this->sql = $sql; |
|
|
|
|
56
|
45 |
|
$this->degenerations = $degenerations; |
57
|
45 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string|null $format |
61
|
|
|
*/ |
62
|
39 |
|
public function setFormat($format) |
|
|
|
|
63
|
|
|
{ |
64
|
39 |
|
$this->format = $format; |
65
|
39 |
|
} |
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
39 |
|
private function applyFormatQuery() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
// FORMAT\s(\w)*$ |
71
|
39 |
|
if (null === $this->format) { |
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
39 |
|
$supportFormats = implode("|",$this->supportFormats); |
|
|
|
|
75
|
|
|
|
76
|
39 |
|
$this->sql = trim($this->sql); |
|
|
|
|
77
|
39 |
|
if (substr($this->sql, -1) == ';') { |
|
|
|
|
78
|
|
|
$this->sql = substr($this->sql, 0, -1); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
39 |
|
$matches = []; |
82
|
39 |
|
if (preg_match_all('%(' . $supportFormats . ')%ius', $this->sql, $matches)) { |
|
|
|
|
83
|
|
|
|
84
|
|
|
// skip add "format json" |
85
|
2 |
|
if (isset($matches[0])) |
86
|
|
|
{ |
|
|
|
|
87
|
|
|
|
88
|
2 |
|
$this->format = trim(str_ireplace('format', '', $matches[0][0])); |
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
} |
91
|
|
|
} else { |
92
|
37 |
|
$this->sql = $this->sql . ' FORMAT ' . $this->format; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
96
|
|
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
39 |
|
} |
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return null|string |
|
|
|
|
104
|
|
|
*/ |
105
|
45 |
|
public function getFormat() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
return $this->format; |
108
|
45 |
|
} |
109
|
|
|
|
110
|
|
|
public function isUseInUrlBindingsParams():bool |
|
|
|
|
111
|
45 |
|
{ |
112
|
|
|
// 'query=select {p1:UInt8} + {p2:UInt8}' -F "param_p1=3" -F "param_p2=4" |
113
|
45 |
|
return preg_match('#{[\w+]+:[\w+()]+}#',$this->sql); |
|
|
|
|
114
|
39 |
|
|
115
|
|
|
} |
|
|
|
|
116
|
|
|
public function getUrlBindingsParams():array |
|
|
|
|
117
|
45 |
|
{ |
118
|
|
|
$out=[]; |
|
|
|
|
119
|
45 |
|
if (sizeof($this->degenerations)) { |
|
|
|
|
120
|
|
|
foreach ($this->degenerations as $degeneration) { |
121
|
45 |
|
if ($degeneration instanceof Degeneration) { |
122
|
45 |
|
$params=$degeneration->getBind(); |
|
|
|
|
123
|
|
|
break; |
124
|
|
|
// need first response |
125
|
|
|
} |
126
|
|
|
} |
127
|
45 |
|
} |
128
|
|
|
if (sizeof($params)) { |
|
|
|
|
129
|
|
|
foreach ($params as $key=>$value) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$out['param_'.$key]=$value; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return $out; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function toSql() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
if ($this->format !== null) { |
140
|
|
|
$this->applyFormatQuery(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (sizeof($this->degenerations)) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
foreach ($this->degenerations as $degeneration) |
146
|
|
|
{ |
147
|
|
|
if ($degeneration instanceof Degeneration) { |
|
|
|
|
148
|
|
|
$this->sql = $degeneration->process($this->sql); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->sql; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function __toString() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
return $this->toSql(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|