Skip to content

Commit ba88466

Browse files
committed
add function renameTable
1 parent 701b184 commit ba88466

File tree

4 files changed

+98
-8
lines changed

4 files changed

+98
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ contains functions for simple operations with ClickHouse.
3939
* **sendFileInsert**($file, $table) - send TabSeparated-file into table (structure autodetect)
4040
* **clearTable**($table [, $sess]) - clear table (DROP and re-create)
4141
* **dropTable**($table [, $sess]) - drop specified table
42+
* **renameTable**($from_name_or_arr [, $to_name] [, $sess]) - rename tables
4243
* **getTableFields**($table, ...) - returns [field_name=>field_type] array
4344
* **getTableInfo**($table [, $extended]) - returns array with info about table
4445
* **getTablesList**([$db] [,$pattern]) - returns tables list by SHOW TABLES request

src/ClickHouseFunctions.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* - >sendFileInsert($file, $table) - send TabSeparated-file into table
1111
* - >dropTable($table [, $sess]) - drop table
1212
* - >clearTable($table [, $sess]) - clear table (DROP and re-create)
13+
* - >renameTable($from_name_or_arr [, $to_name] [, $sess]) - rename tables
1314
* - >getTableFields($table [, $sess]) - returns [field_name=>field_type] array
1415
* - >getTableInfo($table [, $extended]) - returns array with info about table
1516
*
@@ -442,13 +443,15 @@ public function getDatabasesList()
442443
*
443444
* @param string|null $name Database name
444445
* @param string|null $like_pattern pattern for search table, example: d%
446+
* @param string|null $sess session_id
445447
* @return array|string Results in array or string with error description
446448
*/
447-
public function getTablesList($name = null, $like_pattern = null)
449+
public function getTablesList($name = null, $like_pattern = null, $sess = null)
448450
{
449451
return $this->queryStrings('SHOW TABLES ' .
450452
(empty($name) ? '' : $this->from . $name) .
451-
(empty($like_pattern) ? '' : " LIKE '$like_pattern'"));
453+
(empty($like_pattern) ? '' : " LIKE '$like_pattern'"),
454+
false, $sess);
452455
}
453456

454457
/**
@@ -637,6 +640,38 @@ public function clearTable($table, $sess = null)
637640
return $this->queryFalse($create_request, [], $sess);
638641
}
639642

643+
/**
644+
* Rename one table(from_name, to_name) or array of tables
645+
*
646+
* To rename many tables with one query, need to set an array, example:
647+
* - >renameTable(['from_name1' => 'to_name1', 'from_name2' => 'to_name2'])
648+
*
649+
* @param string|array $from_name_or_arr Old name or array [oldname=>newname,...]
650+
* @param string|null $to_name New name, or ignored if first parameter is array
651+
* @param string|null $sess session_id
652+
* @return boolean|string Return false if ok, or string with error description
653+
*/
654+
public function renameTable($from_name_or_arr, $to_name = null, $sess = null)
655+
{
656+
if (!\is_array($from_name_or_arr)) {
657+
if (empty($to_name)) {
658+
return "table name not specified";
659+
} else {
660+
$from_name_or_arr = [$from_name_or_arr => $to_name];
661+
}
662+
}
663+
$sql = '';
664+
foreach ($from_name_or_arr as $from_name => $to_name) {
665+
if (empty($sql)) {
666+
$sql = "RENAME TABLE ";
667+
} else {
668+
$sql .=", ";
669+
}
670+
$sql .= "`$from_name` TO `$to_name`";
671+
}
672+
return $this->queryFalse($sql, [], $sess);
673+
}
674+
640675
/**
641676
* Send file to ClickHouse-server and insert into table
642677
*

src/ClickHouseQuery.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,12 @@ public function queryValue($sql, $post_data = null, $sess = null)
202202

203203
/**
204204
* Return strings array (using TabSeparated-formats for data transfer)
205-
*
206-
* If results have more of one column, strings need be explode by tab
207-
*
208-
* If $with_names_types is true, first 2 strings of results in array
209-
* contain names and types of returned columns.
205+
* - If results have more of one column, strings need be explode by tab
206+
* - If $with_names_types is true, first 2 strings of results
207+
* contains names and types of returned columns.
210208
*
211209
* Nuances:
212-
* - Returned data not unescaped!
210+
* - Returned data not unescaped
213211
* - If have option 'extreme' or 'WITH TOTALS' requires filtering extra lines
214212
*
215213
* @param string $sql SQL-request

tests/src/ClickHouseFunctionsTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,62 @@ public function testDropTable()
352352
$this->assertFalse($arr);
353353
}
354354

355+
/**
356+
* @covers ierusalim\ClickHouse\ClickHouseFunctions::renameTable
357+
*/
358+
public function testRenameTable()
359+
{
360+
$ch = $this->object;
361+
362+
$prefix = 'tempren';
363+
$postfix = 'xxx';
364+
365+
$table_from = $prefix . 'from';
366+
$table_next = $prefix . 'next';
367+
$table_to = $prefix . 'to';
368+
369+
$ans = $ch->createTableQuick($table_from, [
370+
'id' => 'Int16',
371+
'dt' => ['Date', 'now()']
372+
], 2);
373+
$this->assertFalse($ans);
374+
375+
$ans = $ch->renameTable($table_from, $table_to);
376+
377+
$arr = $ch->getTablesList(null, $table_to);
378+
$this->assertEquals($table_to, $arr[0]);
379+
380+
$ans = $ch->createTableQuick($table_next, [
381+
'id' => 'Int16',
382+
'dt' => ['Date', 'now()']
383+
], 2);
384+
$this->assertFalse($ans);
385+
386+
$arr = $ch->getTablesList(null, $prefix .'%');
387+
388+
$ren_arr = [];
389+
foreach ($arr as $table) {
390+
if (substr($table, -strlen($postfix)) != $postfix) {
391+
$ren_arr[$table] = $table . $postfix;
392+
}
393+
}
394+
$ans = $ch->renameTable($ren_arr);
395+
396+
$arr = $ch->getTablesList(null, $prefix . '%');
397+
$drop_count = 0;
398+
foreach ($arr as $drop_table) {
399+
if (\substr($table, -\strlen($postfix)) != $postfix) {
400+
$this->assertFalse($ch->dropTable($drop_table));
401+
$drop_count++;
402+
}
403+
}
404+
$this->assertGreaterThan(1, $drop_count);
405+
$this->assertEquals(\count($arr), $drop_count);
406+
407+
// test bad param
408+
$this->assertTrue(\is_string($ch->renameTable($table_from)));
409+
}
410+
355411
/**
356412
* @covers ierusalim\ClickHouse\ClickHouseFunctions::clearTable
357413
*/

0 commit comments

Comments
 (0)