Skip to content

Commit 9ae5def

Browse files
committed
ok
1 parent 79f8850 commit 9ae5def

File tree

3 files changed

+95
-19
lines changed

3 files changed

+95
-19
lines changed

src/ClickHouseAPI.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ public function doGet($query, $h_opt = [])
572572
{
573573
$user = $this->user;
574574
$password = $this->pass;
575-
$h_opt_arr = \array_merge(\compact('query', 'user', 'password'), $h_opt);
575+
$database = $this->getOption('database');
576+
$h_opt_arr = \array_merge(\compact('query', 'user', 'password', 'database'), $h_opt);
576577
return $this->doApiCall($this->server_url, $h_opt_arr);
577578
}
578579
}

src/ClickHouseFunctions.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* - >setCurrentDatabase($db [, $sess]) - set current database by 'USE db' request
2121
* - >getCurrentDatabase([$sess]) - return results of 'SELECT currentDatabase()'
2222
*
23-
* - >getVersion() - return version of ClickHouse server
23+
* - >getVersion() - return version of ClickHouse server (function moved to ClickHouseAPI)
2424
* - >getUptime() - return server uptime in seconds
2525
* - >getSystemSettings() - get information from system.settings as array [name=>value]
2626
*
@@ -309,6 +309,7 @@ public function sqlTableQuick($table_name, $fields_arr, $if_not_exist = 1)
309309

310310
/**
311311
* Parse source $fields_arr from format [field_names => types[ defaults]]
312+
* to format with keys [create, type_full, type_name, type_src, default, bytes]
312313
*
313314
* @param array $fields_arr Array of elements [field_name]=>[field_type]
314315
* @return array Each element contains [create, type_name, default, ...]
@@ -392,37 +393,60 @@ public function getUptime()
392393
}
393394

394395
/**
395-
* Get current database name for current or specified session
396+
* Get current database name.
396397
*
397-
* Function use SQL-query 'SELECT currentDatabase()'
398+
* if option 'database' is not empty, return database from options.
399+
* Otherwise using SQL-query 'SELECT currentDatabase()' for current or specified session
398400
*
399401
* Keep in mind that current database can be set in two ways:
400-
* - by setCurrentDatabase() via SQL-request 'USE $db'
401-
* - by setOption('database', $db), in this case may use getOption('database')
402+
* - by option 'database', in this case '&database=...' is sent with each request
403+
* - by SQL-request 'USE $db' - it only makes sense when the sessions supported
402404
*
403-
* @param string|null $sess session_id
404-
* @return string|boolean String with current db-name or false if error
405+
* @param string|null|true $sess session_id (or true for read only 'database' option)
406+
* @return string|false String with current db-name or false if error
405407
*/
406408
public function getCurrentDatabase($sess = null)
407409
{
410+
$database = $this->getOption('database');
411+
if (!empty($database) || $sess === true) {
412+
return $database;
413+
}
408414
return $this->queryValue('SELECT currentDatabase()', null, $sess);
409415
}
410416

411417
/**
412418
* Set current database by name for current or specified session.
413419
*
414-
* Function send SQL-query 'USE $db'
420+
* Function send SQL-query 'USE $db' if sessions supported
415421
*
416-
* However, one must know that there is another way to specified
417-
* current database for any query ->setOption('database', $db)
422+
* If sessions not supported or parameter $sess is boolean true,
423+
* then set current database by option ->setOption('database', $db)
418424
*
419-
* @param string $db Database name
420-
* @param string|null $sess session_id
421-
* @return string|boolean True if ok, false if error
425+
* @param string $db Database name
426+
* @param string|null|true $sess session_id or true for use database-option
427+
* @return string|false false if ok, or string with error description
422428
*/
423429
public function setCurrentDatabase($db, $sess = null)
424430
{
425-
return $this->queryTrue("USE $db", [], $sess);
431+
if ($sess === true || !$this->isSupported('session_id')) {
432+
$this->setOption('database', $db);
433+
return false;
434+
} else {
435+
return $this->queryFalse("USE " . $db, [], $sess);
436+
}
437+
}
438+
439+
440+
public function dropDatabase($db_name, $if_exists = false)
441+
{
442+
$sql = "DROP DATABASE " . ($if_exists ? 'IF EXISTS ':'');
443+
return $this->queryFalse($sql . $db_name);
444+
}
445+
446+
public function createDatabase($db_name, $if_not_exists = false)
447+
{
448+
$sql = "CREATE DATABASE " . ($if_not_exists ? 'IF NOT EXISTS ':'');
449+
return $this->queryFalse($sql . $db_name);
426450
}
427451

428452
/**

tests/src/ClickHouseFunctionsTest.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,40 @@ public function testDelTypeAlias()
101101
$this->assertEquals($alias, $ch->changeIfIsAlias($alias));
102102
}
103103

104+
/**
105+
* @covers ierusalim\ClickHouse\ClickHouseFunctions::createDatabase
106+
*/
107+
public function testCreateDatabase()
108+
{
109+
$ch = $this->object;
110+
111+
$tmpdb = 'tmpdbfordel';
112+
113+
$this->assertFalse($ch->createDatabase($tmpdb, true));
114+
115+
$db_arr = $ch->getDatabasesList();
116+
117+
$this->assertTrue(array_search($tmpdb, $db_arr) !== false);
118+
119+
// $this->assertFalse($ch->dropDatabase($tmpdb));
120+
}
121+
122+
/**
123+
* @covers ierusalim\ClickHouse\ClickHouseFunctions::dropDatabase
124+
*/
125+
public function testDropDatabase()
126+
{
127+
$ch = $this->object;
128+
129+
$tmpdb = 'tmpdbfordel';
130+
131+
// $this->assertFalse($ch->createDatabase($tmpdb, true));
132+
133+
$this->assertFalse($ch->dropDatabase($tmpdb));
134+
$db_arr = $ch->getDatabasesList();
135+
$this->assertFalse(array_search($tmpdb, $db_arr));
136+
}
137+
104138
/**
105139
* @covers ierusalim\ClickHouse\ClickHouseFunctions::getCurrentDatabase
106140
*/
@@ -117,9 +151,9 @@ public function testGetCurrentDatabase()
117151
$db_2 = 'system';
118152

119153
$ans = $ch->setCurrentDatabase($db_1, $session_id_1);
120-
$this->assertTrue($ans);
154+
$this->assertFalse($ans);
121155
$ans = $ch->setCurrentDatabase($db_2, $session_id_2);
122-
$this->assertTrue($ans);
156+
$this->assertFalse($ans);
123157

124158
$ch->setSession($session_id_1);
125159
$db_name = $ch->getCurrentDatabase();
@@ -133,6 +167,11 @@ public function testGetCurrentDatabase()
133167
$this->assertEquals($db_name, $db_1);
134168
$db_name = $ch->getCurrentDatabase($session_id_2);
135169
$this->assertEquals($db_name, $db_2);
170+
171+
$ch->setOption('database', 'system');
172+
$this->assertEquals('system', $ch->getCurrentDatabase());
173+
$this->assertEquals('system', $ch->getCurrentDatabase(true));
174+
$ch->setOption('database', null);
136175
} else {
137176
echo '-';
138177
}
@@ -145,6 +184,14 @@ public function testSetCurrentDatabase()
145184
{
146185
$ch = $this->object;
147186

187+
$ch->setCurrentDatabase('system', true);
188+
$this->assertEquals('system', $ch->getOption('database'));
189+
$this->assertEquals('system', $ch->getCurrentDatabase());
190+
if ($ch->isSupported('session_id')) {
191+
$this->assertEquals('system', $ch->queryValue('SELECT currentDatabase()'));
192+
}
193+
$ch->setOption('database', null);
194+
148195
if ($ch->isSupported('session_id')) {
149196
$ch->setSession();
150197
$session_id_1 = $ch->setSession();
@@ -154,9 +201,9 @@ public function testSetCurrentDatabase()
154201
$db_2 = 'system';
155202

156203
$ans = $ch->setCurrentDatabase($db_1, $session_id_1);
157-
$this->assertTrue($ans);
204+
$this->assertFalse($ans);
158205
$ans = $ch->setCurrentDatabase($db_2, $session_id_2);
159-
$this->assertTrue($ans);
206+
$this->assertFalse($ans);
160207

161208
$db_name = $ch->getCurrentDatabase($session_id_1);
162209
$this->assertEquals($db_name, $db_1);
@@ -311,6 +358,10 @@ public function testGetNumbers()
311358
$ch = $this->object;
312359
if ($ch->isSupported('query')) {
313360
$arr = $ch->getNumbers(100);
361+
if (count($arr)==1) {
362+
echo "Error getNumbers: ";
363+
print_r($arr);
364+
}
314365
$this->assertEquals(100, count($arr));
315366
} else {
316367
echo '-';

0 commit comments

Comments
 (0)