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.

136 lines
3.6 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. return $this->query($command, $params)->affectedRows();
  51. }
  52. public function update($collection, $data, $condition = array(), $multiple = true, $upsert = false, $safe = true)
  53. {
  54. $command = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->getCollection($collection));
  55. $params = array(
  56. 'data' => $data,
  57. 'condition' => $condition,
  58. 'multiple' => $multiple,
  59. 'upsert' => $upsert,
  60. 'safe' => $safe
  61. );
  62. return $this->query($command, $params)->affectedRows();
  63. }
  64. public function delete($collection, $condition = array(), $safe = true)
  65. {
  66. $command = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->getCollection($collection));
  67. $params = array(
  68. 'condition' => $condition,
  69. 'safe' => $safe
  70. );
  71. return $this->query($command, $params)->affectedRows();
  72. }
  73. /**
  74. * @param mixed $request
  75. * @return DbStatement
  76. */
  77. public function prepare($request)
  78. {
  79. return new MongoStatement($this, $request);
  80. }
  81. public function getInsertId($table = null, $key = null)
  82. {
  83. return $this->last_insert_id;
  84. }
  85. public function isConnected()
  86. {
  87. if (!is_null($this->connection)) {
  88. return $this->connection->connected;
  89. } else {
  90. return false;
  91. }
  92. }
  93. public function disconnect()
  94. {
  95. if ($this->isConnected()) {
  96. $this->connection->close();
  97. }
  98. $this->connection = null;
  99. }
  100. protected function connect()
  101. {
  102. if ($this->connection) {
  103. return;
  104. }
  105. $host = $this->config['hostname'];
  106. $port = isset($this->config['port']) ? ':' . (string) $this->config['port'] : '';
  107. $this->config = array(
  108. 'username' => $this->config['username'],
  109. 'password' => $this->config['password'],
  110. 'db' => $this->config['database']
  111. );
  112. $this->connection = new Mongo('mongodb://' . $host . $port, $this->config);
  113. $this->db = $this->connection->selectDB($this->config['db']);
  114. }
  115. }