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.

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