JBoss Data Grid HotRod C++ Client  7.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
QueryUtils.h
Go to the documentation of this file.
1 /*
2  * QueryUtils.h
3  *
4  * Created on: Apr 7, 2016
5  * Author: rigazilla
6  */
7 
8 #ifndef INCLUDE_INFINISPAN_HOTROD_QUERYUTILS_H_
9 #define INCLUDE_INFINISPAN_HOTROD_QUERYUTILS_H_
10 #include "query.pb.h"
11 #include <tuple>
12 using namespace org::infinispan::protostream;
13 
14 template <class T> bool unwrapResults(QueryResponse resp, std::vector<T> &res)
15 {
16  if (resp.projectionsize()>0)
17  { // Query has select
18  return false;
19  }
20  for (int i=0; i<resp.results_size(); i++)
21  {
22  const WrappedMessage &wm =resp.results(i);
23  if ( wm.has_wrappedbytes() )
24  {
25  WrappedMessage wmn;
26  wmn.ParseFromString(wm.wrappedbytes());
27  if (wmn.has_wrappedmessagebytes()) {
28  sample_bank_account::User u1;
29  u1.ParseFromString(wmn.wrappedmessagebytes());
30  res.push_back(u1);
31  }
32  }
33  }
34  return true;
35 }
36 
37 template <typename T> T unwrapSingleValue(const WrappedMessage& wm);
38 
39 template <> std::string unwrapSingleValue<std::string>(const WrappedMessage& wm)
40 {
41  if (wm.has_wrappedstring())
42  {
43  return wm.wrappedstring();
44  }
45  else
46  {
47  throw "std::string not found in response";
48  }
49 }
50 
51 template <> int unwrapSingleValue<int>(const WrappedMessage& wm)
52 {
53  if (wm.has_wrappedint32())
54  {
55  return wm.wrappedint32();
56  }
57  else if (wm.has_wrappedint64())
58  {
59  return wm.wrappedint64();
60  }
61  else
62  {
63  throw "std::string not found in response";
64  }
65 }
66 
67 template <typename T> T unwrapSingleResult(const QueryResponse &qr)
68 {
69  return unwrapSingleValue<T>(qr.results(0));
70 }
71 
72 
73 #if !defined (_MSC_VER) || (_MSC_VER>=1800)
74 template <typename H, typename... Params> std::tuple<H, Params...> popTuple(QueryResponse &resp, int &k)
75 {
76  H s = unwrapSingleValue<H>(resp.results(k++));
77  std::tuple<Params...> p=popTuple<Params... >(resp,k);
78  return std::tuple_cat(std::tie(s),p);
79 }
80 
81 template<>
82 std::tuple<std::string> popTuple<std::string>(QueryResponse & resp, int &k)
83  {
84  std::string s(unwrapSingleValue<std::string>(resp.results(k++)));
85  return std::make_tuple<std::string>(std::move(s));
86 }
87 
88 template<>
89 std::tuple<int> popTuple<int>(QueryResponse & resp, int &k)
90  {
91  int s(unwrapSingleValue<int>(resp.results(k++)));
92  return std::make_tuple<int>(std::move(s));
93 }
94 
95 
96 template<typename... Params> bool unwrapProjection(QueryResponse &resp, std::vector<std::tuple<Params...> > &prjRes)
97 {
98  if (resp.projectionsize() == 0) {
99  return false;
100  }
101  int numTuple = resp.results_size() / resp.projectionsize();
102  int k = 0;
103  for (int i = 0; i < numTuple; i++) {
104  std::tuple<Params...> tp= popTuple<Params...>(resp, k) ;
105  prjRes.push_back(tp);
106  }
107  return true;
108 }
109 #endif
110 
111 #endif /* INCLUDE_INFINISPAN_HOTROD_QUERYUTILS_H_ */
bool unwrapProjection(QueryResponse &resp, std::vector< std::tuple< Params...> > &prjRes)
Definition: QueryUtils.h:96
T unwrapSingleValue(const WrappedMessage &wm)
int unwrapSingleValue< int >(const WrappedMessage &wm)
Definition: QueryUtils.h:51
bool unwrapResults(QueryResponse resp, std::vector< T > &res)
Definition: QueryUtils.h:14
std::tuple< int > popTuple< int >(QueryResponse &resp, int &k)
Definition: QueryUtils.h:89
T unwrapSingleResult(const QueryResponse &qr)
Definition: QueryUtils.h:67
std::tuple< H, Params...> popTuple(QueryResponse &resp, int &k)
Definition: QueryUtils.h:74