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.

176 lines
5.1 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. public function find($collection, $condition = array(), $fields = array())
  35. {
  36. $command = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->getCollection($collection));
  37. $params = array(
  38. 'condition' => $condition,
  39. 'fields' => $fields
  40. );
  41. return $this->query($command, $params);
  42. }
  43. public function get($collection, $condition, $fields = array())
  44. {
  45. $command = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->getCollection($collection));
  46. $params = array(
  47. 'condition' => $condition,
  48. 'fields' => $fields,
  49. 'multiple' => false
  50. );
  51. return $this->query($command, $params);
  52. }
  53. public function findAndModify($collection, $query, $update, $sort = array(), $field = array(), $upsert = false, $new = false, $remove = false)
  54. {
  55. $command = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->getCollection($collection));
  56. $cmd = array(
  57. 'findAndModify' => $collection,
  58. 'query' => $query,
  59. 'update' => $update,
  60. 'sort' => $sort,
  61. 'fields' => $field,
  62. 'new' => $new,
  63. 'upsert' => $upsert,
  64. 'remove' => $remove
  65. );
  66. $params = array('command' => $cmd);
  67. return $this->query($command, $params);
  68. }
  69. public function command($collection, $cmd)
  70. {
  71. $command = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->getCollection($collection));
  72. $params = array('command' => $cmd);
  73. return $this->query($command, $params);
  74. }
  75. public function insert($collection, $data, $multiple = false, $safe = true)
  76. {
  77. $command = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->getCollection($collection));
  78. $params = array(
  79. 'data' => $data,
  80. 'safe' => $safe,
  81. 'multiple' => $multiple
  82. );
  83. $result = $this->query($command, $params);
  84. $this->last_insert_id = $result->getInsertId();
  85. return $result->affectedRows();
  86. }
  87. public function update($collection, $data, $condition = array(), $multiple = true, $upsert = false, $safe = true)
  88. {
  89. $command = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->getCollection($collection));
  90. $params = array(
  91. 'data' => $data,
  92. 'condition' => $condition,
  93. 'multiple' => $multiple,
  94. 'upsert' => $upsert,
  95. 'safe' => $safe
  96. );
  97. return $this->query($command, $params)->affectedRows();
  98. }
  99. public function delete($collection, $condition = array(), $safe = true)
  100. {
  101. $command = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->getCollection($collection));
  102. $params = array(
  103. 'condition' => $condition,
  104. 'safe' => $safe
  105. );
  106. return $this->query($command, $params)->affectedRows();
  107. }
  108. /**
  109. * @param mixed $request
  110. * @return DbStatement
  111. */
  112. public function prepare($request)
  113. {
  114. return new MongoStatement($this, $request);
  115. }
  116. public function getInsertId($table = null, $key = null)
  117. {
  118. return $this->last_insert_id;
  119. }
  120. public function isConnected()
  121. {
  122. if (!is_null($this->connection)) {
  123. return $this->connection->connected;
  124. } else {
  125. return false;
  126. }
  127. }
  128. public function disconnect()
  129. {
  130. if ($this->isConnected()) {
  131. $this->connection->close();
  132. }
  133. $this->connection = null;
  134. }
  135. protected function connect()
  136. {
  137. if ($this->connection) {
  138. return;
  139. }
  140. $host = $this->config['hostname'];
  141. $port = isset($this->config['port']) ? ':' . (string) $this->config['port'] : '';
  142. $this->config = array(
  143. 'username' => $this->config['username'],
  144. 'password' => $this->config['password'],
  145. 'db' => $this->config['database']
  146. );
  147. $this->connection = new Mongo('mongodb://' . $host . $port, $this->config);
  148. $this->db = $this->connection->selectDB($this->config['db']);
  149. }
  150. }