You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.9 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage db
  7. * @since 2011-11-10
  8. */
  9. /**
  10. * @property Mongo $connection
  11. */
  12. class MongoDriver extends NoSqlDbDriver
  13. {
  14. protected $last_insert_id = 0;
  15. protected $db_name = 'admin';
  16. protected $db = null;
  17. protected function getCollection($name)
  18. {
  19. if (!$this->connection) {
  20. $this->connect();
  21. }
  22. return $this->connection->selectCollection($this->db, $name);
  23. }
  24. public function count($collection, $query = array(), $limit = 0, $skip = 0)
  25. {
  26. $command = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT, $this->getCollection($collection));
  27. $params = array(
  28. 'condition' => $query,
  29. 'limit' => $limit,
  30. 'skip' => $skip
  31. );
  32. return $this->query($command, $params)->affectedRows();
  33. }
  34. /**
  35. * @param string $collection Mongo collection name to search in
  36. * @param array $condition Search conditions
  37. * @param array $fields Fields to return in result set
  38. * @return MongoStatement
  39. */
  40. public function find($collection, $condition = array(), $fields = array())
  41. {
  42. $command = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->getCollection($collection));
  43. $params = array(
  44. 'condition' => $condition,
  45. 'fields' => $fields
  46. );
  47. return $this->query($command, $params);
  48. }
  49. public function findAndModify($collection, $query, $update, $sort = array(), $field = array(), $upsert = false, $new = false, $remove = false)
  50. {
  51. $command = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->getCollection($collection));
  52. $cmd = array(
  53. 'findAndModify' => $collection,
  54. 'query' => $query,
  55. 'update' => $update,
  56. 'sort' => $sort,
  57. 'fields' => $field,
  58. 'new' => $new,
  59. 'upsert' => $upsert,
  60. 'remove' => $remove
  61. );
  62. $params = array('command' => $cmd);
  63. return $this->query($command, $params);
  64. }
  65. public function command($collection, $cmd)
  66. {
  67. $command = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->getCollection($collection));
  68. $params = array('command' => $cmd);
  69. return $this->query($command, $params);
  70. }
  71. public function insert($collection, $data, $multiple = false, $safe = true)
  72. {
  73. $command = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->getCollection($collection));
  74. $params = array(
  75. 'data' => $data,
  76. 'safe' => $safe,
  77. 'multiple' => $multiple
  78. );
  79. $result = $this->query($command, $params);
  80. $this->last_insert_id = $result->getInsertId();
  81. return $result->affectedRows();
  82. }
  83. public function update($collection, $data, $condition = array(), $multiple = true, $upsert = false, $safe = true)
  84. {
  85. $command = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->getCollection($collection));
  86. $params = array(
  87. 'data' => $data,
  88. 'condition' => $condition,
  89. 'multiple' => $multiple,
  90. 'upsert' => $upsert,
  91. 'safe' => $safe
  92. );
  93. return $this->query($command, $params)->affectedRows();
  94. }
  95. public function delete($collection, $condition = array(), $safe = true)
  96. {
  97. $command = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->getCollection($collection));
  98. $params = array(
  99. 'condition' => $condition,
  100. 'safe' => $safe
  101. );
  102. return $this->query($command, $params)->affectedRows();
  103. }
  104. /**
  105. * @param mixed $request
  106. * @return MongoStatement
  107. */
  108. public function prepare($request)
  109. {
  110. return new MongoStatement($this, $request);
  111. }
  112. public function getInsertId($table = null, $key = null)
  113. {
  114. return $this->last_insert_id;
  115. }
  116. public function isConnected()
  117. {
  118. if (!is_null($this->connection)) {
  119. return $this->connection->connected;
  120. } else {
  121. return false;
  122. }
  123. }
  124. public function disconnect()
  125. {
  126. if ($this->isConnected()) {
  127. $this->connection->close();
  128. }
  129. $this->connection = null;
  130. }
  131. protected function connect()
  132. {
  133. if ($this->connection) {
  134. return;
  135. }
  136. $host = $this->config['hostname'];
  137. $port = isset($this->config['port']) ? ':' . (string) $this->config['port'] : '';
  138. $this->config = array(
  139. 'username' => $this->config['username'],
  140. 'password' => $this->config['password'],
  141. 'db' => $this->config['database']
  142. );
  143. $this->connection = new Mongo('mongodb://' . $host . $port, $this->config);
  144. $this->db = $this->connection->selectDB($this->config['db']);
  145. }
  146. }