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
|
|
|
/** |
26
|
|
|
* Query constructor. |
27
|
|
|
* @param $sql |
28
|
|
|
* @param array $degenerations |
29
|
|
|
*/ |
30
|
35 |
|
public function __construct($sql,$degenerations=[]) |
31
|
|
|
{ |
32
|
35 |
|
if (!trim($sql)) |
33
|
|
|
{ |
34
|
1 |
|
throw new QueryException('Empty Query'); |
35
|
|
|
} |
36
|
35 |
|
$this->sql = $sql; |
37
|
35 |
|
$this->degenerations=$degenerations; |
38
|
35 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $format |
42
|
|
|
*/ |
43
|
35 |
|
public function setFormat($format) |
44
|
|
|
{ |
45
|
35 |
|
$this->format = $format; |
46
|
35 |
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
35 |
|
private function applyFormatQuery() |
50
|
|
|
{ |
51
|
|
|
// FORMAT\s(\w)*$ |
52
|
35 |
|
if (null === $this->format) return false; |
53
|
|
|
$supportFormats= |
54
|
35 |
|
"FORMAT\\s+TSV|FORMAT\\s+TSVRaw|FORMAT\\s+TSVWithNames|FORMAT\\s+TSVWithNamesAndTypes|FORMAT\\s+Vertical|FORMAT\\s+JSONCompact|FORMAT\\s+JSONEachRow|FORMAT\\s+TSKV|FORMAT\\s+TabSeparatedWithNames|FORMAT\\s+TabSeparatedWithNamesAndTypes|FORMAT\\s+TabSeparatedRaw|FORMAT\\s+BlockTabSeparated|FORMAT\\s+CSVWithNames|FORMAT\\s+CSV|FORMAT\\s+JSON|FORMAT\\s+TabSeparated"; |
55
|
|
|
|
56
|
35 |
|
$matches=[]; |
57
|
35 |
|
if (preg_match_all('%('.$supportFormats.')%ius',$this->sql,$matches)){ |
58
|
|
|
|
59
|
|
|
// skip add "format json" |
60
|
1 |
|
if (isset($matches[0])) |
61
|
|
|
{ |
62
|
|
|
|
63
|
1 |
|
$this->format=trim(str_ireplace('format','',$matches[0][0])); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
else { |
68
|
35 |
|
$this->sql = $this->sql . ' FORMAT ' . $this->format; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
35 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return null|string |
80
|
|
|
*/ |
81
|
35 |
|
public function getFormat() |
82
|
|
|
{ |
83
|
|
|
|
84
|
35 |
|
return $this->format; |
85
|
|
|
} |
86
|
|
|
|
87
|
35 |
|
public function toSql() |
88
|
|
|
{ |
89
|
35 |
|
if ($this->format !== null) { |
90
|
35 |
|
$this->applyFormatQuery(); |
91
|
|
|
} |
92
|
|
|
|
93
|
35 |
|
if (sizeof($this->degenerations)) |
94
|
|
|
{ |
95
|
35 |
|
foreach ($this->degenerations as $degeneration) |
96
|
|
|
{ |
97
|
35 |
|
if ($degeneration instanceof Degeneration) { |
98
|
35 |
|
$this->sql=$degeneration->process($this->sql); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
35 |
|
return $this->sql; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function __toString() |
110
|
|
|
{ |
111
|
|
|
return $this->toSql(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|