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.

202 lines
4.3 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. class MongoCommandBuilder
  10. {
  11. const FIND = 'Find';
  12. const INSERT = 'Insert';
  13. const UPDATE = 'Update';
  14. const REMOVE = 'Remove';
  15. const COMMAND = 'Command';
  16. static public function factory($type, $collection = null)
  17. {
  18. $class = ucfirst($type) . 'MongoCommand';
  19. $command = new $class();
  20. $command->setCollection($collection);
  21. return $command;
  22. }
  23. }
  24. abstract class MongoDbCommand
  25. {
  26. protected $collection;
  27. public function execute()
  28. {
  29. if ($this->checkParams()) {
  30. return $this->concreteExecute();
  31. } else {
  32. throw new GeneralException(get_called_class() . ' error. Bind all required params first.');
  33. }
  34. }
  35. public function bindParam($name, $value)
  36. {
  37. if (property_exists($this, $name)) {
  38. $this->$name = $value;
  39. }
  40. return $this;
  41. }
  42. public function setCollection($collection)
  43. {
  44. $this->collection = $collection;
  45. return $this;
  46. }
  47. abstract protected function concreteExecute();
  48. abstract protected function checkParams();
  49. /**
  50. * @TODO: implement method in subclasses for Profiler
  51. */
  52. public function __toString()
  53. {
  54. return get_called_class();
  55. }
  56. }
  57. class FindMongoCommand extends MongoDbCommand
  58. {
  59. protected $condition;
  60. protected $fields;
  61. protected $multiple = true;
  62. protected function concreteExecute()
  63. {
  64. if($this->multiple) {
  65. return $this->collection->find($this->condition, $this->fields);
  66. } else {
  67. return $this->collection->findOne($this->condition, $this->fields);
  68. }
  69. }
  70. protected function checkParams()
  71. {
  72. if (isset($this->collection) && isset($this->condition) && isset($this->fields)) {
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }
  78. }
  79. class InsertMongoCommand extends MongoDbCommand
  80. {
  81. protected $data;
  82. protected $safe = true;
  83. protected $insertId = false;
  84. protected function concreteExecute()
  85. {
  86. $result = $this->collection->insert($this->data, array('safe' => $this->safe));
  87. $this->insertId = $this->data['_id'];
  88. return $result;
  89. }
  90. protected function checkParams()
  91. {
  92. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  93. return true;
  94. } else {
  95. return false;
  96. }
  97. }
  98. public function getInsertId()
  99. {
  100. return $this->insertId;
  101. }
  102. }
  103. class UpdateMongoCommand extends MongoDbCommand
  104. {
  105. protected $condition;
  106. protected $data;
  107. protected $multiple = true;
  108. protected $upsert = false;
  109. protected $safe = true;
  110. protected function concreteExecute()
  111. {
  112. return $this->collection->update($this->condition, $this->data,
  113. array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
  114. }
  115. protected function checkParams()
  116. {
  117. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  118. isset($this->upsert) && isset($this->safe)
  119. ) {
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. }
  125. }
  126. class RemoveMongoCommand extends MongoDbCommand
  127. {
  128. protected $condition;
  129. protected $safe = true;
  130. protected function concreteExecute()
  131. {
  132. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  133. }
  134. protected function checkParams()
  135. {
  136. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142. }
  143. class CommandMongoCommand extends MongoDbCommand
  144. {
  145. protected $command;
  146. protected function concreteExecute()
  147. {
  148. $db = $this->collection->db;
  149. if($db instanceof MongoDB) {
  150. return $db->command($this->command);
  151. } else {
  152. return false;
  153. }
  154. }
  155. protected function checkParams()
  156. {
  157. if (isset($this->collection) && isset($this->command)) {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. }