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.

188 lines
5.2 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. * @return bool
  122. */
  123. protected function driverExecute($request)
  124. {
  125. $this->result = false;
  126. $mongo = $this->driver->getConnection();
  127. if ($mongo instanceof Mongo) {
  128. if (Config::get('PROFILER_DETAILS')) {
  129. $profiler = Profiler::getInstance()->profilerCommand('Mongo', $request);
  130. $result = $request->execute();
  131. $profiler->end();
  132. } else {
  133. $result = $request->execute();
  134. }
  135. if ($result === false) {
  136. throw new GeneralException('MongoDB request error.');
  137. }
  138. if ($result instanceof MongoCursor || is_array($result)) {
  139. $this->result = $result;
  140. if (is_array($result) && isset($result['value'])) {
  141. $this->result = $result['value'];
  142. }
  143. if (is_array($result) && isset($result['values'])) {
  144. $this->result = $result['values'];
  145. }
  146. if (is_array($result) && isset($result['upserted'])) {
  147. $this->result = $result['n'] > 1 ? $result['n'] : $result['upserted'];
  148. }
  149. } elseif (is_int($result)) {
  150. $this->result = $result;
  151. }
  152. if ($request instanceof InsertMongoCommand) {
  153. $this->insertId = $request->getInsertId();
  154. }
  155. return true;
  156. } else {
  157. throw new GeneralException('No connection to MongoDB server.');
  158. }
  159. }
  160. public function bindParam($param, &$value)
  161. {
  162. $this->request->bindParam($param, $value);
  163. }
  164. protected function assemble()
  165. {
  166. return $this->request;
  167. }
  168. public function getInsertId()
  169. {
  170. return $this->insertId;
  171. }
  172. }