|
1 |
| -<?php |
2 |
| -namespace MongoDB; |
3 |
| - |
4 |
| -use MongoDB\Manager; |
5 |
| -use MongoDB\Query; |
6 |
| -use MongoDB\ReadPreference; |
7 |
| -use MongoDB\WriteBatch; |
8 |
| - |
9 |
| -class QueryFlags { |
10 |
| - const TAILABLE_CURSOR = 0x02; |
11 |
| - const SLAVE_OKAY = 0x04; |
12 |
| - const OPLOG_REPLY = 0x08; |
13 |
| - const NO_CURSOR_TIMEOUT = 0x10; |
14 |
| - const AWAIT_DATA = 0x20; |
15 |
| - const EXHAUST = 0x40; |
16 |
| - const PARTIAL = 0x80; |
17 |
| -} |
18 |
| -class CursorType { |
19 |
| - const NON_TAILABLE = 0x00; |
20 |
| - const TAILABLE = QueryFlags::TAILABLE_CURSOR; |
21 |
| - /* QueryFlags::TAILABLE_CURSOR | QueryFlags::AWAIT_DATA; */ |
22 |
| - const TAILABLE_AWAIT = 0x22; |
23 |
| -} |
24 |
| - |
25 |
| -class Collection { |
26 |
| - const INSERT = 0x01; |
27 |
| - const UPDATE = 0x02; |
28 |
| - const DELETE = 0x04; |
29 |
| - |
30 |
| - protected $manager; |
31 |
| - protected $rp; |
32 |
| - protected $wc; |
33 |
| - protected $ns; |
34 |
| - |
35 |
| - function __construct(Manager $manager, $ns, WriteConcern $wc = null, ReadPreference $rp = null) { |
36 |
| - $this->manager = $manager; |
37 |
| - $this->ns = $ns; |
38 |
| - $this->wc = $wc; |
39 |
| - $this->rp = $rp; |
40 |
| - } |
41 |
| - |
42 |
| - function find(array $filter = array(), array $options = array()) { /* {{{ {{{ */ |
43 |
| - $options = array_merge($this->getFindOptions(), $options); |
44 |
| - |
45 |
| - $query = $this->_buildQuery($filter, $options); |
46 |
| - |
47 |
| - $cursor = $this->manager->executeQuery($this->ns, $query, $this->rp); |
48 |
| - |
49 |
| - return $cursor; |
50 |
| - } /* }}} */ |
51 |
| - function getFindOptions() { /* {{{ */ |
52 |
| - return array( |
53 |
| - /** |
54 |
| - * Get partial results from a mongos if some shards are down (instead of throwing an error). |
55 |
| - * |
56 |
| - * @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query |
57 |
| - */ |
58 |
| - "allowPartialResults" => false, |
59 |
| - |
60 |
| - /** |
61 |
| - * The number of documents to return per batch. |
62 |
| - * |
63 |
| - * @see http://docs.mongodb.org/manual/reference/method/cursor.batchSize/ |
64 |
| - */ |
65 |
| - "batchSize" => 101, |
66 |
| - |
67 |
| - /** |
68 |
| - * Attaches a comment to the query. If $comment also exists |
69 |
| - * in the modifiers document, the comment field overwrites $comment. |
70 |
| - * |
71 |
| - * @see http://docs.mongodb.org/manual/reference/operator/meta/comment/ |
72 |
| - */ |
73 |
| - "comment" => "", |
74 |
| - |
75 |
| - /** |
76 |
| - * Indicates the type of cursor to use. This value includes both |
77 |
| - * the tailable and awaitData options. |
78 |
| - * The default is NON_TAILABLE. |
79 |
| - * |
80 |
| - * @see http://docs.mongodb.org/manual/reference/operator/meta/comment/ |
81 |
| - */ |
82 |
| - "cursorType" => CursorType::NON_TAILABLE, |
83 |
| - |
84 |
| - /** |
85 |
| - * The maximum number of documents to return. |
86 |
| - * |
87 |
| - * @see http://docs.mongodb.org/manual/reference/method/cursor.limit/ |
88 |
| - */ |
89 |
| - "limit" => 0, |
90 |
| - |
91 |
| - /** |
92 |
| - * The maximum amount of time to allow the query to run. If $maxTimeMS also exists |
93 |
| - * in the modifiers document, the maxTimeMS field overwrites $maxTimeMS. |
94 |
| - * |
95 |
| - * @see http://docs.mongodb.org/manual/reference/operator/meta/maxTimeMS/ |
96 |
| - */ |
97 |
| - "maxTimeMS" => 0, |
98 |
| - |
99 |
| - /** |
100 |
| - * Meta-operators modifying the output or behavior of a query. |
101 |
| - * |
102 |
| - * @see http://docs.mongodb.org/manual/reference/operator/query-modifier/ |
103 |
| - */ |
104 |
| - "modifiers" => array(), |
105 |
| - |
106 |
| - /** |
107 |
| - * The server normally times out idle cursors after an inactivity period (10 minutes) |
108 |
| - * to prevent excess memory use. Set this option to prevent that. |
109 |
| - * |
110 |
| - * @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query |
111 |
| - */ |
112 |
| - "noCursorTimeout" => false, |
113 |
| - |
114 |
| - /** |
115 |
| - * Internal replication use only - driver should not set |
116 |
| - * |
117 |
| - * @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query |
118 |
| - */ |
119 |
| - "oplogReplay" => false, |
120 |
| - |
121 |
| - /** |
122 |
| - * Limits the fields to return for all matching documents. |
123 |
| - * |
124 |
| - * @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/ |
125 |
| - */ |
126 |
| - "projection" => array(), |
127 |
| - |
128 |
| - /** |
129 |
| - * The number of documents to skip before returning. |
130 |
| - * |
131 |
| - * @see http://docs.mongodb.org/manual/reference/method/cursor.skip/ |
132 |
| - */ |
133 |
| - "skip" => 0, |
134 |
| - |
135 |
| - /** |
136 |
| - * The order in which to return matching documents. If $orderby also exists |
137 |
| - * in the modifiers document, the sort field overwrites $orderby. |
138 |
| - * |
139 |
| - * @see http://docs.mongodb.org/manual/reference/method/cursor.sort/ |
140 |
| - */ |
141 |
| - "sort" => array(), |
142 |
| - ); |
143 |
| - } /* }}} */ |
144 |
| - protected function _opQueryFlags($options) { /* {{{ */ |
145 |
| - $flags = 0; |
146 |
| - |
147 |
| - $flags |= $options["allowPartialResults"] ? QueryFlags::PARTIAL : 0; |
148 |
| - $flags |= $options["cursorType"] ? $options["cursorType"] : 0; |
149 |
| - $flags |= $options["oplogReplay"] ? QueryFlags::OPLOG_REPLY: 0; |
150 |
| - $flags |= $options["noCursorTimeout"] ? QueryFlags::NO_CURSOR_TIMEOUT : 0; |
151 |
| - |
152 |
| - return $flags; |
153 |
| - } /* }}} */ |
154 |
| - protected function _buildQuery($filter, $options) { /* {{{ */ |
155 |
| - if ($options["comment"]) { |
156 |
| - $options["modifiers"]['$comment'] = $options["comment"]; |
157 |
| - } |
158 |
| - if ($options["maxTimeMS"]) { |
159 |
| - $options["modifiers"]['$maxTimeMS'] = $options["maxTimeMS"]; |
160 |
| - } |
161 |
| - if ($options["sort"]) { |
162 |
| - $options['$orderby'] = $options["sort"]; |
163 |
| - } |
164 |
| - |
165 |
| - $flags = $this->_opQueryFlags($options); |
166 |
| - $options["cursorFlags"] = $flags; |
167 |
| - |
168 |
| - |
169 |
| - $query = new Query($filter, $options); |
170 |
| - |
171 |
| - return $query; |
172 |
| - } /* }}} */ |
173 |
| - /* }}} */ |
174 |
| - |
175 |
| - protected function _writeSingle($filter, $type, array $options = array(), $newobj = array()) { /* {{{ */ |
176 |
| - $options = array_merge($this->getWriteOptions(), $options); |
177 |
| - |
178 |
| - $batch = new WriteBatch($options["ordered"]); |
179 |
| - switch($type) { |
180 |
| - case self::INSERT: |
181 |
| - $batch->insert($filter); |
182 |
| - break; |
183 |
| - |
184 |
| - case self::DELETE: |
185 |
| - $batch->delete($filter, $options); |
186 |
| - break; |
187 |
| - |
188 |
| - case self::UPDATE: |
189 |
| - $batch->update($filter, $newobj, $options); |
190 |
| - break; |
191 |
| - } |
192 |
| - |
193 |
| - return $this->manager->executeWriteBatch($this->ns, $batch, $this->wc); |
194 |
| - } /* }}} */ |
195 |
| - function getWriteOptions() { /* {{{ */ |
196 |
| - return array( |
197 |
| - "ordered" => false, |
198 |
| - "upsert" => false, |
199 |
| - "limit" => 1, |
200 |
| - ); |
201 |
| - } /* }}} */ |
202 |
| - |
203 |
| - function insertOne(array $filter) { /* {{{ */ |
204 |
| - return $this->_writeSingle($filter, self::INSERT); |
205 |
| - } /* }}} */ |
206 |
| - function deleteOne(array $filter) { /* {{{ */ |
207 |
| - return $this->_writeSingle($filter, self::DELETE); |
208 |
| - } /* }}} */ |
209 |
| - function deleteMany(array $filter) { /* {{{ */ |
210 |
| - return $this->_writeSingle($filter, self::DELETE, array("limit" => 0)); |
211 |
| - } /* }}} */ |
212 |
| - function updateOne(array $filter, array $update, array $options = array()) { /* {{{ */ |
213 |
| - if (key($update)[0] != '$') { |
214 |
| - throw new \RuntimeException("First key in \$update must be a \$operator"); |
215 |
| - } |
216 |
| - return $this->_writeSingle($filter, self::UPDATE, $options, $update); |
217 |
| - } /* }}} */ |
218 |
| - function replaceOne(array $filter, array $update, array $options = array()) { /* {{{ */ |
219 |
| - if (key($update)[0] == '$') { |
220 |
| - throw new \RuntimeException("First key in \$update must NOT be a \$operator"); |
221 |
| - } |
222 |
| - return $this->_writeSingle($filter, self::UPDATE, $options, array('$set' => $update)); |
223 |
| - } /* }}} */ |
224 |
| - function updateMany(array $filter, $update, array $options = array()) { /* {{{ */ |
225 |
| - return $this->_writeSingle($filter, self::UPDATE, $options + array("limit" => 0), $update); |
226 |
| - } /* }}} */ |
227 |
| -} |
228 | 1 |
|
0 commit comments