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.

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