-
Notifications
You must be signed in to change notification settings - Fork 5
/
RethinkDB.pas
3343 lines (2849 loc) · 111 KB
/
RethinkDB.pas
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
unit RethinkDB;
{$D-}
{$L-}
interface
uses SysUtils, simpleSock, ql2, jsonDoc;
type
TRethinkDB=class;//forward
IRethinkDBTerm=interface;//forward
IRethinkDBDatabase=interface;//forward
IRethinkDBDatum=interface;//forward
IRethinkDBBool=interface;//forward
IRethinkDBArray=interface;//forward
IRethinkDBObject=interface;//forward
IRethinkDBGeometry=interface;//forward
IRethinkDBSequence=interface;//forward
IRethinkDBStream=interface;//forward
IRethinkDBTable=interface;//forward
//////////////////////////////////////////////////////
//// By default no "r" is declared, but if you want,
//// include this in your project:
//
//r=TRethinkDB;
TRethinkDBTerms=array of IRethinkDBTerm;
TRethinkDB=class(TObject)
protected
class function x(const s:WideString):IRethinkDBTerm; overload;
class function x(b:boolean):IRethinkDBTerm; overload;
class function x(v:integer):IRethinkDBTerm; overload;
class function x(d:double):IRethinkDBTerm; overload;
class function x(d:IJSONDocument):IRethinkDBTerm; overload;
class function xt(const p:IRethinkDBTerm;const a:array of IRethinkDBObject):TRethinkDBTerms;
class function xa(const p:IRethinkDBTerm;const a:array of WideString):TRethinkDBTerms;
class function xo(const p:IRethinkDBTerm;const a:array of OleVariant):TRethinkDBTerms;
class function xx(const v:OleVariant):IRethinkDBTerm;
public
class function db(const DBName:WideString;
const Options:IJSONDocument=nil):IRethinkDBDatabase;
class function table(const TableName:WideString;
const Options:IJSONDocument=nil):IRethinkDBTable;
class function dbCreate(const DBName:WideString):IRethinkDBObject;
class function dbDrop(const DBName:WideString):IRethinkDBObject;
class function dbList:IRethinkDBArray;
class function row:IRethinkDBDatum; overload;
class function row(const FieldName:WideString):IRethinkDBDatum; overload;
//TODO: property field[const FieldName:WideString]:IRethinkDBDatum read row; default;
class function fn(const FnBody:IRethinkDBTerm;ArgCount:integer=1):IRethinkDBTerm; overload;
class function fn(const FnBody:IJSONDocument;ArgCount:integer=1):IRethinkDBTerm; overload;
class function arg(const ArgIndex:integer):IRethinkDBDatum; overload;
class function arg(const ArgIndex:integer;const FieldName:WideString):IRethinkDBDatum; overload;
class function map(const sequences:array of IRethinkDBSequence;const fn:IRethinkDBTerm):IRethinkDBStream; overload;
class function map(const arrays:array of IRethinkDBArray;const fn:IRethinkDBTerm):IRethinkDBArray; overload;
class function union(const stream:IRethinkDBStream;
const sequences:array of IRethinkDBSequence):IRethinkDBStream; overload;
class function union(const stream:IRethinkDBStream; const sequences:array of IRethinkDBSequence;
const interleave:OleVariant):IRethinkDBStream; overload;
class function union(const array_:IRethinkDBDatum;
const sequences:array of IRethinkDBSequence):IRethinkDBDatum; overload;
class function union(const array_:IRethinkDBDatum; const sequences:array of IRethinkDBSequence;
const interleave:OleVariant):IRethinkDBDatum; overload;
class function uuid(const Input:WideString=''):IRethinkDBDatum;
class function http(const URL:WideString;const Options:IJSONDocument=nil):IRethinkDBDatum;
class function asc(const FieldName:WideString):IRethinkDBTerm;
class function desc(const FieldName:WideString):IRethinkDBTerm;
class function literal(const Doc:IJSONDocument):IRethinkDBTerm;
class function random:IRethinkDBDatum; overload;
class function random(a,b:integer):IRethinkDBDatum; overload;
class function random(a,b:double):IRethinkDBDatum; overload;
class function group(const sequence:IRethinkDBSequence;
const field:WideString;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
class function group(const sequence:IRethinkDBSequence;
const func:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
class function reduce(const sequence:IRethinkDBSequence;const func:IRethinkDBTerm):IRethinkDBDatum;
class function count(x:IRethinkDBTerm):IRethinkDBDatum;
class function sum(const sequence:IRethinkDBSequence;const field:WideString):IRethinkDBDatum; overload;
class function sum(const sequence:IRethinkDBSequence;const func:IRethinkDBTerm):IRethinkDBDatum; overload;
class function avg(const sequence:IRethinkDBSequence;const field:WideString):IRethinkDBDatum; overload;
class function avg(const sequence:IRethinkDBSequence;const func:IRethinkDBTerm):IRethinkDBDatum; overload;
class function min(const sequence:IRethinkDBSequence;const field:WideString):IRethinkDBDatum; overload;
class function min(const sequence:IRethinkDBSequence;const func:IRethinkDBTerm):IRethinkDBDatum; overload;
class function min_index(const sequence:IRethinkDBSequence;const indexName:WideString):IRethinkDBDatum; overload;
class function max(const sequence:IRethinkDBSequence;const field:WideString):IRethinkDBDatum; overload;
class function max(const sequence:IRethinkDBSequence;const func:IRethinkDBTerm):IRethinkDBDatum; overload;
class function max_index(const sequence:IRethinkDBSequence;const indexName:WideString):IRethinkDBDatum; overload;
class function distinct(const sequence:IRethinkDBSequence):IRethinkDBArray; overload;
class function distinct(const table:IRethinkDBTable;const indexName:WideString=''):IRethinkDBStream; overload;
class function contains(const sequence:IRethinkDBSequence;const v:OleVariant):IRethinkDBBool; overload;
class function contains(const sequence:IRethinkDBSequence;const vv:array of OleVariant):IRethinkDBBool; overload;
class function now:IRethinkDBDatum;
class function time(dYear, dMonth, dDay: word; const timeZone: WideString):IRethinkDBDatum; overload;
class function time(dYear, dMonth, dDay, tHour, tMinute, tSecond: word;
const timeZone: WideString):IRethinkDBDatum; overload;
class function epochTime(e: int64):IRethinkDBDatum;
class function iso8601(const d:WideString;const defaultTimeZone:WideString=''):IRethinkDBDatum;
//TODO: args
//TODO: binary
//TODO: expr (currently xx?)
class function do_(const fn:IRethinkDBTerm):IRethinkDBTerm; overload;
class function do_(const fn:IRethinkDBTerm; const args:array of OleVariant):IRethinkDBTerm; overload;
class function branch(const test:IRethinkDBBool; const trueAction, falseAction:IRethinkDBTerm):IRethinkDBTerm;
class function error(const Msg:WideString): IRethinkDBTerm;
class function info(const x:IRethinkDBTerm): IRethinkDBObject;
class function js(const jsCode:WideString):IRethinkDBDatum; overload;
class function js(const jsCode:WideString;timeoutSeconds:double):IRethinkDBDatum; overload;
class function json_(const jsonCode:WideString):IRethinkDBDatum;
class function range: IRethinkDBStream; overload;
class function range(endValue:integer): IRethinkDBStream; overload;
class function range(startValue,endValue:integer): IRethinkDBStream; overload;
class function point(longitude,latitude:double):IRethinkDBGeometry;
class function line(const points:array of IRethinkDBGeometry):IRethinkDBGeometry;
class function polygon(const points:array of IRethinkDBGeometry):IRethinkDBGeometry;
class function geoJSON(const geoJSON_:IJSONDocument):IRethinkDBGeometry;
class function circle(const point:IRethinkDBGeometry;radius:double;
const Options:IJSONDocument=nil):IRethinkDBGeometry;
class function distance(const geometry1,geometry2:IRethinkDBGeometry;
const Options:IJSONDocument=nil):IRethinkDBDatum;
end;
{$IFDEF DEBUG}
TRethinkDBConnectionListener=procedure(Sender:TObject;InOut:boolean;Token:int64;const Data:UTF8String) of object;
{$ENDIF}
TRethinkDBConnection=class(TObject)
private
FSock:TTcpSocket;
FData:UTF8String;
FDataSize,FDataIndex:cardinal;
FToken:int64;//TODO: async: not here!
{$IFDEF DEBUG}
FListener:TRethinkDBConnectionListener;
{$ENDIF}
function IsConnected:boolean;
procedure Fail(const x:string);
function AuthEx(const d:IJSONDocument):IJSONDocument;
function SendTerm(const t:IRethinkDBTerm):int64;
procedure Build(const s:UTF8String);
function ReadDoc(token:int64;const dd: IJSONDocument): TResponseType;
procedure SendSimple(token:int64;qt:TQueryType);
public
procedure AfterConstruction; override;
destructor Destroy; override;
procedure Connect(const Host,UserName,Password:WideString;
Port:cardinal=28015);
//TODO: reconnect
procedure Close;
//TODO: use
function ServerInfo: IJSONDocument;
property Connected:boolean read IsConnected;
{$IFDEF DEBUG}
property Listener:TRethinkDBConnectionListener read FListener write FListener;
{$ENDIF}
end;
IRethinkDBResultSet=interface(IUnknown)
function Get(const d:IJSONDocument):boolean; stdcall;
function DataReady:boolean; stdcall;
procedure Stop; stdcall;
end;
TRethinkDBBuilder=procedure(const x:UTF8String) of object;
IRethinkDBTerm=interface(IUnknown)
['{914532C9-49C6-4A46-81E5-40DA7EF7D1BD}']
function Execute(Connection:TRethinkDBConnection;
const Options:IJSONDocument=nil):IJSONDocument;
function Run(Connection:TRethinkDBConnection;
const Options:IJSONDocument=nil):IRethinkDBResultSet;
//assert: all IRethinkDBTerm single use! //TODO: replace with central chaining
procedure Chain(Next:IRethinkDBTerm);
function Next:IRethinkDBTerm;
procedure Build(b:TRethinkDBBuilder);
function do_(const fn:IRethinkDBTerm):IRethinkDBTerm; overload;
function do_(const fn:IRethinkDBTerm; const args:array of OleVariant):IRethinkDBTerm; overload;
function typeOf:IRethinkDBDatum;
function info:IRethinkDBObject;
end;
IRethinkDBDatum=interface(IRethinkDBTerm)
['{F956274B-6368-48B4-805F-E93EEF83A856}']
function eq(const v:OleVariant):IRethinkDBBool;
function ne(const v:OleVariant):IRethinkDBBool;
function lt(const v:OleVariant):IRethinkDBBool;
function le(const v:OleVariant):IRethinkDBBool;
function gt(const v:OleVariant):IRethinkDBBool;
function ge(const v:OleVariant):IRethinkDBBool;
function coerceToString:IRethinkDBDatum;
function toJSON:IRethinkDBDatum;
//number
function add_(const v:OleVariant):IRethinkDBDatum;
function sub_(const v:OleVariant):IRethinkDBDatum;
function mul_(const v:OleVariant):IRethinkDBDatum;
function div_(const v:OleVariant):IRethinkDBDatum;
function mod_(const v:OleVariant):IRethinkDBDatum;
function floor:IRethinkDBDatum;
function ceil:IRethinkDBDatum;
function round:IRethinkDBDatum;
//string
function concat(const v:OleVariant):IRethinkDBDatum;
function slice_n1(startOffset:cardinal;leftBoundOpen:boolean=false):IRethinkDBDatum;
function slice_n(startOffset,endOffset:cardinal;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBDatum;
function count: IRethinkDBDatum;
function match(const RegExp:WideString):IRethinkDBObject;
function split(const Separator:WideString):IRethinkDBArray; overload;
function split(const Separator:WideString;MaxSplits:integer):IRethinkDBArray; overload;
function upcase: IRethinkDBDatum;
function downcase: IRethinkDBDatum;
//TODO: coerceToBinary
//array see IRethinkDBArray
//object see IRethinkDBObject
//function ungroup:IRethinkDBArray;//?
//date/time
function inTimezone(const tz:WideString):IRethinkDBDatum;
function timezone:IRethinkDBDatum;
function during(const startTime,endTime:IRethinkDBDatum;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBBool;
function date:IRethinkDBDatum;
function timeOfDay:IRethinkDBDatum;
function year:IRethinkDBDatum;
function month:IRethinkDBDatum;
function day:IRethinkDBDatum;
function dayOfWeek:IRethinkDBDatum;
function dayOfYear:IRethinkDBDatum;
function hours:IRethinkDBDatum;
function minutes:IRethinkDBDatum;
function seconds:IRethinkDBDatum;
function toIso8601:IRethinkDBDatum;
function toEpochTime:IRethinkDBDatum;
function default(const Value:OleVariant):IRethinkDBDatum; overload;
function default(const fn:IRethinkDBTerm):IRethinkDBDatum; overload;
end;
IRethinkDBBool=interface(IRethinkDBDatum)
['{863FDB51-4D60-4369-AC91-6AD20F363C0C}']
function and_(const b:IRethinkDBBool):IRethinkDBBool;
function or_(const b:IRethinkDBBool):IRethinkDBBool;
function not_:IRethinkDBBool;
function branch(const trueAction, falseAction:IRethinkDBTerm):IRethinkDBTerm;
end;
IRethinkDBArray=interface(IRethinkDBTerm)//*:
//actually both IRethinkDBDatum and IRethinkDBSequence,
//but interfaces don't do multiple inheritance
//so reverted to first common parent IRethinkDBTerm
//(single TRethinkDBDatum does implementation anyway)
['{18DF4808-F0DE-40BB-86A5-C967FE43CB9A}']
function filter_a(const KeyValue:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBArray; overload;
function filter_a(const Predicate:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBArray; overload;
function innerJoin(const otherArray,predicate:IRethinkDBTerm):IRethinkDBArray;
function outerJoin(const otherArray,predicate:IRethinkDBTerm):IRethinkDBArray;
function append(const v:OleVariant):IRethinkDBDatum;
function prepend(const v:OleVariant):IRethinkDBDatum;
function difference(const v:OleVariant):IRethinkDBDatum;
function zip:IRethinkDBArray;
function map(const fn:IRethinkDBTerm):IRethinkDBArray; overload;
function map(const arrays:array of IRethinkDBArray;const fn:IRethinkDBTerm):IRethinkDBArray; overload;
function withFields(const selectors:array of OleVariant):IRethinkDBArray;
function concatMap(const fn:IRethinkDBTerm):IRethinkDBArray; overload;
function skip(n:integer):IRethinkDBArray;
function limit(n:integer):IRethinkDBArray;
function slice_a1(startOffset:cardinal;leftBoundOpen:boolean=false):IRethinkDBArray;
function slice_a(startOffset,endOffset:cardinal;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBArray;
function union_a1(const sequence:IRethinkDBSequence):IRethinkDBArray;
function union_a2(const sequence:IRethinkDBSequence;const interleave:OleVariant):IRethinkDBArray;
function union_a(const sequences:array of IRethinkDBSequence):IRethinkDBArray;
function union_a3(const sequences:array of IRethinkDBSequence;const interleave:OleVariant):IRethinkDBArray;
function sample_a(n:integer):IRethinkDBArray;
function without_a(const PathSpec:array of OleVariant):IRethinkDBArray;
function merge_a(const x:array of IRethinkDBObject):IRethinkDBArray;
function count:IRethinkDBDatum;
function coerceToObject:IRethinkDBObject;
function setInsert(const Value:OleVariant):IRethinkDBArray;
function setIntersection(const x:IRethinkDBArray):IRethinkDBArray;
function setUnion(const x:IRethinkDBArray):IRethinkDBArray;
function setDifference(const x:IRethinkDBArray):IRethinkDBArray;
function field_a(Idx:integer):IRethinkDBDatum;
function insertAt(Offset:integer;const Value:OleVariant):IRethinkDBArray;
function spliceAt(Offset:integer;const Values:array of OleVariant):IRethinkDBArray;
function deleteAt(Offset:integer):IRethinkDBArray; overload;
function deleteAt(StartOffset,EndOffset:integer):IRethinkDBArray; overload;
function changeAt(Offset:integer;const Value:OleVariant):IRethinkDBArray;
property field[Idx:integer]:IRethinkDBDatum read field_a; default;
end;
IRethinkDBObject=interface(IRethinkDBDatum)
['{025863D6-4BF0-4FB8-8E4D-F6A34FF022D9}']
function field_o(const FieldName:WideString):IRethinkDBDatum;
function count:IRethinkDBDatum;
function keys:IRethinkDBArray;
function values:IRethinkDBArray;
function hasFields_o(const PathSpec:array of OleVariant):IRethinkDBBool;
function pluck_o(const PathSpec:array of OleVariant):IRethinkDBObject;
function without_o(const PathSpec:array of OleVariant):IRethinkDBObject;
function merge_o(const x:array of IRethinkDBObject):IRethinkDBObject;
function getField_o(const FieldName:WideString):IRethinkDBDatum;
function coerceToArray:IRethinkDBArray;
property field[const FieldName:WideString]:IRethinkDBDatum read field_o; default;
end;
IRethinkDBGeometry=interface(IRethinkDBDatum)
['{C12B37A1-246A-42AF-B0C5-BCB0B77B8A09}']
function toGeoJSON:IRethinkDBObject;
function polygonSub(polygon2:IRethinkDBGeometry):IRethinkDBGeometry;
function distance(geometry:IRethinkDBGeometry;
const Options:IJSONDocument=nil):IRethinkDBDatum;
function fill:IRethinkDBGeometry;
function includes(const geometry:IRethinkDBGeometry):IRethinkDBBool;
function intersects(const geometry:IRethinkDBGeometry):IRethinkDBBool;
end;
IRethinkDBDatabase=interface(IRethinkDBTerm)
['{D3D670F6-C882-4935-9E10-7714E4E903FB}']
function table(const TableName:WideString;
const Options:IJSONDocument=nil):IRethinkDBTable;
function tableCreate(const TableName:WideString;
const Options:IJSONDocument=nil):IRethinkDBObject;
function tableDrop(const TableName:WideString):IRethinkDBObject;
function tableList:IRethinkDBArray;
function wait(const Options:IJSONDocument=nil):IRethinkDBObject;
end;
IRethinkDBSelection=interface;//forward
IRethinkDBSequence=interface(IRethinkDBTerm)
['{A1CD9948-CE84-45DF-8EFA-3EA5DCF5D172}']
function innerJoin(const otherSequence,predicate:IRethinkDBTerm):IRethinkDBStream;
function outerJoin(const otherSequence,predicate:IRethinkDBTerm):IRethinkDBStream;
function eqJoin(const leftFieldOrFunction,rightTable:IRethinkDBTerm;
const Options:IJSONDocument=nil):IRethinkDBSequence;
function map(const fn:IRethinkDBTerm):IRethinkDBStream; overload;
function map(const sequences:array of IRethinkDBSequence;const fn:IRethinkDBTerm):IRethinkDBStream; overload;
//NOTICE: I've tried overload, but interface aliases can't handle overloads
function orderBy_s1(const v:OleVariant):IRethinkDBArray;
function orderBy_s(const vv:array of OleVariant):IRethinkDBArray;
function skip(n:integer):IRethinkDBStream;
function limit(n:integer):IRethinkDBStream;
function nth_s(n:integer):IRethinkDBObject;
function offsetsOf(DatumOrPredicate:OleVariant):IRethinkDBArray;
function isEmpty:IRethinkDBBool;
function sample_s(n:integer):IRethinkDBSelection;
function group(const field:WideString;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
function group(const func:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
function reduce(const func:IRethinkDBTerm):IRethinkDBDatum;
function fold(const base,func:IRethinkDBTerm):IRethinkDBDatum; overload;
function fold(const base,func,emit,finalEmit:IRethinkDBTerm):IRethinkDBSequence; overload;
function sum(const field:WideString):IRethinkDBDatum; overload;
function sum(const func:IRethinkDBTerm):IRethinkDBDatum; overload;
function avg(const field:WideString):IRethinkDBDatum; overload;
function avg(const func:IRethinkDBTerm):IRethinkDBDatum; overload;
function min(const field:WideString):IRethinkDBDatum; overload;
function min(const func:IRethinkDBTerm):IRethinkDBDatum; overload;
function min_index(const indexName:WideString):IRethinkDBDatum; overload;
function max(const field:WideString):IRethinkDBDatum; overload;
function max(const func:IRethinkDBTerm):IRethinkDBDatum; overload;
function max_index(const indexName:WideString):IRethinkDBDatum; overload;
function distinct_s:IRethinkDBArray;
function hasFields_s(const PathSpec:array of OleVariant):IRethinkDBBool;
function pluck_s(const PathSpec:array of OleVariant):IRethinkDBStream;
function without_s(const PathSpec:array of OleVariant):IRethinkDBStream;
function merge_s(const x:array of IRethinkDBObject):IRethinkDBStream;
function field_s(const FieldName:WideString):IRethinkDBSequence;
function getField_s(const FieldName:WideString):IRethinkDBSequence;
function forEach(const fn:IRethinkDBTerm):IRethinkDBTerm;
function default(const Value:OleVariant):IRethinkDBDatum; overload;
function default(const fn:IRethinkDBTerm):IRethinkDBDatum; overload;
function coerceToArray:IRethinkDBArray;
function coerceToObject:IRethinkDBObject;
function includes(const geometry:IRethinkDBGeometry):IRethinkDBSequence;
function intersects(const geometry:IRethinkDBGeometry):IRethinkDBSequence;
property field[const FieldName:WideString]:IRethinkDBSequence read field_s; default;
end;
IRethinkDBStream=interface(IRethinkDBSequence)
['{542AEDF9-A9DE-42DA-81DA-B85E87903518}']
function changes(const Options:IJSONDocument=nil):IRethinkDBStream;
function filter_s(const KeyValue:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
function filter_s(const Predicate:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
function zip:IRethinkDBStream;
function concatMap(const fn:IRethinkDBTerm):IRethinkDBStream;
function slice_z1(startOffset:cardinal;leftBoundOpen:boolean=false):IRethinkDBStream;
function slice_z(startOffset,endOffset:cardinal;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBStream;
function union_z1(const sequence:IRethinkDBSequence):IRethinkDBStream;
function union_z2(const sequence:IRethinkDBSequence;const interleave:OleVariant):IRethinkDBStream;
function union_z(const sequences:array of IRethinkDBSequence):IRethinkDBStream;
function union_z3(const sequences:array of IRethinkDBSequence;const interleave:OleVariant):IRethinkDBStream;
function sample_z(n:integer):IRethinkDBArray;
function ungroup:IRethinkDBDatum;
end;
IRethinkDBSelection=interface(IRethinkDBStream)
['{DED262BA-4C88-46CD-86C4-83F1E9E8AEAA}']
function update(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function update(const fn:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function replace(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function replace(const fn:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function delete(const Options:IJSONDocument=nil):IRethinkDBObject;
function sync:IRethinkDBObject;
function filter_x(const KeyValue:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBSelection; overload;
function filter_x(const Predicate:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBSelection; overload;
function orderBy_x1(const v:OleVariant):IRethinkDBSelection{<IRethinkDBArray>};
function orderBy_x(const vv:array of OleVariant):IRethinkDBSelection{<IRethinkDBArray>};
function slice_s1(startOffset:cardinal;leftBoundOpen:boolean=false):IRethinkDBSelection;
function slice_s(startOffset,endOffset:cardinal;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBSelection;
function nth_x(n:integer):IRethinkDBSelection{<IRethinkDBObject>};
end;
IRethinkDBSingleSelection=interface(IRethinkDBObject)
['{7D47E895-4DCC-4B5C-A956-3BCD777E3719}']
function update(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function update(const fn:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function replace(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function replace(const fn:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function delete(const Options:IJSONDocument=nil):IRethinkDBObject;
end;
IRethinkDBTableSlice=interface(IRethinkDBSelection)
['{E7A1448D-3C62-42CB-A4A8-8DB333A31BC5}']
function between(const LowerKey,UpperKey:WideString;const Options:IJSONDocument=nil):IRethinkDBTableSlice;
end;
IRethinkDBTable=interface(IRethinkDBSelection)
['{6A872B89-99D4-4102-8CAE-1F5D1831F15D}']
function indexCreate(const IndexName:WideString;const IndexFunction:IRethinkDBTerm=nil;
const Options:IJSONDocument=nil):IRethinkDBObject;
function indexDrop(const IndexName:WideString):IRethinkDBObject;
function indexList:IRethinkDBArray;
function indexRename(const OldName,NewName:WideString):IRethinkDBObject;
function indexStatus:IRethinkDBArray; overload;
function indexStatus(const IndexNames:array of WideString):IRethinkDBTerm; overload;
function indexWait:IRethinkDBArray; overload;
function indexWait(const IndexNames:array of WideString):IRethinkDBTerm; overload;
function insert(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function insert(const docs:array of IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function get(const Key:WideString):IRethinkDBSingleSelection;
function getAll(const Keys:array of WideString;const Options:IJSONDocument=nil):IRethinkDBSelection;
function between(const LowerKey,UpperKey:WideString;const Options:IJSONDocument=nil):IRethinkDBTableSlice;
function orderBy_t1(const v:OleVariant):IRethinkDBTableSlice;
function orderBy_t(const vv:array of OleVariant):IRethinkDBTableSlice;
function distinct_t(const indexName:WideString=''):IRethinkDBStream;
function getIntersecting(const geometry:IRethinkDBGeometry;const indexName:WideString):IRethinkDBSelection;
function getNearest(const geometry:IRethinkDBGeometry;const Options:IJSONDocument):IRethinkDBArray;
function status:IRethinkDBSingleSelection;
function wait(const Options:IJSONDocument=nil):IRethinkDBObject;
end;
//TODO: IRethinkDBBinary
//slice,count,coerceToString
/////////////
// implementations
TRethinkDBImplBaseObj=TJSONImplBaseObj;
TRethinkDBTerm=class(TRethinkDBImplBaseObj,IRethinkDBTerm)//abstract
private
FNext:IRethinkDBTerm;
protected
function Execute(Connection:TRethinkDBConnection;
const Options:IJSONDocument=nil):IJSONDocument;
function Run(Connection:TRethinkDBConnection;
const Options:IJSONDocument=nil):IRethinkDBResultSet;
procedure Chain(Next:IRethinkDBTerm);
function Next:IRethinkDBTerm;
procedure Build(b:TRethinkDBBuilder); virtual; abstract;
function do_(const fn:IRethinkDBTerm):IRethinkDBTerm; overload;
function do_(const fn:IRethinkDBTerm; const args:array of OleVariant):IRethinkDBTerm; overload;
function typeOf:IRethinkDBDatum;
function info:IRethinkDBObject;
public
constructor Create;
end;
TRethinkDBValueClass=class of TRethinkDBValue;
TRethinkDBValue=class(TRethinkDBTerm,IRethinkDBTerm)
private
FTermType:TTermType;
FFirstArg:IRethinkDBTerm;
FOptions:IJSONDocument;
constructor Create; overload;//hide
function PrepOrderBy(rt:TRethinkDBValueClass;const vv:array of OleVariant):IRethinkDBTerm;
protected
procedure Build(b:TRethinkDBBuilder); override;
public
constructor Create(tt:TTermType;const arg:IRethinkDBTerm;
const opt:IJSONDocument=nil); overload;
constructor Create(tt:TTermType;const args:array of IRethinkDBTerm;
const opt:IJSONDocument=nil); overload;
end;
TRethinkDBConstant=class(TRethinkDBTerm,IRethinkDBTerm)
private
FData:UTF8String;
protected
procedure Build(b:TRethinkDBBuilder); override;
public
constructor Create(const Literal:UTF8String);
end;
//https://rethinkdb.com/api/javascript/merge/
TRethinkDBDatum=class(TRethinkDBValue,IRethinkDBDatum,IRethinkDBArray,
IRethinkDBObject,IRethinkDBGeometry,IRethinkDBSingleSelection)
//IRethinkDBDatum
function eq(const v:OleVariant):IRethinkDBBool;
function ne(const v:OleVariant):IRethinkDBBool;
function lt(const v:OleVariant):IRethinkDBBool;
function le(const v:OleVariant):IRethinkDBBool;
function gt(const v:OleVariant):IRethinkDBBool;
function ge(const v:OleVariant):IRethinkDBBool;
function coerceToString:IRethinkDBDatum;
function toJSON:IRethinkDBDatum;
function add_(const v:OleVariant):IRethinkDBDatum;
function sub_(const v:OleVariant):IRethinkDBDatum;
function mul_(const v:OleVariant):IRethinkDBDatum;
function div_(const v:OleVariant):IRethinkDBDatum;
function mod_(const v:OleVariant):IRethinkDBDatum;
function floor:IRethinkDBDatum;
function ceil:IRethinkDBDatum;
function round:IRethinkDBDatum;
function concat(const v:OleVariant):IRethinkDBDatum;
function slice_n1(startOffset:cardinal;leftBoundOpen:boolean=false):IRethinkDBDatum;
function slice_n(startOffset,endOffset:cardinal;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBDatum;
function count: IRethinkDBDatum;
function match(const RegExp:WideString):IRethinkDBObject;
function split(const Separator:WideString):IRethinkDBArray; overload;
function split(const Separator:WideString;MaxSplits:integer):IRethinkDBArray; overload;
function upcase: IRethinkDBDatum;
function downcase: IRethinkDBDatum;
function inTimezone(const tz:WideString):IRethinkDBDatum;
function timezone: IRethinkDBDatum;
function during(const startTime,endTime:IRethinkDBDatum;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBBool;
function date:IRethinkDBDatum;
function timeOfDay:IRethinkDBDatum;
function year:IRethinkDBDatum;
function month:IRethinkDBDatum;
function day:IRethinkDBDatum;
function dayOfWeek:IRethinkDBDatum;
function dayOfYear:IRethinkDBDatum;
function hours:IRethinkDBDatum;
function minutes:IRethinkDBDatum;
function seconds:IRethinkDBDatum;
function toIso8601:IRethinkDBDatum;
function toEpochTime:IRethinkDBDatum;
function default(const Value:OleVariant):IRethinkDBDatum; overload;
function default(const fn:IRethinkDBTerm):IRethinkDBDatum; overload;
//IRethinkDBArray
function filter_a(const KeyValue:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBArray; overload;
function filter_a(const Predicate:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBArray; overload;
function innerJoin(const otherArray,predicate:IRethinkDBTerm):IRethinkDBArray;
function outerJoin(const otherArray,predicate:IRethinkDBTerm):IRethinkDBArray;
function coerceToObject:IRethinkDBObject;
function append(const v:OleVariant):IRethinkDBDatum;
function prepend(const v:OleVariant):IRethinkDBDatum;
function difference(const v:OleVariant):IRethinkDBDatum;
function zip:IRethinkDBArray;
function map(const fn:IRethinkDBTerm):IRethinkDBArray; overload;
function map(const arrays:array of IRethinkDBArray;const fn:IRethinkDBTerm):IRethinkDBArray; overload;
function withFields(const selectors:array of OleVariant):IRethinkDBArray;
function concatMap(const fn:IRethinkDBTerm):IRethinkDBArray; overload;
function skip(n:integer):IRethinkDBArray;
function limit(n:integer):IRethinkDBArray;
function slice_a1(startOffset:cardinal;leftBoundOpen:boolean=false):IRethinkDBArray;
function slice_a(startOffset,endOffset:cardinal;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBArray;
function union_a1(const sequence:IRethinkDBSequence):IRethinkDBArray;
function union_a2(const sequence:IRethinkDBSequence;const interleave:OleVariant):IRethinkDBArray;
function union_a(const sequences:array of IRethinkDBSequence):IRethinkDBArray;
function union_a3(const sequences:array of IRethinkDBSequence;const interleave:OleVariant):IRethinkDBArray;
function sample_a(n:integer):IRethinkDBArray;
function without_a(const PathSpec:array of OleVariant):IRethinkDBArray;
function merge_a(const x:array of IRethinkDBObject):IRethinkDBArray;
function setInsert(const Value:OleVariant):IRethinkDBArray;
function setIntersection(const x:IRethinkDBArray):IRethinkDBArray;
function setUnion(const x:IRethinkDBArray):IRethinkDBArray;
function setDifference(const x:IRethinkDBArray):IRethinkDBArray;
function field_a(Idx:integer):IRethinkDBDatum;
function insertAt(Offset:integer;const Value:OleVariant):IRethinkDBArray;
function spliceAt(Offset:integer;const Values:array of OleVariant):IRethinkDBArray;
function deleteAt(Offset:integer):IRethinkDBArray; overload;
function deleteAt(StartOffset,EndOffset:integer):IRethinkDBArray; overload;
function changeAt(Offset:integer;const Value:OleVariant):IRethinkDBArray;
//IRethinkDBObject
function keys:IRethinkDBArray;
function values:IRethinkDBArray;
function field_o(const FieldName:WideString):IRethinkDBDatum;
function hasFields_o(const PathSpec:array of OleVariant):IRethinkDBBool;
function pluck_o(const PathSpec:array of OleVariant):IRethinkDBObject;
function without_o(const PathSpec:array of OleVariant):IRethinkDBObject;
function merge_o(const x:array of IRethinkDBObject):IRethinkDBObject;
function getField_o(const FieldName:WideString):IRethinkDBDatum;
function coerceToArray:IRethinkDBArray;
//IRethinkDBGeometry
function toGeoJSON:IRethinkDBObject;
function polygonSub(polygon2:IRethinkDBGeometry):IRethinkDBGeometry;
function distance(geometry:IRethinkDBGeometry;
const Options:IJSONDocument=nil):IRethinkDBDatum;
function fill:IRethinkDBGeometry;
function includes(const geometry:IRethinkDBGeometry):IRethinkDBBool;
function intersects(const geometry:IRethinkDBGeometry):IRethinkDBBool;
//IRethinkDBSingleSelection
function update(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function update(const fn:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function replace(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function replace(const fn:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function delete(const Options:IJSONDocument=nil):IRethinkDBObject;
end;
TRethinkDBBool=class(TRethinkDBDatum,IRethinkDBBool)
function and_(const b:IRethinkDBBool):IRethinkDBBool;
function or_(const b:IRethinkDBBool):IRethinkDBBool;
function not_:IRethinkDBBool;
function branch(const trueAction, falseAction:IRethinkDBTerm):IRethinkDBTerm;
end;
TRethinkDBDatabase=class(TRethinkDBValue,IRethinkDBDatabase)
protected
function table(const TableName:WideString;
const Options:IJSONDocument=nil):IRethinkDBTable;
function tableCreate(const TableName:WideString;
const Options:IJSONDocument=nil):IRethinkDBObject;
function tableDrop(const TableName:WideString):IRethinkDBObject;
function tableList:IRethinkDBArray;
function wait(const Options:IJSONDocument=nil):IRethinkDBObject;
end;
TRethinkDBSet=class(TRethinkDBValue,IRethinkDBSequence,IRethinkDBStream,
IRethinkDBSelection,IRethinkDBTable,IRethinkDBTableSlice)
//IRethinkDBSequence
function innerJoin(const otherSequence,predicate:IRethinkDBTerm):IRethinkDBStream;
function outerJoin(const otherSequence,predicate:IRethinkDBTerm):IRethinkDBStream;
function eqJoin(const leftFieldOrFunction,rightTable:IRethinkDBTerm;
const Options:IJSONDocument=nil):IRethinkDBSequence;
function map(const fn:IRethinkDBTerm):IRethinkDBStream; overload;
function map(const sequences:array of IRethinkDBSequence;const fn:IRethinkDBTerm):IRethinkDBStream; overload;
function withFields(const selectors:array of OleVariant):IRethinkDBStream;
function orderBy_s1(const v:OleVariant):IRethinkDBArray;
function orderBy_s(const vv:array of OleVariant):IRethinkDBArray;
function skip(n:integer):IRethinkDBStream;
function limit(n:integer):IRethinkDBStream;
function nth_s(n:integer):IRethinkDBObject;
function offsetsOf(DatumOrPredicate:OleVariant):IRethinkDBArray;
function isEmpty:IRethinkDBBool;
function sample_s(n:integer):IRethinkDBSelection;
function group(const field:WideString;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
function group(const func:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
function reduce(const func:IRethinkDBTerm):IRethinkDBDatum;
function fold(const base,func:IRethinkDBTerm):IRethinkDBDatum; overload;
function fold(const base,func,emit,finalEmit:IRethinkDBTerm):IRethinkDBSequence; overload;
function sum(const field:WideString):IRethinkDBDatum; overload;
function sum(const func:IRethinkDBTerm):IRethinkDBDatum; overload;
function avg(const field:WideString):IRethinkDBDatum; overload;
function avg(const func:IRethinkDBTerm):IRethinkDBDatum; overload;
function min(const field:WideString):IRethinkDBDatum; overload;
function min(const func:IRethinkDBTerm):IRethinkDBDatum; overload;
function min_index(const indexName:WideString):IRethinkDBDatum; overload;
function max(const field:WideString):IRethinkDBDatum; overload;
function max(const func:IRethinkDBTerm):IRethinkDBDatum; overload;
function max_index(const indexName:WideString):IRethinkDBDatum; overload;
function distinct_s:IRethinkDBArray;
function hasFields_s(const PathSpec:array of OleVariant):IRethinkDBBool;
function pluck_s(const PathSpec:array of OleVariant):IRethinkDBStream;
function without_s(const PathSpec:array of OleVariant):IRethinkDBStream;
function merge_s(const x:array of IRethinkDBObject):IRethinkDBStream;
function field_s(const FieldName:WideString):IRethinkDBSequence;
function getField_s(const FieldName:WideString):IRethinkDBSequence;
function forEach(const fn:IRethinkDBTerm):IRethinkDBTerm;
function default(const Value:OleVariant):IRethinkDBDatum; overload;
function default(const fn:IRethinkDBTerm):IRethinkDBDatum; overload;
function coerceToArray:IRethinkDBArray;
function coerceToObject:IRethinkDBObject;
function includes(const geometry:IRethinkDBGeometry):IRethinkDBSequence;
function intersects(const geometry:IRethinkDBGeometry):IRethinkDBSequence;
//IRethinkDBStream
function changes(const Options:IJSONDocument=nil):IRethinkDBStream;
function filter_s(const KeyValue:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
function filter_s(const Predicate:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBStream; overload;
function zip:IRethinkDBStream;
function concatMap(const fn:IRethinkDBTerm):IRethinkDBStream;
function slice_z1(startOffset:cardinal;leftBoundOpen:boolean=false):IRethinkDBStream;
function slice_z(startOffset,endOffset:cardinal;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBStream;
function union_z1(const sequence:IRethinkDBSequence):IRethinkDBStream;
function union_z2(const sequence:IRethinkDBSequence;const interleave:OleVariant):IRethinkDBStream;
function union_z(const sequences:array of IRethinkDBSequence):IRethinkDBStream;
function union_z3(const sequences:array of IRethinkDBSequence;const interleave:OleVariant):IRethinkDBStream;
function sample_z(n:integer):IRethinkDBArray;
function ungroup:IRethinkDBDatum;
//IRethinkDBSelection
function update(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function update(const fn:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function replace(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function replace(const fn:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function delete(const Options:IJSONDocument=nil):IRethinkDBObject;
function sync:IRethinkDBObject;
function filter_x(const KeyValue:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBSelection; overload;
function filter_x(const Predicate:IRethinkDBTerm;const Options:IJSONDocument=nil):IRethinkDBSelection; overload;
function orderBy_x1(const v:OleVariant):IRethinkDBSelection{<IRethinkDBArray>};
function orderBy_x(const vv:array of OleVariant):IRethinkDBSelection{<IRethinkDBArray>};
function slice_s1(startOffset:cardinal;leftBoundOpen:boolean=false):IRethinkDBSelection;
function slice_s(startOffset,endOffset:cardinal;leftBoundOpen:boolean=false;
rightBoundOpen:boolean=true):IRethinkDBSelection;
function nth_x(n:integer):IRethinkDBSelection{<IRethinkDBObject>};
//IRethinkDBTable
function indexCreate(const IndexName:WideString;const IndexFunction:IRethinkDBTerm=nil;
const Options:IJSONDocument=nil):IRethinkDBObject;
function indexDrop(const IndexName:WideString):IRethinkDBObject;
function indexList:IRethinkDBArray;
function indexRename(const OldName,NewName:WideString):IRethinkDBObject;
function indexStatus:IRethinkDBArray; overload;
function indexStatus(const IndexNames:array of WideString):IRethinkDBTerm; overload;
function indexWait:IRethinkDBArray; overload;
function indexWait(const IndexNames:array of WideString):IRethinkDBTerm; overload;
function insert(const doc:IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function insert(const docs:array of IJSONDocument;const Options:IJSONDocument=nil):IRethinkDBObject; overload;
function get(const Key:WideString):IRethinkDBSingleSelection;
function getAll(const Keys:array of WideString;const Options:IJSONDocument=nil):IRethinkDBSelection;
function between(const LowerKey,UpperKey:WideString;const Options:IJSONDocument=nil):IRethinkDBTableSlice;
function orderBy_t1(const v:OleVariant):IRethinkDBTableSlice;
function orderBy_t(const vv:array of OleVariant):IRethinkDBTableSlice;
function distinct_t(const indexName:WideString=''):IRethinkDBStream;
function getIntersecting(const geometry:IRethinkDBGeometry;const indexName:WideString):IRethinkDBSelection;
function getNearest(const geometry:IRethinkDBGeometry;const Options:IJSONDocument):IRethinkDBArray;
function status:IRethinkDBSingleSelection;
function wait(const Options:IJSONDocument=nil):IRethinkDBObject;
end;
TRethinkDBResultSet=class(TRethinkDBImplBaseObj,IRethinkDBResultSet)
private
FConnection:TRethinkDBConnection;
FToken:int64;
FLastRes:TResponseType;
FData:IJSONDocument;
FSet:IJSONDocArray;
FSetIndex:integer;
procedure CheckData;
protected
constructor Create(rdb:TRethinkDBConnection;token:int64);
public
function Get(const d:IJSONDocument):boolean; stdcall;
function DataReady:boolean; stdcall;
procedure Stop; stdcall;
destructor Destroy; override;
end;
ERethinkDBError=class(Exception);
ERethinkDBErrorCode=class(ERethinkDBError)
private
FCode:integer;
public
constructor Create(const Msg:string;Code:integer);
constructor CreateFromDoc(const d:IJSONDocument);
property Code:integer read FCode;
end;
ERethinkDBClientError=class(ERethinkDBErrorCode);
ERethinkDBCompileError=class(ERethinkDBErrorCode);
ERethinkDBRuntimeError=class(ERethinkDBErrorCode);
ERethinkDBGetNoDataReady=class(ERethinkDBError);
{$IF not Declared(UTF8ToWideString)}
{$DEFINE NOT_DECLARED_UTF8ToWideString}
{$IFEND}
{$IFDEF NOT_DECLARED_UTF8ToWideString}
function UTF8ToWideString(const s: UTF8String): WideString;
{$ENDIF}
implementation
uses Variants, RethinkDBAuth;
{$IFDEF NOT_DECLARED_UTF8ToWideString}
function UTF8ToWideString(const s: UTF8String): WideString;
begin
Result:=UTF8Decode(s);
end;
{$ENDIF}
function StringEscape(const s:UTF8String): UTF8String;
var
s1,s2:UTF8String;
i1,i2,l1,l2:integer;
begin
//Result:='"'+StringReplace(s,'"','\"',[rfReplaceAll])+'"';
s1:=UTF8Encode(s);
l1:=Length(s1);
l2:=l1+2;
i1:=0;
i2:=1;
SetLength(s2,l2);
s2[1]:='"';
while i1<l1 do
begin
inc(i1);
if s1[i1]='"' then //more?
begin
if i2=l2 then
begin
inc(l2,$100);
SetLength(s2,l2);
end;
inc(i2);
s2[i2]:='\';
end;
if i2=l2 then
begin
inc(l2,$100);
SetLength(s2,l2);
end;
inc(i2);
s2[i2]:=s1[i1];
end;
if i2=l2 then
begin
inc(l2,$100);
SetLength(s2,l2);
end;
inc(i2);
s2[i2]:='"';
SetLength(s2,i2);
Result:=s2;
end;
function FloatToStrX(d:double):string;
var
ods:char;
begin
{$if CompilerVersion >= 24}
ods:=FormatSettings.DecimalSeparator;
{$else}
ods:=DecimalSeparator;
{$ifend}
try
{$if CompilerVersion >= 24}
FormatSettings.DecimalSeparator:='.';
{$else}
DecimalSeparator:='.';
{$ifend}
Result:=FloatToStr(d);
finally
{$if CompilerVersion >= 24}
FormatSettings.DecimalSeparator:=ods;
{$else}
DecimalSeparator:=ods;
{$ifend}
end;
end;
type
TRethinkDBFuncBody=class(TRethinkDBTerm,IRethinkDBTerm)
private
FData:IJSONDocument;
protected
procedure Build(b:TRethinkDBBuilder); override;
public
constructor Create(const Data:IJSONDocument);
end;
{ TRethinkDB }
class function TRethinkDB.x(const s:WideString): IRethinkDBTerm;
begin
Result:=TRethinkDBConstant.Create(StringEscape(s));
end;
class function TRethinkDB.x(b: boolean): IRethinkDBTerm;
begin
if b then
Result:=TRethinkDBConstant.Create('true')
else
Result:=TRethinkDBConstant.Create('false');
end;
class function TRethinkDB.x(v: integer): IRethinkDBTerm;
begin
Result:=TRethinkDBConstant.Create(IntToStr(v));
end;
class function TRethinkDB.x(d:double):IRethinkDBTerm;
begin
Result:=TRethinkDBConstant.Create(FloatToStrX(d));
end;
class function TRethinkDB.xx(const v: OleVariant): IRethinkDBTerm;
var
vt:TVarType;
i,j,k:integer;
a:array of IRethinkDBTerm;
d:IJSONDocument;
begin
vt:=VarType(v);
if (vt and varArray)=0 then
case VarType(v) of
varEmpty,varNull:
Result:=TRethinkDBConstant.Create('null');
varSmallint,varInteger,
varShortInt,varByte,varWord,varLongWord,varInt64,
$0015://varWord64
Result:=TRethinkDBConstant.Create(UTF8Encode(VarToWideStr(v)));
varSingle,varDouble,varCurrency,
$000E://varDecimal
Result:=TRethinkDBConstant.Create(UTF8Encode(FloatToStrX(double(v))));
//varDate://TODO
varOleStr:
Result:=x(VarToWideStr(v));
varDispatch,varUnknown:
if IUnknown(v).QueryInterface(IJSONDocument,d)=S_OK then
Result:=TRethinkDBValue.Create(TermType_MAKE_OBJ,nil,d)
else
raise ERethinkDBError.Create('Unsupported variant interface');
//varError:?
varBoolean: