EMongoMigration.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * EMongoMigration is designed to be used together with the "yiic migratemongo" command.
  4. *
  5. * It provides a set of convenient methods for manipulating database data.
  6. * For example, the {@link insert} method can be used to easily insert data into
  7. * a mongo collection; the {@link createCollection} method can be used to create a new collection.
  8. * Compared with the same methods in {@link MongoCollection}, these methods will display extra
  9. * information showing the method parameters and execution time, which may be useful when
  10. * applying migrations.
  11. */
  12. abstract class EMongoMigration extends CComponent
  13. {
  14. /**
  15. *
  16. * @var string global collection name to be used for every mongodb operation
  17. * Can also be set dynamically
  18. * @see setCollectionName
  19. * @see getCollectionName
  20. */
  21. protected $collectionName;
  22. /**
  23. * The name of the database
  24. * Used by set/getDbConnection()
  25. * @var EMongoClient
  26. */
  27. private $_db;
  28. /**
  29. * Creates a collection.
  30. * @param string $name The name of the collection.
  31. * @param array $options Options (name=>value) for the operation. See {@link MongoDB::createCollection} for more details.
  32. */
  33. public function createCollection($name, $options = array())
  34. {
  35. echo " > creating collection $name ...";
  36. $time = microtime(true);
  37. if(version_compare(phpversion('mongo'), '1.4.0', '<')){
  38. $options ['capped'] = isset($options['capped']) ? $options['capped'] : false;
  39. $options ['size'] = isset($options['size']) ? $options['size'] : 0;
  40. $options ['max'] = isset($options['max']) ? $options['max'] : 0;
  41. $this->getDbConnection()->getDB()->createCollection($name, $options['capped'], $options['size'], $options['max']);
  42. }else{
  43. $this->getDbConnection()->getDB()->createCollection($name, $options);
  44. }
  45. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  46. }
  47. /**
  48. * Deletes an index from this collection.
  49. *
  50. * @param array $keys Deletes one or multiple indices from name
  51. */
  52. public function deleteIndex($keys)
  53. {
  54. echo " > deleteIndex ...";
  55. $time = microtime(true);
  56. $this->getDbConnection()->{$this->getCollectionName()}->deleteIndex($keys);
  57. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  58. }
  59. /**
  60. * Deletes all indexes from current collection.
  61. */
  62. public function deleteIndexes()
  63. {
  64. echo " > deleteIndexes ...";
  65. $time = microtime(true);
  66. $this->getDbConnection()->{$this->getCollectionName()}->deleteIndexes();
  67. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  68. }
  69. /**
  70. * This method contains the logic to be executed when removing this migration.
  71. * Child classes may override this method if the corresponding migrations can be removed.
  72. * @return boolean Returning false means, the migration will not be applied.
  73. */
  74. public function down()
  75. {
  76. }
  77. /**
  78. * Drops the current collection.
  79. */
  80. public function drop()
  81. {
  82. echo " > drop collection {$this->getCollectionName()} ...";
  83. $time = microtime(true);
  84. $this->getDbConnection()->{$this->getCollectionName()}->drop();
  85. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  86. }
  87. /**
  88. * Drops the current collection.
  89. */
  90. public function dropCollection()
  91. {
  92. echo " > dropping collection {$this->getCollectionName()}...";
  93. $time = microtime(true);
  94. $this->getDbConnection()->{$this->getCollectionName()}->drop();
  95. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  96. }
  97. private function indexList($array)
  98. {
  99. $ret = '';
  100. foreach($array as $key => $order){
  101. $ret .= $key . ' (' . ($order == - 1 ? 'desc' : 'asc') . '), ';
  102. }
  103. return substr($ret, 0, strlen($ret) - 2);
  104. }
  105. /**
  106. * Creates an index on the given field(s), or does nothing if the index already exists.
  107. *
  108. * @param mixed $keys
  109. * An array of fields by which to sort the index on. Each element in the array has as key the field name,
  110. * and as value either 1 for ascending sort, or -1 for descending sort.
  111. * @param array $options
  112. * Options (name=>value) for the save operation. See {@link MongoCollection::ensureIndex} for more details.
  113. */
  114. public function ensureIndex($keys, $options = array())
  115. {
  116. echo " > creating index for fields " . $this->indexList ( $keys ) . " ...";
  117. $time = microtime(true);
  118. $this->getDbConnection()->{$this->getCollectionName()}->ensureIndex($keys, $options);
  119. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  120. }
  121. /**
  122. * Runs JavaScript code on the database server.
  123. *
  124. * @param mixed $code @link MongoCode or string to execute.
  125. * @param array $args Arguments to be passed to code.
  126. */
  127. public function execute($code, $args = array())
  128. {
  129. echo " > execute command: $code ...";
  130. $time = microtime(true);
  131. $this->getDbConnection()->getDB()->execute($code, $args);
  132. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  133. }
  134. /**
  135. *
  136. * @return string The current selected collection
  137. */
  138. public function getCollectionName()
  139. {
  140. if($this->collectionName === null){
  141. throw new CException(
  142. Yii::t(
  143. 'yii',
  144. 'You need to set a collection first in order to execute mongodb operations'
  145. )
  146. );
  147. }
  148. return $this->collectionName;
  149. }
  150. /**
  151. *
  152. * @param string $collectionName Name of the connection on which mongodb operations are applied to
  153. */
  154. public function setCollectionName($collectionName)
  155. {
  156. echo " > switching to collecton {$collectionName}.\n";
  157. $this->collectionName = $collectionName;
  158. }
  159. /**
  160. * Returns the currently active database connection.
  161. * By default, the 'db' application component will be returned and activated.
  162. * You can call {@link setDbConnection} to switch to a different database connection.
  163. * Methods such as {@link insert}, {@link createCollection} will use this database connection
  164. * to perform DB queries.
  165. *
  166. * @throws CException if "db" application component is not configured
  167. * @return EMongoClient the currently active mongodb connection
  168. */
  169. public function getDbConnection()
  170. {
  171. if($this->_db === null){
  172. $this->_db = Yii::app ()->getComponent('mongodb');
  173. if(!$this->_db instanceof EMongoClient){
  174. throw new CException(Yii::t('yii', 'The "db" application component must be configured to be a EMongoClient object.'));
  175. }
  176. }
  177. return $this->_db;
  178. }
  179. /**
  180. * Inserts a document into current collection.
  181. *
  182. * @param mixed $a An array or object. If an object is used, it may not have protected or private properties.
  183. * @param array $options Options (name=>value) for the insert operation. See {@link MongoCollection::insert} for more details.
  184. */
  185. public function insert($a, $options = array())
  186. {
  187. echo " > insert document ...";
  188. $time = microtime(true);
  189. $this->getDbConnection()->{$this->getCollectionName()}->insert($a, $options);
  190. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  191. }
  192. /**
  193. * Deletes a document from current collection.
  194. *
  195. * @param array $criteria Description of records to remove.
  196. * @param array $options Options (name=>value) for the remove operation. See {@link MongoCollection::remove} for more details.
  197. */
  198. public function remove($criteria, $options = array())
  199. {
  200. echo " > remove document ...";
  201. $time = microtime(true);
  202. $this->getDbConnection()->{$this->getCollectionName()}->remove($criteria, $options);
  203. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  204. }
  205. /**
  206. * Saves a document into current collection.
  207. *
  208. * @param mixed $a Array or object to save. If an object is used, it may not have protected or private properties.
  209. * @param array $options Options (name=>value) for the save operation. See {@link MongoCollection::save} for more details.
  210. */
  211. public function save($a, $options = array())
  212. {
  213. echo " > save document ...";
  214. $time = microtime(true);
  215. $this->getDbConnection()->{$this->getCollectionName()}->save($a, $options);
  216. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  217. }
  218. /**
  219. * Sets the currently active database connection.
  220. * The database connection will be used by the methods such as {@link insert}, {@link createCollection}.
  221. *
  222. * @param EMongoClient $db the database connection component
  223. */
  224. public function setDbConnection($db)
  225. {
  226. $this->_db = $db;
  227. }
  228. /**
  229. * This method contains the logic to be executed when applying this migration.
  230. * Child classes may implement this method to provide actual migration logic.
  231. *
  232. * @return boolean Returning false means, the migration will not be applied.
  233. */
  234. public function up()
  235. {
  236. }
  237. /**
  238. * Updates a document within the current collection.
  239. *
  240. * @param array $criteria Description of the objects to update.
  241. * @param array $options Options (name=>value) for the update operation. See {@link MongoCollection::update} for more details.
  242. */
  243. public function update($criteria, $new_object, $options = array())
  244. {
  245. echo " > update document ...";
  246. $time = microtime(true);
  247. $this->getDbConnection()->{$this->getCollectionName()}->update($criteria, $new_object, $options);
  248. echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
  249. }
  250. }