Jpp
JDB/JSelector.hh
Go to the documentation of this file.
1 #ifndef __JDB_JSELECTOR__
2 #define __JDB_JSELECTOR__
3 
4 #include <istream>
5 #include <ostream>
6 #include <string>
7 #include <vector>
8 
9 #include "JLang/JException.hh"
10 #include "JLang/JLangToolkit.hh"
11 #include "JMath/JMath.hh"
12 
13 #include "dbclient/KM3NeTDBClient.h"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace JDATABASE {};
21 namespace JPP { using namespace JDATABASE; }
22 
23 namespace JDATABASE {
24 
26  using KM3NeT::DB::Selector;
27 
28 
29  /**
30  * Available operands.
31  */
32  static const Selector::RelOp* const OPERAND[] =
33  {
34  &Selector::RelOp::Equal,
35  &Selector::RelOp::Different,
36  &Selector::RelOp::Less,
37  &Selector::RelOp::LessEqual,
38  &Selector::RelOp::Greater,
39  &Selector::RelOp::GreaterEqual
40  };
41 
42 
43  /**
44  * Number of available operands.
45  */
46  static const int NUMBER_OF_OPERANDS = sizeof(OPERAND) / sizeof(OPERAND[0]);
47 
48 
49  /**
50  * Get operand.
51  *
52  * \param index index
53  * \return operand
54  */
55  inline const Selector::RelOp& getOperand(const int index)
56  {
57  if (index >= 0 && index <= NUMBER_OF_OPERANDS)
58  return *OPERAND[index];
59  else
60  THROW(JIndexOutOfRange, "Invalid index " << index);
61  }
62 
63 
64  /**
65  * Auxiliary class for specifying selection of database data.
66  */
67  class JSelector :
68  public std::vector<Selector>,
69  public JMATH::JMath<JSelector>
70  {
71  public:
72  /**
73  * End-of-line.
74  */
75  static const char EOL = ';';
76 
77 
78  /**
79  * Default constructor.
80  */
82  {}
83 
84 
85  /**
86  * Constructor.
87  *
88  * \param key key
89  * \param value value
90  * \param operand operand
91  */
92  template<class T>
93  JSelector(const std::string& key, const T value, const Selector::RelOp& operand = Selector::RelOp::Equal)
94  {
95  add(key, value, operand);
96  }
97 
98 
99  /**
100  * Constructor.
101  *
102  * \param data_member data member
103  * \param value value
104  * \param operand operand
105  */
106  template<class JClass_t, class JType_t>
107  JSelector(JType_t JClass_t::*data_member, const JType_t value, const Selector::RelOp& operand = Selector::RelOp::Equal)
108  {
109  add(data_member, value, operand);
110  }
111 
112 
113  /**
114  * Constructor.
115  *
116  * \param data_member data member
117  * \param value value
118  * \param operand operand
119  */
120  template<class JClass_t>
121  JSelector(std::string JClass_t::*data_member, const std::string value, const Selector::RelOp& operand = Selector::RelOp::Equal)
122  {
123  add(data_member, value, operand);
124  }
125 
126 
127  /**
128  * Add selection.
129  *
130  * \param selection selection
131  * \return this selection
132  */
133  JSelector& add(const JSelector& selection)
134  {
135  using namespace std;
136 
137  copy(selection.begin(), selection.end(), back_inserter(*this));
138 
139  return *this;
140  }
141 
142 
143  /**
144  * Add selection.
145  *
146  * \param key key
147  * \param value value
148  * \param operand operand
149  * \return this selection
150  */
151  template<class T>
152  JSelector& add(const std::string& key, const T value, const Selector::RelOp& operand = Selector::RelOp::Equal)
153  {
154  using namespace std;
155 
156  ostringstream os;
157 
158  os << value;
159 
160  push_back(Selector(key.c_str(), os.str().c_str(), operand));
161 
162  return *this;
163  }
164 
165 
166  /**
167  * Add selection.
168  *
169  * \param data_member data member
170  * \param value value
171  * \param operand operand
172  * \return this selection
173  */
174  template<class JClass_t, class JType_t>
175  inline JSelector& add(JType_t JClass_t::*data_member, const JType_t value, const Selector::RelOp& operand = Selector::RelOp::Equal)
176  {
177  return add(getColumn(data_member), value, operand);
178  }
179 
180 
181  /**
182  * Add selection.
183  *
184  * \param data_member data member
185  * \param value value
186  * \param operand operand
187  * \return this selection
188  */
189  template<class JClass_t>
190  inline JSelector& add(std::string JClass_t::*data_member, const std::string& value, const Selector::RelOp& operand = Selector::RelOp::Equal)
191  {
192  return add(getColumn(data_member), value, operand);
193  }
194 
195 
196  /**
197  * Read selector from input stream.
198  *
199  * \param in input stream
200  * \param selector selector
201  * \return input stream
202  */
203  friend inline std::istream& operator>>(std::istream& in, JSelector& selector)
204  {
205  using namespace std;
206  using namespace JPP;
207 
208  string buffer;
209 
210  getline(in, buffer, JSelector::EOL);
211 
212  int operand = -1;
213 
214  for (int i = 0; i != NUMBER_OF_OPERANDS; ++i) {
215 
216  string::size_type pos = buffer.find(getOperand(i).Render());
217 
218  if (pos != string::npos && (operand == -1 || getOperand(i).Render().length() > getOperand(operand).Render().length())) {
219  operand = i;
220  }
221  }
222 
223  if (operand != -1) {
224 
225  string::size_type pos = buffer.find(getOperand(operand).Render());
226 
227  selector.push_back(Selector(JPP::trim(buffer.substr(0, pos)).c_str(),
228  JPP::trim(buffer.substr(pos + getOperand(operand).Render().length())).c_str(),
229  getOperand(operand)));
230 
231  } else {
232 
233  in.setstate(ios::badbit);
234  }
235 
236  return in;
237  }
238 
239 
240  /**
241  * Write selector to output stream.
242  *
243  * \param out output stream
244  * \param selector selector
245  * \return output stream
246  */
247  friend inline std::ostream& operator<<(std::ostream& out, const JSelector& selector)
248  {
249  for (const_iterator i = selector.begin(); i != selector.end(); ++i) {
250  out << i->Name << i->RelationalOperator.Render() << i->Value << JSelector::EOL << std::endl;
251  }
252 
253  return out;
254  }
255  };
256 }
257 
258 #endif
JException.hh
JDATABASE::JSelector::JSelector
JSelector()
Default constructor.
Definition: JDB/JSelector.hh:81
JDATABASE::JSelector::JSelector
JSelector(const std::string &key, const T value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Constructor.
Definition: JDB/JSelector.hh:93
JLANG::JIndexOutOfRange
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
JDATABASE::JSelector::JSelector
JSelector(std::string JClass_t::*data_member, const std::string value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Constructor.
Definition: JDB/JSelector.hh:121
JDATABASE::getColumn
const char * getColumn(JType_t JTable_t::*data_member)
Get column name.
Definition: JDB.hh:347
JDATABASE::getOperand
const Selector::RelOp & getOperand(const int index)
Get operand.
Definition: JDB/JSelector.hh:55
std::vector
Definition: JSTDTypes.hh:12
JDATABASE::NUMBER_OF_OPERANDS
static const int NUMBER_OF_OPERANDS
Number of available operands.
Definition: JDB/JSelector.hh:46
JDATABASE::JSelector::add
JSelector & add(std::string JClass_t::*data_member, const std::string &value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Add selection.
Definition: JDB/JSelector.hh:190
JMATH::JMath
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
JAANET::copy
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:152
JDATABASE
Auxiliary classes and methods for database I/O.
Definition: JAHRS.hh:12
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JDATABASE::JSelector::operator<<
friend std::ostream & operator<<(std::ostream &out, const JSelector &selector)
Write selector to output stream.
Definition: JDB/JSelector.hh:247
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JDATABASE::OPERAND
static const Selector::RelOp *const OPERAND[]
Available operands.
Definition: JDB/JSelector.hh:32
JDATABASE::JSelector::JSelector
JSelector(JType_t JClass_t::*data_member, const JType_t value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Constructor.
Definition: JDB/JSelector.hh:107
JDATABASE::JSelector::add
JSelector & add(JType_t JClass_t::*data_member, const JType_t value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Add selection.
Definition: JDB/JSelector.hh:175
JMath.hh
JDATABASE::JSelector::add
JSelector & add(const std::string &key, const T value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Add selection.
Definition: JDB/JSelector.hh:152
JDATABASE::JSelector::EOL
static const char EOL
End-of-line.
Definition: JDB/JSelector.hh:75
std
Definition: jaanetDictionary.h:36
JDATABASE::JSelector::add
JSelector & add(const JSelector &selection)
Add selection.
Definition: JDB/JSelector.hh:133
JDATABASE::JSelector
Auxiliary class for specifying selection of database data.
Definition: JDB/JSelector.hh:67
JLANG::trim
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
JDATABASE::JSelector::operator>>
friend std::istream & operator>>(std::istream &in, JSelector &selector)
Read selector from input stream.
Definition: JDB/JSelector.hh:203
JLANG::getline
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
JLangToolkit.hh