1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ClickHouseDB; |
4
|
|
|
|
5
|
|
|
use ClickHouseDB\Transport\Http; |
6
|
|
|
|
7
|
|
|
class Settings |
8
|
|
|
{ |
9
|
|
|
/** |
|
|
|
|
10
|
|
|
* @var Http |
11
|
|
|
*/ |
12
|
|
|
private $client = null; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
|
|
|
|
15
|
|
|
* @var array |
|
|
|
|
16
|
|
|
*/ |
17
|
|
|
private $settings = []; |
18
|
|
|
|
19
|
|
|
private $_ReadOnlyUser = false; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
private $_isHttps = false; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Settings constructor. |
|
|
|
|
28
|
|
|
* @param Http $client |
|
|
|
|
29
|
|
|
*/ |
30
|
67 |
|
public function __construct(Http $client) |
31
|
|
|
{ |
32
|
|
|
$default = [ |
33
|
67 |
|
'extremes' => false, |
34
|
|
|
'readonly' => true, |
35
|
|
|
'max_execution_time' => 20, |
36
|
|
|
'enable_http_compression' => 0, |
37
|
|
|
'https' => false, |
38
|
|
|
]; |
39
|
|
|
|
40
|
67 |
|
$this->settings = $default; |
41
|
67 |
|
$this->client = $client; |
|
|
|
|
42
|
67 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string|int $key |
|
|
|
|
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
54 |
|
public function get($key) |
49
|
|
|
{ |
50
|
54 |
|
if (!$this->is($key)) { |
|
|
|
|
51
|
|
|
return null; |
52
|
|
|
} |
53
|
54 |
|
return $this->settings[$key]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string|int $key |
|
|
|
|
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
54 |
|
public function is($key) |
|
|
|
|
61
|
|
|
{ |
62
|
54 |
|
return isset($this->settings[$key]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string|int $key |
|
|
|
|
68
|
|
|
* @param mixed $value |
|
|
|
|
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
67 |
|
public function set($key, $value) |
72
|
|
|
{ |
73
|
67 |
|
$this->settings[$key] = $value; |
74
|
67 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function getDatabase() |
81
|
|
|
{ |
82
|
|
|
return $this->get('database'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $db |
|
|
|
|
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
67 |
|
public function database($db) |
|
|
|
|
90
|
|
|
{ |
91
|
67 |
|
$this->set('database', $db); |
92
|
67 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
45 |
|
public function getTimeOut() |
99
|
|
|
{ |
100
|
45 |
|
return $this->get('max_execution_time'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return mixed|null |
105
|
|
|
*/ |
106
|
44 |
|
public function isEnableHttpCompression() |
107
|
|
|
{ |
108
|
44 |
|
return $this->getSetting('enable_http_compression'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param bool|int $flag |
|
|
|
|
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
67 |
|
public function enableHttpCompression($flag) |
116
|
|
|
{ |
117
|
67 |
|
$this->set('enable_http_compression', intval($flag)); |
|
|
|
|
118
|
67 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
1 |
|
public function https($flag = true) |
|
|
|
|
123
|
|
|
{ |
124
|
1 |
|
$this->set('https', $flag); |
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
54 |
|
public function isHttps() |
|
|
|
|
129
|
|
|
{ |
130
|
54 |
|
return $this->get('https'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param int|bool $flag |
|
|
|
|
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function readonly($flag) |
139
|
|
|
{ |
140
|
|
|
$this->set('readonly', $flag); |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $session_id |
|
|
|
|
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
2 |
|
public function session_id($session_id) |
|
|
|
|
149
|
|
|
{ |
150
|
2 |
|
$this->set('session_id', $session_id); |
151
|
2 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return mixed|bool |
156
|
|
|
*/ |
157
|
45 |
|
public function getSessionId() |
158
|
|
|
{ |
159
|
45 |
|
if (empty($this->settings['session_id'])) { |
160
|
45 |
|
return false; |
161
|
|
|
} |
162
|
2 |
|
return $this->get('session_id'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return string|bool |
167
|
|
|
*/ |
168
|
2 |
|
public function makeSessionId() |
169
|
|
|
{ |
170
|
2 |
|
$this->session_id(sha1(uniqid('', true))); |
|
|
|
|
171
|
2 |
|
return $this->getSessionId(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param int|float $time |
|
|
|
|
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
2 |
|
public function max_execution_time($time) |
|
|
|
|
179
|
|
|
{ |
180
|
2 |
|
$this->set('max_execution_time', $time); |
181
|
2 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return array |
|
|
|
|
186
|
|
|
*/ |
187
|
44 |
|
public function getSettings() |
|
|
|
|
188
|
|
|
{ |
189
|
44 |
|
return $this->settings; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param array $settings_array |
|
|
|
|
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
1 |
|
public function apply($settings_array) |
|
|
|
|
197
|
|
|
{ |
198
|
1 |
|
foreach ($settings_array as $key => $value) { |
199
|
1 |
|
$this->set($key, $value); |
200
|
|
|
} |
201
|
|
|
|
202
|
1 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param int|bool $flag |
207
|
|
|
*/ |
208
|
|
|
public function setReadOnlyUser($flag) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$this->_ReadOnlyUser = $flag; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
44 |
|
public function isReadOnlyUser() |
|
|
|
|
217
|
|
|
{ |
218
|
44 |
|
return $this->_ReadOnlyUser; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string $name |
|
|
|
|
223
|
|
|
* @return mixed|null |
224
|
|
|
*/ |
225
|
45 |
|
public function getSetting($name) |
|
|
|
|
226
|
|
|
{ |
227
|
45 |
|
if (!isset($this->settings[$name])) { |
|
|
|
|
228
|
|
|
return null; |
229
|
|
|
} |
230
|
|
|
|
231
|
45 |
|
return $this->get($name); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|