-
Notifications
You must be signed in to change notification settings - Fork 15
/
Connection.php
190 lines (163 loc) · 4.23 KB
/
Connection.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
<?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 ClickHouseDB\Client;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
/**
* @method Command createCommand($sql = null, $params = [])
*
* @author Dmitry Bashkarev <dmitry@bashkarev.com>
*/
class Connection extends \yii\db\Connection
{
/**
* @inheritdoc
*/
public $commandClass = 'bashkarev\clickhouse\Command';
/**
* Additional options you can pass to clickhouse client constructor
*
* @var array
*/
public $clientOptions = [];
/**
* @var Client
*/
private $_client;
/**
* @var Schema
*/
private $_schema;
/**
* @inheritdoc
*/
public function __construct($config = [])
{
if (!isset($config['driverName'])) {
$config['driverName'] = 'clickhouse';
}
parent::__construct($config);
}
/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function open()
{
if ($this->_client === null) {
$config = $this->parseDsn();
$this->_client = new Client(array_merge([
'host' => $config['host'] ?? '127.0.0.1',
'port' => $config['port'] ?? 8123,
'username' => $this->username,
'password' => $this->password,
], $this->clientOptions),
array_merge([
'database' => $config['database'] ?? 'default',
], $this->attributes ?? [])
);
}
}
/**
* @throws InvalidConfigException
*/
private function parseDsn(): array
{
$parts = explode(';', $this->dsn);
$config = [];
foreach ($parts as $part) {
$paramValue = explode('=', $part);
if (empty($paramValue[0])) {
throw new InvalidConfigException("Invalid (empty) param name in dsn");
}
if (empty($paramValue[1])) {
throw new InvalidConfigException("Invalid (empty) param '{$paramValue[0]}' value in dsn");
}
$config[$paramValue[0]] = $paramValue[1];
}
return $config;
}
/**
* @inheritdoc
*/
public function close()
{
if ($this->_client !== null) {
$this->_client = null;
}
}
/**
* @param string $sql
* @return \ClickHouseDB\Statement
*/
public function execute(string $sql)
{
$this->open();
return $this->_client->write($sql);
}
/**
* @param string $sql
* @return \ClickHouseDB\Statement
* @throws \Exception
*/
public function executeSelect(string $sql)
{
$this->open();
return $this->_client->select($sql);
}
/**
* @return Schema
* @throws InvalidConfigException
*/
public function getSchema()
{
if ($this->_schema === null) {
$this->_schema = Yii::createObject([
'class' => 'bashkarev\clickhouse\Schema',
'db' => $this
]);
}
return $this->_schema;
}
/**
* @inheritdoc
*/
public function getIsActive()
{
return ($this->_client !== null);
}
public function getClient(): Client
{
return $this->_client;
}
/**
* @inheritdoc
* @throws NotSupportedException
*/
public function beginTransaction($isolationLevel = null)
{
throw new NotSupportedException('In the clickhouse database, transactions are not supported');
}
/**
* @inheritdoc
* @throws NotSupportedException
*/
public function getTransaction($isolationLevel = null)
{
throw new NotSupportedException('In the clickhouse database, transactions are not supported');
}
/**
* @inheritdoc
* @throws NotSupportedException
*/
public function transaction(callable $callback, $isolationLevel = null)
{
throw new NotSupportedException('In the clickhouse database, transactions are not supported');
}
}