forked from hamiltop/rethinkdb-elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.ex
1819 lines (1480 loc) · 63.1 KB
/
query.ex
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
defmodule RethinkDB.Query do
@moduledoc """
Querying API for RethinkDB
"""
alias RethinkDB.Q
require RethinkDB.Query.Macros
import RethinkDB.Query.Macros
@type t :: %Q{}
@type reql_string :: String.t|t
@type reql_number :: integer|float|t
@type reql_array :: [term]|t
@type reql_bool :: boolean|t
@type reql_obj :: %{}|t
@type reql_datum :: term
@type reql_func0 :: (() -> term)|t
@type reql_func1 :: (term -> term)|t
@type reql_func2 :: (term, term -> term)|t
@type reql_opts :: %{}
@type reql_binary :: %RethinkDB.Pseudotypes.Binary{}|binary|t
@type reql_geo_point :: %RethinkDB.Pseudotypes.Geometry.Point{}|{reql_number,reql_number}|t
@type reql_geo_line :: %RethinkDB.Pseudotypes.Geometry.Line{}|t
@type reql_geo_polygon :: %RethinkDB.Pseudotypes.Geometry.Polygon{}|t
@type reql_geo :: reql_geo_point|reql_geo_line|reql_geo_polygon
@type reql_time :: %RethinkDB.Pseudotypes.Time{}|t
#
#Aggregation Functions
#
@doc """
Takes a stream and partitions it into multiple groups based on the fields or
functions provided.
With the multi flag single documents can be assigned to multiple groups,
similar to the behavior of multi-indexes. When multi is True and the grouping
value is an array, documents will be placed in each group that corresponds to
the elements of the array. If the array is empty the row will be ignored.
"""
@spec group(Q.reql_array, Q.reql_func1 | Q.reql_string | [Q.reql_func1 | Q.reql_string] ) :: Q.t
def group(seq, keys) when is_list(keys), do: %Q{query: [144, [wrap(seq) | Enum.map(keys, &wrap/1)]]}
def group(seq, key), do: group(seq, [key])
@doc """
Takes a grouped stream or grouped data and turns it into an array of objects
representing the groups. Any commands chained after ungroup will operate on
this array, rather than operating on each group individually. This is useful if
you want to e.g. order the groups by the value of their reduction.
The format of the array returned by ungroup is the same as the default native
format of grouped data in the JavaScript driver and data explorer.
end
"""
@spec ungroup(Q.t) :: Q.t
operate_on_single_arg(:ungroup, 150)
@doc """
Produce a single value from a sequence through repeated application of a
reduction function.
The reduction function can be called on:
* two elements of the sequence
* one element of the sequence and one result of a previous reduction
* two results of previous reductions
The reduction function can be called on the results of two previous
reductions because the reduce command is distributed and parallelized across
shards and CPU cores. A common mistaken when using the reduce command is to
suppose that the reduction is executed from left to right.
"""
@spec reduce(Q.reql_array, Q.reql_func2) :: Q.t
operate_on_two_args(:reduce, 37)
@doc """
Counts the number of elements in a sequence. If called with a value, counts
the number of times that value occurs in the sequence. If called with a
predicate function, counts the number of elements in the sequence where that
function returns `true`.
If count is called on a binary object, it will return the size of the object
in bytes.
"""
@spec count(Q.reql_array) :: Q.t
operate_on_single_arg(:count, 43)
@spec count(Q.reql_array, Q.reql_string | Q.reql_func1) :: Q.t
operate_on_two_args(:count, 43)
@doc """
Sums all the elements of a sequence. If called with a field name, sums all
the values of that field in the sequence, skipping elements of the sequence
that lack that field. If called with a function, calls that function on every
element of the sequence and sums the results, skipping elements of the sequence
where that function returns `nil` or a non-existence error.
Returns 0 when called on an empty sequence.
"""
@spec sum(Q.reql_array) :: Q.t
operate_on_single_arg(:sum, 145)
@spec sum(Q.reql_array, Q.reql_string|Q.reql_func1) :: Q.t
operate_on_two_args(:sum, 145)
@doc """
Averages all the elements of a sequence. If called with a field name,
averages all the values of that field in the sequence, skipping elements of the
sequence that lack that field. If called with a function, calls that function
on every element of the sequence and averages the results, skipping elements of
the sequence where that function returns None or a non-existence error.
Produces a non-existence error when called on an empty sequence. You can
handle this case with `default`.
"""
@spec avg(Q.reql_array) :: Q.t
operate_on_single_arg(:avg, 146)
@spec avg(Q.reql_array, Q.reql_string|Q.reql_func1) :: Q.t
operate_on_two_args(:avg, 146)
@doc """
Finds the minimum element of a sequence. The min command can be called with:
* a field name, to return the element of the sequence with the smallest value in
that field;
* an index, to return the element of the sequence with the smallest value in that
index;
* a function, to apply the function to every element within the sequence and
return the element which returns the smallest value from the function, ignoring
any elements where the function returns None or produces a non-existence error.
Calling min on an empty sequence will throw a non-existence error; this can be
handled using the `default` command.
"""
@spec min(Q.reql_array, Q.reql_opts | Q.reql_string | Q.reql_func1) :: Q.t
def min(seq, opts) when is_map(opts), do: %Q{query: [147, [wrap(seq)], opts]}
operate_on_single_arg(:min, 147)
operate_on_two_args(:min, 147)
@doc """
Finds the maximum element of a sequence. The max command can be called with:
* a field name, to return the element of the sequence with the smallest value in
that field;
* an index, to return the element of the sequence with the smallest value in that
index;
* a function, to apply the function to every element within the sequence and
return the element which returns the smallest value from the function, ignoring
any elements where the function returns None or produces a non-existence error.
Calling max on an empty sequence will throw a non-existence error; this can be
handled using the `default` command.
"""
@spec max(Q.reql_array, Q.reql_opts | Q.reql_string | Q.reql_func1) :: Q.t
def max(seq, opts) when is_map(opts), do: %Q{query: [148, [wrap(seq)], opts]}
operate_on_single_arg(:max, 148)
operate_on_two_args(:max, 148)
@doc """
Removes duplicates from elements in a sequence.
The distinct command can be called on any sequence, a table, or called on a
table with an index.
"""
@spec distinct(Q.reql_array, Q.reql_opts) :: Q.t
def distinct(seq, opts) when is_map(opts), do: %Q{query: [42, [wrap(seq)], opts]}
operate_on_single_arg(:distinct, 42)
@doc """
When called with values, returns `true` if a sequence contains all the specified
values. When called with predicate functions, returns `true` if for each
predicate there exists at least one element of the stream where that predicate
returns `true`.
"""
@spec contains(Q.reql_array, Q.reql_array | Q.reql_func1 | Q.t) :: Q.t
operate_on_seq_and_list(:contains, 93)
operate_on_two_args(:contains, 93)
#
#Control Strucutres
#
@doc """
`args` is a special term that’s used to splice an array of arguments into
another term. This is useful when you want to call a variadic term such as
`get_all` with a set of arguments produced at runtime.
This is analogous to Elixir's `apply`.
"""
@spec args(Q.reql_array) :: Q.t
operate_on_single_arg(:args, 154)
@doc """
Encapsulate binary data within a query.
Only a limited subset of ReQL commands may be chained after binary:
* coerce_to can coerce binary objects to string types
* count will return the number of bytes in the object
* slice will treat bytes like array indexes (i.e., slice(10,20) will return bytes
* 10–19)
* type_of returns PTYPE<BINARY>
* info will return information on a binary object.
"""
@spec binary(Q.reql_binary) :: Q.t
def binary(%RethinkDB.Pseudotypes.Binary{data: data}), do: binary(data)
def binary(data), do: do_binary(%{"$reql_type$" => "BINARY", "data" => :base64.encode(data)})
def do_binary(data), do: %Q{query: [155, [data]]}
@doc """
Call an anonymous function using return values from other ReQL commands or
queries as arguments.
The last argument to do (or, in some forms, the only argument) is an expression
or an anonymous function which receives values from either the previous
arguments or from prefixed commands chained before do. The do command is
essentially a single-element map, letting you map a function over just one
document. This allows you to bind a query result to a local variable within the
scope of do, letting you compute the result just once and reuse it in a complex
expression or in a series of ReQL commands.
Arguments passed to the do function must be basic data types, and cannot be
streams or selections. (Read about ReQL data types.) While the arguments will
all be evaluated before the function is executed, they may be evaluated in any
order, so their values should not be dependent on one another. The type of do’s
result is the type of the value returned from the function or last expression.
"""
@spec do_r(Q.reql_datum | Q.reql_func0, Q.reql_func1) :: Q.t
operate_on_single_arg(:do_r, 64)
def do_r(data, f) when is_function(f), do: %Q{query: [64, [wrap(f), wrap(data)]]}
@doc """
If the `test` expression returns False or None, the false_branch will be
evaluated. Otherwise, the true_branch will be evaluated.
The branch command is effectively an if renamed due to language constraints.
"""
@spec branch(Q.reql_datum, Q.reql_datum, Q.reql_datum) :: Q.t
def branch(test, true_branch, false_branch) do
%Q{query: [65, [wrap(test), wrap(true_branch), wrap(false_branch)]]}
end
@doc """
Loop over a sequence, evaluating the given write query for each element.
"""
@spec for_each(Q.reql_array, Q.reql_func1) :: Q.t
operate_on_two_args(:for_each, 68)
@doc """
Throw a runtime error.
"""
@spec error(Q.reql_string) :: Q.t
operate_on_single_arg(:error, 12)
@doc """
Handle non-existence errors. Tries to evaluate and return its first argument.
If an error related to the absence of a value is thrown in the process, or if
its first argument returns nil, returns its second argument. (Alternatively,
the second argument may be a function which will be called with either the text
of the non-existence error or nil.)
"""
@spec default(Q.t, Q.t) :: Q.t
operate_on_two_args(:default, 92)
@doc """
Create a javascript expression.
`timeout` is the number of seconds before `js` times out. The default value
is 5 seconds.
"""
@spec js(Q.reql_string, Q.reql_number) :: Q.t
operate_on_single_arg(:js, 11)
def js(javascript, number), do: %Q{query: [11, [wrap(javascript)], %{timeout: number}]}
@doc """
Convert a value of one type into another.
* a sequence, selection or object can be coerced to an array
* an array of key-value pairs can be coerced to an object
* a string can be coerced to a number
* any datum (single value) can be coerced to a string
* a binary object can be coerced to a string and vice-versa
"""
@spec coerce_to(Q.reql_datum, Q.reql_string) :: Q.t
operate_on_two_args(:coerce_to, 51)
@doc """
Gets the type of a value.
"""
@spec type_of(Q.reql_datum) :: Q.t
operate_on_single_arg(:type_of, 52)
@doc """
Get information about a ReQL value.
"""
@spec info(Q.t) :: Q.t
operate_on_single_arg(:info, 79)
@doc """
Parse a JSON string on the server.
"""
@spec json(Q.reql_string) :: Q.t
operate_on_single_arg(:json, 98)
@doc """
Retrieve data from the specified URL over HTTP. The return type depends on
the result_format option, which checks the Content-Type of the response by
default.
"""
@spec http(Q.reql_string, Q.reql_obj) :: Q.t
def http(url, opts \\ %{}), do: %Q{query: [153, [url], opts]}
@doc """
Return a UUID (universally unique identifier), a string that can be used as a unique ID.
"""
@spec uuid() :: Q.t
operate_on_zero_args(:uuid, 169)
#
#Database Operations
#
@doc """
Create a database. A RethinkDB database is a collection of tables, similar to
relational databases.
If successful, the command returns an object with two fields:
* dbs_created: always 1.
* config_changes: a list containing one object with two fields, old_val and
new_val:
* old_val: always null.
* new_val: the database’s new config value.
If a database with the same name already exists, the command throws
RqlRuntimeError.
Note: Only alphanumeric characters and underscores are valid for the database
name.
"""
@spec db_create(Q.reql_string) :: Q.t
operate_on_single_arg(:db_create, 57)
@doc """
Drop a database. The database, all its tables, and corresponding data will be deleted.
If successful, the command returns an object with two fields:
* dbs_dropped: always 1.
* tables_dropped: the number of tables in the dropped database.
* config_changes: a list containing one two-field object, old_val and new_val:
* old_val: the database’s original config value.
* new_val: always None.
If the given database does not exist, the command throws RqlRuntimeError.
"""
@spec db_drop(Q.reql_string) :: Q.t
operate_on_single_arg(:db_drop, 58)
@doc """
List all database names in the system. The result is a list of strings.
"""
@spec db_list :: Q.t
operate_on_zero_args(:db_list, 59)
#
#Geospatial Queries
#
@doc """
Construct a circular line or polygon. A circle in RethinkDB is a polygon or
line approximating a circle of a given radius around a given center, consisting
of a specified number of vertices (default 32).
The center may be specified either by two floating point numbers, the latitude
(−90 to 90) and longitude (−180 to 180) of the point on a perfect sphere (see
Geospatial support for more information on ReQL’s coordinate system), or by a
point object. The radius is a floating point number whose units are meters by
default, although that may be changed with the unit argument.
Optional arguments available with circle are:
- num_vertices: the number of vertices in the polygon or line. Defaults to 32.
- geo_system: the reference ellipsoid to use for geographic coordinates. Possible
values are WGS84 (the default), a common standard for Earth’s geometry, or
unit_sphere, a perfect sphere of 1 meter radius.
- unit: Unit for the radius distance. Possible values are m (meter, the default),
km (kilometer), mi (international mile), nm (nautical mile), ft (international
foot).
- fill: if `true` (the default) the circle is filled, creating a polygon; if `false`
the circle is unfilled (creating a line).
"""
@spec circle(Q.reql_geo, Q.reql_number, Q.reql_opts) :: Q.t
operate_on_two_args(:circle, 165)
@doc """
Compute the distance between a point and another geometry object. At least one
of the geometry objects specified must be a point.
Optional arguments available with distance are:
- geo_system: the reference ellipsoid to use for geographic coordinates. Possible
values are WGS84 (the default), a common standard for Earth’s geometry, or
unit_sphere, a perfect sphere of 1 meter radius.
- unit: Unit to return the distance in. Possible values are m (meter, the
default), km (kilometer), mi (international mile), nm (nautical mile), ft
(international foot).
If one of the objects is a polygon or a line, the point will be projected onto
the line or polygon assuming a perfect sphere model before the distance is
computed (using the model specified with geo_system). As a consequence, if the
polygon or line is extremely large compared to Earth’s radius and the distance
is being computed with the default WGS84 model, the results of distance should
be considered approximate due to the deviation between the ellipsoid and
spherical models.
"""
@spec distance(Q.reql_geo, Q.reql_geo, Q.reql_opts) :: Q.t
operate_on_two_args(:distance, 162)
@doc """
Convert a Line object into a Polygon object. If the last point does not
specify the same coordinates as the first point, polygon will close the polygon
by connecting them.
"""
@spec fill(Q.reql_line) :: Q.t
operate_on_single_arg(:fill, 167)
@doc """
Convert a GeoJSON object to a ReQL geometry object.
RethinkDB only allows conversion of GeoJSON objects which have ReQL
equivalents: Point, LineString, and Polygon. MultiPoint, MultiLineString, and
MultiPolygon are not supported. (You could, however, store multiple points,
lines and polygons in an array and use a geospatial multi index with them.)
Only longitude/latitude coordinates are supported. GeoJSON objects that use
Cartesian coordinates, specify an altitude, or specify their own coordinate
reference system will be rejected.
"""
@spec geojson(Q.reql_obj) :: Q.t
operate_on_single_arg(:geojson, 157)
@doc """
Convert a ReQL geometry object to a GeoJSON object.
"""
@spec to_geojson(Q.reql_obj) :: Q.t
operate_on_single_arg(:to_geojson, 158)
@doc """
Get all documents where the given geometry object intersects the geometry
object of the requested geospatial index.
The index argument is mandatory. This command returns the same results as
`filter(r.row('index')) |> intersects(geometry)`. The total number of results
is limited to the array size limit which defaults to 100,000, but can be
changed with the `array_limit` option to run.
"""
@spec get_intersecting(Q.reql_array, Q.reql_geo, Q.reql_opts) :: Q.t
operate_on_two_args(:get_intersecting, 166)
@doc """
Get all documents where the specified geospatial index is within a certain
distance of the specified point (default 100 kilometers).
The index argument is mandatory. Optional arguments are:
* max_results: the maximum number of results to return (default 100).
* unit: Unit for the distance. Possible values are m (meter, the default), km
(kilometer), mi (international mile), nm (nautical mile), ft (international
foot).
* max_dist: the maximum distance from an object to the specified point (default
100 km).
* geo_system: the reference ellipsoid to use for geographic coordinates. Possible
values are WGS84 (the default), a common standard for Earth’s geometry, or
unit_sphere, a perfect sphere of 1 meter radius.
The return value will be an array of two-item objects with the keys dist and
doc, set to the distance between the specified point and the document (in the
units specified with unit, defaulting to meters) and the document itself,
respectively.
"""
@spec get_nearest(Q.reql_array, Q.reql_geo, Q.reql_opts) :: Q.t
operate_on_two_args(:get_nearest, 168)
@doc """
Tests whether a geometry object is completely contained within another. When
applied to a sequence of geometry objects, includes acts as a filter, returning
a sequence of objects from the sequence that include the argument.
"""
@spec includes(Q.reql_geo, Q.reql_geo) :: Q.t
operate_on_two_args(:includes, 164)
@doc """
Tests whether two geometry objects intersect with one another. When applied to
a sequence of geometry objects, intersects acts as a filter, returning a
sequence of objects from the sequence that intersect with the argument.
"""
@spec intersects(Q.reql_geo, Q.reql_geo) :: Q.t
operate_on_two_args(:intersects, 163)
@doc """
Construct a geometry object of type Line. The line can be specified in one of
two ways:
- Two or more two-item arrays, specifying latitude and longitude numbers of the
line’s vertices;
- Two or more Point objects specifying the line’s vertices.
"""
@spec line([Q.reql_geo]) :: Q.t
operate_on_list(:line, 160)
@doc """
Construct a geometry object of type Point. The point is specified by two
floating point numbers, the longitude (−180 to 180) and latitude (−90 to 90) of
the point on a perfect sphere.
"""
@spec point(Q.reql_geo) :: Q.t
def point({la,lo}), do: point(la, lo)
operate_on_two_args(:point, 159)
@doc """
Construct a geometry object of type Polygon. The Polygon can be specified in
one of two ways:
Three or more two-item arrays, specifying latitude and longitude numbers of the
polygon’s vertices;
* Three or more Point objects specifying the polygon’s vertices.
* Longitude (−180 to 180) and latitude (−90 to 90) of vertices are plotted on a
perfect sphere. See Geospatial support for more information on ReQL’s
coordinate system.
If the last point does not specify the same coordinates as the first point,
polygon will close the polygon by connecting them. You cannot directly
construct a polygon with holes in it using polygon, but you can use polygon_sub
to use a second polygon within the interior of the first to define a hole.
"""
@spec polygon([Q.reql_geo]) :: Q.t
operate_on_list(:polygon, 161)
@doc """
Use polygon2 to “punch out” a hole in polygon1. polygon2 must be completely
contained within polygon1 and must have no holes itself (it must not be the
output of polygon_sub itself).
"""
@spec polygon_sub(Q.reql_geo, Q.reql_geo) :: Q.t
operate_on_two_args(:polygon_sub, 171)
#
#Joins Queries
#
@doc """
Returns an inner join of two sequences. The returned sequence represents an
intersection of the left-hand sequence and the right-hand sequence: each row of
the left-hand sequence will be compared with each row of the right-hand
sequence to find all pairs of rows which satisfy the predicate. Each matched
pair of rows of both sequences are combined into a result row. In most cases,
you will want to follow the join with `zip` to combine the left and right results.
Note that `inner_join` is slower and much less efficient than using `eqJoin` or
`flat_map` with `get_all`. You should avoid using `inner_join` in commands when
possible.
iex> table("people") |> inner_join(
table("phone_numbers"), &(eq(&1["id"], &2["person_id"])
) |> run
"""
@spec inner_join(Q.reql_array, Q.reql_array, Q.reql_func2) :: Q.t
operate_on_three_args(:inner_join, 48)
@doc """
Returns a left outer join of two sequences. The returned sequence represents
a union of the left-hand sequence and the right-hand sequence: all documents in
the left-hand sequence will be returned, each matched with a document in the
right-hand sequence if one satisfies the predicate condition. In most cases,
you will want to follow the join with `zip` to combine the left and right results.
Note that `outer_join` is slower and much less efficient than using `flat_map`
with `get_all`. You should avoid using `outer_join` in commands when possible.
iex> table("people") |> outer_join(
table("phone_numbers"), &(eq(&1["id"], &2["person_id"])
) |> run
"""
@spec outer_join(Q.reql_array, Q.reql_array, Q.reql_func2) :: Q.t
operate_on_three_args(:outer_join, 49)
@doc """
Join tables using a field on the left-hand sequence matching primary keys or
secondary indexes on the right-hand table. `eq_join` is more efficient than other
ReQL join types, and operates much faster. Documents in the result set consist
of pairs of left-hand and right-hand documents, matched when the field on the
left-hand side exists and is non-null and an entry with that field’s value
exists in the specified index on the right-hand side.
The result set of `eq_join` is a stream or array of objects. Each object in the
returned set will be an object of the form `{ left: <left-document>, right:
<right-document> }`, where the values of left and right will be the joined
documents. Use the zip command to merge the left and right fields together.
iex> table("people") |> eq_join(
"id", table("phone_numbers"), %{index: "person_id"}
) |> run
"""
@spec eq_join(Q.reql_array, Q.reql_string, Q.reql_array, %{}) :: Q.t
operate_on_three_args(:eq_join, 50)
@doc """
Used to ‘zip’ up the result of a join by merging the ‘right’ fields into
‘left’ fields of each member of the sequence.
iex> table("people") |> eq_join(
"id", table("phone_numbers"), %{index: "person_id"}
) |> zip |> run
"""
@spec zip(Q.reql_array) :: Q.t
operate_on_single_arg(:zip, 72)
#
#Math and Logic Queries
#
@doc """
Sum two numbers, concatenate two strings, or concatenate 2 arrays.
iex> add(1, 2) |> run conn
%RethinkDB.Record{data: 3}
iex> add("hello", " world") |> run conn
%RethinkDB.Record{data: "hello world"}
iex> add([1,2], [3,4]) |> run conn
%RethinkDB.Record{data: [1,2,3,4]}
"""
@spec add((Q.reql_number | Q.reql_string), (Q.reql_number | Q.reql_string)) :: Q.t
operate_on_two_args(:add, 24)
@doc """
Add multiple values.
iex> add([1, 2]) |> run conn
%RethinkDB.Record{data: 3}
iex> add(["hello", " world"]) |> run
%RethinkDB.Record{data: "hello world"}
"""
@spec add([(Q.reql_number | Q.reql_string | Q.reql_array)]) :: Q.t
operate_on_list(:add, 24)
@doc """
Subtract two numbers.
iex> sub(1, 2) |> run conn
%RethinkDB.Record{data: -1}
"""
@spec sub(Q.reql_number, Q.reql_number) :: Q.t
operate_on_two_args(:sub, 25)
@doc """
Subtract multiple values. Left associative.
iex> sub([9, 1, 2]) |> run conn
%RethinkDB.Record{data: 6}
"""
@spec sub([Q.reql_number]) :: Q.t
operate_on_list(:sub, 25)
@doc """
Multiply two numbers, or make a periodic array.
iex> mul(2,3) |> run conn
%RethinkDB.Record{data: 6}
iex> mul([1,2], 2) |> run conn
%RethinkDB.Record{data: [1,2,1,2]}
"""
@spec mul((Q.reql_number | Q.reql_array), (Q.reql_number | Q.reql_array)) :: Q.t
operate_on_two_args(:mul, 26)
@doc """
Multiply multiple values.
iex> mul([2,3,4]) |> run conn
%RethinkDB.Record{data: 24}
"""
@spec mul([(Q.reql_number | Q.reql_array)]) :: Q.t
operate_on_list(:mul, 26)
@doc """
Divide two numbers.
iex> divide(12, 4) |> run conn
%RethinkDB.Record{data: 3}
"""
@spec divide(Q.reql_number, Q.reql_number) :: Q.t
operate_on_two_args(:divide, 27)
@doc """
Divide a list of numbers. Left associative.
iex> divide([12, 2, 3]) |> run conn
%RethinkDB.Record{data: 2}
"""
@spec divide([Q.reql_number]) :: Q.t
operate_on_list(:divide, 27)
@doc """
Find the remainder when dividing two numbers.
iex> mod(23, 4) |> run conn
%RethinkDB.Record{data: 3}
"""
@spec mod(Q.reql_number, Q.reql_number) :: Q.t
operate_on_two_args(:mod, 28)
@doc """
Compute the logical “and” of two values.
iex> and(true, true) |> run conn
%RethinkDB.Record{data: true}
iex> and(false, true) |> run conn
%RethinkDB.Record{data: false}
"""
@spec and_r(Q.reql_bool, Q.reql_bool) :: Q.t
operate_on_two_args(:and_r, 67)
@doc """
Compute the logical “and” of all values in a list.
iex> and_r([true, true, true]) |> run conn
%RethinkDB.Record{data: true}
iex> and_r([false, true, true]) |> run conn
%RethinkDB.Record{data: false}
"""
@spec and_r([Q.reql_bool]) :: Q.t
operate_on_list(:and_r, 67)
@doc """
Compute the logical “or” of two values.
iex> or_r(true, false) |> run conn
%RethinkDB.Record{data: true}
iex> or_r(false, false) |> run conn
%RethinkDB.Record{data: false}
"""
@spec or_r(Q.reql_bool, Q.reql_bool) :: Q.t
operate_on_two_args(:or_r, 66)
@doc """
Compute the logical “or” of all values in a list.
iex> or_r([true, true, true]) |> run conn
%RethinkDB.Record{data: true}
iex> or_r([false, true, true]) |> run conn
%RethinkDB.Record{data: false}
"""
@spec or_r([Q.reql_bool]) :: Q.t
operate_on_list(:or_r, 66)
@doc """
Test if two values are equal.
iex> eq(1,1) |> run conn
%RethinkDB.Record{data: true}
iex> eq(1, 2) |> run conn
%RethinkDB.Record{data: false}
"""
@spec eq(Q.reql_datum, Q.reql_datum) :: Q.t
operate_on_two_args(:eq, 17)
@doc """
Test if all values in a list are equal.
iex> eq([2, 2, 2]) |> run conn
%RethinkDB.Record{data: true}
iex> eq([2, 1, 2]) |> run conn
%RethinkDB.Record{data: false}
"""
@spec eq([Q.reql_datum]) :: Q.t
operate_on_list(:eq, 17)
@doc """
Test if two values are not equal.
iex> ne(1,1) |> run conn
%RethinkDB.Record{data: false}
iex> ne(1, 2) |> run conn
%RethinkDB.Record{data: true}
"""
@spec ne(Q.reql_datum, Q.reql_datum) :: Q.t
operate_on_two_args(:ne, 18)
@doc """
Test if all values in a list are not equal.
iex> ne([2, 2, 2]) |> run conn
%RethinkDB.Record{data: false}
iex> ne([2, 1, 2]) |> run conn
%RethinkDB.Record{data: true}
"""
@spec ne([Q.reql_datum]) :: Q.t
operate_on_list(:ne, 18)
@doc """
Test if one value is less than the other.
iex> lt(2,1) |> run conn
%RethinkDB.Record{data: false}
iex> lt(1, 2) |> run conn
%RethinkDB.Record{data: true}
"""
@spec lt(Q.reql_datum, Q.reql_datum) :: Q.t
operate_on_two_args(:lt, 19)
@doc """
Test if all values in a list are less than the next. Left associative.
iex> lt([1, 4, 2]) |> run conn
%RethinkDB.Record{data: false}
iex> lt([1, 4, 5]) |> run conn
%RethinkDB.Record{data: true}
"""
@spec lt([Q.reql_datum]) :: Q.t
operate_on_list(:lt, 19)
@doc """
Test if one value is less than or equal to the other.
iex> le(1,1) |> run conn
%RethinkDB.Record{data: true}
iex> le(1, 2) |> run conn
%RethinkDB.Record{data: true}
"""
@spec le(Q.reql_datum, Q.reql_datum) :: Q.t
operate_on_two_args(:le, 20)
@doc """
Test if all values in a list are less than or equal to the next. Left associative.
iex> le([1, 4, 2]) |> run conn
%RethinkDB.Record{data: false}
iex> le([1, 4, 4]) |> run conn
%RethinkDB.Record{data: true}
"""
@spec le([Q.reql_datum]) :: Q.t
operate_on_list(:le, 20)
@doc """
Test if one value is greater than the other.
iex> gt(1,2) |> run conn
%RethinkDB.Record{data: false}
iex> gt(2,1) |> run conn
%RethinkDB.Record{data: true}
"""
@spec gt(Q.reql_datum, Q.reql_datum) :: Q.t
operate_on_two_args(:gt, 21)
@doc """
Test if all values in a list are greater than the next. Left associative.
iex> gt([1, 4, 2]) |> run conn
%RethinkDB.Record{data: false}
iex> gt([10, 4, 2]) |> run conn
%RethinkDB.Record{data: true}
"""
@spec gt([Q.reql_datum]) :: Q.t
operate_on_list(:gt, 21)
@doc """
Test if one value is greater than or equal to the other.
iex> ge(1,1) |> run conn
%RethinkDB.Record{data: true}
iex> ge(2, 1) |> run conn
%RethinkDB.Record{data: true}
"""
@spec ge(Q.reql_datum, Q.reql_datum) :: Q.t
operate_on_two_args(:ge, 22)
@doc """
Test if all values in a list are greater than or equal to the next. Left associative.
iex> le([1, 4, 2]) |> run conn
%RethinkDB.Record{data: false}
iex> le([10, 4, 4]) |> run conn
%RethinkDB.Record{data: true}
"""
@spec ge([Q.reql_datum]) :: Q.t
operate_on_list(:ge, 22)
@doc """
Compute the logical inverse (not) of an expression.
iex> not(true) |> run conn
%RethinkDB.Record{data: false}
"""
@spec not_r(Q.reql_bool) :: Q.t
operate_on_single_arg(:not_r, 23)
@doc """
Generate a random float between 0 and 1.
iex> random |> run conn
%RethinkDB.Record{data: 0.43}
"""
@spec random :: Q.t
def random, do: %Q{query: [151, []]}
@doc """
Generate a random value in the range [0,upper). If upper is an integer then the
random value will be an interger. If upper is a float it will be a float.
iex> random(5) |> run conn
%RethinkDB.Record{data: 3}
iex> random(5.0) |> run conn
%RethinkDB.Record{data: 3.7}
"""
@spec random(Q.reql_number) :: Q.t
def random(upper) when is_integer(upper), do: %Q{query: [151, [upper]]}
def random(upper) when is_float(upper), do: %Q{query: [151, [upper], %{float: true}]}
@doc """
Generate a random value in the range [lower,upper). If either arg is an integer then the
random value will be an interger. If one of them is a float it will be a float.
iex> random(5, 10) |> run conn
%RethinkDB.Record{data: 8}
iex> random(5.0, 15.0,) |> run conn
%RethinkDB.Record{data: 8.34}
"""
@spec random(Q.reql_number, Q.reql_number) :: Q.t
def random(lower, upper) when is_integer(lower) and is_integer(upper) do
%Q{query: [151, [lower, upper]]}
end
def random(lower, upper) when is_float(lower) or is_float(upper) do
%Q{query: [151, [lower, upper], %{float: true}]}
end
#
#Selection Queries
#
@doc """
Reference a database.
"""
@spec db(Q.reql_string) :: Q.t
operate_on_single_arg(:db, 14)
@doc """
Return all documents in a table. Other commands may be chained after table to
return a subset of documents (such as get and filter) or perform further
processing.
There are two optional arguments.
* useOutdated: if true, this allows potentially out-of-date data to be returned,
with potentially faster reads. It also allows you to perform reads from a
secondary replica if a primary has failed. Default false.
* identifierFormat: possible values are name and uuid, with a default of name. If
set to uuid, then system tables will refer to servers, databases and tables by
UUID rather than name. (This only has an effect when used with system tables.)
"""
@spec table(Q.reql_string, Q.reql_opts) :: Q.t
@spec table(Q.t, Q.reql_string, Q.reql_opts) :: Q.t
operate_on_single_arg(:table, 15)
operate_on_two_args(:table, 15)
@doc """
Get a document by primary key.
If no document exists with that primary key, get will return nil.
"""
@spec get(Q.t, Q.reql_datum) :: Q.t
operate_on_two_args(:get, 16)