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.

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