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.

297 lines
7.5 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 $multiple = false;
  101. protected function concreteExecute()
  102. {
  103. if (!$this->multiple) {
  104. $result = $this->collection->insert($this->data, array('safe' => $this->safe));
  105. $this->insertId = $this->data['_id'];
  106. } else {
  107. $result = $this->collection->batchInsert($this->data, array('safe' => $this->safe));
  108. }
  109. return $result;
  110. }
  111. protected function checkParams()
  112. {
  113. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. public function getInsertId()
  120. {
  121. return $this->insertId;
  122. }
  123. public function __toString()
  124. {
  125. if ($this->checkParams()) {
  126. $result = 'Collection: ' . $this->collection . PHP_EOL;
  127. ob_start();
  128. var_dump($this->data);
  129. $data = ob_get_clean();
  130. $result .= 'Data: ' . $data . PHP_EOL;
  131. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  132. $result .= 'Bulk insert: ' . $mult . PHP_EOL;
  133. $safe = $this->safe ? 'TRUE' : 'FALSE';
  134. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  135. return $result;
  136. } else {
  137. return 'Command properties not set';
  138. }
  139. }
  140. }
  141. class UpdateMongoCommand extends MongoDbCommand
  142. {
  143. protected $condition;
  144. protected $data;
  145. protected $multiple = true;
  146. protected $upsert = false;
  147. protected $safe = true;
  148. protected function concreteExecute()
  149. {
  150. return $this->collection->update($this->condition, $this->data,
  151. array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
  152. }
  153. protected function checkParams()
  154. {
  155. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  156. isset($this->upsert) && isset($this->safe)
  157. ) {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. public function __toString()
  164. {
  165. if ($this->checkParams()) {
  166. $result = 'Collection: ' . $this->collection . PHP_EOL;
  167. ob_start();
  168. var_dump($this->condition);
  169. $condition = ob_get_clean();
  170. $result .= 'Condition: ' . $condition . PHP_EOL;
  171. ob_start();
  172. var_dump($this->data);
  173. $data = ob_get_clean();
  174. $result .= 'Data: ' . $data . PHP_EOL;
  175. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  176. $result .= 'Multiple fields: ' . $mult . PHP_EOL;
  177. $upsert = $this->upsert ? 'TRUE' : 'FALSE';
  178. $result .= 'Upsert: ' . $upsert . PHP_EOL;
  179. $safe = $this->safe ? 'TRUE' : 'FALSE';
  180. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  181. return $result;
  182. } else {
  183. return 'Command properties not set';
  184. }
  185. }
  186. }
  187. class RemoveMongoCommand extends MongoDbCommand
  188. {
  189. protected $condition;
  190. protected $safe = true;
  191. protected function concreteExecute()
  192. {
  193. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  194. }
  195. protected function checkParams()
  196. {
  197. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  198. return true;
  199. } else {
  200. return false;
  201. }
  202. }
  203. public function __toString()
  204. {
  205. if ($this->checkParams()) {
  206. $result = 'Collection: ' . $this->collection . PHP_EOL;
  207. ob_start();
  208. var_dump($this->condition);
  209. $condition = ob_get_clean();
  210. $result .= 'Condition: ' . $condition . PHP_EOL;
  211. $safe = $this->safe ? 'TRUE' : 'FALSE';
  212. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  213. return $result;
  214. } else {
  215. return 'Command properties not set';
  216. }
  217. }
  218. }
  219. class CommandMongoCommand extends MongoDbCommand
  220. {
  221. protected $command;
  222. protected function concreteExecute()
  223. {
  224. $db = $this->collection->db;
  225. if ($db instanceof MongoDB) {
  226. return $db->command($this->command);
  227. } else {
  228. return false;
  229. }
  230. }
  231. protected function checkParams()
  232. {
  233. if (isset($this->collection) && isset($this->command)) {
  234. return true;
  235. } else {
  236. return false;
  237. }
  238. }
  239. public function __toString()
  240. {
  241. if ($this->checkParams()) {
  242. $result = 'Collection: ' . $this->collection . PHP_EOL;
  243. ob_start();
  244. var_dump($this->command);
  245. $command = ob_get_clean();
  246. $result .= 'Command: ' . $command . PHP_EOL;
  247. return $result;
  248. } else {
  249. return 'Command properties not set';
  250. }
  251. }
  252. }