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.

150 lines
3.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-15
  8. */
  9. /**
  10. * @property MongoDriver $driver
  11. * @property MongoCursor $result
  12. */
  13. class MongoStatement extends DbStatement
  14. {
  15. public function order($sort = array())
  16. {
  17. if ($this->result instanceof MongoCursor) {
  18. $this->result->sort($sort);
  19. return $this;
  20. } else {
  21. throw new Exception('MongoStatement error. Impossible order results of opened cursor.');
  22. }
  23. }
  24. public function skip($skip = 0)
  25. {
  26. if ($this->result instanceof MongoCursor) {
  27. $this->result->skip($skip);
  28. return $this;
  29. } else {
  30. throw new Exception('MongoStatement error. Impossible skip results of opened cursor.');
  31. }
  32. }
  33. public function limit($limit = 0)
  34. {
  35. if ($this->result instanceof MongoCursor) {
  36. $this->result->limit($limit);
  37. return $this;
  38. } else {
  39. throw new Exception('MongoStatement error. Impossible limit results of opened cursor.');
  40. }
  41. }
  42. public function fetch($style = Db::FETCH_OBJ)
  43. {
  44. if (!$this->result) {
  45. return false;
  46. }
  47. $row = false;
  48. switch ($style) {
  49. case Db::FETCH_OBJ:
  50. $row = $this->fetchObject();
  51. break;
  52. case Db::FETCH_ASSOC:
  53. if ($this->result instanceof MongoCursor) {
  54. $row = $this->result->getNext();
  55. } else {
  56. $row = $this->result;
  57. }
  58. break;
  59. default:
  60. throw new Exception('Invalid fetch mode "' . $style . '" specified');
  61. }
  62. return $row;
  63. }
  64. public function fetchObject($class = 'stdClass')
  65. {
  66. if ($this->result instanceof MongoCursor) {
  67. $row = $this->result->getNext();
  68. } else {
  69. $row = $this->result;
  70. }
  71. if (is_array($row) && isset($row['_id'])) {
  72. $row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS);
  73. } else {
  74. $row = false;
  75. }
  76. return $row;
  77. }
  78. public function close()
  79. {
  80. $this->result = null;
  81. }
  82. /**
  83. * @return int
  84. */
  85. public function affectedRows()
  86. {
  87. if (is_array($this->result)) {
  88. if (isset($this->result['ok']) && $this->result['ok'] == 1 && isset($this->result['n'])) {
  89. return $this->result['n'];
  90. } else {
  91. return false;
  92. }
  93. }
  94. return false;
  95. }
  96. public function numRows()
  97. {
  98. if ($this->result instanceof MongoCursor) {
  99. return $this->result->count();
  100. } else {
  101. return false;
  102. }
  103. }
  104. /**
  105. * @param MongoDbComand $request
  106. * @return bool
  107. */
  108. protected function driverExecute($request)
  109. {
  110. $mongo = $this->driver->getConnection();
  111. if ($mongo instanceof Mongo) {
  112. if (DEBUG) {
  113. $profiler = Profiler::getInstance()->profilerCommand('Mongo', $request);
  114. $result = $request->execute();
  115. $profiler->end();
  116. } else {
  117. $result = $request->execute();
  118. }
  119. if ($result === false) {
  120. throw new Exception('MongoDB request error.');
  121. }
  122. if ($result instanceof MongoCursor || is_array($result)) {
  123. $this->result = $result;
  124. }
  125. return true;
  126. } else {
  127. throw new Exception('No connection to MongoDB server.');
  128. }
  129. }
  130. public function bindParam($param, &$value)
  131. {
  132. $this->request->bindParam($param, $value);
  133. }
  134. protected function assemble()
  135. {
  136. return $this->request;
  137. }
  138. }