/
Query.scala
1422 lines (1190 loc) · 75.7 KB
/
Query.scala
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2011 Foursquare Labs Inc. All Rights Reserved.
package com.foursquare.rogue
import com.foursquare.rogue.MongoHelpers.{
AndCondition, MongoBuilder, MongoModify, MongoOrder, MongoSelect, QueryExecutor}
import com.mongodb.{BasicDBObjectBuilder, DBObject, WriteConcern}
import net.liftweb.common.{Box, Full}
import net.liftweb.mongodb.MongoDB
import net.liftweb.mongodb.record.{MongoRecord, MongoMetaRecord}
import net.liftweb.record.Field
import org.bson.types.BasicBSONList
import scala.collection.mutable.ListBuffer
import scala.collection.immutable.ListMap
// ***************************************************************************
// *** Builders
// ***************************************************************************
/**
* The definition of methods needed to build a query.
*
* <p> To construct a query, an instance of a query-builder needs to be created. That's
* done by using an implicit conversion from an instance of a meta-record. In code, the
* user writes a query by calling one of the query construction methods on an instance of
* the query-record meta-record instance. The implicit conversion will construct an appropriate
* query builder from the meta-record.</p>
*
* <p> Query builders are parameterized using a collection of <em>phantom types</em>.
* For our purposes here, phantom types are types which are inferred by the type system,
* rather than being explicitly provided by users. The phantom types are inferred by the type
* system on the basis of what clauses are contained in the query. For example, if there's a
* ordering clause, that constrains the types so that the type system must infer a type parameter
* of type "Ordered". This use of phantom types allows the type system to prevent a range of
* query errors - for example, if two query clauses have incompatible ordering constraints, the
* type system will reject it.</p>
*
* <p> The specific mechanics of the type inference process are based on implicit parameters. A
* type can only get inferred into an expression based on a parameter. But we don't want people
* to have to specify parameters explicitly - that would wreck the syntax. Instead, we use implicit
* parameters. The inference system will find an implicit parameter that is type compatible with
* what's used in the rest of the expression.
*
* @param M the record type being queried.
* @param R
* @param Ord a phantom type which defines the sorting behavior of the query.
* @param Sel a phantom type which defines the selection criteria of the query.
* @param Lim a phantom type which defines the result-size limit of the query.
* @param Sk a phantom type which defines the skip-size of the query.
* @param Or a phantom type which defines whether this query is joined to another query-clause by
* an or-clause. The type system will guarantee that the query and its or-connected query
* have the same types.
*/
trait AbstractQuery[M <: MongoRecord[M], R,
Ord <: MaybeOrdered,
Sel <: MaybeSelected,
Lim <: MaybeLimited,
Sk <: MaybeSkipped,
Or <: MaybeHasOrClause] {
def meta: M with MongoMetaRecord[M]
def master: MongoMetaRecord[M]
/**
* Adds a where clause to a query.
*/
def where[F](clause: M => QueryClause[F]): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
/**
* Adds another and-connected clause to the query.
*/
def and[F](clause: M => QueryClause[F]): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
/**
* Adds a scan clause to a query.
*/
def scan[F](clause: M => QueryClause[F]): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
/**
* Adds an iscan clause to a query.
*/
def iscan[F](clause: M => QueryClause[F]): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
def whereOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
def andOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
def scanOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
def iscanOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
def raw(f: BasicDBObjectBuilder => Unit): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
/**
* Chains an "or" subquery to the current query.
*
* <p> The use of the implicit parameter here is key to how the Rogue type checking
* mechanics work. In order to attach an "or" clause to a query, the query as it exists
* must <em>not</em> yet have an or-clause. So the implicit parameter, which carries
* the phantom type information, must be "HasNoOrClause" before this is called. After it's called,
* you can see that the "MaybeHasOrClause" type parameter is changed, and is now specifically
* bound to "HasOrClause", rather than to a type variable.</p>
*/
def or(subqueries: (M with MongoMetaRecord[M] =>
AbstractQuery[M, M, Unordered, Unselected, Unlimited, Unskipped, _])*)
(implicit ev: Or =:= HasNoOrClause): AbstractQuery[M, R, Ord, Sel, Lim, Sk, HasOrClause]
/**
* Like "or", this uses the Rogue phantom-type/implicit parameter mechanics. To call this
* method, the query must <em>not</em> yet have an ordering clause attached. This is captured
* by the implicit parameter being constrained to be "Unordered". After this is called, the
* type signature of the returned query is updated so that the "MaybeOrdered" type parameter is
* now Ordered.
*/
def orderAsc[V](field: M => QueryField[V, M])
(implicit ev: Ord =:= Unordered): AbstractQuery[M, R, Ordered, Sel, Lim, Sk, Or]
def orderDesc[V](field: M => QueryField[V, M])
(implicit ev: Ord =:= Unordered): AbstractQuery[M, R, Ordered, Sel, Lim, Sk, Or]
def andAsc[V](field: M => QueryField[V, M])
(implicit ev: Ord =:= Ordered): AbstractQuery[M, R, Ordered, Sel, Lim, Sk, Or]
def andDesc[V](field: M => QueryField[V, M])
(implicit ev: Ord =:= Ordered): AbstractQuery[M, R, Ordered, Sel, Lim, Sk, Or]
/**
* Places a limit on the size of the returned result.
*
* <p> Like "or", this uses the Rogue phantom-type/implicit parameter mechanics. To call this
* method, the query must <em>not</em> yet have a limit clause attached. This is captured
* by the implicit parameter being constrained to be "Unlimited". After this is called, the
* type signature of the returned query is updated so that the "MaybeLimited" type parameter is
* now Limited.</p>
*/
def limit(n: Int)(implicit ev: Lim =:= Unlimited): AbstractQuery[M, R, Ord, Sel, Limited, Sk, Or]
def limitOpt(n: Option[Int])(implicit ev: Lim =:= Unlimited): AbstractQuery[M, R, Ord, Sel, Limited, Sk, Or]
/**
* Adds a skip to the query.
*
* <p> Like {@link or}, this uses the Rogue phantom-type/implicit parameter mechanics. To call this
* method, the query must <em>not</em> yet have a skip clause attached. This is captured
* by the implicit parameter being constrained to be {@link Unskipped}. After this is called, the
* type signature of the returned query is updated so that the {@link MaybeSkipped} type parameter is
* now {@link Skipped}.</p>
*/
def skip(n: Int)(implicit ev: Sk =:= Unskipped): AbstractQuery[M, R, Ord, Sel, Lim, Skipped, Or]
def skipOpt(n: Option[Int])(implicit ev: Sk =:= Unskipped): AbstractQuery[M, R, Ord, Sel, Lim, Skipped, Or]
/**
* Gets the size of the query result. This should only be called on queries that do not
* have limits or skips.
*/
def count()(implicit ev1: Lim =:= Unlimited, ev2: Sk =:= Unskipped): Long
/**
* Returns the number of distinct values returned by a query. The query must not have
* limit or skip clauses.
*/
def countDistinct[V](field: M => QueryField[V, M])
(implicit ev1: Lim =:= Unlimited,
ev2: Sk =:= Unskipped): Long
/**
* Checks if there are any records that match this query.
*/
def exists()(implicit ev1: Lim =:= Unlimited,
ev2: Sk =:= Unskipped): Boolean
/**
* Executes a function on each record value returned by a query.
* @param f a function to be invoked on each fetched record.
* @return nothing.
*/
def foreach(f: R => Unit): Unit
/**
* Execute the query, returning all of the records that match the query.
* @return a list containing the records that match the query
*/
def fetch(): List[R]
/**
* Execute a query, returning no more than a specified number of result records. The
* query must not have a limit clause.
* @param limit the maximum number of records to return.
*/
def fetch(limit: Int)(implicit ev: Lim =:= Unlimited): List[R]
/**
* fetch a batch of results, and execute a function on each element of the list.
* @param f the function to invoke on the records that match the query.
* @return a list containing the results of invoking the function on each record.
*/
def fetchBatch[T](batchSize: Int)(f: List[R] => List[T]): List[T]
/**
* Fetches the first record that matches the query. The query must not contain a "limited" clause.
* @return an option record containing either the first result that matches the
* query, or None if there are no records that match.
*/
def get()(implicit ev: Lim =:= Unlimited): Option[R]
/**
* Fetches the records that match the query in paginated form. The query must not contain
* a "limit" clause.
* @param countPerPage the number of records to be contained in each page of the result.
*/
def paginate(countPerPage: Int)(implicit ev1: Lim =:= Unlimited,
ev2: Sk =:= Unskipped): BasePaginatedQuery[M, R]
def noop()(implicit ev1: Sel =:= Unselected,
ev2: Lim =:= Unlimited,
ev3: Sk =:= Unskipped): AbstractModifyQuery[M]
/**
* Delete all of the records that match the query. The query must not contain any "skip",
* "limit", or "select" clauses. Sends the delete operation to mongo, and returns - does
* <em>not</em> wait for the delete to be finished.
*/
def bulkDelete_!!()(implicit ev1: Sel =:= Unselected,
ev2: Lim =:= Unlimited,
ev3: Sk =:= Unskipped): Unit
/**
* Delete all of the recurds that match the query. The query must not contain any "skip",
* "limit", or "select" clauses. Sends the delete operation to mongo, and waits for the
* delete operation to complete before returning to the caller.
*/
def blockingBulkDelete_!!(concern: WriteConcern)(implicit ev1: Sel =:= Unselected,
ev2: Lim =:= Unlimited,
ev3: Sk =:= Unskipped): Unit
/**
* Finds the first record that matches the query (if any), fetches it, and then deletes it.
* A copy of the deleted record is returned to the caller.
*/
def findAndDeleteOne(): Option[R]
def signature(): String
/**
* Return a string containing details about how the query would be executed in mongo.
* In particular, this is useful for finding out what indexes will be used by the query.
*/
def explain(): String
def maxScan(max: Int): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
def comment(c: String): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
/**
* Set a flag to indicate whether this query may hit secondaries. This only
* really makes sense if you're using replica sets. If this field is
* unspecified, rogue will leave the option untouched, so you'll use
* secondaries or not depending on how you configure the mongo java driver.
* Also, this only works if you're doing a query -- findAndModify, updates,
* and deletes always go to the primaries.
*
* For more info, see
* http://www.mongodb.org/display/DOCS/Querying#Querying-slaveOk%28QueryingSecondaries%29.
*/
def setSlaveOk(b: Boolean): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
/**
* Adds a select clause to the query. The use of this method constrains the type
* signature of the query to force the "Sel" field to be type "Selected".
*
* <p> The use of the implicit parameter here is key to how the Rogue type checking
* mechanics work. In order to attach a "select" clause to a query, the query as it exists
* must <em>not</em> have a select clause yet. So the implicit parameter, which carries
* the phantom type information, must be "Unselected" before this is called. After it's called,
* you can see that the "MaybeSelected" type parameter is changed, and is now specifically
* bound to "Selected", rather than to a type variable.</p>
*/
def select[F1](f: M => SelectField[F1, M])
(implicit ev: Sel =:= Unselected): AbstractQuery[M, F1, Ord, Selected, Lim, Sk, Or]
def select[F1, F2](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M])
(implicit ev: Sel =:= Unselected): AbstractQuery[M, (F1, F2), Ord, Selected, Lim, Sk, Or]
def select[F1, F2, F3](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M])
(implicit ev: Sel =:= Unselected):
AbstractQuery[M, (F1, F2, F3), Ord, Selected, Lim, Sk, Or]
def select[F1, F2, F3, F4](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M])
(implicit ev: Sel =:= Unselected):
AbstractQuery[M, (F1, F2, F3, F4), Ord, Selected, Lim, Sk, Or]
def select[F1, F2, F3, F4, F5](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M])
(implicit ev: Sel =:= Unselected):
AbstractQuery[M, (F1, F2, F3, F4, F5), Ord, Selected, Lim, Sk, Or]
def select[F1, F2, F3, F4, F5, F6](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M])
(implicit ev: Sel =:= Unselected):
AbstractQuery[M, (F1, F2, F3, F4, F5, F6), Ord, Selected, Lim, Sk, Or]
def select[F1, F2, F3, F4, F5, F6, F7](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M])
(implicit ev: Sel =:= Unselected):
AbstractQuery[M, (F1, F2, F3, F4, F5, F6, F7), Ord, Selected, Lim, Sk, Or]
def select[F1, F2, F3, F4, F5, F6, F7, F8](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M])
(implicit ev: Sel =:= Unselected):
AbstractQuery[M, (F1, F2, F3, F4, F5, F6, F7, F8), Ord, Selected, Lim, Sk, Or]
def select[F1, F2, F3, F4, F5, F6, F7, F8, F9](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
f9: M => SelectField[F9, M])
(implicit ev: Sel =:= Unselected):
AbstractQuery[M, (F1, F2, F3, F4, F5, F6, F7, F8, F9), Ord, Selected, Lim, Sk, Or]
def select[F1, F2, F3, F4, F5, F6, F7, F8, F9, F10](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
f9: M => SelectField[F9, M],
f10: M => SelectField[F10, M])
(implicit ev: Sel =:= Unselected):
AbstractQuery[M, (F1, F2, F3, F4, F5, F6, F7, F8, F9, F10), Ord, Selected, Lim, Sk, Or]
def selectCase[F1, CC](f: M => SelectField[F1, M], create: F1 => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def selectCase[F1, F2, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
create: (F1, F2) => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def selectCase[F1, F2, F3, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
create: (F1, F2, F3) => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def selectCase[F1, F2, F3, F4, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
create: (F1, F2, F3, F4) => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def selectCase[F1, F2, F3, F4, F5, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
create: (F1, F2, F3, F4, F5) => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def selectCase[F1, F2, F3, F4, F5, F6, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
create: (F1, F2, F3, F4, F5, F6) => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def selectCase[F1, F2, F3, F4, F5, F6, F7, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
create: (F1, F2, F3, F4, F5, F6, F7) => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def selectCase[F1, F2, F3, F4, F5, F6, F7, F8, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
create: (F1, F2, F3, F4, F5, F6, F7, F8) => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def selectCase[F1, F2, F3, F4, F5, F6, F7, F8, F9, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
f9: M => SelectField[F9, M],
create: (F1, F2, F3, F4, F5, F6, F7, F8, F9) => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def selectCase[F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
f9: M => SelectField[F9, M],
f10: M => SelectField[F10, M],
create: (F1, F2, F3, F4, F5, F6, F7, F8, F9, F10) => CC)
(implicit ev: Sel =:= Unselected): AbstractQuery[M, CC, Ord, Selected, Lim, Sk, Or]
def hint(h: MongoIndex[M]): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or]
}
case class BaseQuery[M <: MongoRecord[M], R,
Ord <: MaybeOrdered,
Sel <: MaybeSelected,
Lim <: MaybeLimited,
Sk <: MaybeSkipped,
Or <: MaybeHasOrClause](
override val meta: M with MongoMetaRecord[M],
lim: Option[Int],
sk: Option[Int],
maxScan: Option[Int],
comment: Option[String],
hint: Option[ListMap[String, Any]],
condition: AndCondition,
order: Option[MongoOrder],
select: Option[MongoSelect[R, M]],
slaveOk: Option[Boolean]) extends AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or] {
// The meta field on the MongoMetaRecord (as an instance of MongoRecord)
// points to the master MongoMetaRecord. This is here in case you have a
// second MongoMetaRecord pointing to the slave.
override lazy val master = meta.meta
private def addClause[F](clause: M => QueryClause[F],
expectedIndexBehavior: MaybeIndexed):
AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or] = {
clause(meta) match {
case cl: EmptyQueryClause[_] => new BaseEmptyQuery[M, R, Ord, Sel, Lim, Sk, Or]
case cl => {
val newClause = cl.withExpectedIndexBehavior(expectedIndexBehavior)
this.copy(condition = condition.copy(clauses = newClause :: condition.clauses))
}
}
}
override def where[F](clause: M => QueryClause[F]) =
addClause(clause, expectedIndexBehavior = Index)
override def and[F](clause: M => QueryClause[F]) =
addClause(clause, expectedIndexBehavior = Index)
override def iscan[F](clause: M => QueryClause[F]) =
addClause(clause, expectedIndexBehavior = IndexScan)
override def scan[F](clause: M => QueryClause[F]) =
addClause(clause, expectedIndexBehavior = DocumentScan)
private def addClauseOpt[V, F](opt: Option[V])
(clause: (M, V) => QueryClause[F],
expectedIndexBehavior: MaybeIndexed) = {
opt match {
case Some(v) => addClause(clause(_, v), expectedIndexBehavior)
case None => this
}
}
override def whereOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]) =
addClauseOpt(opt)(clause, expectedIndexBehavior = Index)
override def andOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]) =
addClauseOpt(opt)(clause, expectedIndexBehavior = Index)
override def iscanOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]) =
addClauseOpt(opt)(clause, expectedIndexBehavior = IndexScan)
override def scanOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]) =
addClauseOpt(opt)(clause, expectedIndexBehavior = DocumentScan)
override def raw(f: BasicDBObjectBuilder => Unit) = {
val newClause = new RawQueryClause(f)
this.copy(condition = condition.copy(clauses = newClause :: condition.clauses))
}
override def or(subqueries: (M with MongoMetaRecord[M] =>
AbstractQuery[M, M, Unordered, Unselected, Unlimited, Unskipped, _])*)
(implicit ev: Or =:= HasNoOrClause): AbstractQuery[M, R, Ord, Sel, Lim, Sk, HasOrClause] = {
val orCondition = QueryHelpers.orConditionFromQueries(subqueries.toList.map(q => q(meta)))
this.copy(condition = condition.copy(orCondition = Some(orCondition)))
}
override def orderAsc[V](field: M => QueryField[V, M])
(implicit ev: Ord =:= Unordered): BaseQuery[M, R, Ordered, Sel, Lim, Sk, Or] =
this.copy(order = Some(MongoOrder(List((field(meta).field.name, true)))))
override def orderDesc[V](field: M => QueryField[V, M])
(implicit ev: Ord =:= Unordered): BaseQuery[M, R, Ordered, Sel, Lim, Sk, Or] =
this.copy(order = Some(MongoOrder(List((field(meta).field.name, false)))))
override def andAsc[V](field: M => QueryField[V, M])
(implicit ev: Ord =:= Ordered): BaseQuery[M, R, Ordered, Sel, Lim, Sk, Or] =
this.copy(order = Some(MongoOrder((field(meta).field.name, true) :: order.get.terms)))
override def andDesc[V](field: M => QueryField[V, M])
(implicit ev: Ord =:= Ordered): BaseQuery[M, R, Ordered, Sel, Lim, Sk, Or] =
this.copy(order = Some(MongoOrder((field(meta).field.name, false) :: order.get.terms)))
override def limit(n: Int)(implicit ev: Lim =:= Unlimited): BaseQuery[M, R, Ord, Sel, Limited, Sk, Or] =
this.copy(lim = Some(n))
override def limitOpt(n: Option[Int])(implicit ev: Lim =:= Unlimited): BaseQuery[M, R, Ord, Sel, Limited, Sk, Or] =
this.copy(lim = n)
override def skip(n: Int)(implicit ev: Sk =:= Unskipped): BaseQuery[M, R, Ord, Sel, Lim, Skipped, Or] =
this.copy(sk = Some(n))
override def skipOpt(n: Option[Int])(implicit ev: Sk =:= Unskipped): BaseQuery[M, R, Ord, Sel, Lim, Skipped, Or] =
this.copy(sk = n)
private[rogue] def parseDBObject(dbo: DBObject): R = select match {
case Some(MongoSelect(Nil, transformer)) =>
// A MongoSelect clause exists, but has empty fields. Return null.
// This is used for .exists(), where we just want to check the number
// of returned results is > 0.
transformer(null)
case Some(MongoSelect(fields, transformer)) =>
val inst = fields.head.field.owner
def setInstanceFieldFromDbo(field: Field[_, M]) = {
inst.fieldByName(field.name) match {
case Full(fld) => fld.setFromAny(dbo.get(field.name))
case _ => {
val splitName = field.name.split('.').toList
Box.!!(splitName.foldLeft(dbo: Object)((obj: Object, fieldName: String) => {
obj match {
case dbl: BasicBSONList =>
(for {
index <- 0 to dbl.size - 1
val item: DBObject = dbl.get(index).asInstanceOf[DBObject]
} yield item.get(fieldName)).toList
case dbo: DBObject =>
dbo.get(fieldName)
case null => null
}
}))
}
}
}
setInstanceFieldFromDbo(inst.fieldByName("_id").open_!)
transformer(fields.map(fld => fld(setInstanceFieldFromDbo(fld.field))))
case None => meta.fromDBObject(dbo).asInstanceOf[R]
}
private def drainBuffer[A, B](from: ListBuffer[A],
to: ListBuffer[B],
f: List[A] => List[B], size: Int): Unit = {
if (from.size >= size) {
to ++= f(from.toList)
from.clear
}
}
override def count()(implicit ev1: Lim =:= Unlimited, ev2: Sk =:= Unskipped): Long =
QueryExecutor.condition("count", this)(meta.count(_))
override def countDistinct[V](field: M => QueryField[V, M])
(implicit ev1: Lim =:= Unlimited, ev2: Sk =:= Unskipped): Long =
QueryExecutor.condition("countDistinct", this)(meta.countDistinct(field(meta).field.name, _))
override def exists()(implicit ev1: Lim =:= Unlimited, ev2: Sk =:= Unskipped): Boolean =
this.copy(select = Some(MongoSelect[Null, M](Nil, _ => null))).limit(1).fetch().size > 0
override def foreach(f: R => Unit): Unit =
QueryExecutor.query("find", this, None)(dbo => f(parseDBObject(dbo)))
override def fetch(): List[R] = {
val rv = new ListBuffer[R]
QueryExecutor.query("find", this, None)(dbo => rv += parseDBObject(dbo))
rv.toList
}
override def fetch(limit: Int)(implicit ev: Lim =:= Unlimited): List[R] =
this.limit(limit).fetch()
override def fetchBatch[T](batchSize: Int)(f: List[R] => List[T]): List[T] = {
val rv = new ListBuffer[T]
val buf = new ListBuffer[R]
QueryExecutor.query("find", this, Some(batchSize)) { dbo =>
buf += parseDBObject(dbo)
drainBuffer(buf, rv, f, batchSize)
}
drainBuffer(buf, rv, f, 1)
rv.toList
}
override def get()(implicit ev: Lim =:= Unlimited): Option[R] =
fetch(1).headOption
override def paginate(countPerPage: Int)(implicit ev1: Lim =:= Unlimited, ev2: Sk =:= Unskipped) = {
new BasePaginatedQuery(this.copy(), countPerPage)
}
override def noop()(implicit ev1: Sel =:= Unselected,
ev2: Lim =:= Unlimited,
ev3: Sk =:= Unskipped) = BaseModifyQuery(this, MongoModify(Nil))
// Always do modifications against master (not meta, which could point to slave)
override def bulkDelete_!!()(implicit ev1: Sel =:= Unselected,
ev2: Lim =:= Unlimited,
ev3: Sk =:= Unskipped): Unit =
QueryExecutor.condition("remove", this)(master.bulkDelete_!!(_))
override def blockingBulkDelete_!!(concern: WriteConcern)(implicit ev1: Sel =:= Unselected,
ev2: Lim =:= Unlimited,
ev3: Sk =:= Unskipped): Unit =
QueryExecutor.condition("remove", this) { qry =>
MongoDB.useCollection(master.mongoIdentifier, master.collectionName) { coll =>
coll.remove(qry, concern)
}
}
override def findAndDeleteOne(): Option[R] = {
val mod = BaseFindAndModifyQuery(this, MongoModify(Nil))
QueryExecutor.findAndModify(mod, returnNew=false, upsert=false, remove=true)(this.parseDBObject _)
}
override def toString: String =
MongoBuilder.buildQueryString("find", this)
override def signature(): String =
MongoBuilder.buildSignature(this)
override def explain(): String =
QueryExecutor.explain("find", this)
override def maxScan(max: Int): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or] = this.copy(maxScan = Some(max))
override def comment(c: String): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or] = this.copy(comment = Some(c))
override def setSlaveOk(b: Boolean): AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or] = this.copy(slaveOk = Some(b))
override def hint(index: MongoIndex[M]) = this.copy(hint = Some(index.asListMap))
override def select[F1](f: M => SelectField[F1, M])
(implicit ev: Sel =:= Unselected): BaseQuery[M, F1, Ord, Selected, Lim, Sk, Or] = {
selectCase(f, (f: F1) => f)
}
override def select[F1, F2](f1: M => SelectField[F1, M], f2: M => SelectField[F2, M])
(implicit ev: Sel =:= Unselected): BaseQuery[M, (F1, F2), Ord, Selected, Lim, Sk, Or] = {
selectCase(f1, f2, (f1: F1, f2: F2) => (f1, f2))
}
override def select[F1, F2, F3](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M])
(implicit ev: Sel =:= Unselected): BaseQuery[M, (F1, F2, F3), Ord, Selected, Lim, Sk, Or] = {
selectCase(f1, f2, f3, (f1: F1, f2: F2, f3: F3) => (f1, f2, f3))
}
override def select[F1, F2, F3, F4](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M])
(implicit ev: Sel =:= Unselected): BaseQuery[M, (F1, F2, F3, F4), Ord, Selected, Lim, Sk, Or] = {
selectCase(f1, f2, f3, f4, (f1: F1, f2: F2, f3: F3, f4: F4) => (f1, f2, f3, f4))
}
override def select[F1, F2, F3, F4, F5](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M])
(implicit ev: Sel =:= Unselected):
BaseQuery[M, (F1, F2, F3, F4, F5), Ord, Selected, Lim, Sk, Or] = {
selectCase(f1, f2, f3, f4, f5, (f1: F1, f2: F2, f3: F3, f4: F4, f5: F5) => (f1, f2, f3, f4, f5))
}
override def select[F1, F2, F3, F4, F5, F6](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M])
(implicit ev: Sel =:= Unselected):
BaseQuery[M, (F1, F2, F3, F4, F5, F6), Ord, Selected, Lim, Sk, Or] = {
selectCase(f1, f2, f3, f4, f5, f6, (f1: F1, f2: F2, f3: F3, f4: F4, f5: F5, f6: F6) => (f1, f2, f3, f4, f5, f6))
}
override def select[F1, F2, F3, F4, F5, F6, F7](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M])
(implicit ev: Sel =:= Unselected):
BaseQuery[M, (F1, F2, F3, F4, F5, F6, F7), Ord, Selected, Lim, Sk, Or] = {
selectCase(f1, f2, f3, f4, f5, f6, f7,
(f1: F1, f2: F2, f3: F3, f4: F4, f5: F5, f6: F6, f7: F7) => (f1, f2, f3, f4, f5, f6, f7))
}
override def select[F1, F2, F3, F4, F5, F6, F7, F8](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M])
(implicit ev: Sel =:= Unselected):
BaseQuery[M, (F1, F2, F3, F4, F5, F6, F7, F8), Ord, Selected, Lim, Sk, Or] = {
selectCase(f1, f2, f3, f4, f5, f6, f7, f8,
(f1: F1, f2: F2, f3: F3, f4: F4, f5: F5, f6: F6, f7: F7, f8: F8) => (f1, f2, f3, f4, f5, f6, f7, f8))
}
override def select[F1, F2, F3, F4, F5, F6, F7, F8, F9](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
f9: M => SelectField[F9, M])
(implicit ev: Sel =:= Unselected):
BaseQuery[M, (F1, F2, F3, F4, F5, F6, F7, F8, F9), Ord, Selected, Lim, Sk, Or] = {
selectCase(f1, f2, f3, f4, f5, f6, f7, f8, f9,
(f1: F1, f2: F2, f3: F3, f4: F4, f5: F5, f6: F6, f7: F7, f8: F8, f9: F9) =>
(f1, f2, f3, f4, f5, f6, f7, f8, f9))
}
override def select[F1, F2, F3, F4, F5, F6, F7, F8, F9, F10](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
f9: M => SelectField[F9, M],
f10: M => SelectField[F10, M])
(implicit ev: Sel =:= Unselected):
BaseQuery[M, (F1, F2, F3, F4, F5, F6, F7, F8, F9, F10), Ord, Selected, Lim, Sk, Or] = {
selectCase(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10,
(f1: F1, f2: F2, f3: F3, f4: F4, f5: F5, f6: F6, f7: F7, f8: F8, f9: F9, f10: F10) =>
(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10))
}
def selectCase[F1, CC](f: M => SelectField[F1, M],
create: F1 => CC)(implicit ev: Sel =:= Unselected):
BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f(inst))
val transformer = (xs: List[_]) => create(xs.head.asInstanceOf[F1])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
def selectCase[F1, F2, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
create: (F1, F2) => CC)
(implicit ev: Sel =:= Unselected): BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f1(inst), f2(inst))
val transformer = (xs: List[_]) => create(xs(0).asInstanceOf[F1], xs(1).asInstanceOf[F2])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
def selectCase[F1, F2, F3, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
create: (F1, F2, F3) => CC)
(implicit ev: Sel =:= Unselected): BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f1(inst), f2(inst), f3(inst))
val transformer = (xs: List[_]) => create(xs(0).asInstanceOf[F1],
xs(1).asInstanceOf[F2],
xs(2).asInstanceOf[F3])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
def selectCase[F1, F2, F3, F4, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
create: (F1, F2, F3, F4) => CC)
(implicit ev: Sel =:= Unselected):
BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f1(inst), f2(inst), f3(inst), f4(inst))
val transformer = (xs: List[_]) => create(xs(0).asInstanceOf[F1],
xs(1).asInstanceOf[F2],
xs(2).asInstanceOf[F3],
xs(3).asInstanceOf[F4])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
def selectCase[F1, F2, F3, F4, F5, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
create: (F1, F2, F3, F4, F5) => CC)
(implicit ev: Sel =:= Unselected): BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f1(inst), f2(inst), f3(inst), f4(inst), f5(inst))
val transformer = (xs: List[_]) => create(xs(0).asInstanceOf[F1],
xs(1).asInstanceOf[F2],
xs(2).asInstanceOf[F3],
xs(3).asInstanceOf[F4],
xs(4).asInstanceOf[F5])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
def selectCase[F1, F2, F3, F4, F5, F6, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
create: (F1, F2, F3, F4, F5, F6) => CC)
(implicit ev: Sel =:= Unselected): BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f1(inst), f2(inst), f3(inst), f4(inst), f5(inst), f6(inst))
val transformer = (xs: List[_]) => create(xs(0).asInstanceOf[F1],
xs(1).asInstanceOf[F2],
xs(2).asInstanceOf[F3],
xs(3).asInstanceOf[F4],
xs(4).asInstanceOf[F5],
xs(5).asInstanceOf[F6])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
def selectCase[F1, F2, F3, F4, F5, F6, F7, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
create: (F1, F2, F3, F4, F5, F6, F7) => CC)
( implicit ev: Sel =:= Unselected): BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f1(inst), f2(inst), f3(inst), f4(inst), f5(inst), f6(inst), f7(inst))
val transformer = (xs: List[_]) => create(xs(0).asInstanceOf[F1],
xs(1).asInstanceOf[F2],
xs(2).asInstanceOf[F3],
xs(3).asInstanceOf[F4],
xs(4).asInstanceOf[F5],
xs(5).asInstanceOf[F6],
xs(6).asInstanceOf[F7])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
def selectCase[F1, F2, F3, F4, F5, F6, F7, F8, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
create: (F1, F2, F3, F4, F5, F6, F7, F8) => CC)
(implicit ev: Sel =:= Unselected): BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f1(inst), f2(inst), f3(inst), f4(inst), f5(inst), f6(inst), f7(inst), f8(inst))
val transformer = (xs: List[_]) => create(xs(0).asInstanceOf[F1],
xs(1).asInstanceOf[F2],
xs(2).asInstanceOf[F3],
xs(3).asInstanceOf[F4],
xs(4).asInstanceOf[F5],
xs(5).asInstanceOf[F6],
xs(6).asInstanceOf[F7],
xs(7).asInstanceOf[F8])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
def selectCase[F1, F2, F3, F4, F5, F6, F7, F8, F9, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
f9: M => SelectField[F9, M],
create: (F1, F2, F3, F4, F5, F6, F7, F8, F9) => CC)
(implicit ev: Sel =:= Unselected): BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f1(inst), f2(inst), f3(inst), f4(inst), f5(inst), f6(inst), f7(inst), f8(inst), f9(inst))
val transformer = (xs: List[_]) => create(xs(0).asInstanceOf[F1],
xs(1).asInstanceOf[F2],
xs(2).asInstanceOf[F3],
xs(3).asInstanceOf[F4],
xs(4).asInstanceOf[F5],
xs(5).asInstanceOf[F6],
xs(6).asInstanceOf[F7],
xs(7).asInstanceOf[F8],
xs(8).asInstanceOf[F9])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
def selectCase[F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, CC](f1: M => SelectField[F1, M],
f2: M => SelectField[F2, M],
f3: M => SelectField[F3, M],
f4: M => SelectField[F4, M],
f5: M => SelectField[F5, M],
f6: M => SelectField[F6, M],
f7: M => SelectField[F7, M],
f8: M => SelectField[F8, M],
f9: M => SelectField[F9, M],
f10: M => SelectField[F10, M],
create: (F1, F2, F3, F4, F5, F6, F7, F8, F9, F10) => CC)
(implicit ev: Sel =:= Unselected): BaseQuery[M, CC, Ord, Selected, Lim, Sk, Or] = {
val inst = meta.createRecord
val fields = List(f1(inst), f2(inst), f3(inst), f4(inst), f5(inst),
f6(inst), f7(inst), f8(inst), f9(inst), f10(inst))
val transformer = (xs: List[_]) => create(xs(0).asInstanceOf[F1],
xs(1).asInstanceOf[F2],
xs(2).asInstanceOf[F3],
xs(3).asInstanceOf[F4],
xs(4).asInstanceOf[F5],
xs(5).asInstanceOf[F6],
xs(6).asInstanceOf[F7],
xs(7).asInstanceOf[F8],
xs(8).asInstanceOf[F9],
xs(9).asInstanceOf[F10])
this.copy(select = Some(MongoSelect(fields, transformer)))
}
}
class BaseEmptyQuery[M <: MongoRecord[M], R,
Ord <: MaybeOrdered,
Sel <: MaybeSelected,
Lim <: MaybeLimited,
Sk <: MaybeSkipped,
Or <: MaybeHasOrClause] extends AbstractQuery[M, R, Ord, Sel, Lim, Sk, Or] {
override lazy val meta = throw new Exception("tried to read meta field of an EmptyQuery")
override lazy val master = throw new Exception("tried to read master field of an EmptyQuery")
override def where[F](clause: M => QueryClause[F]) = this
override def and[F](clause: M => QueryClause[F]) = this
override def iscan[F](clause: M => QueryClause[F]) = this
override def scan[F](clause: M => QueryClause[F]) = this
override def whereOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]) = this
override def andOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]) = this
override def iscanOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]) = this
override def scanOpt[V, F](opt: Option[V])(clause: (M, V) => QueryClause[F]) = this
override def raw(f: BasicDBObjectBuilder => Unit) = this
override def or(subqueries: (M with MongoMetaRecord[M] =>
AbstractQuery[M, M, Unordered, Unselected, Unlimited, Unskipped, _])*)
(implicit ev: Or =:= HasNoOrClause) = new BaseEmptyQuery[M, R, Ord, Sel, Lim, Sk, HasOrClause]
override def orderAsc[V](field: M => QueryField[V, M])(implicit ev: Ord =:= Unordered) =
new BaseEmptyQuery[M, R, Ordered, Sel, Lim, Sk, Or]
override def orderDesc[V](field: M => QueryField[V, M])(implicit ev: Ord =:= Unordered) =
new BaseEmptyQuery[M, R, Ordered, Sel, Lim, Sk, Or]
override def andAsc[V](field: M => QueryField[V, M])(implicit ev: Ord =:= Ordered) =
new BaseEmptyQuery[M, R, Ordered, Sel, Lim, Sk, Or]
override def andDesc[V](field: M => QueryField[V, M])(implicit ev: Ord =:= Ordered) =
new BaseEmptyQuery[M, R, Ordered, Sel, Lim, Sk, Or]
override def limit(n: Int)(implicit ev: Lim =:= Unlimited) =
new BaseEmptyQuery[M, R, Ord, Sel, Limited, Sk, Or]
override def limitOpt(n: Option[Int])(implicit ev: Lim =:= Unlimited) =
new BaseEmptyQuery[M, R, Ord, Sel, Limited, Sk, Or]
override def skip(n: Int)(implicit ev: Sk =:= Unskipped) =
new BaseEmptyQuery[M, R, Ord, Sel, Lim, Skipped, Or]