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.

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