Jpp  15.0.1-rc.2-highQE
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
9 #include "JLang/JNullStream.hh"
11 #include "JLang/JRedirectString.hh"
12 #include "JLang/JSingleton.hh"
13 #include "JLang/JLangToolkit.hh"
14 #include "JROOT/JRootClass.hh"
15 #include "JROOT/JRootDictionary.hh"
16 #include "JROOT/JRootStreamer.hh"
17 
18 #include "JDB/JDBDictionary.hh"
19 
20 
21 /**
22  * \author mdejong
23  */
24 namespace JDATABASE {}
25 namespace JPP { using namespace JDATABASE; }
26 
27 namespace JDATABASE {
28 
29  using JLANG::JSingleton;
30  using KM3NeT::DB::ResultSet;
31  using KM3NeT::DB::Selector;
32  using JROOT::JRootReader;
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().buffer.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 (buffer.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  buffer.push_back(cls.find(parameter.c_str()));
156  }
157  }
158 
159  for (unsigned int i = 0; i != rs.FieldCount(); ++i) {
160 
161  if (buffer[i].is_valid()) {
162 
163  JRedirectString redirect(*this, rs.GetString(i));
164 
165  this->getObject(buffer[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> buffer; //!< 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
TObject * getObject(const JRootObjectID &id)
Get first TObject with given identifier.
Simple singleton class.
Definition: JSingleton.hh:18
static void reset()
Reset internal lookup table and default value.
Definition: JDBReader.hh:109
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
Implementation for ASCII input of objects with ROOT dictionary.
ASCII I/O of objects with ROOT dictionary.
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:75
T value
default value
Definition: JDBReader.hh:174
do set_variable OUTPUT_DIRECTORY $WORKDIR T
bool is_valid(const json &js)
Check validity of JSon data.
std::vector< JROOT::JRootReadableClass > buffer
field count -&gt; data member
Definition: JDBReader.hh:176
std::string to_upper(const std::string &value)
Convert all character to upper case.
JRootReader & getObject(T &object)
Read object.
static const std::string SPECIAL_CHARACTERS
Special characters.
Definition: JDBReader.hh:38
static void set(const std::vector< Selector > &selection)
Set default value corresponding to given selection.
Definition: JDBReader.hh:122
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1618
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
JDBReader()
Default constructor.
Definition: JDBReader.hh:101
Auxiliary class to read object with ROOT dictionary from database.
Definition: JDBReader.hh:94
esac $JPP_BIN JLogger sh $LOGGER until pgrep JGetMessage</dev/null > dev null