Skip to content

Commit 89cbbe7

Browse files
committed
for ReadOnly users need set : client->setReadOnlyUser(true); or $confi['readonly'] , see exam19_readonly_user.php
1 parent c7a7fcf commit 89cbbe7

File tree

6 files changed

+109
-3
lines changed

6 files changed

+109
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,10 @@ MIT
466466

467467
ChangeLog
468468
---
469+
### 2016-12-09
470+
- for ReadOnly users need set : `client->setReadOnlyUser(true);` or `$confi['readonly']` , see exam19_readonly_user.php
471+
472+
469473
### 2016-11-25
470474
- `client->truncateTable('tableName')`
471475
- `cluster->getMasterNodeForTable('dbName.tableName') // node have is_leader=1`

example/exam18_log_queries.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@
1414
$db->enableLogQueries()->enableHttpCompression();
1515
//----------------------------------------
1616
print_r($db->select('SELECT * FROM system.query_log')->rows());
17-
18-
//----------------------------------------

example/exam19_readonly_user.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
include_once __DIR__ . '/../include.php';
4+
5+
$config = [
6+
'host' => '192.168.1.20',
7+
'port' => '8123',
8+
'username' => 'ro',
9+
'password' => 'ro'
10+
];
11+
12+
$db = new ClickHouseDB\Client($config);
13+
14+
15+
$db->enableExtremes(true)->enableHttpCompression();
16+
$db->setReadOnlyUser(true);
17+
18+
19+
// exec
20+
$db->showDatabases();
21+
22+
// ----------------------------
23+
24+
25+
$config = [
26+
'host' => '192.168.1.20',
27+
'port' => '8123',
28+
'username' => 'ro',
29+
'password' => 'ro',
30+
'readonly' => true
31+
];
32+
33+
$db = new ClickHouseDB\Client($config);
34+
35+
//$db->enableLogQueries()->enableHttpCompression();
36+
//----------------------------------------
37+
//print_r($db->select('SELECT * FROM system.query_log')->rows());
38+
39+
//----------------------------------------
40+
41+
$db->enableExtremes(true)->enableHttpCompression();
42+
43+
44+
45+
$db->showDatabases();
46+
47+
echo "OK?\n";
48+
// ---------

src/Client.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class Client
3535
*/
3636
private $_connect_port = false;
3737

38+
/**
39+
* @var bool
40+
*/
41+
private $_connect_user_readonly=false;
3842
/**
3943
* @var array
4044
*/
@@ -91,8 +95,27 @@ public function __construct($connect_params, $settings = [])
9195
$this->settings()->apply($settings);
9296
}
9397

98+
99+
if (isset($connect_params['readonly']))
100+
{
101+
$this->setReadOnlyUser($connect_params['readonly']);
102+
}
103+
104+
105+
106+
94107
}
95108

109+
/**
110+
* если у пользовалетя установленно только чтение в конфиге
111+
*
112+
* @param $flag
113+
*/
114+
public function setReadOnlyUser($flag)
115+
{
116+
$this->_connect_user_readonly=$flag;
117+
$this->settings()->setReadOnlyUser($this->_connect_user_readonly);
118+
}
96119
/**
97120
* Очистить пред обработку запроса [шаблонизация]
98121
*

src/Settings.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Settings
2020
*/
2121
private $settings = [];
2222

23+
private $_ReadOnlyUser=false;
24+
2325

2426
/**
2527
* Settings constructor.
@@ -143,6 +145,23 @@ public function apply($settings_array)
143145
return $this;
144146
}
145147

148+
/**
149+
* @param $flag
150+
*/
151+
public function setReadOnlyUser($flag)
152+
{
153+
$this->_ReadOnlyUser=$flag;
154+
}
155+
156+
/**
157+
*
158+
*
159+
*/
160+
public function isReadOnlyUser()
161+
{
162+
return $this->_ReadOnlyUser;
163+
}
164+
146165
/**
147166
* @param $name
148167
* @return mixed|null

src/Transport/Http.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ private function getUrl($params = [])
146146
$settings = array_merge($settings, $params);
147147
}
148148

149+
150+
if ($this->settings()->isReadOnlyUser())
151+
{
152+
unset($settings['extremes']);
153+
unset($settings['readonly']);
154+
unset($settings['enable_http_compression']);
155+
unset($settings['max_execution_time']);
156+
157+
}
158+
159+
149160
return $this->getUri() . '?' . http_build_query($settings);
150161
}
151162

@@ -195,6 +206,9 @@ private function makeRequest(Query $query, $urlParams = [], $query_as_string = f
195206
$new = $this->newRequest($extendinfo);
196207
$new->url($url);
197208

209+
210+
211+
198212
if (!$query_as_string) {
199213
$new->parameters_json($sql);
200214
}
@@ -311,7 +325,7 @@ public function getRequestRead(Query $query, $whereInFile = null, $writeToFile =
311325
// if result to file
312326
if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
313327
$query->setFormat($writeToFile->fetchFormat());
314-
$urlParams['extremes'] = false;
328+
unset($urlParams['extremes']);
315329
}
316330
// ---------------------------------------------------------------------------------
317331
// makeRequest read

0 commit comments

Comments
 (0)