Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
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
20namespace JDATABASE {};
21namespace JPP { using namespace JDATABASE; }
22
23namespace 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 const 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 const string::size_type pos = buffer.find(getOperand(operand).Render());
225
226 const string key = JPP::trim(buffer.substr(0, pos));
227 const string value = JPP::trim(buffer.substr(pos + getOperand(operand).Render().length()));
228
229 if (key .find_first_of("=!<>") == string::npos &&
230 value.find_first_of("=!<>") == string::npos) {
231
232 selector.push_back(Selector(key.c_str(), value.c_str(), getOperand(operand)));
233
234 } else {
235
236 in.setstate(ios::badbit);
237 }
238
239 } else {
240
241 in.setstate(ios::badbit);
242 }
243 }
244
245 return in;
246 }
247
248
249 /**
250 * Write selector to output stream.
251 *
252 * \param out output stream
253 * \param selector selector
254 * \return output stream
255 */
256 friend inline std::ostream& operator<<(std::ostream& out, const JSelector& selector)
257 {
258 for (const_iterator i = selector.begin(); i != selector.end(); ++i) {
259 out << i->Name << i->RelationalOperator.Render() << i->Value << JSelector::EOL << std::endl;
260 }
261
262 return out;
263 }
264 };
265}
266
267#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Base class for data structures with artithmetic capabilities.
Auxiliary class for specifying selection of database data.
JSelector()
Default constructor.
JSelector & add(const JSelector &selection)
Add selection.
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.
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.
static const char EOL
End-of-line.
JSelector & add(JType_t JClass_t::*data_member, const JType_t value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Add selection.
JSelector & add(std::string JClass_t::*data_member, const std::string &value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Add selection.
JSelector(std::string JClass_t::*data_member, const std::string value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Constructor.
JSelector & add(const std::string &key, const T value, const Selector::RelOp &operand=Selector::RelOp::Equal)
Add selection.
Exception for accessing an index in a collection that is outside of its range.
Auxiliary classes and methods for database I/O.
Definition JAHRS.hh:14
static const int NUMBER_OF_OPERANDS
Number of available operands.
const Selector::RelOp & getOperand(const int index)
Get operand.
const char * getColumn(JType_t JTable_t::*data_member)
Get column name.
Definition JDB.hh:386
static const Selector::RelOp *const OPERAND[]
Available operands.
std::string trim(const std::string &buffer)
Trim string.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary base class for aritmetic operations of derived class types.
Definition JMath.hh:347