Jpp  17.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
25  using JMATH::JMath;
27  using KM3NeT::DB::Selector;
28 
29 
30  /**
31  * Available operands.
32  */
33  static const Selector::RelOp* const OPERAND[] =
34  {
35  &Selector::RelOp::Equal,
36  &Selector::RelOp::Different,
37  &Selector::RelOp::Less,
38  &Selector::RelOp::LessEqual,
39  &Selector::RelOp::Greater,
40  &Selector::RelOp::GreaterEqual
41  };
42 
43 
44  /**
45  * Number of available operands.
46  */
47  static const int NUMBER_OF_OPERANDS = sizeof(OPERAND) / sizeof(OPERAND[0]);
48 
49 
50  /**
51  * Get operand.
52  *
53  * \param index index
54  * \return operand
55  */
56  inline const Selector::RelOp& getOperand(const int index)
57  {
58  if (index >= 0 && index <= NUMBER_OF_OPERANDS)
59  return *OPERAND[index];
60  else
61  THROW(JIndexOutOfRange, "Invalid index " << index);
62  }
63 
64 
65  /**
66  * Auxiliary class for specifying selection of database data.
67  */
68  class JSelector :
69  public std::vector<Selector>,
70  public JMath<JSelector>
71  {
72  public:
73  /**
74  * End-of-line.
75  */
76  static const char EOL = ';';
77 
78 
79  /**
80  * Default constructor.
81  */
83  {}
84 
85 
86  /**
87  * Constructor.
88  *
89  * \param key key
90  * \param value value
91  * \param operand operand
92  */
93  template<class T>
94  JSelector(const std::string& key, const T value, const Selector::RelOp& operand = Selector::RelOp::Equal)
95  {
96  add(key, value, operand);
97  }
98 
99 
100  /**
101  * Constructor.
102  *
103  * \param data_member data member
104  * \param value value
105  * \param operand operand
106  */
107  template<class JClass_t, class JType_t>
108  JSelector(JType_t JClass_t::*data_member, const JType_t value, const Selector::RelOp& operand = Selector::RelOp::Equal)
109  {
110  add(data_member, value, operand);
111  }
112 
113 
114  /**
115  * Constructor.
116  *
117  * \param data_member data member
118  * \param value value
119  * \param operand operand
120  */
121  template<class JClass_t>
122  JSelector(std::string JClass_t::*data_member, const std::string value, const Selector::RelOp& operand = Selector::RelOp::Equal)
123  {
124  add(data_member, value, operand);
125  }
126 
127 
128  /**
129  * Add selection.
130  *
131  * \param selection selection
132  * \return this selection
133  */
134  JSelector& add(const JSelector& selection)
135  {
136  using namespace std;
137 
138  copy(selection.begin(), selection.end(), back_inserter(*this));
139 
140  return *this;
141  }
142 
143 
144  /**
145  * Add selection.
146  *
147  * \param key key
148  * \param value value
149  * \param operand operand
150  * \return this selection
151  */
152  template<class T>
153  JSelector& add(const std::string& key, const T value, const Selector::RelOp& operand = Selector::RelOp::Equal)
154  {
155  using namespace std;
156 
157  ostringstream os;
158 
159  os << value;
160 
161  push_back(Selector(key.c_str(), os.str().c_str(), operand));
162 
163  return *this;
164  }
165 
166 
167  /**
168  * Add selection.
169  *
170  * \param data_member data member
171  * \param value value
172  * \param operand operand
173  * \return this selection
174  */
175  template<class JClass_t, class JType_t>
176  inline JSelector& add(JType_t JClass_t::*data_member, const JType_t value, const Selector::RelOp& operand = Selector::RelOp::Equal)
177  {
178  return add(getColumn(data_member), value, operand);
179  }
180 
181 
182  /**
183  * Add selection.
184  *
185  * \param data_member data member
186  * \param value value
187  * \param operand operand
188  * \return this selection
189  */
190  template<class JClass_t>
191  inline JSelector& add(std::string JClass_t::*data_member, const std::string& value, const Selector::RelOp& operand = Selector::RelOp::Equal)
192  {
193  return add(getColumn(data_member), value, operand);
194  }
195 
196 
197  /**
198  * Read selector from input stream.
199  *
200  * \param in input stream
201  * \param selector selector
202  * \return input stream
203  */
204  friend inline std::istream& operator>>(std::istream& in, JSelector& selector)
205  {
206  using namespace std;
207  using namespace JPP;
208 
209  for (string buffer; getline(in, buffer, JSelector::EOL); ) {
210 
211  int operand = -1;
212 
213  for (int i = 0; i != NUMBER_OF_OPERANDS; ++i) {
214 
215  string::size_type pos = buffer.find(getOperand(i).Render());
216 
217  if (pos != string::npos && (operand == -1 || getOperand(i).Render().length() > getOperand(operand).Render().length())) {
218  operand = i;
219  }
220  }
221 
222  if (operand != -1) {
223 
224  string::size_type pos = buffer.find(getOperand(operand).Render());
225 
226  selector.push_back(Selector(JPP::trim(buffer.substr(0, pos)).c_str(),
227  JPP::trim(buffer.substr(pos + getOperand(operand).Render().length())).c_str(),
228  getOperand(operand)));
229 
230  } else {
231 
232  in.setstate(ios::badbit);
233  }
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
JSelector & add(JType_t JClass_t::*data_member, const JType_t value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Add selection.
Exceptions.
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:110
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
friend std::ostream & operator<<(std::ostream &out, const JSelector &selector)
Write selector to output stream.
JSelector(JType_t JClass_t::*data_member, const JType_t value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Constructor.
Auxiliary class for specifying selection of database data.
const char * getColumn(JType_t JTable_t::*data_member)
Get column name.
Definition: JDB.hh:376
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
JSelector()
Default constructor.
JSelector & add(const JSelector &selection)
Add selection.
static const int NUMBER_OF_OPERANDS
Number of available operands.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
static const char EOL
End-of-line.
const Selector::RelOp & getOperand(const int index)
Get operand.
friend std::istream & operator>>(std::istream &in, JSelector &selector)
Read selector from input stream.
JSelector(const std::string &key, const T value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Constructor.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139
Base class for data structures with artithmetic capabilities.
JSelector(std::string JClass_t::*data_member, const std::string value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Constructor.
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
JSelector & add(std::string JClass_t::*data_member, const std::string &value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Add selection.
static const Selector::RelOp *const OPERAND[]
Available operands.
JSelector & add(const std::string &key, const T value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Add selection.