PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/lib/mongo/RMongo.php

https://github.com/quibusus/rockmongo
PHP | 296 lines | 134 code | 26 blank | 136 comment | 18 complexity | 4c09aaa12a81915b4e07b4fed668b6d1 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. // Not possible with auth in new version of mongodb
  116. return array();
  117. return $this->_mongo->listDBs();
  118. }
  119. /**
  120. * Connect pair servers
  121. *
  122. * @return boolean
  123. */
  124. public function pairConnect() {
  125. if (method_exists($this->_mongo, "pairConnect")) {
  126. return $this->_mongo->pairConnect();
  127. }
  128. return false;
  129. }
  130. /**
  131. * Create pair persist connection
  132. *
  133. * @param string $username
  134. * @param string $password
  135. * @return boolean
  136. */
  137. public function pairPersistConnect($username = "" , $password = "") {
  138. if (method_exists($this->_mongo, "pairPersistConnect")) {
  139. return $this->_mongo->pairPersistConnect($username, $password);
  140. }
  141. return false;
  142. }
  143. /**
  144. * Create persist connection
  145. *
  146. * @param string $username Username
  147. * @param string $password Password
  148. * @return boolean
  149. */
  150. public function persistConnect($username = "" , $password = "" ) {
  151. if (method_exists($this->_mongo, "persistConnect")) {
  152. return $this->_mongo->persistConnect($username, $password);
  153. }
  154. return false;
  155. }
  156. /**
  157. * Get previous error
  158. *
  159. * @return array
  160. */
  161. public function prevError() {
  162. if (method_exists($this->_mongo, "prevError")) {
  163. return $this->_mongo->prevError();
  164. }
  165. return array();
  166. }
  167. /**
  168. * Reset error
  169. *
  170. * @return array
  171. */
  172. public function resetError() {
  173. if (method_exists($this->_mongo, "resetError")) {
  174. return $this->_mongo->resetError();
  175. }
  176. return array();
  177. }
  178. /**
  179. * Gets a database collection
  180. *
  181. * @param string $db The database name
  182. * @param string $collection The collection name
  183. * @return MongoCollection
  184. */
  185. public function selectCollection($db, $collection) {
  186. return $this->_mongo->selectCollection($db, $collection);
  187. }
  188. /**
  189. * Gets a database
  190. *
  191. * @param string $db The database name
  192. * @return MongoDB
  193. */
  194. public function selectDB($db) {
  195. return $this->_mongo->selectDB($db);
  196. }
  197. /**
  198. * Set the read preference for this connection
  199. *
  200. * @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
  201. * @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
  202. * @return boolean
  203. */
  204. public function setReadPreference($readPreference, array $tags = array()) {
  205. if (method_exists($this->_mongo, "setReadPreference")) {
  206. return $this->_mongo->setReadPreference($readPreference, $tags);
  207. }
  208. return false;
  209. }
  210. /**
  211. * Change slaveOkay setting for this connection
  212. *
  213. * @param boolean $ok If reads should be sent to secondary members of a replica set for all possible queries using this Mongo instance
  214. * @return boolean
  215. */
  216. public function setSlaveOkay($ok) {
  217. if (method_exists($this->_mongo, "setSlaveOkay")) {
  218. return $this->_mongo->setSlaveOkay($ok);
  219. }
  220. return false;
  221. }
  222. /**
  223. * String representation of this connection
  224. *
  225. * @return string
  226. */
  227. public function __toString() {
  228. return $this->_mongo->__toString();
  229. }
  230. /**
  231. * Get mongo driver version
  232. *
  233. * @return string
  234. * @since 1.1.4
  235. */
  236. public static function getVersion() {
  237. if (class_exists("MongoClient")) {
  238. return MongoClient::VERSION;
  239. }
  240. if (class_exists("Mongo")) {
  241. return Mongo::VERSION;
  242. }
  243. return "0";
  244. }
  245. /**
  246. * Compare another version with current version
  247. *
  248. * @param string $version Version to compare
  249. * @return integer -1,0,1
  250. * @since 1.1.4
  251. */
  252. public static function compareVersion($version) {
  253. $currentVersion = self::getVersion();
  254. preg_match("/^[\\.\\d]+/", $currentVersion, $match);
  255. $number = $match[0];
  256. return version_compare($number, $version);
  257. }
  258. static function setLastInsertId($lastId) {
  259. self::$_lastId = $lastId;
  260. }
  261. /**
  262. * Enter description here...
  263. *
  264. * @return string
  265. */
  266. static function lastInsertId() {
  267. return self::$_lastId;
  268. }
  269. }
  270. ?>