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.

113 lines
3.0 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_inset_id = 0;
  15. protected $db_name = 'admin';
  16. protected function getCollection($name)
  17. {
  18. return $this->connection->selectCollection($this->db, $name);
  19. }
  20. public function find($collection, $condition = array(), $fields = array(), $cache_key = null)
  21. {
  22. return $this->getCollection($collection)->find($condition, $fields);
  23. }
  24. public function get($collection, $condition, $fields = array())
  25. {
  26. return $this->getCollection($collection)->findOne($condition, $fields);
  27. }
  28. public function insert($collection, $data, $safe = true)
  29. {
  30. $result = $this->getCollection($collection)->insert($data, array('safe' => $safe));
  31. if (isset($result['ok']) && $result['ok'] == 1) {
  32. $this->last_inset_id = (string) $data['_id'];
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38. public function update($collection, $data, $condition = array(), $upsert = false, $safe = true)
  39. {
  40. $result = $this->getCollection($collection)->update($condition, $data, array('upsert' => $upsert, 'safe' => $safe));
  41. if (isset($result['ok']) && $result['ok'] == 1) {
  42. if(isset($result['updatedExisting']) && isset($result['upserted'])) {
  43. $this->last_inset_id = (string) $result['upserted'];
  44. }
  45. return $result['n'];
  46. } else {
  47. return false;
  48. }
  49. }
  50. public function delete($collection, $condition = array(), $safe = true)
  51. {
  52. $result = $this->getCollection($collection)->remove($condition, array('safe' => $safe));
  53. if (isset($result['ok']) && $result['ok'] == 1) {
  54. return $result['n'];
  55. } else {
  56. return false;
  57. }
  58. }
  59. public function getInsertId($table = null, $key = null)
  60. {
  61. return $this->last_inset_id;
  62. }
  63. public function isConnected()
  64. {
  65. if (!is_null($this->connection)) {
  66. return $this->connection->connected;
  67. } else {
  68. return false;
  69. }
  70. }
  71. public function disconnect()
  72. {
  73. if ($this->isConnected()) {
  74. $this->connection->close();
  75. }
  76. $this->connection = null;
  77. }
  78. protected function connect()
  79. {
  80. if ($this->connection) {
  81. return;
  82. }
  83. $host = $this->config['hostname'];
  84. $port = isset($this->config['port']) ? ':' . (string) $this->config['port'] : '';
  85. $this->config = array(
  86. 'username' => $this->config['username'],
  87. 'password' => $this->config['password'],
  88. 'db' => $this->config['database']
  89. );
  90. $this->connection = new Mongo('mongodb://' . $host . $port, $this->config);
  91. $this->db = $this->connection->selectDB($this->config['db']);
  92. }
  93. }