forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilder.php
344 lines (290 loc) · 10.6 KB
/
Builder.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
declare(strict_types=1);
namespace MongoDB\Laravel\Eloquent;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use MongoDB\BSON\Document;
use MongoDB\Builder\Type\QueryInterface;
use MongoDB\Builder\Type\SearchOperatorInterface;
use MongoDB\Driver\CursorInterface;
use MongoDB\Driver\Exception\BulkWriteException;
use MongoDB\Laravel\Connection;
use MongoDB\Laravel\Helpers\QueriesRelationships;
use MongoDB\Laravel\Query\AggregationBuilder;
use MongoDB\Model\BSONDocument;
use function array_key_exists;
use function array_merge;
use function collect;
use function is_array;
use function is_object;
use function iterator_to_array;
use function property_exists;
/**
* @method \MongoDB\Laravel\Query\Builder toBase()
* @template TModel of Model
*/
class Builder extends EloquentBuilder
{
private const DUPLICATE_KEY_ERROR = 11000;
use QueriesRelationships;
/**
* The methods that should be returned from query builder.
*
* @var array
*/
protected $passthru = [
'average',
'avg',
'count',
'dd',
'doesntexist',
'dump',
'exists',
'getbindings',
'getconnection',
'getgrammar',
'insert',
'insertgetid',
'insertorignore',
'insertusing',
'max',
'min',
'autocomplete',
'pluck',
'pull',
'push',
'raw',
'sum',
'tomql',
];
/**
* @return ($function is null ? AggregationBuilder : self)
*
* @inheritdoc
*/
public function aggregate($function = null, $columns = ['*'])
{
$result = $this->toBase()->aggregate($function, $columns);
return $result ?: $this;
}
/**
* Performs a full-text search of the field or fields in an Atlas collection.
*
* @see https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/
*
* @return Collection<int, TModel>
*/
public function search(
SearchOperatorInterface|array $operator,
?string $index = null,
?array $highlight = null,
?bool $concurrent = null,
?string $count = null,
?string $searchAfter = null,
?string $searchBefore = null,
?bool $scoreDetails = null,
?array $sort = null,
?bool $returnStoredSource = null,
?array $tracking = null,
): Collection {
$results = $this->toBase()->search($operator, $index, $highlight, $concurrent, $count, $searchAfter, $searchBefore, $scoreDetails, $sort, $returnStoredSource, $tracking);
return $this->model->hydrate($results->all());
}
/**
* Performs a semantic search on data in your Atlas Vector Search index.
* NOTE: $vectorSearch is only available for MongoDB Atlas clusters, and is not available for self-managed deployments.
*
* @see https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/
*
* @return Collection<int, TModel>
*/
public function vectorSearch(
string $index,
string $path,
array $queryVector,
int $limit,
bool $exact = false,
QueryInterface|array $filter = [],
int|null $numCandidates = null,
): Collection {
$results = $this->toBase()->vectorSearch($index, $path, $queryVector, $limit, $exact, $filter, $numCandidates);
return $this->model->hydrate($results->all());
}
/** @inheritdoc */
public function update(array $values, array $options = [])
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
$relation = $this->model->getParentRelation();
if ($relation) {
$relation->performUpdate($this->model, $values);
return 1;
}
return $this->toBase()->update($this->addUpdatedAtColumn($values), $options);
}
/** @inheritdoc */
public function insert(array $values)
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
$relation = $this->model->getParentRelation();
if ($relation) {
$relation->performInsert($this->model, $values);
return true;
}
return parent::insert($values);
}
/** @inheritdoc */
public function insertGetId(array $values, $sequence = null)
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
$relation = $this->model->getParentRelation();
if ($relation) {
$relation->performInsert($this->model, $values);
return $this->model->getKey();
}
return parent::insertGetId($values, $sequence);
}
/** @inheritdoc */
public function delete()
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
$relation = $this->model->getParentRelation();
if ($relation) {
$relation->performDelete($this->model);
return $this->model->getKey();
}
return parent::delete();
}
/** @inheritdoc */
public function increment($column, $amount = 1, array $extra = [])
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
$relation = $this->model->getParentRelation();
if ($relation) {
$value = $this->model->{$column};
// When doing increment and decrements, Eloquent will automatically
// sync the original attributes. We need to change the attribute
// temporary in order to trigger an update query.
$this->model->{$column} = null;
$this->model->syncOriginalAttribute($column);
return $this->model->update([$column => $value]);
}
return parent::increment($column, $amount, $extra);
}
/** @inheritdoc */
public function decrement($column, $amount = 1, array $extra = [])
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
$relation = $this->model->getParentRelation();
if ($relation) {
$value = $this->model->{$column};
// When doing increment and decrements, Eloquent will automatically
// sync the original attributes. We need to change the attribute
// temporary in order to trigger an update query.
$this->model->{$column} = null;
$this->model->syncOriginalAttribute($column);
return $this->model->update([$column => $value]);
}
return parent::decrement($column, $amount, $extra);
}
/** @inheritdoc */
public function raw($value = null)
{
// Get raw results from the query builder.
$results = $this->query->raw($value);
// Convert MongoCursor results to a collection of models.
if ($results instanceof CursorInterface) {
$results->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']);
$results = $this->query->aliasIdForResult(iterator_to_array($results));
return $this->model->hydrate($results);
}
// Convert MongoDB Document to a single object.
if (is_object($results) && (property_exists($results, '_id') || property_exists($results, 'id'))) {
$results = (array) match (true) {
$results instanceof BSONDocument => $results->getArrayCopy(),
$results instanceof Document => $results->toPHP(['root' => 'array', 'document' => 'array', 'array' => 'array']),
default => $results,
};
}
// The result is a single object.
if (is_array($results) && (array_key_exists('_id', $results) || array_key_exists('id', $results))) {
$results = $this->query->aliasIdForResult($results);
return $this->model->newFromBuilder($results);
}
return $results;
}
public function firstOrCreate(array $attributes = [], array $values = [])
{
$instance = (clone $this)->where($attributes)->first();
if ($instance !== null) {
return $instance;
}
// createOrFirst is not supported in transaction.
if ($this->getConnection()->getSession()?->isInTransaction()) {
return $this->create(array_merge($attributes, $values));
}
return $this->createOrFirst($attributes, $values);
}
public function createOrFirst(array $attributes = [], array $values = [])
{
// The duplicate key error would abort the transaction. Using the regular firstOrCreate in that case.
if ($this->getConnection()->getSession()?->isInTransaction()) {
return $this->firstOrCreate($attributes, $values);
}
try {
return $this->create(array_merge($attributes, $values));
} catch (BulkWriteException $e) {
if ($e->getCode() === self::DUPLICATE_KEY_ERROR) {
return $this->where($attributes)->first() ?? throw $e;
}
throw $e;
}
}
/**
* Add the "updated at" column to an array of values.
* TODO Remove if https://github.com/laravel/framework/commit/6484744326531829341e1ff886cc9b628b20d73e
* will be reverted
* Issue in laravel/frawework https://github.com/laravel/framework/issues/27791.
*
* @return array
*/
protected function addUpdatedAtColumn(array $values)
{
if (! $this->model->usesTimestamps() || $this->model->getUpdatedAtColumn() === null) {
return $values;
}
$column = $this->model->getUpdatedAtColumn();
$values = array_merge(
[$column => $this->model->freshTimestampString()],
$values,
);
return $values;
}
public function getConnection(): Connection
{
return $this->query->getConnection();
}
/** @inheritdoc */
protected function ensureOrderForCursorPagination($shouldReverse = false)
{
if (empty($this->query->orders)) {
$this->enforceOrderBy();
}
if ($shouldReverse) {
$this->query->orders = collect($this->query->orders)
->map(static fn (int $direction) => $direction === 1 ? -1 : 1)
->toArray();
}
return collect($this->query->orders)
->map(static fn ($direction, $column) => [
'column' => $column,
'direction' => $direction === 1 ? 'asc' : 'desc',
])->values();
}
}