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.

189 lines
5.3 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. if (is_array($this->result) && isset($this->result['retval'])) {
  57. return $this->result['retval'];
  58. }
  59. switch ($style) {
  60. case Db::FETCH_OBJ:
  61. $row = $this->fetchObject();
  62. break;
  63. case Db::FETCH_ASSOC:
  64. if ($this->result instanceof MongoCursor) {
  65. $row = $this->result->getNext();
  66. } else {
  67. $row = $this->result;
  68. }
  69. break;
  70. default:
  71. throw new GeneralException('Invalid fetch mode "' . $style . '" specified');
  72. }
  73. return $row;
  74. }
  75. public function fetchObject($class = 'stdClass')
  76. {
  77. if ($this->result instanceof MongoCursor) {
  78. $row = $this->result->getNext();
  79. } else {
  80. $row = $this->result;
  81. }
  82. if (is_array($row) && isset($row['_id'])) {
  83. $row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS);
  84. } else {
  85. $row = false;
  86. }
  87. return $row;
  88. }
  89. public function close()
  90. {
  91. $this->result = null;
  92. }
  93. /**
  94. * @return int
  95. */
  96. public function affectedRows()
  97. {
  98. if (is_array($this->result)) {
  99. if (isset($this->result['ok']) && $this->result['ok'] == 1) {
  100. if (isset($this->result['n'])) {
  101. return $this->result['n'];
  102. }
  103. } else {
  104. return false;
  105. }
  106. } elseif (is_int($this->result) || $this->result instanceof MongoId) {
  107. return $this->result;
  108. }
  109. return false;
  110. }
  111. public function numRows()
  112. {
  113. if ($this->result instanceof MongoCursor) {
  114. return $this->result->count();
  115. } else {
  116. return false;
  117. }
  118. }
  119. /**
  120. * @param MongoDbCommand $request
  121. * @throws GeneralException
  122. * @return bool
  123. */
  124. protected function driverExecute($request)
  125. {
  126. $this->result = false;
  127. $mongo = $this->driver->getConnection();
  128. if ($mongo instanceof Mongo) {
  129. if (Config::get('PROFILER_DETAILS')) {
  130. $profiler = Profiler::getInstance()->profilerCommand('Mongo', $request);
  131. $result = $request->execute();
  132. $profiler->end();
  133. } else {
  134. $result = $request->execute();
  135. }
  136. if ($result === false) {
  137. throw new GeneralException('MongoDB request error.');
  138. }
  139. if ($result instanceof MongoCursor || is_array($result)) {
  140. $this->result = $result;
  141. if (is_array($result) && isset($result['value'])) {
  142. $this->result = $result['value'];
  143. }
  144. if (is_array($result) && isset($result['values'])) {
  145. $this->result = $result['values'];
  146. }
  147. if (is_array($result) && isset($result['upserted'])) {
  148. $this->result = $result['n'] > 1 ? $result['n'] : $result['upserted'];
  149. }
  150. } elseif (is_int($result)) {
  151. $this->result = $result;
  152. }
  153. if ($request instanceof InsertMongoCommand) {
  154. $this->insertId = $request->getInsertId();
  155. }
  156. return true;
  157. } else {
  158. throw new GeneralException('No connection to MongoDB server.');
  159. }
  160. }
  161. public function bindParam($param, &$value)
  162. {
  163. $this->request->bindParam($param, $value);
  164. }
  165. protected function assemble()
  166. {
  167. return $this->request;
  168. }
  169. public function getInsertId()
  170. {
  171. return $this->insertId;
  172. }
  173. }