PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/lib/mongo/RMongo.php

http://github.com/iwind/rockmongo
PHP | 294 lines | 133 code | 26 blank | 135 comment | 18 complexity | 4b4bddb91ce94c8ddf8a9d1e5451f22f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * RMongo for compatibility between Mongo and MongoClient
  4. *
  5. * @author Liu <q@yun4s.cn>
  6. */
  7. class RMongo {
  8. private static $_lastId;
  9. private $_mongo;
  10. /**
  11. * Contruct a new object
  12. *
  13. * @param string $server Server definition
  14. * @param array $options Options
  15. */
  16. public function __construct($server, array $options = array()) {
  17. if (class_exists("MongoClient")) {
  18. $this->_mongo = new MongoClient($server, $options);
  19. }
  20. else {
  21. $this->_mongo = new Mongo($server, $options);
  22. }
  23. }
  24. /**
  25. * Closes this connection
  26. *
  27. * @param boolean|string $connection Connection
  28. * @return boolean
  29. */
  30. public function close($connection) {
  31. return $this->_mongo->close($connection);
  32. }
  33. /**
  34. * Connects to a database server
  35. */
  36. public function connect() {
  37. return $this->_mongo->connect();
  38. }
  39. /**
  40. * Drops a database
  41. *
  42. * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database
  43. * @return array
  44. */
  45. public function dropDB($db) {
  46. if (!is_object($db)) {
  47. $db = $this->selectDB($db);
  48. }
  49. if (method_exists($db, "drop")) {
  50. return $db->drop();
  51. }
  52. if (method_exists($this->_mongo, "dropDB")) {
  53. $this->_mongo->dropDB($db);
  54. }
  55. }
  56. /**
  57. * Force server to response error
  58. */
  59. public function forceError() {
  60. if (method_exists($this->_mongo, "forceError")) {
  61. return $this->_mongo->forceError();
  62. }
  63. return false;
  64. }
  65. /**
  66. * Gets a database
  67. *
  68. * @param string $dbname The database name
  69. * @return MongoDB
  70. */
  71. public function __get($dbname) {
  72. return $this->_mongo->$dbname;
  73. }
  74. /**
  75. * Updates status for all associated hosts
  76. *
  77. * @return array
  78. * @todo implement it under different versions
  79. */
  80. public function getHosts() {
  81. if (method_exists($this->_mongo, "getHosts")) {
  82. return $this->_mongo->getHosts();
  83. }
  84. return array();
  85. }
  86. /**
  87. * Get the read preference for this connection
  88. *
  89. * @return array
  90. * @todo implement it under different versions
  91. */
  92. public function getReadPreference() {
  93. if (method_exists($this->_mongo, "getReadPreference")) {
  94. return $this->_mongo->getReadPreference();
  95. }
  96. return array();
  97. }
  98. /**
  99. * Get last erro
  100. *
  101. * @return array
  102. */
  103. public function lastError() {
  104. if (method_exists($this->_mongo, "lastError")) {
  105. return $this->_mongo->lastError();
  106. }
  107. return array();
  108. }
  109. /**
  110. * Lists all of the databases available
  111. *
  112. * @return array
  113. */
  114. public function listDBs() {
  115. return $this->_mongo->listDBs();
  116. }
  117. /**
  118. * Connect pair servers
  119. *
  120. * @return boolean
  121. */
  122. public function pairConnect() {
  123. if (method_exists($this->_mongo, "pairConnect")) {
  124. return $this->_mongo->pairConnect();
  125. }
  126. return false;
  127. }
  128. /**
  129. * Create pair persist connection
  130. *
  131. * @param string $username
  132. * @param string $password
  133. * @return boolean
  134. */
  135. public function pairPersistConnect($username = "" , $password = "") {
  136. if (method_exists($this->_mongo, "pairPersistConnect")) {
  137. return $this->_mongo->pairPersistConnect($username, $password);
  138. }
  139. return false;
  140. }
  141. /**
  142. * Create persist connection
  143. *
  144. * @param string $username Username
  145. * @param string $password Password
  146. * @return boolean
  147. */
  148. public function persistConnect($username = "" , $password = "" ) {
  149. if (method_exists($this->_mongo, "persistConnect")) {
  150. return $this->_mongo->persistConnect($username, $password);
  151. }
  152. return false;
  153. }
  154. /**
  155. * Get previous error
  156. *
  157. * @return array
  158. */
  159. public function prevError() {
  160. if (method_exists($this->_mongo, "prevError")) {
  161. return $this->_mongo->prevError();
  162. }
  163. return array();
  164. }
  165. /**
  166. * Reset error
  167. *
  168. * @return array
  169. */
  170. public function resetError() {
  171. if (method_exists($this->_mongo, "resetError")) {
  172. return $this->_mongo->resetError();
  173. }
  174. return array();
  175. }
  176. /**
  177. * Gets a database collection
  178. *
  179. * @param string $db The database name
  180. * @param string $collection The collection name
  181. * @return MongoCollection
  182. */
  183. public function selectCollection($db, $collection) {
  184. return $this->_mongo->selectCollection($db, $collection);
  185. }
  186. /**
  187. * Gets a database
  188. *
  189. * @param string $db The database name
  190. * @return MongoDB
  191. */
  192. public function selectDB($db) {
  193. return $this->_mongo->selectDB($db);
  194. }
  195. /**
  196. * Set the read preference for this connection
  197. *
  198. * @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
  199. * @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
  200. * @return boolean
  201. */
  202. public function setReadPreference($readPreference, array $tags = array()) {
  203. if (method_exists($this->_mongo, "setReadPreference")) {
  204. return $this->_mongo->setReadPreference($readPreference, $tags);
  205. }
  206. return false;
  207. }
  208. /**
  209. * Change slaveOkay setting for this connection
  210. *
  211. * @param boolean $ok If reads should be sent to secondary members of a replica set for all possible queries using this Mongo instance
  212. * @return boolean
  213. */
  214. public function setSlaveOkay($ok) {
  215. if (method_exists($this->_mongo, "setSlaveOkay")) {
  216. return $this->_mongo->setSlaveOkay($ok);
  217. }
  218. return false;
  219. }
  220. /**
  221. * String representation of this connection
  222. *
  223. * @return string
  224. */
  225. public function __toString() {
  226. return $this->_mongo->__toString();
  227. }
  228. /**
  229. * Get mongo driver version
  230. *
  231. * @return string
  232. * @since 1.1.4
  233. */
  234. public static function getVersion() {
  235. if (class_exists("MongoClient")) {
  236. return MongoClient::VERSION;
  237. }
  238. if (class_exists("Mongo")) {
  239. return Mongo::VERSION;
  240. }
  241. return "0";
  242. }
  243. /**
  244. * Compare another version with current version
  245. *
  246. * @param string $version Version to compare
  247. * @return integer -1,0,1
  248. * @since 1.1.4
  249. */
  250. public static function compareVersion($version) {
  251. $currentVersion = self::getVersion();
  252. preg_match("/^[\\.\\d]+/", $currentVersion, $match);
  253. $number = $match[0];
  254. return version_compare($number, $version);
  255. }
  256. static function setLastInsertId($lastId) {
  257. self::$_lastId = $lastId;
  258. }
  259. /**
  260. * Enter description here...
  261. *
  262. * @return string
  263. */
  264. static function lastInsertId() {
  265. return self::$_lastId;
  266. }
  267. }
  268. ?>