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.

120 lines
3.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. public function fetch($style = Db::FETCH_OBJ)
  16. {
  17. if (!$this->result) {
  18. return false;
  19. }
  20. $row = false;
  21. switch ($style) {
  22. case Db::FETCH_OBJ:
  23. $row = $this->fetchObject();
  24. break;
  25. case Db::FETCH_ASSOC:
  26. if ($this->result instanceof MongoCursor) {
  27. $row = $this->result->getNext();
  28. } else {
  29. $row = $this->result;
  30. }
  31. break;
  32. default:
  33. throw new Exception('Invalid fetch mode "' . $style . '" specified');
  34. }
  35. return $row;
  36. }
  37. public function fetchObject($class = 'stdClass')
  38. {
  39. if ($this->result instanceof MongoCursor) {
  40. $row = $this->result->getNext();
  41. } else {
  42. $row = $this->result;
  43. }
  44. if (is_array($row) && isset($row['_id'])) {
  45. $row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS);
  46. } else {
  47. $row = false;
  48. }
  49. return $row;
  50. }
  51. public function close()
  52. {
  53. // TODO: Implement close() method.
  54. }
  55. /**
  56. * @return int
  57. */
  58. public function affectedRows()
  59. {
  60. if (is_array($this->result)) {
  61. if (isset($this->result['ok']) && $this->result['ok'] == 1 && isset($this->result['n'])) {
  62. return $this->result['n'];
  63. } else {
  64. return false;
  65. }
  66. }
  67. return false;
  68. }
  69. public function numRows()
  70. {
  71. if ($this->result instanceof MongoCursor) {
  72. return $this->result->count();
  73. } else {
  74. return false;
  75. }
  76. }
  77. /**
  78. * @param MongoDbComand $request
  79. * @return bool
  80. */
  81. protected function driverExecute($request)
  82. {
  83. $mongo = $this->driver->getConnection();
  84. if ($mongo instanceof Mongo) {
  85. if (DEBUG) {
  86. $profiler = Profiler::getInstance()->profilerCommand('Mongo', $request);
  87. $result = $request->execute();
  88. $profiler->end();
  89. } else {
  90. $result = $request->execute();
  91. }
  92. if ($result === false) {
  93. throw new Exception('MongoDB request error.');
  94. }
  95. if ($result instanceof MongoCursor || is_array($result)) {
  96. $this->result = $result;
  97. }
  98. return true;
  99. } else {
  100. throw new Exception('No connection to MongoDB server.');
  101. }
  102. }
  103. public function bindParam($param, &$value)
  104. {
  105. $this->request->bindParam($param, $value);
  106. }
  107. protected function assemble()
  108. {
  109. return $this->request;
  110. }
  111. }