rockmongo /app/lib/mongo/RMongo.php

Language PHP Lines 294
MD5 Hash 4b4bddb91ce94c8ddf8a9d1e5451f22f Estimated Cost $3,197 (why?)
Repository git://github.com/iwind/rockmongo.git View Raw File
  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
<?php
/**
 * RMongo for compatibility between Mongo and MongoClient
 *
 * @author Liu <q@yun4s.cn>
 */
class RMongo {
	private static $_lastId;
	
	private $_mongo;
	
	/**
	 * Contruct a new object
	 * 
	 * @param string $server Server definition
	 * @param array $options Options
	 */
	public function __construct($server, array $options = array()) {
		if (class_exists("MongoClient")) {
			$this->_mongo = new MongoClient($server, $options);
		}
		else {
			$this->_mongo = new Mongo($server, $options);
		}
	}
	
	/**
	 * Closes this connection
	 * 
	 * @param boolean|string $connection Connection
	 * @return boolean
	 */
	public function close($connection) {
		return $this->_mongo->close($connection);
	}
	
	/**
	 * Connects to a database server
	 */
	public function connect() {
		return $this->_mongo->connect();
	}
	
	/**
	 * Drops a database
	 * 
	 * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database
	 * @return array
	 */
	public function dropDB($db) {
		if (!is_object($db)) {
			$db = $this->selectDB($db);
		} 
		if (method_exists($db, "drop")) {
			return $db->drop();
		}
		if (method_exists($this->_mongo, "dropDB")) {
			$this->_mongo->dropDB($db);
		}
	}
	
	/**
	 * Force server to response error
	 */
	public function forceError() {
		if (method_exists($this->_mongo, "forceError")) {
			return $this->_mongo->forceError();
		}
		return false;
	}
	
	/**
	 * Gets a database
	 * 
	 * @param string $dbname The database name
	 * @return MongoDB
	 */
	public function __get($dbname) {
		return $this->_mongo->$dbname;
	}
	
	/**
	 * Updates status for all associated hosts
	 * 
	 * @return array
	 * @todo implement it under different versions
	 */
	public function getHosts() {
		if (method_exists($this->_mongo, "getHosts")) {
			return $this->_mongo->getHosts();
		}
		return array();
	}
	
	/**
	 * Get the read preference for this connection
	 * 
	 * @return array
	 * @todo implement it under different versions
	 */
	public function getReadPreference() {
		if (method_exists($this->_mongo, "getReadPreference")) {
			return $this->_mongo->getReadPreference();
		}
		return array();
	}
	
	/**
	 * Get last erro
	 *
	 * @return array
	 */
	public function lastError() {
		if (method_exists($this->_mongo, "lastError")) {
			return $this->_mongo->lastError();
		}
		return array();
	}
	
	/**
	 * Lists all of the databases available
	 * 
	 * @return array
	 */
	public function listDBs() {
		return $this->_mongo->listDBs();
	}
	
	/**
	 * Connect pair servers
	 * 
	 * @return boolean
	 */
	public function pairConnect() {
		if (method_exists($this->_mongo, "pairConnect")) {
			return $this->_mongo->pairConnect();
		} 
		return false;
	}
	
	/**
	 * Create pair persist connection
	 * 
	 * @param string $username
	 * @param string $password
	 * @return boolean
	 */
	public function pairPersistConnect($username = "" , $password = "") {
		if (method_exists($this->_mongo, "pairPersistConnect")) {
			return $this->_mongo->pairPersistConnect($username, $password);
		}
		return false;
	}
	
	/**
	 * Create persist connection
	 * 
	 * @param string $username Username
	 * @param string $password Password
	 * @return boolean
	 */
	public function persistConnect($username = "" , $password = "" ) {
		if (method_exists($this->_mongo, "persistConnect")) {
			return $this->_mongo->persistConnect($username, $password);
		}
		return false;
	}
	
	/**
	 * Get previous error
	 * 
	 * @return array
	 */
	public function prevError() {
		if (method_exists($this->_mongo, "prevError")) {
			return $this->_mongo->prevError();
		}
		return array();
	}
	
	/**
	 * Reset error
	 * 
	 * @return array
	 */
	public function resetError() {
		if (method_exists($this->_mongo, "resetError")) {
			return $this->_mongo->resetError();
		}
		return array();
	}
	
	/**
	 * Gets a database collection
	 * 
	 * @param string $db The database name
	 * @param string $collection The collection name
	 * @return MongoCollection
	 */
	public function selectCollection($db, $collection) {
		return $this->_mongo->selectCollection($db, $collection);
	}
	
	/**
	 * Gets a database
	 * 
	 * @param string $db The database name
	 * @return MongoDB
	 */
	public function selectDB($db) {
		return $this->_mongo->selectDB($db);
	}
	
	/**
	 * Set the read preference for this connection
	 * 
	 * @param int $readPreference The read preference mode: Mongo::RP_PRIMARY, Mongo::RP_PRIMARY_PREFERRED, Mongo::RP_SECONDARY, Mongo::RP_SECONDARY_PREFERRED, or Mongo::RP_NEAREST
	 * @param array $tags An array of zero or more tag sets, where each tag set is itself an array of criteria used to match tags on replica set members
	 * @return boolean
	 */
	public function setReadPreference($readPreference, array $tags = array()) {
		if (method_exists($this->_mongo, "setReadPreference")) {
			return $this->_mongo->setReadPreference($readPreference, $tags);
		}
		return false;
	}
	
	/**
	 * Change slaveOkay setting for this connection
	 *
	 * @param boolean $ok If reads should be sent to secondary members of a replica set for all possible queries using this Mongo instance
	 * @return boolean
	 */
	public function setSlaveOkay($ok) {
		if (method_exists($this->_mongo, "setSlaveOkay")) {
			return $this->_mongo->setSlaveOkay($ok);
		}
		return false;
	}
	
	/**
	 * String representation of this connection
	 * 
	 * @return string
	 */
	public function __toString() {
		return $this->_mongo->__toString();
	}
	
	/**
	 * Get mongo driver version
	 * 
	 * @return string
	 * @since 1.1.4
	 */
	public static function getVersion() {
		if (class_exists("MongoClient")) {
			return MongoClient::VERSION;
		}
		if (class_exists("Mongo")) {
			return Mongo::VERSION;
		}
		return "0";
	}
	
	/**
	 * Compare another version with current version
	 * 
	 * @param string $version Version to compare
	 * @return integer -1,0,1
	 * @since 1.1.4
	 */
	public static function compareVersion($version) {
		$currentVersion = self::getVersion();
		preg_match("/^[\\.\\d]+/", $currentVersion, $match);
		$number = $match[0];
		return version_compare($number, $version); 
	}
	
	static function setLastInsertId($lastId) {
		self::$_lastId = $lastId;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return string
	 */
	static function lastInsertId() {
		return self::$_lastId;
	}
}

?>
Back to Top