rethinkdb 1.2.6.1 → 1.4.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rql.rb DELETED
@@ -1,472 +0,0 @@
1
- # Copyright 2010-2012 RethinkDB, all rights reserved.
2
- module RethinkDB
3
- module Mixin_H4x # :nodoc:
4
- def &(l,r) # :nodoc:
5
- RQL.all_h4x(l,r)
6
- end
7
- end
8
-
9
- # This module contains the RQL query construction functions. By far
10
- # the most common way of gaining access to those functions, however,
11
- # is to include/extend RethinkDB::Shortcuts to gain access to the
12
- # shortcut <b>+r+</b>. That shortcut is used in all the examples
13
- # below.
14
- #
15
- # Also, many of the functions here can be called as if they were
16
- # instance methods of a RQL query. So the following are all equivalent:
17
- # r.add(1,2)
18
- # r(1).add(2)
19
- # r(1) + 2
20
- # r.+(1, 2)
21
- module RQL
22
- # A shortcut for Connection::new
23
- def self.connect(*args, &block)
24
- Connection.new(*args, &block)
25
- end
26
-
27
- # Return the most recently opened connection.
28
- def self.last_connection; Connection.last; end
29
-
30
- # Construct a javascript expression, which may refer to variables in scope
31
- # (use <b>+to_s+</b> to get the name of a variable query, or simply splice
32
- # it in). Behaves as if passed to the standard `eval` function in
33
- # JavaScript. If you have a table <b>+table+</b>, the following are
34
- # equivalent:
35
- # table.map{|row| row[:id]}
36
- # table.map{|row| r.js("#{row}.id")}
37
- # table.map{r.js("this.id")} #implicit variable
38
- # table.map{r.js("var a = this.id; a;")} #implicit variable
39
- # As are:
40
- # r.let(:a => 1, :b => 2) { r.add(r.letvar('a'), r.letvar('b'), 1) }
41
- # r.let(:a => 1, :b => 2) { r.js('a+b+1') }
42
- def self.js(str);
43
- JSON_Expression.new [:js, str]
44
- end
45
-
46
- # Refer to the database named <b>+db_name+</b>. Usually used as a
47
- # stepping stone to a table reference. For instance, to refer to
48
- # the table 'tbl' in the database 'db':
49
- # db('db').table('tbl')
50
- def self.db(db_name); Database.new db_name; end
51
-
52
- # Convert from a Ruby datatype to an RQL query. Numbers, strings, booleans,
53
- # arrays, objects, and nil are all converted to their respective JSON types.
54
- # <b>Note:</b> this function is idempotent, so the following are equivalent:
55
- # r.expr(5)
56
- # r.expr(r.expr(5))
57
- # The shortcut `r` also acts like this when used as a function, so
58
- # the following are equivalent:
59
- # r.expr(1)
60
- # r(1)
61
- def self.expr x
62
- return x if x.kind_of? RQL_Query
63
- BT.alt_inspect(case x.class().hash
64
- when Table.hash then x.to_mrs
65
- when String.hash then JSON_Expression.new [:string, x]
66
- when Fixnum.hash then JSON_Expression.new [:number, x]
67
- when Float.hash then JSON_Expression.new [:number, x]
68
- when TrueClass.hash then JSON_Expression.new [:bool, x]
69
- when FalseClass.hash then JSON_Expression.new [:bool, x]
70
- when NilClass.hash then JSON_Expression.new [:json_null]
71
- when Array.hash then JSON_Expression.new [:array, *x.map{|y| expr(y)}]
72
- when Hash.hash then
73
- JSON_Expression.new [:object, *x.map{|var,term| [S.checkdict(var), expr(term)]}]
74
-
75
- else raise TypeError, "RQL::expr can't handle object `#{x.inspect}` of class `#{x.class()}`.
76
- Make sure you're providing a RQL expression or an object that can be coerced
77
- to a JSON type (a String, Fixnum, Float, TrueClass, FalseClass, NilClass, Array,
78
- or Hash)."
79
- end) { x.inspect } end
80
-
81
- # Explicitly construct an RQL variable from a string. See RQL::let.
82
- # r.letvar('varname')
83
- def self.letvar(varname)
84
- BT.alt_inspect(Var_Expression.new [:var, varname]) { "letvar(#{varname.inspect})" }
85
- end
86
-
87
- # Provide a literal JSON string that will be parsed by the server. For
88
- # example, the following are equivalent:
89
- # r.expr({"a" => 5})
90
- # r.json('{"a": 5}')
91
- def self.json(str); JSON_Expression.new [:json, str]; end
92
-
93
- # Construct an error. This is usually used in the branch of an <b>+if+</b>
94
- # expression. For example:
95
- # r.if(r(1) > 2, false, r.error('unreachable'))
96
- # will only run the error query if 1 is greater than 2. If an error query
97
- # does get run, it will be received as a RuntimeError in Ruby, so be
98
- # prepared to handle it.
99
- def self.error(err); JSON_Expression.new [:error, err]; end
100
-
101
- # Test a predicate and execute one of two branches (just like
102
- # Ruby's <b>+if+</b>). For example, if we have a table
103
- # <b>+table+</b>:
104
- # table.update{|row| r.branch(row[:score] < 10, {:score => 10}, {})}
105
- # will change every row with score below 10 in <b>+table+</b> to have score 10.
106
- def self.branch(test, t_branch, f_branch)
107
- tb = S.r(t_branch)
108
- fb = S.r(f_branch)
109
- if tb.kind_of? fb.class
110
- resclass = fb.class
111
- elsif fb.kind_of? tb.class
112
- resclass = tb.class
113
- else
114
- raise TypeError, "Both branches of IF must be of compatible types."
115
- end
116
- resclass.new [:branch, S.r(test), S.r(t_branch), S.r(f_branch)]
117
- end
118
-
119
- # Construct a query that binds some values to variable (as
120
- # specified by <b>+varbinds+</b>) and then executes <b>+body+</b>
121
- # with those variables accessible through RQL::letvar. For
122
- # example, the following are equivalent:
123
- # r.let(:a => 2, :b => 3) { r.letvar('a') + r.letvar('b') }
124
- # r.expr(5)
125
- def self.let(varbinds, &body)
126
- varbinds = varbinds.to_a
127
- varbinds.map! {|name, value| [name.to_s, expr(value)]}
128
- res = S.r(body.call)
129
- res.class.new [:let, varbinds, res]
130
- end
131
-
132
- # Negate a boolean. May also be called as if it were an instance method of
133
- # JSON_Expression for convenience. The following are equivalent:
134
- # r.not(true)
135
- # r(true).not
136
- def self.not(pred); JSON_Expression.new [:call, [:not], [S.r(pred)]]; end
137
-
138
- # Add two or more numbers together. May also be called as if it
139
- # were a instance method of JSON_Expression for convenience, and
140
- # overloads <b><tt>+</tt></b> if the lefthand side is a query.
141
- # The following are all equivalent:
142
- # r.add(1,2)
143
- # r(1).add(2)
144
- # (r(1) + 2) # Note that (1 + r(2)) is *incorrect* because Ruby only
145
- # # overloads based on the lefthand side.
146
- # The following is also legal:
147
- # r.add(1,2,3)
148
- # Add may also be used to concatenate arrays. The following are equivalent:
149
- # r([1,2,3])
150
- # r.add([1, 2], [3])
151
- # r([1,2]) + [3]
152
- def self.add(a, b, *rest)
153
- JSON_Expression.new [:call, [:add], [S.r(a), S.r(b), *(rest.map{|x| S.r x})]];
154
- end
155
-
156
- # Subtract one number from another.
157
- # May also be called as if it were a instance method of JSON_Expression for
158
- # convenience, and overloads <b><tt>-</tt></b> if the lefthand side is a
159
- # query. Also has the shorter synonym <b>+sub+</b>. The following are all
160
- # equivalent:
161
- # r.subtract(1,2)
162
- # r(1).subtract(2)
163
- # r.sub(1,2)
164
- # r(1).sub(2)
165
- # (r(1) - 2) # Note that (1 - r(2)) is *incorrect* because Ruby only
166
- # # overloads based on the lefthand side.
167
- def self.subtract(a, b); JSON_Expression.new [:call, [:subtract], [S.r(a), S.r(b)]]; end
168
-
169
- # Multiply two numbers together. May also be called as if it were
170
- # a instance method of JSON_Expression for convenience, and
171
- # overloads <b><tt>+</tt></b> if the lefthand side is a query.
172
- # Also has the shorter synonym <b>+mul+</b>. The following are
173
- # all equivalent:
174
- # r.multiply(1,2)
175
- # r(1).multiply(2)
176
- # r.mul(1,2)
177
- # r(1).mul(2)
178
- # (r(1) * 2) # Note that (1 * r(2)) is *incorrect* because Ruby only
179
- # # overloads based on the lefthand side.
180
- # The following is also legal:
181
- # r.multiply(1,2,3)
182
- def self.multiply(a, b, *rest)
183
- JSON_Expression.new [:call, [:multiply], [S.r(a), S.r(b), *(rest.map{|x| S.r x})]];
184
- end
185
-
186
- # Divide one number by another. May also be called as if it were
187
- # a instance method of JSON_Expression for convenience, and
188
- # overloads <b><tt>/</tt></b> if the lefthand side is a query.
189
- # Also has the shorter synonym <b>+div+</b>. The following are
190
- # all equivalent:
191
- # r.divide(1,2)
192
- # r(1).divide(2)
193
- # r.div(1,2)
194
- # r(1).div(2)
195
- # (r(1) / 2) # Note that (1 / r(2)) is *incorrect* because Ruby only
196
- # # overloads based on the lefthand side.
197
- def self.divide(a, b); JSON_Expression.new [:call, [:divide], [S.r(a), S.r(b)]]; end
198
-
199
- # Take one number modulo another. May also be called as if it
200
- # were a instance method of JSON_Expression for convenience, and
201
- # overloads <b><tt>%</tt></b> if the lefthand side is a query.
202
- # Also has the shorter synonym <b>+mod+</b>. The following are all
203
- # equivalent:
204
- # r.modulo(1,2)
205
- # r(1).modulo(2)
206
- # r.mod(1,2)
207
- # r(1).mod(2)
208
- # (r(1) % 2) # Note that (1 % r(2)) is *incorrect* because Ruby only
209
- # # overloads based on the lefthand side.
210
- def self.modulo(a, b); JSON_Expression.new [:call, [:modulo], [S.r(a), S.r(b)]]; end
211
-
212
- # Returns true if any of its arguments are true, like <b>+or+</b>
213
- # in ruby (but with arbitrarily many arguments). May also be
214
- # called as if it were a instance method of JSON_Expression for
215
- # convenience, and overloads <b><tt>|</tt></b> if the lefthand
216
- # side is a query. Also has the synonym <b>+or+</b>. The
217
- # following are all equivalent:
218
- # r(true)
219
- # r.any(false, true)
220
- # r.or(false, true)
221
- # r(false).any(true)
222
- # r(false).or(true)
223
- # (r(false) | true) # Note that (false | r(true)) is *incorrect* because
224
- # # Ruby only overloads based on the lefthand side
225
- def self.any(pred, *rest)
226
- JSON_Expression.new [:call, [:any], [S.r(pred), *(rest.map{|x| S.r x})]];
227
- end
228
-
229
- # Returns true if all of its arguments are true, like <b>+and+</b>
230
- # in ruby (but with arbitrarily many arguments). May also be
231
- # called as if it were a instance method of JSON_Expression for
232
- # convenience, and overloads <b><tt>&</tt></b> if the lefthand
233
- # side is a query. Also has the synonym <b>+and+</b>. The
234
- # following are all equivalent:
235
- # r(false)
236
- # r.all(false, true)
237
- # r.and(false, true)
238
- # r(false).all(true)
239
- # r(false).and(true)
240
- # (r(false) & true) # Note that (false & r(true)) is *incorrect* because
241
- # # Ruby only overloads based on the lefthand side
242
- def self.all(pred, *rest)
243
- JSON_Expression.new [:call, [:all], [S.r(pred), *(rest.map{|x| S.r x})]];
244
- end
245
-
246
- # Take the union of 2 or more sequences <b>+seqs+</b>. Note that
247
- # unlike normal set union, duplicate values are preserved. May
248
- # also be called as if it were a instance method of RQL_Query,
249
- # for convenience. For example, if we have a table
250
- # <b>+table+</b>, the following are equivalent:
251
- # r.union(table.map{|row| r[:id]}, table.map{|row| row[:num]})
252
- # table.map{|row| row[:id]}.union(table.map{|row| row[:num]})
253
- def self.union(*seqs)
254
- #TODO: this looks wrong...
255
- if seqs.all? {|x| x.kind_of? Stream_Expression}
256
- resclass = Stream_Expression
257
- elsif seqs.all? {|x| x.kind_of? JSON_Expression}
258
- resclass = JSON_Expression
259
- else
260
- seqs = seqs.map {|x| self.expr x}
261
- resclass = JSON_Expression
262
- end
263
- resclass.new [:call, [:union], seqs.map{|x| S.r x}];
264
- end
265
-
266
- # Merge two objects together with a preference for the object on the right.
267
- # The resulting object has all the attributes in both objects, and if the
268
- # two objects share an attribute, the value from the object on the right
269
- # "wins" and is included in the final object. May also be called as if it
270
- # were an instance method of JSON_Expression, for convenience. The following are
271
- # equivalent:
272
- # r({:a => 10, :b => 2, :c => 30})
273
- # r.merge({:a => 1, :b => 2}, {:a => 10, :c => 30})
274
- # r({:a => 1, :b => 2}).merge({:a => 10, :c => 30})
275
- def self.merge(obj1, obj2)
276
- JSON_Expression.new [:call, [:merge], [S.r(obj1), S.r(obj2)]]
277
- end
278
-
279
- # Check whether two JSON expressions are equal. May also be called as
280
- # if it were a member function of JSON_Expression for convenience. Has the
281
- # synonym <b>+equals+</b>. The following are all equivalent:
282
- # r(true)
283
- # r.eq 1,1
284
- # r(1).eq(1)
285
- # r.equals 1,1
286
- # r(1).equals(1)
287
- # May also be used with more than two arguments. The following are
288
- # equivalent:
289
- # r(false)
290
- # r.eq(1, 1, 2)
291
- def self.eq(*args)
292
- JSON_Expression.new [:call, [:compare, :eq], args.map{|x| S.r x}]
293
- end
294
-
295
- # Check whether two JSON expressions are *not* equal. May also be
296
- # called as if it were a member function of JSON_Expression for convenience. The
297
- # following are all equivalent:
298
- # r(true)
299
- # r.ne 1,2
300
- # r(1).ne(2)
301
- # r.not r.eq(1,2)
302
- # r(1).eq(2).not
303
- # May also be used with more than two arguments. The following are
304
- # equivalent:
305
- # r(true)
306
- # r.ne(1, 1, 2)
307
- def self.ne(*args)
308
- JSON_Expression.new [:call, [:compare, :ne], args.map{|x| S.r x}]
309
- end
310
-
311
- # Check whether one JSON expression is less than another. May also be
312
- # called as if it were a member function of JSON_Expression for convenience. May
313
- # also be called as the infix operator <b><tt> < </tt></b> if the lefthand
314
- # side is a query. The following are all equivalent:
315
- # r(true)
316
- # r.lt 1,2
317
- # r(1).lt(2)
318
- # r(1) < 2
319
- # Note that the following is illegal, because Ruby only overloads infix
320
- # operators based on the lefthand side:
321
- # 1 < r(2)
322
- # May also be used with more than two arguments. The following are
323
- # equivalent:
324
- # r(true)
325
- # r.lt(1, 2, 3)
326
- def self.lt(*args)
327
- JSON_Expression.new [:call, [:compare, :lt], args.map{|x| S.r x}]
328
- end
329
-
330
- # Check whether one JSON expression is less than or equal to another.
331
- # May also be called as if it were a member function of JSON_Expression for
332
- # convenience. May also be called as the infix operator <b><tt> <= </tt></b>
333
- # if the lefthand side is a query. The following are all equivalent:
334
- # r(true)
335
- # r.le 1,1
336
- # r(1).le(1)
337
- # r(1) <= 1
338
- # Note that the following is illegal, because Ruby only overloads infix
339
- # operators based on the lefthand side:
340
- # 1 <= r(1)
341
- # May also be used with more than two arguments. The following are
342
- # equivalent:
343
- # r(true)
344
- # r.le(1, 2, 2)
345
- def self.le(*args)
346
- JSON_Expression.new [:call, [:compare, :le], args.map{|x| S.r x}]
347
- end
348
-
349
- # Check whether one JSON expression is greater than another.
350
- # May also be called as if it were a member function of JSON_Expression for
351
- # convenience. May also be called as the infix operator <b><tt> > </tt></b>
352
- # if the lefthand side is an RQL query. The following are all equivalent:
353
- # r(true)
354
- # r.gt 2,1
355
- # r(2).gt(1)
356
- # r(2) > 1
357
- # Note that the following is illegal, because Ruby only overloads infix
358
- # operators based on the lefthand side:
359
- # 2 > r(1)
360
- # May also be used with more than two arguments. The following are
361
- # equivalent:
362
- # r(true)
363
- # r.gt(3, 2, 1)
364
- def self.gt(*args)
365
- JSON_Expression.new [:call, [:compare, :gt], args.map{|x| S.r x}]
366
- end
367
-
368
- # Check whether one JSON expression is greater than or equal to another.
369
- # May also be called as if it were a member function of JSON_Expression for
370
- # convenience. May also be called as the infix operator <b><tt> >= </tt></b>
371
- # if the lefthand side is a query. The following are all equivalent:
372
- # r(true)
373
- # r.ge 1,1
374
- # r(1).ge(1)
375
- # r(1) >= 1
376
- # Note that the following is illegal, because Ruby only overloads infix
377
- # operators based on the lefthand side:
378
- # 1 >= r(1)
379
- # May also be used with more than two arguments. The following are
380
- # equivalent:
381
- # r(true)
382
- # r.ge(2, 2, 1)
383
- def self.ge(*args)
384
- JSON_Expression.new [:call, [:compare, :ge], args.map{|x| S.r x}]
385
- end
386
-
387
- # Create a new database with name <b>+db_name+</b>. Either
388
- # returns <b>+nil+</b> or raises an error.
389
- def self.db_create(db_name); Meta_Query.new [:create_db, db_name]; end
390
-
391
- # List all databases. Either returns an array of strings or raises an error.
392
- def self.db_list(); Meta_Query.new [:list_dbs]; end
393
-
394
- # Drop the database with name <b>+db_name+</b>. Either returns
395
- # <b>+nil+</b> or raises an error.
396
- def self.db_drop(db_name); Meta_Query.new [:drop_db, db_name]; end
397
-
398
- # Dereference aliases (see utils.rb)
399
- def self.method_missing(m, *args, &block) # :nodoc:
400
- (m2 = C.method_aliases[m]) ? self.send(m2, *args, &block) : super(m, *args, &block)
401
- end
402
-
403
- # Refer to a table by name. When run over a connection, this query uses the
404
- # default database of that connection. If we have a connection <b>+c+</b>
405
- # like so:
406
- # c = r.connect('localhost', 28015, 'db_name')
407
- # then the following are equivalent:
408
- # c.run(r.table('tbl_name'))
409
- # c.run(r.db('db_name').table('tbl_name')
410
- def self.table(name, opts={})
411
- Table.new(:default_db, name, opts)
412
- end
413
-
414
- # A shortcut for Data_Collectors::count
415
- def self.count(*args); Data_Collectors.count(*args); end
416
- # A shortcut for Data_Collectors::sum
417
- def self.sum(*args); Data_Collectors.sum(*args); end
418
- # A shortcut for Data_Collectors::avg
419
- def self.avg(*args); Data_Collectors.avg(*args); end
420
-
421
- # Specify ascending ordering for a given attribute passed to order_by.
422
- def self.asc(attr)
423
- return [attr, true]
424
- end
425
-
426
- # Specify descending ordering for a given attribute passed to order_by.
427
- def self.desc(attr)
428
- return [attr, false]
429
- end
430
-
431
- def self.boolprop(op, l, r) # :nodoc:
432
- badop = l.boolop? ? l : r
433
- if l.boolop? || r.boolop?
434
- raise RuntimeError,"Error: Cannot use infix #{op} operator on infix boolean expression:
435
- #{badop.inspect}
436
- This is almost always a precedence error; try adding parentheses. If you
437
- actually need to compare booleans, use non-infix operators like `r.all(a,b)`
438
- instead of `a & b`."
439
- end
440
- return RQL.send(op, l, r)
441
- end
442
- # def self.boolprop(op, l, r) # :nodoc:
443
- # if l.boolop?
444
- # larg,rarg = l.body[2]
445
- # sexp = [l.body[0], l.body[1], [larg, boolprop(op, rarg, r)]]
446
- # elsif r.boolop?
447
- # larg,rarg = r.body[2]
448
- # sexp = [r.body[0], r.body[1], [boolprop(op, l, larg), rarg]]
449
- # else
450
- # return RQL.send(op, l, r);
451
- # end
452
- # return S.mark_boolop(JSON_Expression.new sexp)
453
- # end
454
-
455
- # See RQL::lt
456
- def self.< (l,r); boolprop(:lt, S.r(l), S.r(r)); end
457
- # See RQL::le
458
- def self.<=(l,r); boolprop(:le, S.r(l), S.r(r)); end
459
- # See RQL::gt
460
- def self.> (l,r); boolprop(:gt, S.r(l), S.r(r)); end
461
- # See RQL::ge
462
- def self.>=(l,r); boolprop(:ge, S.r(l), S.r(r)); end
463
-
464
- def self.|(l,r) # :nodoc:
465
- S.mark_boolop(any(l,r))
466
- end
467
- extend Mixin_H4x
468
- def self.all_h4x(l,r) # :nodoc:
469
- S.mark_boolop(all(l,r))
470
- end
471
- end
472
- end