Skip to content

Commit

Permalink
for ReadOnly users need set : client->setReadOnlyUser(true); or `$c…
Browse files Browse the repository at this point in the history
…onfi['readonly']` , see exam19_readonly_user.php
  • Loading branch information
isublimity committed Dec 9, 2016
1 parent c7a7fcf commit 89cbbe7
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -466,6 +466,10 @@ MIT

ChangeLog
---
### 2016-12-09
- for ReadOnly users need set : `client->setReadOnlyUser(true);` or `$confi['readonly']` , see exam19_readonly_user.php


### 2016-11-25
- `client->truncateTable('tableName')`
- `cluster->getMasterNodeForTable('dbName.tableName') // node have is_leader=1`
Expand Down
2 changes: 0 additions & 2 deletions example/exam18_log_queries.php
Expand Up @@ -14,5 +14,3 @@
$db->enableLogQueries()->enableHttpCompression();
//----------------------------------------
print_r($db->select('SELECT * FROM system.query_log')->rows());

//----------------------------------------
48 changes: 48 additions & 0 deletions example/exam19_readonly_user.php
@@ -0,0 +1,48 @@
<?php

include_once __DIR__ . '/../include.php';

$config = [
'host' => '192.168.1.20',
'port' => '8123',
'username' => 'ro',
'password' => 'ro'
];

$db = new ClickHouseDB\Client($config);


$db->enableExtremes(true)->enableHttpCompression();
$db->setReadOnlyUser(true);


// exec
$db->showDatabases();

// ----------------------------


$config = [
'host' => '192.168.1.20',
'port' => '8123',
'username' => 'ro',
'password' => 'ro',
'readonly' => true
];

$db = new ClickHouseDB\Client($config);

//$db->enableLogQueries()->enableHttpCompression();
//----------------------------------------
//print_r($db->select('SELECT * FROM system.query_log')->rows());

//----------------------------------------

$db->enableExtremes(true)->enableHttpCompression();



$db->showDatabases();

echo "OK?\n";
// ---------
23 changes: 23 additions & 0 deletions src/Client.php
Expand Up @@ -35,6 +35,10 @@ class Client
*/
private $_connect_port = false;

/**
* @var bool
*/
private $_connect_user_readonly=false;
/**
* @var array
*/
Expand Down Expand Up @@ -91,8 +95,27 @@ public function __construct($connect_params, $settings = [])
$this->settings()->apply($settings);
}


if (isset($connect_params['readonly']))
{
$this->setReadOnlyUser($connect_params['readonly']);
}




}

/**
* если у пользовалетя установленно только чтение в конфиге
*
* @param $flag
*/
public function setReadOnlyUser($flag)
{
$this->_connect_user_readonly=$flag;
$this->settings()->setReadOnlyUser($this->_connect_user_readonly);
}
/**
* Очистить пред обработку запроса [шаблонизация]
*
Expand Down
19 changes: 19 additions & 0 deletions src/Settings.php
Expand Up @@ -20,6 +20,8 @@ class Settings
*/
private $settings = [];

private $_ReadOnlyUser=false;


/**
* Settings constructor.
Expand Down Expand Up @@ -143,6 +145,23 @@ public function apply($settings_array)
return $this;
}

/**
* @param $flag
*/
public function setReadOnlyUser($flag)
{
$this->_ReadOnlyUser=$flag;
}

/**
*
*
*/
public function isReadOnlyUser()
{
return $this->_ReadOnlyUser;
}

/**
* @param $name
* @return mixed|null
Expand Down
16 changes: 15 additions & 1 deletion src/Transport/Http.php
Expand Up @@ -146,6 +146,17 @@ private function getUrl($params = [])
$settings = array_merge($settings, $params);
}


if ($this->settings()->isReadOnlyUser())
{
unset($settings['extremes']);
unset($settings['readonly']);
unset($settings['enable_http_compression']);
unset($settings['max_execution_time']);

}


return $this->getUri() . '?' . http_build_query($settings);
}

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




if (!$query_as_string) {
$new->parameters_json($sql);
}
Expand Down Expand Up @@ -311,7 +325,7 @@ public function getRequestRead(Query $query, $whereInFile = null, $writeToFile =
// if result to file
if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
$query->setFormat($writeToFile->fetchFormat());
$urlParams['extremes'] = false;
unset($urlParams['extremes']);
}
// ---------------------------------------------------------------------------------
// makeRequest read
Expand Down

0 comments on commit 89cbbe7

Please sign in to comment.