This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
forked from tyohan/MongoRecord
-
Notifications
You must be signed in to change notification settings - Fork 58
/
EMongoDB.php
228 lines (208 loc) · 5.83 KB
/
EMongoDB.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
<?php
/**
* EMongoDB.php
*
* PHP version 5.2+
*
* @author Dariusz Górecki <[email protected]>
* @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
* @copyright 2011 CleverIT http://www.cleverit.com.pl
* @license http://www.yiiframework.com/license/ BSD license
* @version 1.3
* @category ext
* @package ext.YiiMongoDbSuite
* @since v1.0
*/
/**
* EMongoDB
*
* This is merge work of tyohan, Alexander Makarov and mine
* @since v1.0
*/
class EMongoDB extends CApplicationComponent
{
/**
* @var string host:port
*
* Correct syntax is:
* mongodb://[username:password@]host1[:port1][,host2[:port2:],...]
*
* @example mongodb://localhost:27017
* @since v1.0
*/
public $connectionString;
/**
* @var boolean $autoConnect whether the Mongo connection should be automatically established when
* the component is being initialized. Defaults to true. Note, this property is only
* effective when the EMongoDB object is used as an application component.
* @since v1.0
*/
public $autoConnect = true;
/**
* @var false|string $persistentConnection false for non-persistent connection, string for persistent connection id to use
* @since v1.0
*/
public $persistentConnection = false;
/**
* @var string $dbName name of the Mongo database to use
* @since v1.0
*/
public $dbName = null;
/**
* @var MongoDB $_mongoDb instance of MongoDB driver
*/
private $_mongoDb;
/**
* @var Mongo $_mongoConnection instance of MongoDB driver
*/
private $_mongoConnection;
/**
* If set to TRUE all internal DB operations will use FSYNC flag with data modification requests,
* in other words, all write operations will have to wait for a disc sync!
*
* MongoDB default value for this flag is: FALSE.
*
* @var boolean $fsyncFlag state of FSYNC flag to use with internal connections (global scope)
* @since v1.0
*/
public $fsyncFlag = false;
/**
* If set to TRUE all internal DB operations will use SAFE flag with data modification requests.
*
* When SAFE flag is set to TRUE driver will wait for the response from DB, and throw an exception
* if something went wrong, is fet to false, driver will only send operation to DB but will not wait
* for response from DB.
*
* MongoDB default value for this flag is: FALSE.
*
* @var boolean $safeFlag state of SAFE flag (global scope)
*/
public $safeFlag = false;
/**
* If set to TRUE findAll* methods of models, will return {@see EMongoCursor} instead of
* raw array of models.
*
* Generally you should want to have this set to TRUE as cursor use lazy-loading/instaninating of
* models, this is set to FALSE, by default to keep backwards compatibility.
*
* Note: {@see EMongoCursor} does not implement ArrayAccess interface and cannot be used like an array,
* because offset access to cursor is highly ineffective and pointless.
*
* @var boolean $useCursor state of Use Cursor flag (global scope)
*/
public $useCursor = false;
/**
* Storage location for temporary files used by the GridFS Feature.
* If set to null, component will not use temporary storage
* @var string $gridFStemporaryFolder
*/
public $gridFStemporaryFolder = null;
/**
* Connect to DB if connection is already connected this method doeas nothing
* @since v1.0
*/
public function connect()
{
if(!$this->getConnection()->connected)
return $this->getConnection()->connect();
}
/**
* Returns Mongo connection instance if not exists will create new
*
* @return Mongo
* @throws EMongoException
* @since v1.0
*/
public function getConnection()
{
if($this->_mongoConnection === null)
{
try
{
Yii::trace('Opening MongoDB connection', 'ext.MongoDb.EMongoDB');
if(empty($this->connectionString))
throw new EMongoException(Yii::t('yii', 'EMongoDB.connectionString cannot be empty.'));
if($this->persistentConnection !== false)
$this->_mongoConnection = new Mongo($this->connectionString, array(
'connect'=>$this->autoConnect,
'persist'=>$this->persistentConnection
));
else
$this->_mongoConnection = new Mongo($this->connectionString, array(
'connect'=>$this->autoConnect,
));
return $this->_mongoConnection;
}
catch(MongoConnectionException $e)
{
throw new EMongoException(Yii::t(
'yii',
'EMongoDB failed to open connection: {error}',
array('{error}'=>$e->getMessage())
), $e->getCode());
}
}
else
return $this->_mongoConnection;
}
/**
* Set the connection
*
* @param Mongo $connection
* @since v1.0
*/
public function setConnection(Mongo $connection)
{
$this->_mongoConnection = $connection;
}
/**
* Get MongoDB instance
* @since v1.0
*/
public function getDbInstance()
{
if($this->_mongoDb === null)
return $this->_mongoDb = $this->getConnection()->selectDB($this->dbName);
else
return $this->_mongoDb;
}
/**
* Set MongoDB instance
* Enter description here ...
* @param string $name
* @since v1.0
*/
public function setDbInstance($name)
{
$this->_mongoDb = $this->getConnection()->selectDb($name);
}
/**
* Closes the currently active Mongo connection.
* It does nothing if the connection is already closed.
* @since v1.0
*/
protected function close(){
if($this->_mongoConnection!==null){
$this->_mongoConnection->close();
$this->_mongoConnection=null;
Yii::trace('Closing MongoDB connection', 'ext.MongoDb.EMongoDB');
}
}
/**
* If we have don't use presist connection, close it
* @since v1.0
*/
public function __destruct(){
if(!$this->persistentConnection){
$this->close();
}
}
/**
* Drop the current DB
* @since v1.0
*/
public function dropDb()
{
$this->_mongoDb->drop();
}
}