-
Notifications
You must be signed in to change notification settings - Fork 12
/
Client.php
441 lines (390 loc) · 11.9 KB
/
Client.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
namespace sammaye\mongoyii;
use ReflectionClass;
use yii;
use CApplicationComponent;
use CValidator;
use MongoDB\Client as DriverClient;
use MongoDB\Database as DriverDatabase;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\BSON\ObjectID;
use sammaye\mongoyii\Database;
use sammaye\mongoyii\Exception;
/**
* Client
*
* The MongoDB and MongoClient class combined.
*
* Quite deceptively the magic functions of this class actually represent the DATABASE not the connection.
* This is in contrast to MongoClient whos' own represent the SERVER.
*
* Normally this would represent the MongoClient or Mongo and it is even named after them and implements
* some of their functions but it is not due to the way Yii works.
*/
class Client extends CApplicationComponent
{
/**
* The server string (connection string pre-1.3)
* @var string
*/
public $uri;
/**
* Additional options for the connection constructor
* @var array
*/
public $options = [];
/**
* @var array
*/
public $driverOptions = [];
/**
* The name of the database
* @var string
*/
public $db;
/**
* Enables logging to the profiler
* @var boolean
*/
public $enableProfiling = false;
/**
* Connection status
* @var bool
*/
protected $connectFlag = false;
/**
* @var integer number of seconds that query results can remain valid in cache.
* Use 0 or negative value to indicate not caching query results (the default behavior).
*
* In order to enable query caching, this property must be a positive
* integer and {@link queryCacheID} must point to a valid cache component ID.
*
* The method {@link cache()} is provided as a convenient way of setting this property
* and {@link queryCachingDependency} on the fly.
*
* @see cache
* @see queryCachingDependency
* @see queryCacheID
*/
public $queryCachingDuration = 0;
/**
* @var \CCacheDependency|\ICacheDependency the dependency that will be used when saving query results into cache.
* @see queryCachingDuration
*/
public $queryCachingDependency;
/**
* @var integer the number of SQL statements that need to be cached next.
* If this is 0, then even if query caching is enabled, no query will be cached.
* Note that each time after executing a SQL statement (whether executed on DB server or fetched from
* query cache), this property will be reduced by 1 until 0.
*/
public $queryCachingCount = 0;
/**
* @var string the ID of the cache application component that is used for query caching.
* Defaults to 'cache' which refers to the primary cache application component.
* Set this property to false if you want to disable query caching.
*/
public $queryCacheID = 'cache';
/**
* The Mongo Connection instance
* @var DriverClient
*/
private $client;
/**
* The database instance
* @var \MongoDB
*/
private $dbs;
private $activeDb;
/**
* Caches reflection properties for our objects so we don't have
* to keep getting them
* @var array
*/
private $meta = array();
/**
* The default action is to find a getX whereby X is the $k param
* you input.
* The secondary function, if not getter found, is to get a collection
*/
public function __get($k)
{
$getter = 'get' . $k;
if(method_exists($this, $getter)){
return $this->$getter();
}
return $this->selectDatabase($k);
}
/**
* Will call a function on the database or error out stating that the function does not exist
* @param string $name
* @param array $parameters
* @return mixed
*/
public function __call($name, $parameters = array())
{
if(!method_exists($this->getClient(), $name)){
return parent::__call($name, $parameters);
}
return call_user_func_array(array($this->getClient(), $name), $parameters);
}
public function init()
{
if(!extension_loaded('mongodb')){
throw new Exception(
Yii::t(
'yii',
'We could not find the MongoDB extension ( http://php.net/manual/en/mongodb.installation.php ), please install it'
)
);
}
// We copy this function to add the subdocument validator as a built in validator
CValidator::$builtInValidators['subdocument'] = 'sammaye\mongoyii\validators\SubdocumentValidator';
parent::init();
}
/**
* init Mongodb Connect
* @throws \sammaye\mongoyii\Exception
*/
public function connect() {
if($this->connectFlag){
return ;
}
$this->client = new DriverClient($this->uri, $this->options, $this->driverOptions);
$this->connectFlag = true;
if(!is_array($this->db)){
throw new Exception(Yii::t(
'yii',
'The db option needs to be an array of databases indexed by name'
));
}else{
foreach($this->db as $k => $v){
$options = [];
if(is_numeric($k)){
$name = $v;
}else{
$name = $k;
$options = $v;
}
$this->selectDatabase($name, $options);
}
}
}
/**
* Gets the connection object
* Use this to access the Mongo/MongoClient instance within the extension
* @return DriverClient
*/
public function getClient()
{
return $this->client;
}
/**
* Selects a different database
* @param null $name
* @param array $options
* @return null|\sammaye\mongoyii\Database
* @throws \sammaye\mongoyii\Exception
*/
public function selectDatabase($name = null, $options = [])
{
$this->connect();
if(isset($options['active'])){
$this->activeDb = $name;
}
if($name){
if(isset($this->dbs[$name])){
return $this->dbs[$name];
}
$db = $this->dbs[$name] = new Database(
$this->getClient()->selectDatabase($name, $options),
$this
);
return $db;
}
// If we have a default database set let's go looking for it
if($this->activeDb && isset($this->dbs[$this->activeDb])){
return $this->dbs[$this->activeDb];
}elseif($this->activeDb){
throw new Exception($name . ' is default but does not exist');
}
// By default let's return the first in the list
foreach($this->dbs as $db){
return $db;
}
return null;
}
public function dropDatabase($databaseName, $options = [])
{
if($this->client->dropDatabase($databaseName, $options)){
unset($this->dbs[$databaseName]);
}
}
/**
* A wrapper for the original processing
* @param string $collectionName
* @param string $databaseName
* @param array $options
* @return \MongoCollection
*/
public function selectCollection($collectionName, $options = [], $databaseName = null)
{
if(!$databaseName){
$databaseName = $this->selectDatabase()->getDatabaseName();
}
return $this->getClient()->selectCollection($databaseName, $collectionName, $options);
}
/**
* Sets the document cache for any particular document (EMongoDocument/EMongoModel)
* sent in as the first parameter of this function.
* Will not cache actual EMongoDocument/EMongoModel instances
* only active classes that inherit these
* @param $o
*/
public function setDocumentCache($o)
{
if(
$this->getDocumentCache(get_class($o)) === array() && // Run reflection and cache it if not already there
(get_class($o) != 'Document' && get_class($o) != 'Model') /* We can't cache the model */
){
$_meta = array();
$reflect = new ReflectionClass(get_class($o));
$class_vars = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC); // Pre-defined doc attributes
foreach($class_vars as $prop){
if($prop->isStatic()){
continue;
}
$docBlock = $prop->getDocComment();
$field_meta = array(
'name' => $prop->getName(),
'virtual' => $prop->isProtected() || preg_match('/@virtual/i', $docBlock) <= 0 ? false : true
);
// Lets fetch the data type for this field
// Since we always fetch the data type for this field we make a regex that will only pick out the first
if(preg_match('/@var ([a-zA-Z]+)/', $docBlock, $matches) > 0){
$field_meta['type'] = $matches[1];
}
$_meta[$prop->getName()] = $field_meta;
}
$this->meta[get_class($o)] = $_meta;
}
}
/**
* Get a list of the fields (attributes) for a document from cache
* @param string $name
* @param boolean $include_virtual
* @return array
*/
public function getFieldCache($name, $include_virtual = false)
{
$doc = isset($this->meta[$name]) ? $this->meta[$name] : array();
$fields = array();
foreach($doc as $name => $opts){
if($include_virtual || !$opts['virtual']){
$fields[] = $name;
}
}
return $fields;
}
/**
* Just gets the document cache for a model
* @param string $name
* @return array
*/
public function getDocumentCache($name)
{
return isset($this->meta[$name]) ? $this->meta[$name] : array();
}
/**
* Create ObjectId from timestamp.
* This function is not actively used it is
* here as a helper for anyone who needs it
* @param int $yourTimestamp
* @return \MongoId
*/
public function createMongoIdFromTimestamp($yourTimestamp)
{
static $inc = 0;
$ts = pack('N', $yourTimestamp);
$m = substr(md5(gethostname ()), 0, 3);
$pid = pack('n', getmypid());
$trail = substr(pack('N', $inc ++), 1, 3);
$bin = sprintf("%s%s%s%s", $ts, $m, $pid, $trail);
$id = '';
for($i = 0; $i < 12; $i ++){
$id .= sprintf("%02X", ord($bin[$i]));
}
return new ObjectID($id);
}
/**
* Sets the parameters for query caching.
* This method can be used to enable or disable query caching.
* By setting the $duration parameter to be 0, the query caching will be disabled.
* Otherwise, query results of the new SQL statements executed next will be saved in cache
* and remain valid for the specified duration.
* If the same query is executed again, the result may be fetched from cache directly
* without actually executing the SQL statement.
*
* @param integer $duration
* the number of seconds that query results may remain valid in cache.
* If this is 0, the caching will be disabled.
* @param \CCacheDependency|\ICacheDependency $dependency
* the dependency that will be used when saving
* the query results into cache.
* @param integer $queryCount
* number of SQL queries that need to be cached after calling this method. Defaults to 1,
* meaning that the next SQL query will be cached.
* @return static the connection instance itself.
* @since 1.1.7
*/
public function cache($duration, $dependency = null, $queryCount = 1)
{
$this->queryCachingDuration = $duration;
$this->queryCachingDependency = $dependency;
$this->queryCachingCount = $queryCount;
return $this;
}
public function getSerialisedQuery($criteria = array(), $fields = array(), $sort = array(), $skip = 0, $limit = null)
{
$query = array(
'$query' => $criteria,
'$fields' => $fields,
'$sort' => $sort,
'$skip' => $skip,
'$limit' => $limit
);
return json_encode($query);
}
/**
*
* @return array the first element indicates the number of query statements executed,
* and the second element the total time spent in query execution.
*/
public function getStats()
{
$logger = Yii::getLogger();
$timings = $logger->getProfilingResults(null, 'extensions.MongoYii.EMongoDocument.findOne');
$count = count($timings);
$time = array_sum($timings);
$timings = $logger->getProfilingResults(null, 'extensions.MongoYii.EMongoDocument.insert');
$count += count($timings);
$time += array_sum($timings);
$timings = $logger->getProfilingResults(null, 'extensions.MongoYii.EMongoDocument.find');
$count += count($timings);
$time += array_sum($timings);
$timings = $logger->getProfilingResults(null, 'extensions.MongoYii.EMongoDocument.deleteByPk');
$count += count($timings);
$time += array_sum($timings);
$timings = $logger->getProfilingResults(null, 'extensions.MongoYii.EMongoDocument.updateByPk');
$count += count($timings);
$time += array_sum($timings);
$timings = $logger->getProfilingResults(null, 'extensions.MongoYii.EMongoDocument.updateAll');
$count += count($timings);
$time += array_sum($timings);
$timings = $logger->getProfilingResults(null, 'extensions.MongoYii.EMongoDocument.deleteAll');
$count += count($timings);
$time += array_sum($timings);
return array($count, $time);
}
}