This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnection.php
232 lines (197 loc) · 6.61 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
<?php
namespace ext\activedocument;
use \Yii,
\CApplicationComponent,
\CLogger;
class Connection extends CApplicationComponent {
public $driver;
public $schemaCachingDuration = 0;
public $schemaCachingExclude = array();
public $schemaCacheID = 'activecache';
/* public $queryCachingDuration=0;
public $queryCachingDependency;
public $queryCachingCount=0;
public $queryCacheID='cache'; */
public $autoConnect = true;
/* public $emulatePrepare=false;
public $enableParamLogging=false;
public $enableProfiling=false; */
public $containerPrefix;
/* public $initSQLs; */
public $driverMap = array(
'memory' => '\ext\activedocument\drivers\memory\Adapter',
'riak' => '\ext\activedocument\drivers\riak\Adapter',
'mongo' => '\ext\activedocument\drivers\mongo\Adapter',
#'redis' => '\ext\activedocument\drivers\redis\Adapter',
);
protected $_attributes = array();
private $_active = false;
/**
* @var \ext\activedocument\Adapter
*/
protected $_adapter;
/**
* When enabled, profiles requests using Yii::beginProfile && Yii::endProfile
*
* @var bool
*/
public $enableProfiling = false;
public function __construct($driver = '', array $attributes = array()) {
$this->driver = $driver;
$this->_attributes = $attributes;
}
public function __sleep() {
$this->close();
return array_keys(get_object_vars($this));
}
public function init() {
parent::init();
if ($this->autoConnect)
$this->setActive(true);
}
public function getActive() {
return $this->_active;
}
public function setActive($value) {
if ($value != $this->_active) {
if ($value)
$this->open();
else
$this->close();
}
}
/* public function cache($duration, $dependency=null, $queryCount=1)
{
$this->queryCachingDuration=$duration;
$this->queryCachingDependency=$dependency;
$this->queryCachingCount=$queryCount;
return $this;
} */
protected function open() {
if ($this->_adapter === null) {
if (empty($this->driver))
throw new Exception(Yii::t('yii', 'Connection.driver cannot be empty.'));
try {
Yii::trace('Opening data storage connection', 'ext.activedocument.Connection');
$this->_adapter = $this->createConnectionInstance();
$this->_active = true;
} catch (Exception $e) {
if (YII_DEBUG) {
throw new Exception(Yii::t('yii', 'Connection failed to open the data storage connection: {error}', array('{error}' => $e->getMessage())), (int)$e->getCode(), $e->errorInfo);
} else {
Yii::log($e->getMessage(), CLogger::LEVEL_ERROR, 'exception.ActiveDocument.Exception');
throw new Exception(Yii::t('yii', 'Connection failed to open the data storage connection.'), (int)$e->getCode(), $e->errorInfo);
}
}
}
}
protected function close() {
Yii::trace('Closing data storage connection', 'ext.activedocument.Connection');
$this->_adapter = null;
$this->_active = false;
}
protected function createConnectionInstance() {
if (isset($this->driverMap[$this->driver]))
return new $this->driverMap[$this->driver]($this, $this->_attributes);
else
throw new Exception(Yii::t('yii', 'Connection does not support {driver} storage adapter.', array('{driver}' => $this->driver)));
}
/**
* @return \ext\activedocument\Adapter
*/
public function getAdapter() {
$this->setActive(true);
return $this->_adapter;
}
/* public function getColumnCase() {
return $this->getAttribute(ACO::ATTR_CASE);
}
public function setColumnCase($value) {
$this->setAttribute(ACO::ATTR_CASE, $value);
}
public function getNullConversion() {
return $this->getAttribute(ACO::ATTR_ORACLE_NULLS);
}
public function setNullConversion($value) {
$this->setAttribute(ACO::ATTR_ORACLE_NULLS, $value);
}
public function getAutoCommit() {
return $this->getAttribute(ACO::ATTR_AUTOCOMMIT);
}
public function setAutoCommit($value) {
$this->setAttribute(ACO::ATTR_AUTOCOMMIT, $value);
}
public function getPersistent() {
return $this->getAttribute(ACO::ATTR_PERSISTENT);
}
public function setPersistent($value) {
return $this->setAttribute(ACO::ATTR_PERSISTENT, $value);
}
public function getClientVersion() {
return $this->getAttribute(ACO::ATTR_CLIENT_VERSION);
}
public function getConnectionStatus() {
return $this->getAttribute(ACO::ATTR_CONNECTION_STATUS);
}
public function getPrefetch() {
return $this->getAttribute(ACO::ATTR_PREFETCH);
}
public function getServerInfo() {
return $this->getAttribute(ACO::ATTR_SERVER_INFO);
}
public function getServerVersion() {
return $this->getAttribute(ACO::ATTR_SERVER_VERSION);
}
public function getTimeout() {
return $this->getAttribute(ACO::ATTR_TIMEOUT);
} */
/**
* @todo Needs refactoring
*
* @param $name
* @return mixed
*/
public function getAttribute($name) {
$this->setActive(true);
return $this->_adapter->getAttribute($name);
}
/**
* @todo Needs refactoring
*
* @param $name
* @param $value
*/
public function setAttribute($name, $value) {
if ($this->_adapter instanceof Adapter)
$this->_adapter->setAttribute($name, $value);
else
$this->_attributes[$name] = $value;
}
/**
* @todo Needs refactoring
*
* @return array
*/
public function getAttributes() {
return $this->_attributes;
}
/**
* @todo Needs refactoring
*
* @param $values
*/
public function setAttributes($values) {
foreach ($values as $name => $value)
$this->_attributes[$name] = $value;
}
public function getStats() {
$logger = Yii::getLogger();
$timings = $logger->getProfilingResults(null, 'ext.activedocument.query.*');
$count = count($timings);
$time = array_sum($timings);
$timings = $logger->getProfilingResults(null, 'ext.activedocument.execute.*');
$count += count($timings);
$time += array_sum($timings);
return array($count, $time);
}
}