forked from jurajmasar/rethink-db-cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrql.hpp
379 lines (322 loc) · 12.5 KB
/
rql.hpp
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
#ifndef RETHINK_DB_DRIVER_RQL
#define RETHINK_DB_DRIVER_RQL
#include "proto/rdb_protocol/ql2.pb.h"
#include <vector>
using namespace std;
using namespace com::rethinkdb;
namespace com {
namespace rethinkdb {
namespace driver {
class connection;
class RQL_Array;
class RQL_Object;
class RQL_String;
class RQL_Database;
class RQL_Table;
class RQL { // "Top" according to photobuf data structure specification
public:
RQL() : term(Term()) {};
vector<shared_ptr<Response>> run();
/* -------------------------------------------------------------------- */
shared_ptr<RQL_Database> db(const RQL_String& db_name);
shared_ptr<RQL_Database> db(const string& db_name);
shared_ptr<RQL_Array> db_list();
shared_ptr<RQL_Object> db_create(const RQL_String& db_name);
shared_ptr<RQL_Object> db_create(const string& db_name);
shared_ptr<RQL_Object> db_drop(const RQL_String& db_name);
shared_ptr<RQL_Object> db_drop(const string& db_name);
/* -------------------------------------------------------------------- */
virtual shared_ptr<RQL_Table> table(const RQL_String& table_name);
virtual shared_ptr<RQL_Table> table(const string& table_name);
virtual shared_ptr<RQL_Object> table_create(const RQL_String& table_name);
virtual shared_ptr<RQL_Object> table_create(const string& table_name);
virtual shared_ptr<RQL_Array> table_drop(const RQL_String& table_name);
virtual shared_ptr<RQL_Array> table_drop(const string& table_name);
virtual shared_ptr<RQL_Array> table_list();
/* -------------------------------------------------------------------- */
Term term;
shared_ptr <connection> conn;
protected:
void check_response(shared_ptr<Response> response);
};
class RQL_Datum : public virtual RQL {};
class RQL_String : public RQL_Datum {
public:
RQL_String(const string& str) {
term.set_type(Term::TermType::Term_TermType_DATUM);
term.mutable_datum()->set_type(Datum::DatumType::Datum_DatumType_R_STR);
term.mutable_datum()->set_r_str(str);
}
};
class RQL_Number : public RQL_Datum {
public:
RQL_Number(double number) {
term.set_type(Term::TermType::Term_TermType_DATUM);
term.mutable_datum()->set_type(Datum::DatumType::Datum_DatumType_R_NUM);
term.mutable_datum()->set_r_num(number);
}
};
class RQL_Object : public RQL_Datum {
public:
RQL_Object() {};
RQL_Object(const string& key, const RQL_Datum& value) {
term.set_type(Term::TermType::Term_TermType_DATUM);
term.mutable_datum()->set_type(Datum::DatumType::Datum_DatumType_R_OBJECT);
Datum_AssocPair* ptr = term.mutable_datum()->add_r_object();
ptr->set_key(key);
*(ptr->mutable_val()) = value.term.datum();
}
};
class RQL_Function : public RQL {};
class RQL_Ordering : public RQL {
public:
static shared_ptr<RQL_Ordering> asc(const RQL_String& str) {
shared_ptr<RQL_Ordering> ordering(new RQL_Ordering());
ordering->term.set_type(Term::TermType::Term_TermType_ASC);
*(ordering->term.add_args()) = str.term;
return ordering;
}
static shared_ptr<RQL_Ordering> asc(const string& str) {
return asc(RQL_String(str));
}
static shared_ptr<RQL_Ordering> desc(const RQL_String& str) {
shared_ptr<RQL_Ordering> ordering(new RQL_Ordering());
ordering->term.set_type(Term::TermType::Term_TermType_DESC);
*(ordering->term.add_args()) = str.term;
return ordering;
}
static shared_ptr<RQL_Ordering> desc(const string& str) {
return desc(RQL_String(str));
}
};
class RQL_Pathspec : public RQL {};
class RQL_Sequence : public virtual RQL {
public:
shared_ptr<RQL_Sequence> filter(const RQL_Function& object) {
shared_ptr<RQL_Sequence> sequence(new RQL_Sequence());
sequence->term.set_type(Term::TermType::Term_TermType_FILTER);
*(sequence->term.add_args()) = this->term;
*(sequence->term.add_args()) = object.term;
sequence->conn = this->conn;
return sequence;
}
shared_ptr<RQL_Sequence> filter(const RQL_Object& object) {
shared_ptr<RQL_Sequence> sequence(new RQL_Sequence());
sequence->term.set_type(Term::TermType::Term_TermType_FILTER);
*(sequence->term.add_args()) = this->term;
*(sequence->term.add_args()) = object.term;
sequence->conn = this->conn;
return sequence;
}
shared_ptr<RQL_Sequence> order_by(const vector<shared_ptr<RQL_Ordering>>& orderings) {
shared_ptr<RQL_Sequence> sequence(new RQL_Sequence());
sequence->term.set_type(Term::TermType::Term_TermType_ORDERBY);
*(sequence->term.add_args()) = this->term;
for_each(orderings.begin(), orderings.end(), [sequence](shared_ptr<RQL_Ordering> ordering) {
*(sequence->term.add_args()) = ordering->term;
});
sequence->conn = this->conn;
return sequence;
}
shared_ptr<RQL_Sequence> order_by(const RQL_Ordering& ordering) {
shared_ptr<RQL_Sequence> sequence(new RQL_Sequence());
sequence->term.set_type(Term::TermType::Term_TermType_ORDERBY);
*(sequence->term.add_args()) = this->term;
*(sequence->term.add_args()) = ordering.term;
sequence->conn = this->conn;
return sequence;
}
shared_ptr<RQL_Sequence> skip(const RQL_Number& number) {
shared_ptr<RQL_Sequence> sequence(new RQL_Sequence());
sequence->term.set_type(Term::TermType::Term_TermType_SKIP);
*(sequence->term.add_args()) = this->term;
*(sequence->term.add_args()) = number.term;
sequence->conn = this->conn;
return sequence;
}
shared_ptr<RQL_Sequence> skip(size_t number) {
return skip(RQL_Number((double)number));
}
shared_ptr<RQL_Sequence> limit(const RQL_Number& number) {
shared_ptr<RQL_Sequence> sequence(new RQL_Sequence());
sequence->term.set_type(Term::TermType::Term_TermType_LIMIT);
*(sequence->term.add_args()) = this->term;
*(sequence->term.add_args()) = number.term;
sequence->conn = this->conn;
return sequence;
}
shared_ptr<RQL_Sequence> limit(size_t number) {
return limit(RQL_Number((double)number));
}
};
class RQL_Stream : public RQL_Sequence {};
class RQL_Stream_Selection : public RQL_Stream {
public:
shared_ptr<RQL_Stream_Selection> between(const RQL_Datum& d1, const RQL_Datum& d2) {
shared_ptr<RQL_Stream_Selection> selection(new RQL_Stream_Selection());
selection->term.set_type(Term::TermType::Term_TermType_BETWEEN);
*(selection->term.add_args()) = this->term;
*(selection->term.add_args()) = d1.term;
*(selection->term.add_args()) = d2.term;
selection->conn = this->conn;
return selection;
}
// "delete" is a reserved word in C++ :-(
shared_ptr<RQL_Object> remove() {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_DELETE);
*(object->term.add_args()) = this->term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Object> update(const RQL_Function& o) {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_UPDATE);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = o.term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Object> update(const RQL_Object& o) {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_UPDATE);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = o.term;
object->conn = this->conn;
return object;
}
};
class RQL_Null : public RQL_Datum {};
class RQL_Bool : public RQL_Datum {
public:
RQL_Bool(bool b) {
term.set_type(Term::TermType::Term_TermType_DATUM);
term.mutable_datum()->set_type(Datum::DatumType::Datum_DatumType_R_BOOL);
term.mutable_datum()->set_r_bool(b);
}
};
class RQL_Single_Selection : public RQL_Object{
public:
// "delete" is a reserved word in C++ :-(
shared_ptr<RQL_Object> remove() {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_DELETE);
*(object->term.add_args()) = this->term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Object> update(const RQL_Function& o) {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_UPDATE);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = o.term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Object> update(const RQL_Object& o) {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_UPDATE);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = o.term;
object->conn = this->conn;
return object;
}
};
class RQL_Table : public RQL_Stream_Selection {
public:
shared_ptr<RQL_Single_Selection> get(const RQL_String& key) {
shared_ptr<RQL_Single_Selection> object(new RQL_Single_Selection());
object->term.set_type(Term::TermType::Term_TermType_GET);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = key.term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Single_Selection> get(const string& key) {
return get(RQL_String(key));
}
shared_ptr<RQL_Single_Selection> get(const RQL_Number& key) {
shared_ptr<RQL_Single_Selection> object(new RQL_Single_Selection());
object->term.set_type(Term::TermType::Term_TermType_GET);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = key.term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Single_Selection> get(double key) {
return get(RQL_Number(key));
}
shared_ptr<RQL_Object> insert(const RQL_Object& o) {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_INSERT);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = o.term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Object> insert(const RQL_Sequence& sequence) {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_INSERT);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = sequence.term;
object->conn = this->conn;
return object;
}
};
class RQL_Array : public RQL_Datum, public RQL_Sequence {
public:
RQL_Array() {}
RQL_Array(const vector < shared_ptr < RQL_Datum >> ¶ms) {
term.set_type(Term::TermType::Term_TermType_MAKE_ARRAY);
for_each(params.begin(), params.end(), [this](shared_ptr<RQL_Datum> datum) {
*(term.add_args()) = datum->term;
});
}
};
class RQL_Database : public RQL {
public:
shared_ptr<RQL_Table> table(const RQL_String& table_name) {
shared_ptr<RQL_Table> object(new RQL_Table());
object->term.set_type(Term::TermType::Term_TermType_TABLE);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = table_name.term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Table> table(const string& table_name) {
return table(RQL_String(table_name));
}
shared_ptr<RQL_Object> table_create(const RQL_String& table_name) {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_TABLE_CREATE);
*(object->term.add_args()) = this->term;
*(object->term.add_args()) = table_name.term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Object> table_create(const string& table_name) {
return table_create(RQL_String(table_name));
}
shared_ptr<RQL_Array> table_drop(const RQL_String& table_name) {
shared_ptr<RQL_Array> array(new RQL_Array());
array->term.set_type(Term::TermType::Term_TermType_TABLE_DROP);
*(array->term.add_args()) = this->term;
*(array->term.add_args()) = table_name.term;
array->conn = this->conn;
return array;
}
shared_ptr<RQL_Array> table_drop(const string& table_name) {
return table_drop(RQL_String(table_name));
}
shared_ptr<RQL_Array> table_list() {
shared_ptr<RQL_Array> array(new RQL_Array());
array->term.set_type(Term::TermType::Term_TermType_TABLE_LIST);
*(array->term.add_args()) = this->term;
array->conn = this->conn;
return array;
}
};
}
}
}
#endif