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