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.

289 lines
7.2 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. abstract public function __toString();
  53. }
  54. class FindMongoCommand extends MongoDbCommand
  55. {
  56. protected $condition;
  57. protected $fields;
  58. protected $multiple = true;
  59. protected function concreteExecute()
  60. {
  61. if ($this->multiple) {
  62. return $this->collection->find($this->condition, $this->fields);
  63. } else {
  64. return $this->collection->findOne($this->condition, $this->fields);
  65. }
  66. }
  67. protected function checkParams()
  68. {
  69. if (isset($this->collection) && isset($this->condition) && isset($this->fields)) {
  70. return true;
  71. } else {
  72. return false;
  73. }
  74. }
  75. public function __toString()
  76. {
  77. if ($this->checkParams()) {
  78. $result = 'Collection: ' . $this->collection . PHP_EOL;
  79. ob_start();
  80. var_dump($this->condition);
  81. $condition = ob_get_clean();
  82. $result .= 'Condition: ' . $condition . PHP_EOL;
  83. ob_start();
  84. var_dump($this->fields);
  85. $fields = ob_get_clean();
  86. $result .= 'Fields: ' . $fields . PHP_EOL;
  87. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  88. $result .= 'Multiple fields: ' . $mult . PHP_EOL;
  89. return $result;
  90. } else {
  91. return 'Command properties not set';
  92. }
  93. }
  94. }
  95. class InsertMongoCommand extends MongoDbCommand
  96. {
  97. protected $data;
  98. protected $safe = true;
  99. protected $insertId = false;
  100. protected function concreteExecute()
  101. {
  102. $result = $this->collection->insert($this->data, array('safe' => $this->safe));
  103. $this->insertId = $this->data['_id'];
  104. return $result;
  105. }
  106. protected function checkParams()
  107. {
  108. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. }
  114. public function getInsertId()
  115. {
  116. return $this->insertId;
  117. }
  118. public function __toString()
  119. {
  120. if ($this->checkParams()) {
  121. $result = 'Collection: ' . $this->collection . PHP_EOL;
  122. ob_start();
  123. var_dump($this->data);
  124. $data = ob_get_clean();
  125. $result .= 'Data: ' . $data . PHP_EOL;
  126. $safe = $this->safe ? 'TRUE' : 'FALSE';
  127. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  128. return $result;
  129. } else {
  130. return 'Command properties not set';
  131. }
  132. }
  133. }
  134. class UpdateMongoCommand extends MongoDbCommand
  135. {
  136. protected $condition;
  137. protected $data;
  138. protected $multiple = true;
  139. protected $upsert = false;
  140. protected $safe = true;
  141. protected function concreteExecute()
  142. {
  143. return $this->collection->update($this->condition, $this->data,
  144. array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
  145. }
  146. protected function checkParams()
  147. {
  148. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  149. isset($this->upsert) && isset($this->safe)
  150. ) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. }
  156. public function __toString()
  157. {
  158. if ($this->checkParams()) {
  159. $result = 'Collection: ' . $this->collection . PHP_EOL;
  160. ob_start();
  161. var_dump($this->condition);
  162. $condition = ob_get_clean();
  163. $result .= 'Condition: ' . $condition . PHP_EOL;
  164. ob_start();
  165. var_dump($this->data);
  166. $data = ob_get_clean();
  167. $result .= 'Data: ' . $data . PHP_EOL;
  168. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  169. $result .= 'Multiple fields: ' . $mult . PHP_EOL;
  170. $upsert = $this->upsert ? 'TRUE' : 'FALSE';
  171. $result .= 'Upsert: ' . $upsert . PHP_EOL;
  172. $safe = $this->safe ? 'TRUE' : 'FALSE';
  173. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  174. return $result;
  175. } else {
  176. return 'Command properties not set';
  177. }
  178. }
  179. }
  180. class RemoveMongoCommand extends MongoDbCommand
  181. {
  182. protected $condition;
  183. protected $safe = true;
  184. protected function concreteExecute()
  185. {
  186. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  187. }
  188. protected function checkParams()
  189. {
  190. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  191. return true;
  192. } else {
  193. return false;
  194. }
  195. }
  196. public function __toString()
  197. {
  198. if ($this->checkParams()) {
  199. $result = 'Collection: ' . $this->collection . PHP_EOL;
  200. ob_start();
  201. var_dump($this->condition);
  202. $condition = ob_get_clean();
  203. $result .= 'Condition: ' . $condition . PHP_EOL;
  204. $safe = $this->safe ? 'TRUE' : 'FALSE';
  205. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  206. return $result;
  207. } else {
  208. return 'Command properties not set';
  209. }
  210. }
  211. }
  212. class CommandMongoCommand extends MongoDbCommand
  213. {
  214. protected $command;
  215. protected function concreteExecute()
  216. {
  217. $db = $this->collection->db;
  218. if ($db instanceof MongoDB) {
  219. return $db->command($this->command);
  220. } else {
  221. return false;
  222. }
  223. }
  224. protected function checkParams()
  225. {
  226. if (isset($this->collection) && isset($this->command)) {
  227. return true;
  228. } else {
  229. return false;
  230. }
  231. }
  232. public function __toString()
  233. {
  234. if ($this->checkParams()) {
  235. $result = 'Collection: ' . $this->collection . PHP_EOL;
  236. ob_start();
  237. var_dump($this->command);
  238. $command = ob_get_clean();
  239. $result .= 'Command: ' . $command . PHP_EOL;
  240. return $result;
  241. } else {
  242. return 'Command properties not set';
  243. }
  244. }
  245. }