forked from sanchezzzhak/kak-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Schema.php
227 lines (199 loc) · 7.07 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
namespace kak\clickhouse;
use Yii;
use yii\db\TableSchema;
use yii\helpers\ArrayHelper;
class Schema extends \yii\db\Schema
{
/** @var $db Connection */
public $db;
public $typeMap = [
'UInt8' => self::TYPE_SMALLINT,
'UInt16' => self::TYPE_INTEGER,
'UInt32' => self::TYPE_INTEGER,
'UInt64' => self::TYPE_BIGINT,
'Int8' => self::TYPE_SMALLINT,
'Int16' => self::TYPE_INTEGER,
'Int32' => self::TYPE_INTEGER,
'Int64' => self::TYPE_BIGINT,
'Float32' => self::TYPE_FLOAT,
'Float64' => self::TYPE_FLOAT,
'String' => self::TYPE_STRING,
'FixedString' => self::TYPE_CHAR,
'Date' => self::TYPE_DATE,
'DateTime' => self::TYPE_DATETIME,
'Enum' => self::TYPE_STRING,
'Enum8' => self::TYPE_STRING,
'Enum16' => self::TYPE_STRING,
'Nullable(UInt8)' => self::TYPE_SMALLINT,
'Nullable(UInt16' => self::TYPE_INTEGER,
'Nullable(UInt32' => self::TYPE_INTEGER,
'Nullable(UInt64' => self::TYPE_BIGINT,
'Nullable(Int8)' => self::TYPE_SMALLINT,
'Nullable(Int16)' => self::TYPE_INTEGER,
'Nullable(Int32)' => self::TYPE_INTEGER,
'Nullable(Int64)' => self::TYPE_BIGINT,
'Nullable(Float32)' => self::TYPE_FLOAT,
'Nullable(Float64)' => self::TYPE_FLOAT,
'Nullable(String)' => self::TYPE_STRING,
'Nullable(FixedString)' => self::TYPE_CHAR,
'Nullable(Date)' => self::TYPE_DATE,
'Nullable(DateTime)' => self::TYPE_DATETIME,
'Nullable(Enum)' => self::TYPE_STRING,
'Nullable(Enum8)' => self::TYPE_STRING,
'Nullable(Enum16)' => self::TYPE_STRING,
//'Array' => null,
//'Tuple' => null,
//'Nested' => null,
];
private $_builder;
/**
* Executes the INSERT command, returning primary key values.
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column data (name => value) to be inserted into the table.
* @return array primary key values or false if the command fails
* @since 2.0.4
*/
public function insert($table, $columns)
{
$columns = $this->hardTypeCastValue($table, $columns);
return parent::insert($table, $columns);
}
/**
* ClickHouse Strong typing data cast
* @param $table
* @param $columns
* @return mixed
*/
protected function hardTypeCastValue($table, $columns)
{
$tableSchema = $this->getTableSchema($table);
foreach ($columns as $name => $value) {
$columns[$name] = $tableSchema->columns[$name]->phpTypecast($value);
}
return $columns;
}
/**
* @return QueryBuilder the query builder for this connection.
*/
public function getQueryBuilder()
{
if ($this->_builder === null) {
$this->_builder = $this->createQueryBuilder();
}
return $this->_builder;
}
public function createQueryBuilder()
{
return new \kak\clickhouse\QueryBuilder($this->db);
}
/**
* Quotes a simple table name for use in a query.
* A simple table name should contain the table name only without any schema prefix.
* If the table name is already quoted, this method will do nothing.
* @param string $name table name
* @return string the properly quoted table name
*/
public function quoteSimpleTableName($name)
{
return strpos($name, "`") !== false ? $name : "`" . $name . "`";
}
/**
* Quotes a simple column name for use in a query.
* A simple column name should contain the column name only without any prefix.
* If the column name is already quoted or is the asterisk character '*', this method will do nothing.
* @param string $name column name
* @return string the properly quoted column name
*/
public function quoteSimpleColumnName($name)
{
return strpos($name, '`') !== false || $name === '*' ? $name : '`' . $name . '`';
}
/**
* @inheritdoc
*/
public function createColumnSchemaBuilder($type, $length = null)
{
return new ColumnSchemaBuilder($type, $length, $this->db);
}
/**
* @param string $str
* @return string
*/
public function quoteValue($str)
{
if (!is_string($str)) {
return $str;
}
return "'" . addcslashes(str_replace("'", "''", $str), "\000\n\r\\\032\047") . "'";
}
/**
* @param string $schema
* @return array
*/
public function findTableNames($schema = '')
{
return ArrayHelper::getColumn($this->db->createCommand('SHOW TABLES')->queryAll(), 'name');
}
/**
* Loads the metadata for the specified table.
* @param string $name table name
* @return null|TableSchema DBMS-dependent table metadata, null if the table does not exist.
*/
protected function loadTableSchema($name)
{
$sql = 'SELECT * FROM system.columns WHERE `table`=:name and `database`=:database FORMAT JSON';
$result = $this->db->createCommand($sql, [
':name' => $name,
':database' => $this->db->database === null ? 'default' : $this->db->database
])->queryAll();
if ($result && isset($result[0])) {
$table = new TableSchema();
$table->schemaName = $result[0]['database'];
$table->name = $name;
$table->fullName = $table->schemaName . '.' . $table->name;
foreach ($result as $info) {
$column = $this->loadColumnSchema($info);
$table->columns[$column->name] = $column;
}
return $table;
}
return null;
}
/**
* Loads the column information into a [[ColumnSchema]] object.
* @param array $info column information
* @return ColumnSchema the column schema object
*/
protected function loadColumnSchema($info)
{
$column = $this->createColumnSchema();
$column->name = $info['name'];
$column->dbType = $info['type'];
$column->type = isset($this->typeMap[$column->dbType]) ? $this->typeMap[$column->dbType] : self::TYPE_STRING;
if (preg_match('/^([\w ]+)(?:\(([^\)]+)\))?$/', $column->dbType, $matches)) {
$type = $matches[1];
$column->dbType = $matches[1] . (isset($matches[2]) ? "({$matches[2]})" : '');
if (isset($this->typeMap[$type])) {
$column->type = $this->typeMap[$type];
}
}
$unsignedTypes = ['UInt8', 'UInt16', 'UInt32', 'UInt64'];
if(in_array($column->dbType, $unsignedTypes)) {
$column->unsigned = true;
}
$column->phpType = $this->getColumnPhpType($column);
if (empty($info['default_type'])) {
$column->defaultValue = $info['default_expression'];
}
return $column;
}
/**
* @return ColumnSchema
* @throws \yii\base\InvalidConfigException
*/
protected function createColumnSchema()
{
return Yii::createObject('kak\clickhouse\ColumnSchema');
}
}