-
Notifications
You must be signed in to change notification settings - Fork 15
/
Schema.php
140 lines (123 loc) · 3.77 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
<?php
/**
* @copyright Copyright (c) 2017 Dmitry Bashkarev
* @license https://github.com/bashkarev/clickhouse/blob/master/LICENSE
* @link https://github.com/bashkarev/clickhouse#readme
*/
namespace bashkarev\clickhouse;
use yii\db\TableSchema;
/**
* @author Dmitry Bashkarev <[email protected]>
*/
class Schema extends \yii\db\mysql\Schema
{
/**
* UUID columnd data type
*/
const TYPE_UUID = 'UUID';
public $columnSchemaClass = 'bashkarev\clickhouse\ColumnSchema';
/**
* toDo Array(T), Tuple(T1, T2, ...), Nested
* @var array
*/
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_STRING,
'DateTime' => self::TYPE_DATETIME,
'Date' => self::TYPE_DATE,
'Enum8' => self::TYPE_STRING,
'Enum16' => self::TYPE_STRING,
'Decimal' => self::TYPE_DECIMAL,
'Decimal32' => self::TYPE_DECIMAL,
'Decimal64' => self::TYPE_DECIMAL,
'Decimal128' => self::TYPE_DECIMAL,
];
/**
* @inheritdoc
*/
protected function loadTableSchema($name)
{
$table = new TableSchema;
$this->resolveTableNames($table, $name);
return $this->findColumns($table) ? $table : null;
}
/**
* @inheritdoc
*/
protected function findColumns($table)
{
$columns = $this->db->createCommand('SELECT * FROM system.columns WHERE database = currentDatabase() AND table = :name', [':name' => $table->name])->queryAll();
if ($columns === []) {
return false;
}
foreach ($columns as $info) {
$column = $this->loadColumnSchema($info);
$table->columns[$column->name] = $column;
}
return true;
}
/**
* @inheritdoc
*/
protected function loadColumnSchema($info)
{
$column = new $this->columnSchemaClass;
$column->name = $info['name'];
$column->dbType = $info['type'];
$column->unsigned = stripos($column->dbType, 'UInt') === 0 || stripos($column->dbType, '(UInt') !== false; // UInt64, Nullable(UInt32)
foreach ($this->typeMap as $dbType => $type) {
if (preg_match("/\(?$dbType\)?/", $column->dbType)) {
$column->type = $type;
break 1;
}
}
if (isset($info['default_type']) && $info['default_type'] !== '') {
$column->defaultValue = $info['default_type'];
}
if (isset($info['default_kind']) && $info['default_kind'] !== '') {
$column->defaultValue = $info['default_kind'];
}
if (
$column->type === self::TYPE_STRING
&& preg_match('/^FixedString\((\d+)\)$/', $column->dbType, $out)
) {
$column->size = (int)$out[1];
}
$column->phpType = $this->getColumnPhpType($column);
return $column;
}
/**
* @inheritdoc
*/
public function quoteValue($str)
{
if (!is_string($str)) {
return $str;
}
return "'" . addcslashes($str, "\000\n\r\\\032\047") . "'";
}
/**
* @return QueryBuilder
*/
public function createQueryBuilder()
{
return new QueryBuilder($this->db);
}
/**
* {@inheritdoc}
*/
public function createColumnSchemaBuilder($type, $length = null)
{
return new ColumnSchemaBuilder($type, $length, $this->db);
}
}