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.

349 lines
9.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. class MongoCommandBuilder
  10. {
  11. const FIND = 'Find';
  12. const COUNT = 'Count';
  13. const INSERT = 'Insert';
  14. const UPDATE = 'Update';
  15. const REMOVE = 'Remove';
  16. const COMMAND = 'Command';
  17. static public function factory($type, $collection = null)
  18. {
  19. $class = ucfirst($type) . 'MongoCommand';
  20. $command = new $class();
  21. $command->setCollection($collection);
  22. return $command;
  23. }
  24. }
  25. abstract class MongoDbCommand
  26. {
  27. protected $collection;
  28. public function execute()
  29. {
  30. if ($this->checkParams()) {
  31. return $this->concreteExecute();
  32. } else {
  33. throw new GeneralException(get_called_class() . ' error. Bind all required params first.');
  34. }
  35. }
  36. public function bindParam($name, $value)
  37. {
  38. if (property_exists($this, $name)) {
  39. $this->$name = $value;
  40. }
  41. return $this;
  42. }
  43. public function setCollection($collection)
  44. {
  45. $this->collection = $collection;
  46. return $this;
  47. }
  48. abstract protected function concreteExecute();
  49. abstract protected function checkParams();
  50. /**
  51. * @TODO: implement method in subclasses for Profiler
  52. */
  53. abstract public function __toString();
  54. }
  55. class FindMongoCommand extends MongoDbCommand
  56. {
  57. protected $condition = array();
  58. protected $fields = array();
  59. protected $multiple = true;
  60. protected function concreteExecute()
  61. {
  62. if ($this->multiple) {
  63. return $this->collection->find($this->condition, $this->fields);
  64. } else {
  65. return $this->collection->findOne($this->condition, $this->fields);
  66. }
  67. }
  68. protected function checkParams()
  69. {
  70. if (isset($this->collection) && isset($this->condition) && isset($this->fields)) {
  71. return true;
  72. } else {
  73. return false;
  74. }
  75. }
  76. public function __toString()
  77. {
  78. if ($this->checkParams()) {
  79. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  80. ob_start();
  81. var_dump($this->condition);
  82. $condition = ob_get_clean();
  83. $result .= 'Condition: ' . $condition;
  84. $result .= 'Type: FIND' . PHP_EOL;
  85. ob_start();
  86. var_dump($this->fields);
  87. $fields = ob_get_clean();
  88. $result .= 'Fields: ' . $fields . PHP_EOL;
  89. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  90. $result .= 'Multiple fields: ' . $mult . PHP_EOL;
  91. return $result;
  92. } else {
  93. return 'Command properties not set';
  94. }
  95. }
  96. }
  97. class CountMongoCommand extends MongoDbCommand
  98. {
  99. protected $condition = array();
  100. protected $limit = 0;
  101. protected $skip = 0;
  102. protected $multiple = true;
  103. protected function concreteExecute()
  104. {
  105. return $this->collection->count($this->condition, $this->limit, $this->skip);
  106. }
  107. protected function checkParams()
  108. {
  109. if (isset($this->collection) && isset($this->condition) && isset($this->limit) && isset($this->skip)) {
  110. return true;
  111. } else {
  112. return false;
  113. }
  114. }
  115. public function __toString()
  116. {
  117. if ($this->checkParams()) {
  118. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  119. $result .= 'Type: COUNT' . PHP_EOL;
  120. ob_start();
  121. var_dump($this->condition);
  122. $condition = ob_get_clean();
  123. $result .= 'Condition: ' . $condition;
  124. $result .= 'Limit: ' . $this->limit . PHP_EOL;
  125. $result .= 'Skip: ' . $this->skip . PHP_EOL;
  126. return $result;
  127. } else {
  128. return 'Command properties not set';
  129. }
  130. }
  131. }
  132. class InsertMongoCommand extends MongoDbCommand
  133. {
  134. protected $data;
  135. protected $safe = true;
  136. protected $insertId = false;
  137. protected $multiple = false;
  138. protected function concreteExecute()
  139. {
  140. $result = null;
  141. if (!$this->multiple) {
  142. $result = $this->collection->insert($this->data, array('safe' => $this->safe));
  143. $this->insertId = $this->data['_id'];
  144. } else {
  145. if (count($this->data)) {
  146. $result = $this->collection->batchInsert($this->data, array('safe' => $this->safe));
  147. }
  148. }
  149. return $result;
  150. }
  151. protected function checkParams()
  152. {
  153. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  154. return true;
  155. } else {
  156. return false;
  157. }
  158. }
  159. public function getInsertId()
  160. {
  161. return $this->insertId;
  162. }
  163. public function __toString()
  164. {
  165. if ($this->checkParams()) {
  166. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  167. $result .= 'Type: INSERT' . PHP_EOL;
  168. ob_start();
  169. var_dump($this->data);
  170. $data = ob_get_clean();
  171. $result .= 'Data: ' . $data;
  172. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  173. $result .= 'Bulk insert: ' . $mult . PHP_EOL;
  174. $safe = $this->safe ? 'TRUE' : 'FALSE';
  175. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  176. return $result;
  177. } else {
  178. return 'Command properties not set';
  179. }
  180. }
  181. }
  182. class UpdateMongoCommand extends MongoDbCommand
  183. {
  184. protected $condition;
  185. protected $data;
  186. protected $multiple = true;
  187. protected $upsert = false;
  188. protected $safe = true;
  189. protected function concreteExecute()
  190. {
  191. return $this->collection->update($this->condition, $this->data,
  192. array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
  193. }
  194. protected function checkParams()
  195. {
  196. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  197. isset($this->upsert) && isset($this->safe)
  198. ) {
  199. return true;
  200. } else {
  201. return false;
  202. }
  203. }
  204. public function __toString()
  205. {
  206. if ($this->checkParams()) {
  207. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  208. $result .= 'Type: UPDATE' . PHP_EOL;
  209. ob_start();
  210. var_dump($this->condition);
  211. $condition = ob_get_clean();
  212. $result .= 'Condition: ' . $condition;
  213. ob_start();
  214. var_dump($this->data);
  215. $data = ob_get_clean();
  216. $result .= 'Data: ' . $data;
  217. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  218. $result .= 'Multiple fields: ' . $mult . PHP_EOL;
  219. $upsert = $this->upsert ? 'TRUE' : 'FALSE';
  220. $result .= 'Upsert: ' . $upsert . PHP_EOL;
  221. $safe = $this->safe ? 'TRUE' : 'FALSE';
  222. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  223. return $result;
  224. } else {
  225. return 'Command properties not set';
  226. }
  227. }
  228. }
  229. class RemoveMongoCommand extends MongoDbCommand
  230. {
  231. protected $condition;
  232. protected $safe = true;
  233. protected function concreteExecute()
  234. {
  235. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  236. }
  237. protected function checkParams()
  238. {
  239. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  240. return true;
  241. } else {
  242. return false;
  243. }
  244. }
  245. public function __toString()
  246. {
  247. if ($this->checkParams()) {
  248. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  249. $result .= 'Type: REMOVE' . PHP_EOL;
  250. ob_start();
  251. var_dump($this->condition);
  252. $condition = ob_get_clean();
  253. $result .= 'Condition: ' . $condition;
  254. $safe = $this->safe ? 'TRUE' : 'FALSE';
  255. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  256. return $result;
  257. } else {
  258. return 'Command properties not set';
  259. }
  260. }
  261. }
  262. class CommandMongoCommand extends MongoDbCommand
  263. {
  264. protected $command;
  265. protected function concreteExecute()
  266. {
  267. $db = $this->collection->db;
  268. if ($db instanceof MongoDB) {
  269. return $db->command($this->command);
  270. } else {
  271. return false;
  272. }
  273. }
  274. protected function checkParams()
  275. {
  276. if (isset($this->collection) && isset($this->command)) {
  277. return true;
  278. } else {
  279. return false;
  280. }
  281. }
  282. public function __toString()
  283. {
  284. if ($this->checkParams()) {
  285. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  286. $result .= 'Type: COMMAND' . PHP_EOL;
  287. ob_start();
  288. var_dump($this->command);
  289. $command = ob_get_clean();
  290. $result .= 'Command: ' . $command;
  291. return $result;
  292. } else {
  293. return 'Command properties not set';
  294. }
  295. }
  296. }