forked from irods/irods_resource_plugin_s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irods_query.hpp
426 lines (363 loc) · 13.4 KB
/
irods_query.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#ifndef IRODS_QUERY_HPP
#define IRODS_QUERY_HPP
#ifdef RODS_SERVER
#include "rsGenQuery.hpp"
#include "specificQuery.h"
#include "rsSpecificQuery.hpp"
#else
#include "genQuery.h"
#include "specificQuery.h"
#endif
#include "rcMisc.h"
#include <algorithm>
char *getCondFromString( char * t );
namespace irods {
namespace query_helper {
#ifdef RODS_SERVER
typedef rsComm_t comm_type;
static const std::function<
int(rsComm_t*,
genQueryInp_t*,
genQueryOut_t**)>
gen_query_fcn{rsGenQuery};
static const std::function<
int(rsComm_t*,
specificQueryInp_t *specificQueryInp,
genQueryOut_t **)>
spec_query_fcn{rsSpecificQuery};
#else
typedef rcComm_t comm_type;
static const std::function<
int(rcComm_t*,
genQueryInp_t*,
genQueryOut_t**)>
gen_query_fcn{rcGenQuery};
static const std::function<
int(rcComm_t*,
specificQueryInp_t *specificQueryInp,
genQueryOut_t **)>
spec_query_fcn{rcSpecificQuery};
#endif
};
class query {
public:
typedef std::vector<std::string> value_type;
enum query_type {
GENERAL = 0,
SPECIFIC = 1
};
static query_type convert_string_to_query_type(
const std::string& _str) {
// default option
if(_str.empty()) {
return GENERAL;
}
const std::string GEN_STR{"general"};
const std::string SPEC_STR{"specific"};
std::string lowered{_str};
std::transform(
lowered.begin(),
lowered.end(),
lowered.begin(),
::tolower);
if(GEN_STR == lowered) {
return GENERAL;
}
else if(SPEC_STR == lowered) {
return SPECIFIC;
}
else {
THROW(
SYS_INVALID_INPUT_PARAM,
_str + " - is not a query type");
}
} // convert_string_to_query_type
class query_impl_base {
public:
size_t size() {
if(!gen_output_) {
return 0;
}
return gen_output_->rowCnt;
}
int cont_idx() {
return gen_output_->continueInx;
}
int row_cnt() {
return gen_output_->rowCnt;
}
std::string query_string() {
return query_string_;
}
bool page_in_flight(const int row_idx_) {
if(row_idx_ < row_cnt()) {
return true;
}
return false;
}
bool query_complete() {
// finished page, and out of pages
if(cont_idx() <= 0) {
return true;
}
return false;
}
value_type capture_results(int _row_idx) {
value_type res;
for(int attr_idx = 0; attr_idx < gen_output_->attriCnt; ++attr_idx) {
uint32_t offset = gen_output_->sqlResult[attr_idx].len * _row_idx;
std::string str{&gen_output_->sqlResult[attr_idx].value[offset]};
res.push_back(str);
}
return res;
}
bool results_valid() {
if(gen_output_) {
return (gen_output_->rowCnt > 0);
}
else {
return false;
}
}
virtual int fetch_page() = 0;
virtual ~query_impl_base() {
freeGenQueryOut(&gen_output_);
}
query_impl_base(
query_helper::comm_type* _comm,
const std::string& _q) :
comm_{_comm},
query_string_{_q},
gen_output_{} {
};
protected:
query_helper::comm_type* comm_;
const std::string& query_string_;
genQueryOut_t* gen_output_;
}; // class query_impl_base
class gen_query_impl : public query_impl_base {
public:
virtual ~gen_query_impl() {
}
int fetch_page() {
if(gen_output_) {
gen_input_.continueInx = gen_output_->continueInx;
freeGenQueryOut(&gen_output_);
}
int ret = query_helper::gen_query_fcn(
comm_,
&gen_input_,
&gen_output_);
return ret;
} // fetch_page
gen_query_impl(
query_helper::comm_type* _comm,
int _max_rows,
const std::string& _query_string) :
query_impl_base(_comm, _query_string) {
memset(&gen_input_, 0, sizeof(gen_input_));
gen_input_.maxRows = _max_rows;
const int fill_err = fillGenQueryInpFromStrCond(
const_cast<char*>(_query_string.c_str()),
&gen_input_);
if(fill_err < 0) {
THROW(
fill_err,
boost::format("query fill failed for [%s]") %
_query_string);
}
} // ctor
private:
genQueryInp_t gen_input_;
}; // class gen_query_impl
class spec_query_impl : public query_impl_base {
public:
virtual ~spec_query_impl() {
}
int fetch_page() {
if(gen_output_) {
spec_input_.continueInx = gen_output_->continueInx;
freeGenQueryOut(&gen_output_);
}
int ret = query_helper::spec_query_fcn(
comm_,
&spec_input_,
&gen_output_);
return ret;
} // fetch_page
spec_query_impl(
query_helper::comm_type* _comm,
int _max_rows,
const std::string& _query_string) :
query_impl_base(_comm, _query_string) {
memset(&spec_input_, 0, sizeof(spec_input_));
spec_input_.maxRows = _max_rows;
spec_input_.sql = const_cast<char*>(_query_string.c_str());
int spec_err = query_helper::spec_query_fcn(
_comm,
&spec_input_,
&gen_output_);
if(spec_err < 0) {
THROW(
spec_err,
boost::format("query fill failed for [%s]") %
_query_string);
}
} // ctor
private:
specificQueryInp_t spec_input_;
}; // class spec_query_impl
class iterator: public std::iterator<
std::forward_iterator_tag,// iterator_category
value_type, // value_type
value_type, // difference_type
const value_type*, // pointer
value_type> { // reference
query_helper::comm_type* comm_;
const std::string query_string_;
uintmax_t max_rows_;//const uintmax_t max_rows_;
uint32_t row_idx_;
genQueryInp_t* gen_input_;
genQueryOut_t* gen_output_;
bool end_iteration_state_;
std::shared_ptr<query_impl_base> query_impl_;
public:
explicit iterator () :
comm_{},
query_string_{},
max_rows_{},
row_idx_{},
gen_input_{},
gen_output_{},
end_iteration_state_{true} {
}
explicit iterator(
std::shared_ptr<query_impl_base> _qimp) :
comm_{},
query_string_{},
max_rows_{},
row_idx_{},
gen_input_{},
gen_output_{},
end_iteration_state_{false},
query_impl_(_qimp) {
}
explicit iterator(
query_helper::comm_type* _comm,
const std::string& _query_string,
const uintmax_t& _max_rows,
genQueryInp_t* _gen_input,
genQueryOut_t* _gen_output) :
comm_{_comm},
query_string_{_query_string},
max_rows_{_max_rows},
row_idx_{},
gen_input_{_gen_input},
gen_output_{_gen_output},
end_iteration_state_{false} {
} // ctor
iterator operator++() {
advance_query();
return *this;
}
iterator operator++(int) {
iterator ret = *this;
++(*this);
return ret;
}
bool operator==(const iterator& _rhs) const {
if(end_iteration_state_ && _rhs.end_iteration_state_) {
return true;
}
//return (query_string_ == _rhs.query_string_);
return (query_impl_->query_string() == _rhs.query_string_);
}
bool operator!=(const iterator& _rhs) const {
bool val = !(*this == _rhs);
return val;
}
value_type operator*() {
return capture_results();
}
void advance_query() {
row_idx_++;
if(query_impl_->page_in_flight(row_idx_)) {
return;
}
if(query_impl_->query_complete()) {
end_iteration_state_ = true;
return;
}
const int query_err = query_impl_->fetch_page();
if(query_err < 0) {
if(CAT_NO_ROWS_FOUND != query_err) {
THROW(
query_err,
boost::format("gen query failed for [%s] on idx %d") %
query_string_ %
gen_input_->continueInx);
}
end_iteration_state_ = true;
} // if
} // advance_query
value_type capture_results() {
return query_impl_->capture_results(row_idx_);
}
}; // class iterator
explicit query(
query_helper::comm_type* _comm,
const std::string& _query_string,
uintmax_t _max_rows = MAX_SQL_ROWS,
query_type _query_type = GENERAL) {
if(_query_type == GENERAL) {
query_impl_ = std::make_shared<gen_query_impl>(
_comm,
_max_rows,
_query_string);
}
else if(_query_type == SPECIFIC) {
query_impl_ = std::make_shared<spec_query_impl>(
_comm,
_max_rows,
_query_string);
}
const int fetch_err = query_impl_->fetch_page();
if(fetch_err < 0) {
if(CAT_NO_ROWS_FOUND == fetch_err) {
iter_ = std::make_unique<iterator>();
}
else {
THROW(
fetch_err,
boost::format("query failed for [%s] type [%d]") %
_query_string %
_query_type);
}
}
if(query_impl_->results_valid()) {
iter_ = std::make_unique<iterator>(query_impl_);
}
else {
iter_ = std::make_unique<iterator>();
}
} // ctor
~query() {
}
iterator begin() {
return *iter_;
}
iterator end() {
return iterator();
}
value_type front() {
return (*(*iter_));
}
size_t size() {
return query_impl_->size();
}
private:
std::unique_ptr<iterator> iter_;
std::shared_ptr<query_impl_base> query_impl_;
}; // class query
} // namespace irods
#endif // IRODS_QUERY_HPP