forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_document_query.go
2361 lines (1996 loc) · 58.4 KB
/
abstract_document_query.go
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
package ravendb
import (
"reflect"
"strconv"
"strings"
"time"
)
// Note: Java's IAbstractDocumentQuery is abstractDocumentQuery
type abstractDocumentQuery struct {
aliasToGroupByFieldName map[string]string
defaultOperator QueryOperator
// Note: rootTypes is not used in Go because we only have one ID property
negate bool
indexName string
collectionName string
currentClauseDepth int
queryRaw string
queryParameters Parameters
isIntersect bool
isGroupBy bool
theSession *InMemoryDocumentSessionOperations
pageSize *int
selectTokens []queryToken
fromToken *fromToken
declareToken *declareToken
loadTokens []*loadToken
fieldsToFetchToken *fieldsToFetchToken
whereTokens []queryToken
groupByTokens []queryToken
orderByTokens []queryToken
start int
conventions *DocumentConventions
timeout time.Duration
theWaitForNonStaleResults bool
includes []string
queryStats *QueryStatistics
disableEntitiesTracking bool
disableCaching bool
isInMoreLikeThis bool
// Go doesn't allow comparing functions so to remove we use index returned
// by add() function. We maintain stable index by never shrinking
// callback arrays. We assume there is no high churn of adding/removing
// callbacks
beforeQueryExecutedCallback []func(*IndexQuery)
afterQueryExecutedCallback []func(*QueryResult)
afterStreamExecutedCallback []func(map[string]interface{})
queryOperation *queryOperation
// to allow "fluid" API, in many methods instead of returning an error, we
// remember it here and return in GetResults()
err error
}
func (q *abstractDocumentQuery) Err() error {
return q.err
}
func (q *abstractDocumentQuery) isDistinct() bool {
if len(q.selectTokens) == 0 {
return false
}
_, ok := q.selectTokens[0].(*distinctToken)
return ok
}
func (q *abstractDocumentQuery) getSession() *InMemoryDocumentSessionOperations {
return q.theSession
}
func (q *abstractDocumentQuery) isDynamicMapReduce() bool {
return len(q.groupByTokens) > 0
}
func getQueryDefaultTimeout() time.Duration {
return time.Second * 15
}
// at this point we assume all
func newAbstractDocumentQuery(opts *DocumentQueryOptions) *abstractDocumentQuery {
res := &abstractDocumentQuery{
defaultOperator: QueryOperatorAnd,
isGroupBy: opts.isGroupBy,
indexName: opts.IndexName,
collectionName: opts.CollectionName,
declareToken: opts.declareToken,
loadTokens: opts.loadTokens,
theSession: opts.session,
aliasToGroupByFieldName: make(map[string]string),
queryParameters: make(map[string]interface{}),
queryStats: NewQueryStatistics(),
queryRaw: opts.rawQuery,
}
if opts.session == nil {
res.err = newIllegalArgumentError("session must be provided")
return res
}
if res.queryRaw == "" {
if opts.IndexName == "" && opts.CollectionName == "" {
res.err = newIllegalArgumentError("Either indexName or collectionName must be specified")
return res
}
res.fromToken = createFromToken(opts.IndexName, opts.CollectionName, opts.fromAlias)
}
f := func(queryResult *QueryResult) {
res.updateStatsAndHighlightings(queryResult)
}
res.addAfterQueryExecutedListener(f)
if opts.session == nil {
res.conventions = NewDocumentConventions()
} else {
res.conventions = opts.session.GetConventions()
}
return res
}
func (q *abstractDocumentQuery) usingDefaultOperator(operator QueryOperator) error {
if len(q.whereTokens) > 0 {
return newIllegalStateError("Default operator can only be set before any where clause is added.")
}
q.defaultOperator = operator
return nil
}
func (q *abstractDocumentQuery) waitForNonStaleResults(waitTimeout time.Duration) {
q.theWaitForNonStaleResults = true
if waitTimeout == 0 {
waitTimeout = getQueryDefaultTimeout()
}
q.timeout = waitTimeout
}
func (q *abstractDocumentQuery) initializeQueryOperation() (*queryOperation, error) {
indexQuery, err := q.GetIndexQuery()
if err != nil {
return nil, err
}
return newQueryOperation(q.theSession, q.indexName, indexQuery, q.fieldsToFetchToken, q.disableEntitiesTracking, false, false)
}
func (q *abstractDocumentQuery) GetIndexQuery() (*IndexQuery, error) {
query, err := q.string()
if err != nil {
return nil, err
}
indexQuery := q.generateIndexQuery(query)
q.invokeBeforeQueryExecuted(indexQuery)
return indexQuery, nil
}
func (q *abstractDocumentQuery) getProjectionFields() []string {
if q.fieldsToFetchToken != nil && q.fieldsToFetchToken.projections != nil {
return q.fieldsToFetchToken.projections
}
return nil
}
func (q *abstractDocumentQuery) randomOrdering() error {
if err := q.assertNoRawQuery(); err != nil {
return err
}
q.noCaching()
q.orderByTokens = append(q.orderByTokens, orderByTokenRandom)
return nil
}
func (q *abstractDocumentQuery) randomOrderingWithSeed(seed string) error {
err := q.assertNoRawQuery()
if err != nil {
return err
}
if stringIsBlank(seed) {
err = q.randomOrdering()
if err != nil {
return err
}
return nil
}
q.noCaching()
q.orderByTokens = append(q.orderByTokens, orderByTokenCreateRandom(seed))
return nil
}
func (q *abstractDocumentQuery) addGroupByAlias(fieldName string, projectedName string) {
q.aliasToGroupByFieldName[projectedName] = fieldName
}
func (q *abstractDocumentQuery) assertNoRawQuery() error {
if q.queryRaw != "" {
return newIllegalStateError("RawQuery was called, cannot modify this query by calling on operations that would modify the query (such as Where, Select, OrderBy, GroupBy, etc)")
}
return nil
}
func (q *abstractDocumentQuery) addParameter(name string, value interface{}) error {
name = strings.TrimPrefix(name, "$")
if _, ok := q.queryParameters[name]; ok {
return newIllegalStateError("The parameter " + name + " was already added")
}
q.queryParameters[name] = value
return nil
}
func (q *abstractDocumentQuery) groupBy(fieldName string, fieldNames ...string) error {
var mapping []*GroupBy
for _, x := range fieldNames {
el := NewGroupByField(x)
mapping = append(mapping, el)
}
return q.groupByFieldWithMethod(NewGroupByField(fieldName), mapping...)
}
func (q *abstractDocumentQuery) groupByFieldWithMethod(field *GroupBy, fields ...*GroupBy) error {
// TODO: if q.fromToken is nil, needs to do this check in ToList()
if q.fromToken != nil && !q.fromToken.isDynamic {
return newIllegalStateError("groupBy only works with dynamic queries")
}
if err := q.assertNoRawQuery(); err != nil {
return err
}
q.isGroupBy = true
fieldName, err := q.ensureValidFieldName(field.Field, false)
if err != nil {
return err
}
q.groupByTokens = append(q.groupByTokens, createGroupByToken(fieldName, field.Method))
if len(fields) == 0 {
return nil
}
for _, item := range fields {
fieldName, err = q.ensureValidFieldName(item.Field, false)
if err != nil {
return err
}
q.groupByTokens = append(q.groupByTokens, createGroupByToken(fieldName, item.Method))
}
return nil
}
func (q *abstractDocumentQuery) groupByKey(fieldName string, projectedName string) error {
if err := q.assertNoRawQuery(); err != nil {
return err
}
q.isGroupBy = true
_, hasProjectedName := q.aliasToGroupByFieldName[projectedName]
_, hasFieldName := q.aliasToGroupByFieldName[fieldName]
if projectedName != "" && hasProjectedName {
aliasedFieldName := q.aliasToGroupByFieldName[projectedName]
if fieldName == "" || strings.EqualFold(fieldName, projectedName) {
fieldName = aliasedFieldName
}
} else if fieldName != "" && hasFieldName {
aliasedFieldName := q.aliasToGroupByFieldName[fieldName]
fieldName = aliasedFieldName
}
q.selectTokens = append(q.selectTokens, createGroupByKeyToken(fieldName, projectedName))
return nil
}
// projectedName is optional
func (q *abstractDocumentQuery) groupBySum(fieldName string, projectedName string) error {
if err := q.assertNoRawQuery(); err != nil {
return err
}
q.isGroupBy = true
var err error
fieldName, err = q.ensureValidFieldName(fieldName, false)
if err != nil {
return err
}
q.selectTokens = append(q.selectTokens, createGroupBySumToken(fieldName, projectedName))
return nil
}
// projectedName is optional
func (q *abstractDocumentQuery) groupByCount(projectedName string) error {
if err := q.assertNoRawQuery(); err != nil {
return err
}
q.isGroupBy = true
t := &groupByCountToken{
fieldName: projectedName,
}
q.selectTokens = append(q.selectTokens, t)
return nil
}
func (q *abstractDocumentQuery) whereTrue() error {
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, "")
if err != nil {
return err
}
tokens := *tokensRef
tokens = append(tokens, trueTokenInstance)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) moreLikeThis() (*moreLikeThisScope, error) {
err := q.appendOperatorIfNeeded(&q.whereTokens)
if err != nil {
return nil, err
}
token := newMoreLikeThisToken()
q.whereTokens = append(q.whereTokens, token)
q.isInMoreLikeThis = true
add := func(o interface{}) string {
return q.addQueryParameter(o)
}
onDispose := func() {
q.isInMoreLikeThis = false
}
return newMoreLikeThisScope(token, add, onDispose), nil
}
func (q *abstractDocumentQuery) include(path string) {
q.includes = append(q.includes, path)
}
func (q *abstractDocumentQuery) take(count int) {
q.pageSize = &count
}
func (q *abstractDocumentQuery) skip(count int) {
q.start = count
}
func (q *abstractDocumentQuery) whereLucene(fieldName string, whereClause string) error {
var err error
fieldName, err = q.ensureValidFieldName(fieldName, false)
if err != nil {
return err
}
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
tokens := *tokensRef
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, fieldName)
if err != nil {
return err
}
whereToken := createWhereTokenWithOptions(whereOperatorLucene, fieldName, q.addQueryParameter(whereClause), nil)
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) openSubclause() error {
q.currentClauseDepth++
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, "")
if err != nil {
return err
}
tokens := *tokensRef
tokens = append(tokens, openSubclauseTokenInstance)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) closeSubclause() error {
q.currentClauseDepth--
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
tokens := *tokensRef
tokens = append(tokens, closeSubclauseTokenInstance)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) where(fieldName string, op string, value interface{}) error {
op = strings.TrimSpace(op)
switch op {
case "=", "==":
return q.whereEquals(fieldName, value)
case "!=":
return q.whereNotEquals(fieldName, value)
case "<":
return q.whereLessThan(fieldName, value)
case "<=":
return q.whereLessThanOrEqual(fieldName, value)
case ">":
return q.whereGreaterThan(fieldName, value)
case ">=":
return q.whereGreaterThanOrEqual(fieldName, value)
default:
return newIllegalArgumentError("'%s' is not a valid op", op)
}
}
func (q *abstractDocumentQuery) whereEquals(fieldName string, value interface{}) error {
params := &whereParams{
fieldName: fieldName,
value: value,
}
return q.whereEqualsWithParams(params)
}
func (q *abstractDocumentQuery) whereEqualsWithMethodCall(fieldName string, method MethodCall) error {
return q.whereEquals(fieldName, method)
}
func (q *abstractDocumentQuery) whereEqualsWithParams(whereParams *whereParams) error {
if q.negate {
q.negate = false
return q.whereNotEqualsWithParams(whereParams)
}
var err error
whereParams.fieldName, err = q.ensureValidFieldName(whereParams.fieldName, whereParams.isNestedPath)
if err != nil {
return err
}
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
if q.ifValueIsMethod(whereOperatorEquals, whereParams, tokensRef) {
return nil
}
transformToEqualValue := q.transformValue(whereParams)
addQueryParameter := q.addQueryParameter(transformToEqualValue)
whereToken := createWhereTokenWithOptions(whereOperatorEquals, whereParams.fieldName, addQueryParameter, newWhereOptionsWithExact(whereParams.isExact))
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) ifValueIsMethod(op whereOperator, whereParams *whereParams, tokensRef *[]queryToken) bool {
if mc, ok := whereParams.value.(*CmpXchg); ok {
n := len(mc.args)
args := make([]string, n)
for i := 0; i < n; i++ {
args[i] = q.addQueryParameter(mc.args[i])
}
opts := newWhereOptionsWithMethod(MethodsTypeCmpXChg, args, mc.accessPath, whereParams.isExact)
token := createWhereTokenWithOptions(op, whereParams.fieldName, "", opts)
tokens := *tokensRef
tokens = append(tokens, token)
*tokensRef = tokens
return true
}
// add more if there are more types that "derive" from MethodCall
// (by embedding MethodCallData)
return false
}
func (q *abstractDocumentQuery) whereNotEquals(fieldName string, value interface{}) error {
params := &whereParams{
fieldName: fieldName,
value: value,
}
return q.whereNotEqualsWithParams(params)
}
func (q *abstractDocumentQuery) whereNotEqualsWithMethod(fieldName string, method MethodCall) error {
return q.whereNotEquals(fieldName, method)
}
func (q *abstractDocumentQuery) whereNotEqualsWithParams(whereParams *whereParams) error {
if q.negate {
q.negate = false
return q.whereEqualsWithParams(whereParams)
}
transformToEqualValue := q.transformValue(whereParams)
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
whereParams.fieldName, err = q.ensureValidFieldName(whereParams.fieldName, whereParams.isNestedPath)
if err != nil {
return err
}
if q.ifValueIsMethod(whereOperatorNotEquals, whereParams, tokensRef) {
return nil
}
whereToken := createWhereTokenWithOptions(whereOperatorNotEquals, whereParams.fieldName, q.addQueryParameter(transformToEqualValue), newWhereOptionsWithExact(whereParams.isExact))
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) negateNext() {
q.negate = !q.negate
}
// mark last created token as exact. only applies to select number of tokens.
// it allows fluid APIs like .Where().Exact()
// will panic if last token wasn't of compatible type as that is considered
// invalid use of API and returning an error would break fluid API
func (q *abstractDocumentQuery) markLastTokenExact() error {
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
tokens := *tokensRef
n := len(tokens)
lastToken := tokens[n-1]
switch tok := lastToken.(type) {
case *whereToken:
if tok.options == nil {
tok.options = newWhereOptionsWithExact(true)
} else {
tok.options.exact = true
}
default:
return newIllegalStateError("expected whereToken, got %T", lastToken)
}
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) whereIn(fieldName string, values []interface{}) error {
var err error
fieldName, err = q.ensureValidFieldName(fieldName, false)
if err != nil {
return err
}
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, fieldName)
if err != nil {
return err
}
whereToken := createWhereToken(whereOperatorIn, fieldName, q.addQueryParameter(q.transformCollection(fieldName, abstractDocumentQueryUnpackCollection(values))))
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) whereStartsWith(fieldName string, value interface{}) error {
whereParams := &whereParams{
fieldName: fieldName,
value: value,
allowWildcards: true,
}
transformToEqualValue := q.transformValue(whereParams)
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
whereParams.fieldName, err = q.ensureValidFieldName(whereParams.fieldName, whereParams.isNestedPath)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, whereParams.fieldName)
if err != nil {
return err
}
whereToken := createWhereToken(whereOperatorStartsWith, whereParams.fieldName, q.addQueryParameter(transformToEqualValue))
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) whereEndsWith(fieldName string, value interface{}) error {
whereParams := &whereParams{
fieldName: fieldName,
value: value,
allowWildcards: true,
}
transformToEqualValue := q.transformValue(whereParams)
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
whereParams.fieldName, err = q.ensureValidFieldName(whereParams.fieldName, whereParams.isNestedPath)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, whereParams.fieldName)
if err != nil {
return err
}
whereToken := createWhereToken(whereOperatorEndsWith, whereParams.fieldName, q.addQueryParameter(transformToEqualValue))
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) whereBetween(fieldName string, start interface{}, end interface{}) error {
var err error
fieldName, err = q.ensureValidFieldName(fieldName, false)
if err != nil {
return err
}
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, fieldName)
if err != nil {
return err
}
startParams := &whereParams{
value: start,
fieldName: fieldName,
}
endParams := &whereParams{
value: end,
fieldName: fieldName,
}
fromParam := interface{}("*")
if start != nil {
fromParam = q.transformValueWithRange(startParams, true)
}
fromParameterName := q.addQueryParameter(fromParam)
toParam := interface{}("NULL")
if end != nil {
toParam = q.transformValueWithRange(endParams, true)
}
toParameterName := q.addQueryParameter(toParam)
whereToken := createWhereTokenWithOptions(whereOperatorBetween, fieldName, "", newWhereOptionsWithFromTo(false, fromParameterName, toParameterName))
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) whereGreaterThan(fieldName string, value interface{}) error {
var err error
fieldName, err = q.ensureValidFieldName(fieldName, false)
if err != nil {
return err
}
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, fieldName)
if err != nil {
return err
}
whereParams := &whereParams{
value: value,
fieldName: fieldName,
}
paramValue := interface{}("*")
if value != nil {
paramValue = q.transformValueWithRange(whereParams, true)
}
parameter := q.addQueryParameter(paramValue)
whereToken := createWhereTokenWithOptions(whereOperatorGreaterThan, fieldName, parameter, nil)
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) whereGreaterThanOrEqual(fieldName string, value interface{}) error {
var err error
fieldName, err = q.ensureValidFieldName(fieldName, false)
if err != nil {
return err
}
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, fieldName)
if err != nil {
return err
}
whereParams := &whereParams{
value: value,
fieldName: fieldName,
}
paramValue := interface{}("*")
if value != nil {
paramValue = q.transformValueWithRange(whereParams, true)
}
parameter := q.addQueryParameter(paramValue)
whereToken := createWhereTokenWithOptions(whereOperatorGreaterThanOrEqual, fieldName, parameter, nil)
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) whereLessThan(fieldName string, value interface{}) error {
var err error
fieldName, err = q.ensureValidFieldName(fieldName, false)
if err != nil {
return err
}
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, fieldName)
if err != nil {
return err
}
whereParams := &whereParams{
value: value,
fieldName: fieldName,
}
paramValue := interface{}("NULL")
if value != nil {
paramValue = q.transformValueWithRange(whereParams, true)
}
parameter := q.addQueryParameter(paramValue)
whereToken := createWhereTokenWithOptions(whereOperatorLessThan, fieldName, parameter, nil)
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) whereLessThanOrEqual(fieldName string, value interface{}) error {
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, fieldName)
if err != nil {
return err
}
whereParams := &whereParams{
value: value,
fieldName: fieldName,
}
paramValue := interface{}("NULL")
if value != nil {
paramValue = q.transformValueWithRange(whereParams, true)
}
parameter := q.addQueryParameter(paramValue)
whereToken := createWhereTokenWithOptions(whereOperatorLessThanOrEqual, fieldName, parameter, nil)
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) whereRegex(fieldName string, pattern string) error {
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
err = q.appendOperatorIfNeeded(tokensRef)
if err != nil {
return err
}
err = q.negateIfNeeded(tokensRef, fieldName)
if err != nil {
return err
}
whereParams := &whereParams{
value: pattern,
fieldName: fieldName,
}
parameter := q.addQueryParameter(q.transformValue(whereParams))
whereToken := createWhereToken(whereOperatorRegex, fieldName, parameter)
tokens := *tokensRef
tokens = append(tokens, whereToken)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) andAlso() error {
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
tokens := *tokensRef
n := len(tokens)
if n == 0 {
return nil
}
lastToken := tokens[n-1]
if _, ok := lastToken.(*queryOperatorToken); ok {
return newIllegalStateError("Cannot add AND, previous token was already an operator token.")
}
tokens = append(tokens, queryOperatorTokenAnd)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) orElse() error {
tokensRef, err := q.getCurrentWhereTokensRef()
if err != nil {
return err
}
tokens := *tokensRef
n := len(tokens)
if n == 0 {
return nil
}
lastToken := tokens[n-1]
if _, ok := lastToken.(*queryOperatorToken); ok {
return newIllegalStateError("Cannot add OR, previous token was already an operator token.")
}
tokens = append(tokens, queryOperatorTokenOr)
*tokensRef = tokens
return nil
}
func (q *abstractDocumentQuery) boost(boost float64) error {
if boost == 1.0 {
return nil
}
tokens, err := q.getCurrentWhereTokens()
if err != nil {
return err
}
n := len(tokens)
if n == 0 {
return newIllegalStateError("Missing where clause")
}
maybeWhereToken := tokens[n-1]
whereToken, ok := maybeWhereToken.(*whereToken)
if !ok {
return newIllegalStateError("Missing where clause")
}
if boost <= 0.0 {
return newIllegalArgumentError("Boost factor must be a positive number")
}
whereToken.options.boost = boost
return nil