| 
1 | 1 | ## This is the implementation of the Multicorn ForeignDataWrapper class that does all of the work in RethinkDB  | 
2 | 2 | ## R.Otten - 2014  | 
3 | 3 | 
 
  | 
 | 4 | +import operator  | 
 | 5 | + | 
4 | 6 | from multicorn import ForeignDataWrapper  | 
5 | 7 | from multicorn.utils import log_to_postgres, ERROR, WARNING, DEBUG  | 
6 | 8 | 
 
  | 
7 | 9 | import rethinkdb as r  | 
8 | 10 | 
 
  | 
 | 11 | + | 
 | 12 | +## Translate a string with an operator in it (eg. ">=") into a function.  | 
 | 13 | +# hint from here:  http://stackoverflow.com/questions/1740726/python-turn-string-into-operator  | 
 | 14 | +# Things that might come from PostgreSQL:  http://www.postgresql.org/docs/9.3/static/functions-comparison.html   | 
 | 15 | +def getOperatorFunction(opr):  | 
 | 16 | + | 
 | 17 | +  '<': operator.lt  | 
 | 18 | +  '>': operator.gt  | 
 | 19 | +  '<=': operator.le  | 
 | 20 | +  '>=': operator.ge  | 
 | 21 | +  '=': operator.eq  | 
 | 22 | +  '<>': operator.ne  | 
 | 23 | +  '!=': operator.ne  | 
 | 24 | +  '@>': operator.contains  | 
 | 25 | +  '<@':   | 
 | 26 | +  '<<':  | 
 | 27 | +  '>>':  | 
 | 28 | +  '&<':  | 
 | 29 | +  '>&':  | 
 | 30 | +  '&&':  | 
 | 31 | +  'is':  | 
 | 32 | +  '~':  | 
 | 33 | +  '~*':  | 
 | 34 | +  '!~':  | 
 | 35 | +  '!~*':  | 
 | 36 | +  '~~':  | 
 | 37 | +  'like':  | 
 | 38 | +  '~~*':  | 
 | 39 | +  'ilike':  | 
 | 40 | +  'similar to':  | 
 | 41 | +  # I'm not sure we'll get "between" so it isn't implemented for now.  | 
 | 42 | +  # I'm not sure 'OR' will get through either as an operator.  | 
 | 43 | +  # We are not going to try to support Geometric object operators at this time.  | 
 | 44 | +  # Nor will we support the Network Address operators yet.  | 
 | 45 | +  # There aren't any json specific comparison operators yet (if there were, we would want to try to implement them)  | 
 | 46 | + | 
 | 47 | +## The Foreign Data Wrapper Class:  | 
9 | 48 | class RethinkdbFDW(ForeignDataWrapper):  | 
10 | 49 | 
 
  | 
11 | 50 |     """  | 
@@ -51,8 +90,8 @@ def execute(self, quals, columns):  | 
51 | 90 | 
 
  | 
52 | 91 |         for qual in quals:  | 
53 | 92 | 
 
  | 
54 |  | -            ## -- this isn't right yet.  | 
55 |  | -            myQuery = myQuery.filter(qual.field_name + ' ' + qual.operator + ' ' + qual.value)  | 
 | 93 | +            operatorFunction = getOperatorFunction(qual.operator)  | 
 | 94 | +            myQuery = myQuery.filter(operatorFunction(r.row[qual.field_name], qual.value))  | 
56 | 95 | 
 
  | 
57 | 96 |          return _run_rethinkdb_action(action=myQuery)  | 
58 | 97 | 
 
  | 
 | 
0 commit comments