This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Query.php
295 lines (253 loc) · 9.19 KB
/
Query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\MongoDB\Query;
use Doctrine\MongoDB\Collection;
use Doctrine\MongoDB\Cursor;
use Doctrine\MongoDB\Database;
use Doctrine\MongoDB\EagerCursor;
use Doctrine\MongoDB\Iterator;
use Doctrine\MongoDB\IteratorAggregate;
/**
* Query is responsible for executing and returning the results from queries built by the
* query builder.
*
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @since 1.0
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
class Query implements IteratorAggregate
{
const TYPE_FIND = 1;
const TYPE_FIND_AND_UPDATE = 2;
const TYPE_FIND_AND_REMOVE = 3;
const TYPE_INSERT = 4;
const TYPE_UPDATE = 5;
const TYPE_REMOVE = 6;
const TYPE_GROUP = 7;
const TYPE_MAP_REDUCE = 8;
const TYPE_DISTINCT_FIELD = 9;
const TYPE_GEO_LOCATION = 10;
const TYPE_COUNT = 11;
/**
* The Database instance.
*
* @var Database
*/
protected $database;
/**
* The Collection instance.
*
* @var Collection
*/
protected $collection;
/**
* Array containing the query data.
*
* @var array
*/
protected $query = array();
/**
* Mongo command prefix
*
* @var string
*/
protected $cmd;
/**
* @var Iterator
*/
protected $iterator;
/**
* Query options
*
* @var array
*/
protected $options;
public function __construct(Database $database, Collection $collection, array $query, array $options, $cmd)
{
$this->database = $database;
$this->collection = $collection;
$this->query = $query;
$this->cmd = $cmd;
$this->options = $options;
}
/**
* Gets an array of information about this query for debugging.
*
* @param string $name
* @return array $debug
*/
public function debug($name = null)
{
$debug = $this->query['query'];
if ($name !== null) {
return $debug[$name];
}
foreach ($debug as $key => $value) {
if ( ! $value) {
unset($debug[$key]);
}
}
return $debug;
}
public function getQuery()
{
return $this->query;
}
public function getType()
{
return $this->query['type'];
}
public function getIterator()
{
if ($this->iterator === null) {
$iterator = $this->execute();
if ($iterator !== null && !$iterator instanceof Iterator) {
throw new \BadMethodCallException('Query execution did not return an iterator. This query may not support returning iterators. ');
}
$this->iterator = $iterator;
}
return $this->iterator;
}
public function execute()
{
switch ($this->query['type']) {
case self::TYPE_FIND:
$cursor = $this->collection->find($this->query['query'], $this->query['select']);
return $this->prepareCursor($cursor);
case self::TYPE_FIND_AND_UPDATE:
if ($this->query['sort']) {
$this->options['sort'] = $this->query['sort'];
}
if ($this->query['select']) {
$this->options['fields'] = $this->query['select'];
}
if ($this->query['upsert']) {
$this->options['upsert'] = true;
}
if ($this->query['new']) {
$this->options['new'] = true;
}
return $this->collection->findAndUpdate($this->query['query'], $this->query['newObj'], $this->options);
case self::TYPE_FIND_AND_REMOVE:
if ($this->query['sort']) {
$this->options['sort'] = $this->query['sort'];
}
if ($this->query['select']) {
$this->options['fields'] = $this->query['select'];
}
return $this->collection->findAndRemove($this->query['query'], $this->options);
case self::TYPE_INSERT:
return $this->collection->insert($this->query['newObj']);
case self::TYPE_UPDATE:
if ($this->query['upsert']) {
$this->options['upsert'] = $this->query['upsert'];
}
if ($this->query['multiple']) {
$this->options['multiple'] = $this->query['multiple'];
}
return $this->collection->update($this->query['query'], $this->query['newObj'], $this->options);
case self::TYPE_REMOVE:
return $this->collection->remove($this->query['query'], $this->options);
case self::TYPE_GROUP:
if (!empty($this->query['query'])) {
$this->query['group']['options']['condition'] = $this->query['query'];
}
$options = array_merge($this->options, $this->query['group']['options']);
return $this->collection->group($this->query['group']['keys'], $this->query['group']['initial'], $this->query['group']['reduce'], $options);
case self::TYPE_MAP_REDUCE:
if (!isset($this->query['mapReduce']['out'])) {
$this->query['mapReduce']['out'] = array('inline' => true);
}
$options = array_merge($this->options, $this->query['mapReduce']['options']);
$cursor = $this->collection->mapReduce($this->query['mapReduce']['map'], $this->query['mapReduce']['reduce'], $this->query['mapReduce']['out'], $this->query['query'], $options);
if ($cursor instanceof Cursor) {
$cursor = $this->prepareCursor($cursor);
}
return $cursor;
case self::TYPE_DISTINCT_FIELD:
return $this->collection->distinct($this->query['distinctField'], $this->query['query'], $this->options);
case self::TYPE_GEO_LOCATION:
if (isset($this->query['limit']) && $this->query['limit']) {
$this->options['num'] = $this->query['limit'];
}
foreach (array('distanceMultiplier', 'maxDistance', 'spherical') as $key) {
if (isset($this->query['geoNear'][$key]) && $this->query['geoNear'][$key]) {
$this->options[$key] = $this->query['geoNear'][$key];
}
}
return $this->collection->near($this->query['geoNear']['near'], $this->query['query'], $this->options);
case self::TYPE_COUNT:
return $this->collection->count($this->query['query']);
}
}
protected function prepareCursor(Cursor $cursor)
{
$cursor->limit($this->query['limit']);
$cursor->skip($this->query['skip']);
$cursor->sort($this->query['sort']);
$cursor->immortal($this->query['immortal']);
if (null !== $this->query['slaveOkay']) {
$cursor->slaveOkay($this->query['slaveOkay']);
}
if ($this->query['snapshot']) {
$cursor->snapshot();
}
foreach ($this->query['hints'] as $keyPattern) {
$cursor->hint($keyPattern);
}
if ($this->query['eagerCursor'] === true) {
$cursor = new EagerCursor($cursor);
}
return $cursor;
}
/**
* Count the number of results for this query.
*
* @param bool $all
* @return integer $count
*/
public function count($all = false)
{
return $this->getIterator()->count($all);
}
/**
* Execute the query and get a single result
*
* @return object $document The single document.
*/
public function getSingleResult()
{
return $this->getIterator()->getSingleResult();
}
/**
* Iterator over the query using the Cursor.
*
* @return Cursor $cursor
*/
public function iterate()
{
return $this->getIterator();
}
/** @inheritDoc */
public function toArray()
{
return $this->getIterator()->toArray();
}
}