Skip to content

Commit 934e84b

Browse files
committed
ok
1 parent 7aa7bb6 commit 934e84b

File tree

8 files changed

+1867
-199
lines changed

8 files changed

+1867
-199
lines changed

src/ClickHouseAPI.php

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@
4444
*/
4545
class ClickHouseAPI
4646
{
47+
/*
48+
* Trait for async requests
49+
*/
4750
use ClickHouseSlots;
51+
/*
52+
* Trait for currentDatabase() and setCurrentDatabase();
53+
*/
54+
use ClickHouseSessions;
55+
4856
/**
4957
* Protocol for access to ClickHouse server
5058
*
@@ -185,13 +193,6 @@ class ClickHouseAPI
185193
*/
186194
public $hook_before_api_call = false;
187195

188-
/**
189-
* Auto-create session_id and send it with each request
190-
*
191-
* @var boolean
192-
*/
193-
public $session_autocreate = false;
194-
195196
/**
196197
* True if running under windows, false otherwise
197198
*
@@ -423,7 +424,7 @@ public function doQuery(
423424
$password = $this->pass;
424425

425426
// Set session if need
426-
if (!empty($session_id) && $this->getSession() != $session_id) {
427+
if (!empty($session_id) && ($this->getSession() !== $session_id)) {
427428
$old_session = $this->setSession($session_id);
428429
} else {
429430
if ($this->session_autocreate && $this->getSession() === null) {
@@ -616,32 +617,6 @@ public function getOption($key)
616617
return isset($this->options[$key]) ? $this->options[$key] : null;
617618
}
618619

619-
/**
620-
* Set session_id to http-request options
621-
* if session_id not specified (or specified as null) create and set random.
622-
*
623-
* @param string|null $session_id session_id or null for generate new id
624-
* @param boolean $overwrite false = set only if session not defined, true = always
625-
* @return string|null Return old value of session_id option
626-
*/
627-
public function setSession($session_id = null, $overwrite = true)
628-
{
629-
if (\is_null($session_id)) {
630-
$session_id = \md5(\uniqid(\mt_rand(0, \PHP_INT_MAX), true));
631-
}
632-
return $this->setOption('session_id', $session_id, $overwrite);
633-
}
634-
635-
/**
636-
* Return current session_id from http-options. Return null if not exists.
637-
*
638-
* @return string|null
639-
*/
640-
public function getSession()
641-
{
642-
return $this->getOption('session_id');
643-
}
644-
645620
/**
646621
* Delete http-option by specified key
647622
*
@@ -733,7 +708,7 @@ public function versionSendQuery()
733708
{
734709
// save to_slot and old session_id for restore it after complete
735710
$old_to_slot = $this->to_slot;
736-
$old_sess = $this->setSession(null, false);
711+
$old_sess = $this->setSession(false, false);
737712

738713
$query = 'SELECT version()';
739714

src/ClickHouseFunctions.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -398,52 +398,6 @@ public function getUptime()
398398
return $this->queryValue('SELECT uptime()');
399399
}
400400

401-
/**
402-
* Get current database name.
403-
*
404-
* if option 'database' is not empty, return database from options.
405-
* Otherwise using SQL-query 'SELECT currentDatabase()' for current or specified session
406-
*
407-
* Keep in mind that current database can be set in two ways:
408-
* - by option 'database', in this case '&database=...' is sent with each request
409-
* - by SQL-request 'USE $db' - it only makes sense when the sessions supported
410-
*
411-
* @param string|null|true $sess session_id (or true for read only 'database' option)
412-
* @return string|false String with current db-name or false if error
413-
*/
414-
public function getCurrentDatabase($sess = null)
415-
{
416-
$this->to_slot = false;
417-
$database = $this->getOption('database');
418-
if (!empty($database) || $sess === true) {
419-
return $database;
420-
}
421-
return $this->queryValue('SELECT currentDatabase()', null, $sess);
422-
}
423-
424-
/**
425-
* Set current database by name for current or specified session.
426-
*
427-
* Function send SQL-query 'USE $db' if sessions supported
428-
*
429-
* If sessions not supported or parameter $sess is boolean true,
430-
* then set current database by option ->setOption('database', $db)
431-
*
432-
* @param string $db Database name
433-
* @param string|null|true $sess session_id or true for use database-option
434-
* @return string|false false if ok, or string with error description
435-
*/
436-
public function setCurrentDatabase($db, $sess = null)
437-
{
438-
$this->to_slot = false;
439-
if ($sess === true || !$this->isSupported('session_id')) {
440-
$this->setOption('database', $db);
441-
return false;
442-
} else {
443-
return $this->queryFalse("USE " . $db, [], $sess);
444-
}
445-
}
446-
447401
/**
448402
* Drop database and remove all tables inside it.
449403
*

0 commit comments

Comments
 (0)