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.

160 lines
4.1 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 && isset($this->result['n'])) {
  90. return $this->result['n'];
  91. } else {
  92. return false;
  93. }
  94. }
  95. return false;
  96. }
  97. public function numRows()
  98. {
  99. if ($this->result instanceof MongoCursor) {
  100. return $this->result->count();
  101. } else {
  102. return false;
  103. }
  104. }
  105. /**
  106. * @param MongoDbComand $request
  107. * @return bool
  108. */
  109. protected function driverExecute($request)
  110. {
  111. $mongo = $this->driver->getConnection();
  112. if ($mongo instanceof Mongo) {
  113. if (DEBUG) {
  114. $profiler = Profiler::getInstance()->profilerCommand('Mongo', $request);
  115. $result = $request->execute();
  116. $profiler->end();
  117. } else {
  118. $result = $request->execute();
  119. }
  120. if ($result === false) {
  121. throw new Exception('MongoDB request error.');
  122. }
  123. if ($result instanceof MongoCursor || is_array($result)) {
  124. $this->result = $result;
  125. }
  126. if($request instanceof InsertMongoCommand) {
  127. $this->insertId = $request->getInsertId();
  128. }
  129. return true;
  130. } else {
  131. throw new Exception('No connection to MongoDB server.');
  132. }
  133. }
  134. public function bindParam($param, &$value)
  135. {
  136. $this->request->bindParam($param, $value);
  137. }
  138. protected function assemble()
  139. {
  140. return $this->request;
  141. }
  142. public function getInsertId()
  143. {
  144. return $this->insertId;
  145. }
  146. }