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.

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