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.

181 lines
4.9 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. } elseif (is_int($this->result)) {
  105. return $this->result;
  106. }
  107. return false;
  108. }
  109. public function numRows()
  110. {
  111. if ($this->result instanceof MongoCursor) {
  112. return $this->result->count();
  113. } else {
  114. return false;
  115. }
  116. }
  117. /**
  118. * @param MongoDbCommand $request
  119. * @return bool
  120. */
  121. protected function driverExecute($request)
  122. {
  123. $mongo = $this->driver->getConnection();
  124. if ($mongo instanceof Mongo) {
  125. if (DEBUG) {
  126. $profiler = Profiler::getInstance()->profilerCommand('Mongo', $request);
  127. $result = $request->execute();
  128. $profiler->end();
  129. } else {
  130. $result = $request->execute();
  131. }
  132. if ($result === false) {
  133. throw new GeneralException('MongoDB request error.');
  134. }
  135. if ($result instanceof MongoCursor || is_array($result)) {
  136. $this->result = $result;
  137. if (is_array($result) && isset($result['value'])) {
  138. $this->result = $result['value'];
  139. }
  140. if (is_array($result) && isset($result['values'])) {
  141. $this->result = $result['values'];
  142. }
  143. } elseif (is_int($result)) {
  144. $this->result = $result;
  145. }
  146. if ($request instanceof InsertMongoCommand) {
  147. $this->insertId = $request->getInsertId();
  148. }
  149. return true;
  150. } else {
  151. throw new GeneralException('No connection to MongoDB server.');
  152. }
  153. }
  154. public function bindParam($param, &$value)
  155. {
  156. $this->request->bindParam($param, $value);
  157. }
  158. protected function assemble()
  159. {
  160. return $this->request;
  161. }
  162. public function getInsertId()
  163. {
  164. return $this->insertId;
  165. }
  166. }