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.

365 lines
9.3 KiB

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