-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrql.cpp
149 lines (115 loc) · 4.39 KB
/
rql.cpp
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
#include "exception.hpp"
#include "connection.hpp"
#include "rql.hpp"
namespace com {
namespace rethinkdb {
namespace driver {
vector<shared_ptr<Response>> RQL::run() {
conn->connect();
// TODO - optargs?
vector<shared_ptr<Response>> responses = vector<shared_ptr<Response>>();
shared_ptr<Response> response;
Query query;
query.set_type(Query::QueryType::Query_QueryType_START);
// generate random token
query.set_token(rand());
*(query.mutable_query()) = term;
// debugging
//query.PrintDebugString();
// write query
conn->write_query(query);
// read response
response = conn->read_response();
this->check_response(response);
// if this point is reached, response is fine
responses.push_back(response);
query.set_type(Query::QueryType::Query_QueryType_CONTINUE);
query.clear_query();
while (response->type() == Response::ResponseType::Response_ResponseType_SUCCESS_PARTIAL) {
// write query
conn->write_query(query);
// read response
response = conn->read_response();
this->check_response(response);
responses.push_back(response);
}
return responses;
}
/* -------------------------------------------------------------------- */
void RQL::check_response(shared_ptr<Response> response) {
switch (response->type()) {
case Response::ResponseType::Response_ResponseType_RUNTIME_ERROR:
throw runtime_error_exception("\n\nResponse received:\n" + response->DebugString());
break;
case Response::ResponseType::Response_ResponseType_CLIENT_ERROR:
throw client_error_exception("\n\nResponse received:\n" + response->DebugString());
break;
case Response::ResponseType::Response_ResponseType_COMPILE_ERROR:
throw compile_error_exception("\n\nResponse received:\n" + response->DebugString());
break;
default:
break;
}
}
/* -------------------------------------------------------------------- */
shared_ptr<RQL_Database> RQL::db(const RQL_String& db_name) {
shared_ptr<RQL_Database> object(new RQL_Database());
object->term.set_type(Term::TermType::Term_TermType_DB);
*(object->term.add_args()) = db_name.term;
object->conn = this->conn;
return object;
};
shared_ptr<RQL_Database> RQL::db(const string& db_name) {
return db(RQL_String(db_name));
};
shared_ptr<RQL_Array> RQL::db_list() {
shared_ptr<RQL_Array> array(new RQL_Array());
array->term.set_type(Term::TermType::Term_TermType_DB_LIST);
array->conn = this->conn;
return array;
}
shared_ptr<RQL_Object> RQL::db_create(const RQL_String& db_name) {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_DB_CREATE);
*(object->term.add_args()) = db_name.term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Object> RQL::db_create(const string& db_name) {
return db_create(RQL_String(RQL_String(db_name)));
}
shared_ptr<RQL_Object> RQL::db_drop(const RQL_String& db_name) {
shared_ptr<RQL_Object> object(new RQL_Object());
object->term.set_type(Term::TermType::Term_TermType_DB_DROP);
*(object->term.add_args()) = db_name.term;
object->conn = this->conn;
return object;
}
shared_ptr<RQL_Object> RQL::db_drop(const string& db_name) {
return db_drop(RQL_String(RQL_String(db_name)));
}
/* -------------------------------------------------------------------- */
shared_ptr<RQL_Table> RQL::table(const RQL_String& table_name) {
return db(this->conn->database)->table(table_name);
}
shared_ptr<RQL_Table> RQL::table(const string& table_name) {
return db(this->conn->database)->table(RQL_String(table_name));
}
shared_ptr<RQL_Object> RQL::table_create(const RQL_String& table_name) {
return db(this->conn->database)->table_create(table_name);
}
shared_ptr<RQL_Object> RQL::table_create(const string& table_name) {
return db(this->conn->database)->table_create(RQL_String(table_name));
}
shared_ptr<RQL_Array> RQL::table_drop(const RQL_String& table_name) {
return db(this->conn->database)->table_drop(table_name);
}
shared_ptr<RQL_Array> RQL::table_drop(const string& table_name) {
return db(this->conn->database)->table_drop(RQL_String(table_name));
}
shared_ptr<RQL_Array> RQL::table_list() {
return db(this->conn->database)->table_list();
}
}
}
}