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.

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