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
/
Adapter.php
179 lines (148 loc) · 5.94 KB
/
Adapter.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
<?php
namespace ext\activedocument;
use \Yii,
\CComponent;
abstract class Adapter extends CComponent {
/**
* @var \ext\activedocument\Connection
*/
protected $_connection;
protected $_storageInstance;
/**
* @var array
*/
protected $_containers = array();
/**
* @var array
*/
protected $_cacheExclude = array();
abstract protected function loadStorageInstance(array $attributes = null);
/**
* @abstract
* @param string $name
* @return \ext\activedocument\Container
*/
abstract protected function loadContainer($name);
/**
* @abstract
* @param \ext\activedocument\Criteria $criteria
* @return int
*/
abstract protected function countInternal(Criteria $criteria);
/**
* @abstract
* @param \ext\activedocument\Criteria $criteria
* @return \ext\activedocument\Object[]
*/
abstract protected function findInternal(Criteria $criteria);
/**
* @param \ext\activedocument\Connection $conn
* @param array|null $attributes optional
*/
public function __construct(Connection $conn, array $attributes = null) {
$this->_connection = $conn;
foreach ($conn->schemaCachingExclude as $name)
$this->_cacheExclude[$name] = true;
$this->_storageInstance = $this->loadStorageInstance($attributes);
}
public function __get($name) {
if (property_exists($this->_storageInstance, $name) || ($this->_storageInstance instanceof CComponent && $this->_storageInstance->canGetProperty($name)))
return $this->_storageInstance->$name;
return parent::__get($name);
}
public function __set($name, $value) {
if (property_exists($this->_storageInstance, $name) || ($this->_storageInstance instanceof CComponent && $this->_storageInstance->canSetProperty($name)))
$this->_storageInstance->$name = $value;
else
return parent::__set($name, $value);
}
public function __isset($name) {
if (property_exists($this->_storageInstance, $name) || ($this->_storageInstance instanceof CComponent && $this->_storageInstance->canGetProperty($name)))
return $this->_storageInstance->$name !== null;
return parent::__isset($name);
}
public function __unset($name) {
if (property_exists($this->_storageInstance, $name) || ($this->_storageInstance instanceof CComponent && $this->_storageInstance->canSetProperty($name)))
return $this->_storageInstance->$name = null;
return parent::__unset($name);
}
public function getStorageInstance() {
return $this->_storageInstance;
}
/**
* @return \ext\activedocument\Connection Data storage connection. The connection is active.
*/
public function getConnection() {
return $this->_connection;
}
/**
* @return array
*/
public function getConfig() {
return $this->_connection->attributes;
}
/**
* @param string $name
* @param array $config
* @return \ext\activedocument\Container
*/
public function getContainer($name, array $config = array()) {
if (isset($this->_containers[$name]))
return $this->_containers[$name];
else {
if (strpos($name, '{{') !== false)
$realName = preg_replace('/\{\{(.*?)\}\}/', $this->_connection->containerPrefix . '$1', $name);
else
$realName = $name;
// temporarily disable query caching
/* if($this->_connection->queryCachingDuration>0)
{
$qcDuration=$this->_connection->queryCachingDuration;
$this->_connection->queryCachingDuration=0;
} */
if (!isset($this->_cacheExclude[$name]) && ($duration = $this->_connection->schemaCachingDuration) > 0 && $this->_connection->schemaCacheID !== false && ($cache = Yii::app()->getComponent($this->_connection->schemaCacheID)) !== null) {
$key = 'activedocument.storageschema.' . $this->_connection->driver . '.' . $name;
if (($container = $cache->get($key)) === false) {
$container = $this->loadContainer($realName);
if ($container !== null)
$cache->set($key, $container, $duration);
}
}
else
$container = $this->loadContainer($realName);
if (!empty($config))
$container->setConfig($config);
/* if(isset($qcDuration)) // re-enable query caching
$this->_connection->queryCachingDuration=$qcDuration; */
return $this->_containers[$name] = $container;
}
}
/**
* @param \ext\activedocument\Criteria $criteria
* @return int
*/
public function count(Criteria $criteria) {
if ($this->getConnection()->enableProfiling) {
$profileToken = 'ext.activedocument.query.count(' . \CVarDumper::dumpAsString(array_filter($criteria->toArray())) . ')';
Yii::beginProfile($profileToken, 'ext.activedocument.query.count');
}
$result = $this->countInternal($criteria);
if ($this->getConnection()->enableProfiling)
Yii::endProfile($profileToken, 'ext.activedocument.query.count');
return $result;
}
/**
* @param \ext\activedocument\Criteria $criteria
* @return \ext\activedocument\Object[]
*/
public function find(Criteria $criteria) {
if ($this->getConnection()->enableProfiling) {
$profileToken = 'ext.activedocument.query.find(' . \CVarDumper::dumpAsString(array_filter($criteria->toArray())) . ')';
Yii::beginProfile($profileToken, 'ext.activedocument.query.find');
}
$result = $this->findInternal($criteria);
if ($this->getConnection()->enableProfiling)
Yii::endProfile($profileToken, 'ext.activedocument.query.find');
return $result;
}
}