This repository has been archived by the owner on Apr 6, 2018. It is now read-only.
forked from Rustavely/yii2-vertica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.php
251 lines (222 loc) · 5.41 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace yii\vertica;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
/**
* @author Vitaliy Zarubint <[email protected]>
* @since 2.0
*/
class Connection extends Component
{
/**
* @event Event an event that is triggered after a DB connection is established
*/
const EVENT_AFTER_OPEN = 'afterOpen';
/**
* @var array the active connect.
*/
public $activeConnect;
/**
* @var string database name
*/
public static $db;
/**
* @var string dsn connect data
*/
public $dsn;
/**
* @var string user database vertica
*/
public $username;
/**
* @var string password database vertica
*/
public $password;
private $_table;
private $_resource;
/**
* @return string database name
*/
public function getDb()
{
if (self::$db === null && $this->activeConnect) {
self::$db = $this->exec('SELECT database_name FROM databases')->scalar();
}
return self::$db;
}
/**
* @param string $table set name table query
*/
public function setTable($table)
{
$this->_table = $table;
}
/**
* @return string table name
*/
public function getTable()
{
return $this->_table;
}
/**
* @param type $sql
* @return \yii\vertica\Connection
*/
public function exec($sql)
{
$this->open();
$this->_resource = odbc_exec($this->activeConnect, $sql);
return $this;
}
/**
* @param string $sql query execute
* @param array $params not worked
* @return boolean
*/
public function execute($sql, $params = [])
{
$stmt = odbc_prepare($this->activeConnect, $sql);
odbc_execute($stmt, $params);
return true;
}
/**
* Get result query in resource
* @return resource
*/
public function resource()
{
return $this->_resource;
}
/**
* @return array
*/
public function one()
{
return odbc_fetch_array($this->_resource);
}
/**
* @return mixed
*/
public function scalar()
{
if ($value = odbc_fetch_array($this->_resource)) {
return array_shift($value);
}
return null;
}
/**
* @return array
*/
public function all()
{
$result = [];
while ($row = odbc_fetch_array($this->_resource)) {
$result[] = $row;
}
return $result;
}
/**
* Returns a value indicating whether the DB connection is established.
* @return boolean whether the DB connection is established
*/
public function getIsActive()
{
$this->open();
return $this->activeConnect !== null;
}
/**
* Establishes a DB connection.
* It does nothing if a DB connection has already been established.
* @throws Exception if connection fails
*/
public function open()
{
if ($this->activeConnect !== null) {
return;
}
try {
$this->activeConnect = odbc_connect($this->dsn, $this->username, $this->password);
} catch (yii\base\ErrorException $ex) {
throw new InvalidConfigException($ex->getMessage());
}
Yii::trace('Opening connection to vertica.', __CLASS__);
$this->initConnection();
}
/**
* Return is connect to params
* @param type $dsn
* @param type $username
* @param type $password
* @param type $error
* @return boolean
*/
public function isConnect($dsn, $username, $password, $error = false)
{
try {
odbc_connect($dsn, $username, $password);
} catch (\yii\base\ErrorException $ex) {
if ($error) {
return $ex->getMessage();
}
return false;
}
return !$error;
}
/**
* Closes the currently active DB connection.
*/
public function close()
{
Yii::trace('Closing connection to vertica.', __CLASS__);
odbc_close($this->activeConnect);
$this->activeConnect = null;
}
/**
* Initializes the DB connection.
* This method is invoked right after the DB connection is established.
* The default implementation triggers an [[EVENT_AFTER_OPEN]] event.
*/
protected function initConnection()
{
$this->trigger(self::EVENT_AFTER_OPEN);
}
/**
* Returns the name of the DB driver for the current [[dsn]].
* @return string name of the DB driver
*/
public function getDriverName()
{
return 'vertica';
}
/**
* Creates a command for execution.
* @param array $config the configuration for the Command class
* @return Command the DB command
*/
public function createCommand($config = [])
{
if (empty($config['db'])) {
$this->open();
$config['db'] = $this;
}
$command = new Command($config);
return $command;
}
/**
* Creates new query builder instance
* @return QueryBuilder
*/
public function getQueryBuilder()
{
return new QueryBuilder($this);
}
public function quoteColumnName($name)
{
return $name;
}
public function quoteTableName($name)
{
return $name;
}
}