Skip to content

Commit

Permalink
add write hook and bitwise operators
Browse files Browse the repository at this point in the history
  • Loading branch information
ba0f3 committed Jan 7, 2020
1 parent 00fed5b commit 53ecbd4
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 8 deletions.
13 changes: 12 additions & 1 deletion private/ql2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type
GET_FIELD = 31
HAS_FIELDS = 32
PLUCK = 33
WITHOUT_R = 34
WITHOUT = 34
MERGE = 35
BETWEEN_DEPRECATED = 36
REDUCE = 37
Expand Down Expand Up @@ -220,3 +220,14 @@ type
FLOOR = 183
CEIL = 184
ROUND = 185
VALUES = 186
FOLD = 187
GRANT = 188
SET_WRITE_HOOK = 189
GET_WRITE_HOOK = 190
BIT_AND = 191
BIT_OR = 192
BIT_XOR = 193
BIT_NOT = 194
BIT_SAL = 195
BIT_SAR = 196
10 changes: 8 additions & 2 deletions private/queries/document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ proc pluck*[T](r: T, n: varargs[string]): RqlQuery =
for x in n:
result.addArg(x)

proc withoutR*[T](r: T, n: varargs[string]): RqlQuery =
NEW_QUERY(WITHOUT_R, r)
proc without*[T](r: T, n: varargs[string]): RqlQuery =
NEW_QUERY(WITHOUT, r)
for x in n:
result.addArg(x)

Expand All @@ -22,6 +22,12 @@ proc merge*[T, U](r: T, n: varargs[proc(x: RqlQuery): U]): RqlQuery =
for f in n:
result.addArg(funcWrap(f))

proc merge*[T](r: T, n: openArray[(string, RqlQuery)]): RqlQuery =
NEW_QUERY(MERGE, r)
for x in n:
result.addArg(&*{x[0]: x[1]})


proc append*[T](r: RqlQuery, t: T): RqlQuery =
NEW_QUERY(APPEND, r, t)

Expand Down
34 changes: 34 additions & 0 deletions private/queries/math.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,37 @@ proc random*(r: RethinkClient, x = 0, y = 1, isFloat = false): RqlQuery =
result.addArg(newDatum(y))
if isFloat:
result.setOption("float", isFloat)

proc bitAnd*(r: RqlQuery, numbers: varargs[int]): RqlQuery =
## bitwise AND
NEW_QUERY(BIT_AND, r)
for n in numbers:
result.addArg(n)

proc bitNot*(r: RqlQuery): RqlQuery =
## bitwise NOT
NEW_QUERY(BIT_NOT, r)

proc bitOr*(r: RqlQuery, numbers: varargs[int]): RqlQuery =
## bitwise OR
NEW_QUERY(BIT_Or, r)
for n in numbers:
result.addArg(n)

proc bitShl*(r: RqlQuery, numbers: varargs[int]): RqlQuery =
## bitwise SHL
NEW_QUERY(BIT_SAL, r)
for n in numbers:
result.addArg(n)

proc bitShr*(r: RqlQuery, numbers: varargs[int]): RqlQuery =
## bitwise SHR
NEW_QUERY(BIT_SAR, r)
for n in numbers:
result.addArg(n)

proc bitXor*(r: RqlQuery, numbers: varargs[int]): RqlQuery =
## bitwise XOR
NEW_QUERY(BIT_XOR, r)
for n in numbers:
result.addArg(n)
14 changes: 14 additions & 0 deletions private/queries/table.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,17 @@ proc changes*[T](r: T, squash = false, includeStates = false): RqlQuery =
result.setOption("squash", squash)
if includeStates:
result.setOption("include_states", includeStates)

proc setWriteHook*(r: RqlQuery): RqlQuery =
## Sets the write hook on a table or overwrites it if one already exists
NEW_QUERY(SET_WRITE_HOOK, r)
result.addArg(DEFAULT)

proc setWriteHook*[U](r: RqlQuery, f: proc(ctx, oldValue, newValue: RqlQuery): U): RqlQuery =
## Sets the write hook on a table or overwrites it if one already exists
NEW_QUERY(SET_WRITE_HOOK, r)
result.addArg(funcWrap(f))

proc getWriteHook*(r: RqlQuery): RqlQuery =
## Gets the write hook of a table, if any
NEW_QUERY(GET_WRITE_HOOK, r)
20 changes: 19 additions & 1 deletion private/rql.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

## This module provides all high-level API for query and manipulate data
import json, future
import json, sugar
import ql2, datum, connection, utils, types


Expand Down Expand Up @@ -245,6 +245,24 @@ proc makeFunc*[T](f: T): RqlQuery =
result.addArg(&[varId])
result.addArg(f)

proc funcWrap*[T](f: proc(ctx, oldValue, newValue: RqlQuery): T): RqlQuery =
NEW_QUERY(FUNC)

let
v1 = makeVar(1)
v2 = makeVar(2)
v3 = makeVar(3)

result.addArg(&*[1, 2, 3])
let res = f(v1, v2, v3)
when res is array:
var arr = newQuery(MAKE_ARRAY)
for x in res:
arr.addArg(x)
result.addArg(arr)
else:
result.addArg(res)

proc `[]`*(r: RqlQuery, s: auto): RqlQuery =
## Operator for create row's fields chain
##
Expand Down
27 changes: 23 additions & 4 deletions tests/test_math_logic.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest, json
import ../rethinkdb

let r = R.connect().repl()

suite "math and logic tests":
let r = R.connect().repl()
var ret: JsonNode

test "add":
let output = 2
var ret: JsonNode
ret = (r.expr(1) + 1).run()
check(ret.num == output)
ret = (1 + r.expr(1)).run()
Expand Down Expand Up @@ -35,4 +35,23 @@ suite "math and logic tests":
expect(RqlRuntimeError):
discard (r.expr("a") + 1).run()

r.close()
test "bit wises":
ret = r.expr(5).bitAnd(3).run()
check(ret.getInt == 1)

ret = r.expr(7).bitNot().run()
check(ret.getInt == -8)

ret = r.expr(5).bitOr(3).run()
check(ret.getInt == 7)

ret = r.expr(5).bitShl(4).run()
check(ret.getInt == 80)

ret = r.expr(32).bitShr(3).run()
check(ret.getInt == 4)

ret = r.expr(6).bitXor(4).run()
check(ret.getInt == 2)

r.close()

0 comments on commit 53ecbd4

Please sign in to comment.