|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Operation; |
| 4 | + |
| 5 | +use MongoDB\Driver\Command; |
| 6 | +use MongoDB\Driver\Server; |
| 7 | +use MongoDB\Driver\BulkWrite; |
| 8 | +use MongoDB\Exception\InvalidArgumentException; |
| 9 | +use MongoDB\Exception\RuntimeException; |
| 10 | +use MongoDB\Exception\UnexpectedTypeException; |
| 11 | +use MongoDB\Model\IndexInput; |
| 12 | + |
| 13 | +/** |
| 14 | + * Operation for the create command. |
| 15 | + * |
| 16 | + * @api |
| 17 | + * @see MongoDB\Database::createCollection() |
| 18 | + * @see http://docs.mongodb.org/manual/reference/command/create/ |
| 19 | + */ |
| 20 | +class CreateCollection implements Executable |
| 21 | +{ |
| 22 | + const USE_POWER_OF_2_SIZES = 1; |
| 23 | + const NO_PADDING = 2; |
| 24 | + |
| 25 | + private $databaseName; |
| 26 | + private $collectionName; |
| 27 | + private $options = array(); |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructs a create command. |
| 31 | + * |
| 32 | + * Supported options: |
| 33 | + * |
| 34 | + * * autoIndexId (boolean): Specify false to disable the automatic creation |
| 35 | + * of an index on the _id field. For replica sets, this option cannot be |
| 36 | + * false. The default is true. |
| 37 | + * |
| 38 | + * * capped (boolean): Specify true to create a capped collection. If set, |
| 39 | + * the size option must also be specified. The default is false. |
| 40 | + * |
| 41 | + * * flags (integer): Options for the MMAPv1 storage engine only. Must be a |
| 42 | + * bitwise combination USE_POWER_OF_2_SIZES and NO_PADDING. The default |
| 43 | + * is USE_POWER_OF_2_SIZES. |
| 44 | + * |
| 45 | + * * max (integer): The maximum number of documents allowed in the capped |
| 46 | + * collection. The size option takes precedence over this limit. |
| 47 | + * |
| 48 | + * * maxTimeMS (integer): The maximum amount of time to allow the query to |
| 49 | + * run. |
| 50 | + * |
| 51 | + * * size (integer): The maximum number of bytes for a capped collection. |
| 52 | + * |
| 53 | + * * storageEngine (document): Storage engine options. |
| 54 | + * |
| 55 | + * @see http://source.wiredtiger.com/2.4.1/struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb |
| 56 | + * @param string $databaseName Database name |
| 57 | + * @param string $collectionName Collection name |
| 58 | + * @param array $options Command options |
| 59 | + * @throws InvalidArgumentException |
| 60 | + */ |
| 61 | + public function __construct($databaseName, $collectionName, array $options = array()) |
| 62 | + { |
| 63 | + if (isset($options['autoIndexId']) && ! is_bool($options['autoIndexId'])) { |
| 64 | + throw new InvalidArgumentTypeException('"autoIndexId" option', $options['autoIndexId'], 'boolean'); |
| 65 | + } |
| 66 | + |
| 67 | + if (isset($options['capped']) && ! is_bool($options['capped'])) { |
| 68 | + throw new InvalidArgumentTypeException('"capped" option', $options['capped'], 'boolean'); |
| 69 | + } |
| 70 | + |
| 71 | + if (isset($options['flags']) && ! is_integer($options['flags'])) { |
| 72 | + throw new InvalidArgumentTypeException('"flags" option', $options['flags'], 'integer'); |
| 73 | + } |
| 74 | + |
| 75 | + if (isset($options['max']) && ! is_integer($options['max'])) { |
| 76 | + throw new InvalidArgumentTypeException('"max" option', $options['max'], 'integer'); |
| 77 | + } |
| 78 | + |
| 79 | + if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) { |
| 80 | + throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer'); |
| 81 | + } |
| 82 | + |
| 83 | + if (isset($options['size']) && ! is_integer($options['size'])) { |
| 84 | + throw new InvalidArgumentTypeException('"size" option', $options['size'], 'integer'); |
| 85 | + } |
| 86 | + |
| 87 | + if (isset($options['storageEngine']) && ! is_array($options['storageEngine']) && ! is_object($options['storageEngine'])) { |
| 88 | + throw new InvalidArgumentTypeException('"storageEngine" option', $options['storageEngine'], 'array or object'); |
| 89 | + } |
| 90 | + |
| 91 | + $this->databaseName = (string) $databaseName; |
| 92 | + $this->collectionName = (string) $collectionName; |
| 93 | + $this->options = $options; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Execute the operation. |
| 98 | + * |
| 99 | + * For servers < 2.6, this will actually perform an insert operation on the |
| 100 | + * database's "system.indexes" collection. |
| 101 | + * |
| 102 | + * @see Executable::execute() |
| 103 | + * @param Server $server |
| 104 | + * @return object Command result document |
| 105 | + */ |
| 106 | + public function execute(Server $server) |
| 107 | + { |
| 108 | + $cursor = $server->executeCommand($this->databaseName, $this->createCommand()); |
| 109 | + $result = current($cursor->toArray()); |
| 110 | + |
| 111 | + if (empty($result['ok'])) { |
| 112 | + throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); |
| 113 | + } |
| 114 | + |
| 115 | + return $result; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Create the create command. |
| 120 | + * |
| 121 | + * @return Command |
| 122 | + */ |
| 123 | + private function createCommand() |
| 124 | + { |
| 125 | + $cmd = array('create' => $this->collectionName); |
| 126 | + |
| 127 | + foreach (array('autoIndexId', 'capped', 'flags', 'max', 'maxTimeMS', 'size') as $option) { |
| 128 | + if (isset($this->options[$option])) { |
| 129 | + $cmd[$option] = $this->options[$option]; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if ( ! empty($this->options['storageEngine'])) { |
| 134 | + $cmd['storageEngine'] = (object) $this->options['storageEngine']; |
| 135 | + } |
| 136 | + |
| 137 | + return new Command($cmd); |
| 138 | + } |
| 139 | +} |
0 commit comments