Skip to content

Commit c31d275

Browse files
author
Yevhen Chornohradskyi
committed
Make ClickHouseSchemaManager use DB name from params instead of default
1 parent b8f1e74 commit c31d275

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/ClickHouseSchemaManager.php

+33-16
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1818
use Doctrine\DBAL\Schema\Column;
1919
use Doctrine\DBAL\Schema\Index;
20+
use Doctrine\DBAL\Schema\Schema;
2021
use Doctrine\DBAL\Schema\View;
2122
use Doctrine\DBAL\Types\Type;
22-
use const CASE_LOWER;
2323
use function array_change_key_case;
2424
use function array_filter;
2525
use function array_key_exists;
@@ -35,6 +35,7 @@
3535
use function strpos;
3636
use function strtolower;
3737
use function trim;
38+
use const CASE_LOWER;
3839

3940
/**
4041
* Schema manager for the ClickHouse DBMS.
@@ -59,10 +60,26 @@ protected function _getPortableViewDefinition($view)
5960
return new View($view['name'], $statement);
6061
}
6162

63+
/**
64+
* Returns a list of all tables in the current database.
65+
*
66+
* @return string[]
67+
*/
68+
public function listTableNames()
69+
{
70+
$database = ($this->_conn->getParams()['stick_to_default'] ?? false) ? $this->_conn->getDatabase() : null;
71+
$sql = $this->_platform->getListTablesSQL($database);
72+
73+
$tables = $this->_conn->fetchAll($sql);
74+
$tableNames = $this->_getPortableTablesList($tables);
75+
76+
return $this->filterAssetNames($tableNames);
77+
}
78+
6279
/**
6380
* {@inheritdoc}
6481
*/
65-
public function listTableIndexes($table) : array
82+
public function listTableIndexes($table): array
6683
{
6784
$tableView = $this->_getPortableViewDefinition(['name' => $table]);
6885

@@ -96,33 +113,33 @@ function (string $column) {
96113
/**
97114
* {@inheritdoc}
98115
*/
99-
protected function _getPortableTableColumnDefinition($tableColumn) : Column
116+
protected function _getPortableTableColumnDefinition($tableColumn): Column
100117
{
101118
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
102119

103-
$dbType = $columnType = trim($tableColumn['type']);
104-
$length = null;
105-
$fixed = false;
120+
$dbType = $columnType = trim($tableColumn['type']);
121+
$length = null;
122+
$fixed = false;
106123
$notnull = true;
107124

108125
if (preg_match('/(Nullable\((\w+)\))/i', $columnType, $matches)) {
109126
$columnType = str_replace($matches[1], $matches[2], $columnType);
110-
$notnull = false;
127+
$notnull = false;
111128
}
112129

113130
if (stripos($columnType, 'fixedstring') === 0) {
114131
// get length from FixedString definition
115132
$length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $columnType);
116133
$dbType = 'fixedstring';
117-
$fixed = true;
134+
$fixed = true;
118135
}
119136

120137
$unsigned = false;
121138
if (stripos($columnType, 'uint') === 0) {
122139
$unsigned = true;
123140
}
124141

125-
if (! isset($tableColumn['name'])) {
142+
if (!isset($tableColumn['name'])) {
126143
$tableColumn['name'] = '';
127144
}
128145

@@ -133,14 +150,14 @@ protected function _getPortableTableColumnDefinition($tableColumn) : Column
133150
}
134151

135152
$options = [
136-
'length' => $length,
137-
'notnull' => $notnull,
138-
'default' => $default,
139-
'primary' => false,
140-
'fixed' => $fixed,
141-
'unsigned' => $unsigned,
153+
'length' => $length,
154+
'notnull' => $notnull,
155+
'default' => $default,
156+
'primary' => false,
157+
'fixed' => $fixed,
158+
'unsigned' => $unsigned,
142159
'autoincrement' => false,
143-
'comment' => null,
160+
'comment' => null,
144161
];
145162

146163
return new Column(

0 commit comments

Comments
 (0)