| 1 | <?php |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 2 | |||
| 3 | namespace ClickHouseDB; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * @see https://clickhouse.yandex/docs/en/operations/settings/ |
||
| 7 | */ |
||
| 8 | class Settings |
||
| 9 | { |
||
| 10 | /** @var mixed[] */ |
||
| 11 | private $settings = []; |
||
| 12 | |||
| 13 | /** @var bool */ |
||
| 14 | private $httpCompressionEnabled = false; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @return mixed|null |
||
| 18 | */ |
||
| 19 | 3 | public function get(string $key) |
|
| 20 | { |
||
| 21 | 3 | if (! $this->isSet($key)) { |
|
| 22 | return null; |
||
| 23 | } |
||
| 24 | |||
| 25 | 3 | return $this->settings[$key]; |
|
| 26 | } |
||
| 27 | |||
| 28 | 4 | public function isSet(string $key) : bool |
|
| 29 | { |
||
| 30 | 4 | return isset($this->settings[$key]); |
|
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param mixed $value |
||
| 35 | */ |
||
| 36 | 7 | public function set(string $key, $value) : void |
|
| 37 | { |
||
| 38 | 7 | $this->settings[$key] = $value; |
|
| 39 | 7 | } |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @return mixed|null |
||
| 43 | */ |
||
| 44 | 63 | public function isHttpCompressionEnabled() |
|
| 45 | { |
||
| 46 | 63 | return $this->httpCompressionEnabled; |
|
| 47 | } |
||
| 48 | |||
| 49 | 63 | public function setHttpCompression(bool $enable) : self |
|
| 50 | { |
||
| 51 | 63 | $this->httpCompressionEnabled = $enable; |
|
| 52 | |||
| 53 | 63 | return $this; |
|
| 54 | } |
||
| 55 | |||
| 56 | public function readonly(int $flag) : self |
||
| 57 | { |
||
| 58 | $this->set('readonly', $flag); |
||
| 59 | |||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $session_id |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | 2 | public function session_id($session_id) |
|
| 68 | { |
||
| 69 | 2 | $this->set('session_id', $session_id); |
|
| 70 | |||
| 71 | 2 | return $this; |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return mixed|bool |
||
| 76 | */ |
||
| 77 | 63 | public function getSessionId() |
|
| 78 | { |
||
| 79 | 63 | if (empty($this->settings['session_id'])) { |
|
| 80 | 63 | return false; |
|
| 81 | } |
||
| 82 | |||
| 83 | 2 | return $this->get('session_id'); |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return string|bool |
||
| 88 | */ |
||
| 89 | 2 | public function makeSessionId() |
|
| 90 | { |
||
| 91 | 2 | $this->session_id(sha1(uniqid('', true))); |
|
| 92 | |||
| 93 | 2 | return $this->getSessionId(); |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param mixed[] $forcedSettings |
||
|
0 ignored issues
–
show
|
|||
| 98 | * @return mixed[] |
||
| 99 | */ |
||
| 100 | 63 | public function getQueryableSettings(array $forcedSettings) : array |
|
| 101 | { |
||
| 102 | 63 | $settings = $this->settings; |
|
| 103 | 63 | if (! empty($forcedSettings)) { |
|
| 104 | 11 | $settings = $forcedSettings + $this->settings; |
|
| 105 | } |
||
| 106 | |||
| 107 | 63 | return $settings; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param array $settings_array |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | 1 | public function apply($settings_array) |
|
| 115 | { |
||
| 116 | 1 | foreach ($settings_array as $key => $value) { |
|
| 117 | 1 | $this->set($key, $value); |
|
| 118 | } |
||
| 119 | |||
| 120 | 1 | return $this; |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $name |
||
| 125 | * @return mixed|null |
||
| 126 | */ |
||
| 127 | 1 | public function getSetting($name) |
|
| 128 | { |
||
| 129 | 1 | if (! isset($this->settings[$name])) { |
|
| 130 | return null; |
||
| 131 | } |
||
| 132 | |||
| 133 | 1 | return $this->get($name); |
|
| 134 | } |
||
| 135 | } |
||
| 136 |