|
20 | 20 | * - >setCurrentDatabase($db [, $sess]) - set current database by 'USE db' request |
21 | 21 | * - >getCurrentDatabase([$sess]) - return results of 'SELECT currentDatabase()' |
22 | 22 | * |
23 | | - * - >getVersion() - return version of ClickHouse server |
| 23 | + * - >getVersion() - return version of ClickHouse server (function moved to ClickHouseAPI) |
24 | 24 | * - >getUptime() - return server uptime in seconds |
25 | 25 | * - >getSystemSettings() - get information from system.settings as array [name=>value] |
26 | 26 | * |
@@ -309,6 +309,7 @@ public function sqlTableQuick($table_name, $fields_arr, $if_not_exist = 1) |
309 | 309 |
|
310 | 310 | /** |
311 | 311 | * Parse source $fields_arr from format [field_names => types[ defaults]] |
| 312 | + * to format with keys [create, type_full, type_name, type_src, default, bytes] |
312 | 313 | * |
313 | 314 | * @param array $fields_arr Array of elements [field_name]=>[field_type] |
314 | 315 | * @return array Each element contains [create, type_name, default, ...] |
@@ -392,37 +393,60 @@ public function getUptime() |
392 | 393 | } |
393 | 394 |
|
394 | 395 | /** |
395 | | - * Get current database name for current or specified session |
| 396 | + * Get current database name. |
396 | 397 | * |
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 |
398 | 400 | * |
399 | 401 | * 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 |
402 | 404 | * |
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 |
405 | 407 | */ |
406 | 408 | public function getCurrentDatabase($sess = null) |
407 | 409 | { |
| 410 | + $database = $this->getOption('database'); |
| 411 | + if (!empty($database) || $sess === true) { |
| 412 | + return $database; |
| 413 | + } |
408 | 414 | return $this->queryValue('SELECT currentDatabase()', null, $sess); |
409 | 415 | } |
410 | 416 |
|
411 | 417 | /** |
412 | 418 | * Set current database by name for current or specified session. |
413 | 419 | * |
414 | | - * Function send SQL-query 'USE $db' |
| 420 | + * Function send SQL-query 'USE $db' if sessions supported |
415 | 421 | * |
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) |
418 | 424 | * |
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 |
422 | 428 | */ |
423 | 429 | public function setCurrentDatabase($db, $sess = null) |
424 | 430 | { |
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); |
426 | 450 | } |
427 | 451 |
|
428 | 452 | /** |
|
0 commit comments