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.

333 lines
8.7 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. protected function arrayToString($array)
  49. {
  50. $string = '[';
  51. if(!empty($array)) {
  52. $string .= PHP_EOL;
  53. }
  54. foreach($array as $key => $value) {
  55. $string .= "\t" . $key . ' = ' . $value . PHP_EOL;
  56. }
  57. $string .= ']' . PHP_EOL;
  58. return $string;
  59. }
  60. abstract protected function concreteExecute();
  61. abstract protected function checkParams();
  62. }
  63. class FindMongoCommand extends MongoDbCommand
  64. {
  65. protected $condition = array();
  66. protected $fields = array();
  67. protected $multiple = true;
  68. protected function concreteExecute()
  69. {
  70. if ($this->multiple) {
  71. return $this->collection->find($this->condition, $this->fields);
  72. } else {
  73. return $this->collection->findOne($this->condition, $this->fields);
  74. }
  75. }
  76. protected function checkParams()
  77. {
  78. if (isset($this->collection) && isset($this->condition) && isset($this->fields)) {
  79. return true;
  80. } else {
  81. return false;
  82. }
  83. }
  84. public function __toString()
  85. {
  86. if ($this->checkParams()) {
  87. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  88. $result .= 'Condition: ' . $this->arrayToString($this->condition);
  89. $result .= 'Type: FIND' . PHP_EOL;
  90. $result .= 'Fields: ' . $this->arrayToString($this->fields);
  91. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  92. $result .= 'Multiple fields: ' . $mult . PHP_EOL;
  93. return $result;
  94. } else {
  95. return 'Command properties not set';
  96. }
  97. }
  98. }
  99. class CountMongoCommand extends MongoDbCommand
  100. {
  101. protected $condition = array();
  102. protected $limit = 0;
  103. protected $skip = 0;
  104. protected $multiple = true;
  105. protected function concreteExecute()
  106. {
  107. return $this->collection->count($this->condition, $this->limit, $this->skip);
  108. }
  109. protected function checkParams()
  110. {
  111. if (isset($this->collection) && isset($this->condition) && isset($this->limit) && isset($this->skip)) {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }
  117. public function __toString()
  118. {
  119. if ($this->checkParams()) {
  120. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  121. $result .= 'Type: COUNT' . PHP_EOL;
  122. $result .= 'Condition: ' . $this->arrayToString($this->condition);
  123. $result .= 'Limit: ' . $this->limit . PHP_EOL;
  124. $result .= 'Skip: ' . $this->skip . PHP_EOL;
  125. return $result;
  126. } else {
  127. return 'Command properties not set';
  128. }
  129. }
  130. }
  131. class InsertMongoCommand extends MongoDbCommand
  132. {
  133. protected $data;
  134. protected $safe = true;
  135. protected $insertId = false;
  136. protected $multiple = false;
  137. protected function concreteExecute()
  138. {
  139. $result = null;
  140. if (!$this->multiple) {
  141. $result = $this->collection->insert($this->data, array('safe' => $this->safe));
  142. $this->insertId = $this->data['_id'];
  143. } else {
  144. if (count($this->data)) {
  145. $result = $this->collection->batchInsert($this->data, array('safe' => $this->safe));
  146. }
  147. }
  148. return $result;
  149. }
  150. protected function checkParams()
  151. {
  152. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. public function getInsertId()
  159. {
  160. return $this->insertId;
  161. }
  162. public function __toString()
  163. {
  164. if ($this->checkParams()) {
  165. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  166. $result .= 'Type: INSERT' . PHP_EOL;
  167. $result .= 'Data: ' . $this->arrayToString($this->data);
  168. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  169. $result .= 'Bulk insert: ' . $mult . PHP_EOL;
  170. $safe = $this->safe ? 'TRUE' : 'FALSE';
  171. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  172. return $result;
  173. } else {
  174. return 'Command properties not set';
  175. }
  176. }
  177. }
  178. class UpdateMongoCommand extends MongoDbCommand
  179. {
  180. protected $condition;
  181. protected $data;
  182. protected $multiple = true;
  183. protected $upsert = false;
  184. protected $safe = true;
  185. protected function concreteExecute()
  186. {
  187. return $this->collection->update($this->condition, $this->data,
  188. array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
  189. }
  190. protected function checkParams()
  191. {
  192. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  193. isset($this->upsert) && isset($this->safe)
  194. ) {
  195. return true;
  196. } else {
  197. return false;
  198. }
  199. }
  200. public function __toString()
  201. {
  202. if ($this->checkParams()) {
  203. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  204. $result .= 'Type: UPDATE' . PHP_EOL;
  205. $result .= 'Condition: ' . $this->arrayToString($this->condition);
  206. $result .= 'Data: ' . $this->arrayToString($this->data);
  207. $mult = $this->multiple ? 'TRUE' : 'FALSE';
  208. $result .= 'Multiple fields: ' . $mult . PHP_EOL;
  209. $upsert = $this->upsert ? 'TRUE' : 'FALSE';
  210. $result .= 'Upsert: ' . $upsert . 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 RemoveMongoCommand extends MongoDbCommand
  220. {
  221. protected $condition;
  222. protected $safe = true;
  223. protected function concreteExecute()
  224. {
  225. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  226. }
  227. protected function checkParams()
  228. {
  229. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  230. return true;
  231. } else {
  232. return false;
  233. }
  234. }
  235. public function __toString()
  236. {
  237. if ($this->checkParams()) {
  238. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  239. $result .= 'Type: REMOVE' . PHP_EOL;
  240. $result .= 'Condition: ' . $this->arrayToString($this->condition);
  241. $safe = $this->safe ? 'TRUE' : 'FALSE';
  242. $result .= 'Safe operation: ' . $safe . PHP_EOL;
  243. return $result;
  244. } else {
  245. return 'Command properties not set';
  246. }
  247. }
  248. }
  249. class CommandMongoCommand extends MongoDbCommand
  250. {
  251. protected $command;
  252. protected function concreteExecute()
  253. {
  254. $db = $this->collection->db;
  255. if ($db instanceof MongoDB) {
  256. return $db->command($this->command);
  257. } else {
  258. return false;
  259. }
  260. }
  261. protected function checkParams()
  262. {
  263. if (isset($this->collection) && isset($this->command)) {
  264. return true;
  265. } else {
  266. return false;
  267. }
  268. }
  269. public function __toString()
  270. {
  271. if ($this->checkParams()) {
  272. $result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
  273. $result .= 'Type: COMMAND' . PHP_EOL;
  274. $result .= 'Command: ' . $this->arrayToString($this->command);
  275. return $result;
  276. } else {
  277. return 'Command properties not set';
  278. }
  279. }
  280. }