Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JDBReader.hh
Go to the documentation of this file.
1#ifndef __JDB_JDBREADER__
2#define __JDB_JDBREADER__
3
4#include <string>
5#include <vector>
6
7#include "dbclient/KM3NeTDBClient.h"
8
12#include "JLang/JSingleton.hh"
13#include "JLang/JLangToolkit.hh"
14#include "JROOT/JRootClass.hh"
17
18#include "JDB/JDBDictionary.hh"
19
20
21/**
22 * \author mdejong
23 */
24namespace JDATABASE {}
25namespace JPP { using namespace JDATABASE; }
26
27namespace JDATABASE {
28
30 using KM3NeT::DB::ResultSet;
31 using KM3NeT::DB::Selector;
33
34
35 /**
36 * Special characters.
37 */
38 static const std::string SPECIAL_CHARACTERS = "!@#$%^&*()-+=[]{};:'\"\\|,.<>/?";
39
40
41 /**
42 * Read object with ROOT dictionary from selector.
43 *
44 * \param selection selection
45 * \param object object
46 * \return true if read; else false
47 */
48 template<class T>
49 inline bool operator>>(const std::vector<Selector>& selection, T& object)
50 {
51 using namespace std;
52 using namespace JPP;
53
54 JRootReader reader(null, JEquationParameters(), JDBDictionary::getInstance());
55
56 JRootReadableClass cls(object);
57
58 for (vector<Selector>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
59
60 string parameter = to_upper(i->Name);
61
62 for (string::const_iterator i = SPECIAL_CHARACTERS.begin(); i != SPECIAL_CHARACTERS.end(); ++i) {
63 for (string::size_type pos; (pos = parameter.find(*i)) != string::npos; ) {
64 parameter.erase(pos);
65 }
66 }
67
68 JRootReadableClass abc = cls.find(parameter.c_str());
69
70 if (abc.is_valid()) {
71
72 JRedirectString redirect(reader, i->Value);
73
74 reader.getObject(abc);
75 }
76 }
77
78 return true;
79 }
80
81
82 /**
83 * Auxiliary class to read object with ROOT dictionary from database.
84 *
85 * The names of the table columns are one-to-one mapped to the names of the data members
86 * of the corresponding data structure using the corresponding ROOT dictionary.\n
87 * For this, all special characters (JDATABASE::SPECIAL_CHARACTERS) are removed from the column names.\n
88 * The static method JDBReader::set can be used to <em>a priori</em> assign default values
89 * to those data members that are defined by the selector used in the query.\n
90 * Note that the list of column names associated to the template parameter is considered to be permanent.\n
91 * If not, the static method JDBReader::reset should be called before the next read operation.
92 */
93 template<class T>
94 struct JDBReader :
95 public JRootReader,
96 public JSingleton< JDBReader<T> >
97 {
98 /**
99 * Default constructor.
100 */
102 JRootReader(JLANG::null, JLANG::JEquationParameters(), JDBDictionary::getInstance())
103 {}
104
105
106 /**
107 * Reset internal lookup table and default value.
108 */
109 static void reset()
110 {
111 JDBReader<T>::getInstance().columns.clear();
112
113 JDBReader<T>::getInstance().value = T();
114 }
115
116
117 /**
118 * Set default value corresponding to given selection.
119 *
120 * \param selection selection
121 */
122 static void set(const std::vector<Selector>& selection)
123 {
124 selection >> JDBReader<T>::getInstance().value;
125 }
126
127
128 /**
129 * Read object with ROOT dictionary from result set.
130 *
131 * \param rs result set
132 * \return object
133 */
134 const T& operator()(ResultSet& rs)
135 {
136 using namespace std;
137 using namespace JPP;
138
139 object = value;
140
141 if (columns.empty()) {
142
143 JRootReadableClass cls(object);
144
145 for (size_t i = 0; i != rs.FieldCount(); ++i) {
146
147 string parameter = rs.FieldName(i);
148
149 for (string::const_iterator i = SPECIAL_CHARACTERS.begin(); i != SPECIAL_CHARACTERS.end(); ++i) {
150 for (string::size_type pos; (pos = parameter.find(*i)) != string::npos; ) {
151 parameter.erase(pos);
152 }
153 }
154
155 columns.push_back(cls.find(parameter.c_str()));
156 }
157 }
158
159 for (unsigned int i = 0; i != rs.FieldCount(); ++i) {
160
161 if (columns[i].is_valid()) {
162
163 JRedirectString redirect(*this, rs.GetString(i));
164
165 this->getObject(columns[i]);
166 }
167 }
168
169 return object;
170 }
171
172 private:
173 T object; //!< internal value
174 T value; //!< default value
175
176 std::vector<JROOT::JRootReadableClass> columns; //!< field count -> data member
177 };
178
179
180 /**
181 * Read object with ROOT dictionary from result set.
182 *
183 * \param rs result set
184 * \param object object
185 * \return true if read; else false
186 */
187 template<class T>
188 inline bool operator>>(ResultSet& rs, T& object)
189 {
191
192 if (rs.Next()) {
193
194 object = reader(rs);
195
196 return true;
197 }
198
199 return false;
200 }
201
202
203 /**
204 * Read multiple objects with ROOT dictionary from result set.
205 *
206 * \param rs result set
207 * \param buffer container of objects with ROOT dictionary
208 * \return true if read; else false
209 */
210 template<class JElement_t, class JAllocator_t>
211 inline bool operator>>(ResultSet& rs, std::vector<JElement_t, JAllocator_t>& buffer)
212 {
213 for (JElement_t object; rs >> object; ) {
214 buffer.push_back(object);
215 }
216
217 return true;
218 }
219}
220
221#endif
ASCII I/O of objects with ROOT dictionary.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
This class can be used to temporarily redirect an input stream to an input string.
Implementation for ASCII input of objects with ROOT dictionary.
JRootReader & getObject(T &object)
Read object.
Auxiliary classes and methods for database I/O.
Definition JAHRS.hh:14
std::istream & operator>>(std::istream &in, JAHRSCalibration &calibration)
Read AHRS calibration from input stream.
static const std::string SPECIAL_CHARACTERS
Special characters.
Definition JDBReader.hh:38
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class to read object with ROOT dictionary from database.
Definition JDBReader.hh:97
const T & operator()(ResultSet &rs)
Read object with ROOT dictionary from result set.
Definition JDBReader.hh:134
T object
internal value
Definition JDBReader.hh:173
JDBReader()
Default constructor.
Definition JDBReader.hh:101
static void set(const std::vector< Selector > &selection)
Set default value corresponding to given selection.
Definition JDBReader.hh:122
T value
default value
Definition JDBReader.hh:174
std::vector< JROOT::JRootReadableClass > columns
field count -> data member
Definition JDBReader.hh:176
static void reset()
Reset internal lookup table and default value.
Definition JDBReader.hh:109
Simple singleton class.
Definition JSingleton.hh:18
static data_type & getInstance()
Get unique instance of template class.
Definition JSingleton.hh:27
bool is_valid() const
Check validity of this addressable class.
JRootAddressableClass find(const char *const name) const
Find addressable base class or data member with given name within current class.
ROOT class for reading into object.