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.

194 lines
4.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 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 Exception(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. class FindMongoCommand extends MongoDbCommand
  51. {
  52. protected $condition;
  53. protected $fields;
  54. protected $multiple = true;
  55. protected function concreteExecute()
  56. {
  57. if($this->multiple) {
  58. return $this->collection->find($this->condition, $this->fields);
  59. } else {
  60. return $this->collection->findOne($this->condition, $this->fields);
  61. }
  62. }
  63. protected function checkParams()
  64. {
  65. if (isset($this->collection) && isset($this->condition) && isset($this->fields)) {
  66. return true;
  67. } else {
  68. return false;
  69. }
  70. }
  71. }
  72. class InsertMongoCommand extends MongoDbCommand
  73. {
  74. protected $data;
  75. protected $safe = true;
  76. protected $insertId = false;
  77. protected function concreteExecute()
  78. {
  79. $result = $this->collection->insert($this->data, array('safe' => $this->safe));
  80. $this->insertId = $this->data['_id'];
  81. return $result;
  82. }
  83. protected function checkParams()
  84. {
  85. if (isset($this->collection) && isset($this->data) && isset($this->safe)) {
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. public function getInsertId()
  92. {
  93. return $this->insertId;
  94. }
  95. }
  96. class UpdateMongoCommand extends MongoDbCommand
  97. {
  98. protected $condition;
  99. protected $data;
  100. protected $multiple = true;
  101. protected $upsert = false;
  102. protected $safe = true;
  103. protected function concreteExecute()
  104. {
  105. return $this->collection->update($this->condition, $this->data,
  106. array('multiple' => $this->multiple, 'upsert' => $this->upsert, 'safe' => $this->safe));
  107. }
  108. protected function checkParams()
  109. {
  110. if (isset($this->collection) && isset($this->condition) && isset($this->data) &&
  111. isset($this->upsert) && isset($this->safe)
  112. ) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. }
  119. class RemoveMongoCommand extends MongoDbCommand
  120. {
  121. protected $condition;
  122. protected $safe = true;
  123. protected function concreteExecute()
  124. {
  125. return $this->collection->remove($this->condition, array('safe' => $this->safe));
  126. }
  127. protected function checkParams()
  128. {
  129. if (isset($this->collection) && isset($this->condition) && isset($this->safe)) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. }
  136. class CommandMongoCommand extends MongoDbCommand
  137. {
  138. protected $command;
  139. protected function concreteExecute()
  140. {
  141. $db = $this->collection->db;
  142. if($db instanceof MongoDB) {
  143. return $db->command($this->command);
  144. } else {
  145. return false;
  146. }
  147. }
  148. protected function checkParams()
  149. {
  150. if (isset($this->collection) && isset($this->command)) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. }
  156. }