forked from yiisoft/yii2-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
158 lines (142 loc) · 4.58 KB
/
Database.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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mongodb;
use yii\base\Object;
use Yii;
/**
* Database represents the Mongo database information.
*
* @property file\Collection $fileCollection Mongo GridFS collection. This property is read-only.
*
* @author Paul Klimov <[email protected]>
* @since 2.0
*/
class Database extends Object
{
/**
* @var Connection MongoDB connection.
*/
public $connection;
/**
* @var string name of this database.
*/
public $name;
/**
* @var Collection[] list of collections.
*/
private $_collections = [];
/**
* @var file\Collection[] list of GridFS collections.
*/
private $_fileCollections = [];
/**
* Returns the Mongo collection with the given name.
* @param string $name collection name
* @param bool $refresh whether to reload the collection instance even if it is found in the cache.
* @return Collection Mongo collection instance.
*/
public function getCollection($name, $refresh = false)
{
if ($refresh || !array_key_exists($name, $this->_collections)) {
$this->_collections[$name] = $this->selectCollection($name);
}
return $this->_collections[$name];
}
/**
* Returns Mongo GridFS collection with given prefix.
* @param string $prefix collection prefix.
* @param bool $refresh whether to reload the collection instance even if it is found in the cache.
* @return file\Collection Mongo GridFS collection.
*/
public function getFileCollection($prefix = 'fs', $refresh = false)
{
if ($refresh || !array_key_exists($prefix, $this->_fileCollections)) {
$this->_fileCollections[$prefix] = $this->selectFileCollection($prefix);
}
return $this->_fileCollections[$prefix];
}
/**
* Selects collection with given name.
* @param string $name collection name.
* @return Collection collection instance.
*/
protected function selectCollection($name)
{
return Yii::createObject([
'class' => 'yii\mongodb\Collection',
'database' => $this,
'name' => $name,
]);
}
/**
* Selects GridFS collection with given prefix.
* @param string $prefix file collection prefix.
* @return file\Collection file collection instance.
*/
protected function selectFileCollection($prefix)
{
return Yii::createObject([
'class' => 'yii\mongodb\file\Collection',
'database' => $this,
'prefix' => $prefix,
]);
}
/**
* Creates MongoDB command associated with this database.
* @param array $document command document contents.
* @return Command command instance.
* @since 2.1
*/
public function createCommand($document = [])
{
return $this->connection->createCommand($document, $this->name);
}
/**
* Creates new collection.
* Note: Mongo creates new collections automatically on the first demand,
* this method makes sense only for the migration script or for the case
* you need to create collection with the specific options.
* @param string $name name of the collection
* @param array $options collection options in format: "name" => "value"
* @return bool whether operation was successful.
* @throws Exception on failure.
*/
public function createCollection($name, $options = [])
{
return $this->createCommand()->createCollection($name, $options);
}
/**
* Drops specified collection.
* @param string $name name of the collection
* @return bool whether operation was successful.
* @since 2.1
*/
public function dropCollection($name)
{
return $this->createCommand()->dropCollection($name);
}
/**
* Returns the list of available collections in this database.
* @param array $condition filter condition.
* @param array $options options list.
* @return array collections information.
* @since 2.1.1
*/
public function listCollections($condition = [], $options = [])
{
return $this->createCommand()->listCollections($condition, $options);
}
/**
* Clears internal collection lists.
* This method can be used to break cycle references between [[Database]] and [[Collection]] instances.
*/
public function clearCollections()
{
$this->_collections = [];
$this->_fileCollections = [];
}
}