-
Notifications
You must be signed in to change notification settings - Fork 15
/
QueryBuilder.php
97 lines (88 loc) · 2.8 KB
/
QueryBuilder.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
<?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\ArrayExpression;
use yii\db\Exception;
/**
* @author Dmitry Bashkarev <dmitry@bashkarev.com>
*/
class QueryBuilder extends \yii\db\QueryBuilder
{
/**
* @var array mapping from abstract column types (keys) to physical column types (values).
*/
public $typeMap = [
Schema::TYPE_CHAR => 'FixedString(1)',
Schema::TYPE_STRING => 'String',
Schema::TYPE_TEXT => 'String',
Schema::TYPE_TINYINT => 'Int8',
Schema::TYPE_SMALLINT => 'Int16',
Schema::TYPE_INTEGER => 'Int32',
Schema::TYPE_BIGINT => 'Int64',
'U'.Schema::TYPE_TINYINT => 'UInt8',
'U'.Schema::TYPE_SMALLINT => 'UInt16',
'U'.Schema::TYPE_INTEGER => 'UInt32',
'U'.Schema::TYPE_BIGINT => 'UInt64',
Schema::TYPE_FLOAT => 'Float32',
Schema::TYPE_DOUBLE => 'Float64',
Schema::TYPE_DECIMAL => 'Decimal(9,2)',
Schema::TYPE_DATETIME => 'DateTime',
Schema::TYPE_TIMESTAMP => 'DateTime',
Schema::TYPE_TIME => 'DateTime',
Schema::TYPE_DATE => 'Date',
Schema::TYPE_BINARY => 'String',
Schema::TYPE_BOOLEAN => 'UInt8',
Schema::TYPE_MONEY => 'Decimal(18,2)',
Schema::TYPE_JSON => 'String'
];
protected function defaultExpressionBuilders()
{
return array_merge(parent::defaultExpressionBuilders(), [
ArrayExpression::class => ArrayExpressionBuilder::class,
]);
}
/**
* @inheritdoc
*/
public function createTable($table, $columns, $options = null)
{
if ($options === null) {
throw new Exception('Specify engine type');
}
return parent::createTable($table, $columns, $options);
}
public function getColumnType($type)
{
// Replacing NULL to Nullable() wrapper
return preg_replace('/^(\w+)(\(\d+(?>\s*,\s*\d+)?\))? NULL(.*)$/i', 'Nullable(\1\2)\3', parent::getColumnType($type));
}
/**
* @inheritdoc
*/
public function buildFrom($tables, &$params)
{
$final = false;
if (isset($params['_final'])) {
$final = $params['_final'];
unset($params['_final']);
}
$from = parent::buildFrom($tables, $params);
if ($final === true) {
$from .= ' FINAL';
}
return $from;
}
/**
* @inheritdoc
*/
public function addColumn($table, $column, $type)
{
return 'ALTER TABLE ' . $this->db->quoteTableName($table)
. ' ADD COLUMN ' . $this->db->quoteColumnName($column) . ' '
. $this->getColumnType($type);
}
}