Jpp - 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 
33  /**
34  * Special characters.
35  */
36  static const std::string SPECIAL_CHARACTERS = "!@#$%^&*()-+=[]{};:'\"\\|,.<>/?";
37 
38 
39  /**
40  * Read ROOT object from selector.
41  *
42  * \param selection selection
43  * \param object object
44  * \return true if read; else false
45  */
46  template<class T>
47  inline bool operator>>(const std::vector<Selector>& selection, T& object)
48  {
49  using namespace std;
50  using namespace JPP;
51 
52  JRootReader reader(null, JEquationParameters(), JDBDictionary::getInstance());
53 
54  JRootReadableClass cls(object);
55 
56  for (vector<Selector>::const_iterator i = selection.begin(); i != selection.end(); ++i) {
57 
58  string parameter = to_upper(i->Name);
59 
60  for (string::const_iterator i = SPECIAL_CHARACTERS.begin(); i != SPECIAL_CHARACTERS.end(); ++i) {
61  for (string::size_type pos; (pos = parameter.find(*i)) != string::npos; ) {
62  parameter.erase(pos);
63  }
64  }
65 
66  JRootReadableClass abc = cls.find(parameter.c_str());
67 
68  if (abc.is_valid()) {
69 
70  JRedirectString redirect(reader, i->Value);
71 
72  reader.getObject(abc);
73  }
74  }
75 
76  return true;
77  }
78 
79 
80  /**
81  * Auxiliary class to read ROOT object from database.
82  *
83  * The names of the table columns are one-to-one mapped to the names of the data members
84  * of the corresponding data structure using the ROOT dictionary.\n
85  * The method JDBReader::set can be used to <em>a priori</em> assign default values
86  * to those data members that are defined by the selector used in the query.\n
87  * Note that the list of column names associated to the template parameter is considered to be permanent.\n
88  * If not, the static method JDBReader::reset should be called before the next read operation.
89  */
90  template<class T>
91  struct JDBReader :
92  public JSingleton< JDBReader<T> >
93  {
94  /**
95  * Default constructor.
96  */
98  object(),
99  reader(JLANG::null, JLANG::JEquationParameters(), JDBDictionary::getInstance())
100  {}
101 
102 
103  /**
104  * Reset internal lookup table.
105  */
106  static void reset()
107  {
108  JDBReader<T>::getInstance().buffer.clear();
109 
110  JDBReader<T>::getInstance().value = T();
111  }
112 
113 
114  /**
115  * Set default value corresponding to given selection.
116  *
117  * \param selection selection
118  */
119  static void set(const std::vector<Selector>& selection)
120  {
121  selection >> JDBReader<T>::getInstance().value;
122  }
123 
124 
125  /**
126  * Read ROOT object from result set.
127  *
128  * \param rs result set
129  * \return object
130  */
131  const T& operator()(ResultSet& rs)
132  {
133  using namespace std;
134  using namespace JPP;
135 
136  object = value;
137 
138  if (buffer.empty()) {
139 
140  JRootReadableClass cls(object);
141 
142  for (size_t i = 0; i != rs.FieldCount(); ++i) {
143 
144  string parameter = rs.FieldName(i);
145 
146  for (string::const_iterator i = SPECIAL_CHARACTERS.begin(); i != SPECIAL_CHARACTERS.end(); ++i) {
147  for (string::size_type pos; (pos = parameter.find(*i)) != string::npos; ) {
148  parameter.erase(pos);
149  }
150  }
151 
152  buffer.push_back(cls.find(parameter.c_str()));
153  }
154  }
155 
156  for (unsigned int i = 0; i != rs.FieldCount(); ++i) {
157 
158  if (buffer[i].is_valid()) {
159 
160  JRedirectString redirect(reader, rs.GetString(i));
161 
162  reader.getObject(buffer[i]);
163  }
164  }
165 
166  return object;
167  }
168 
169  private:
170  T object; //!< internal value
171  T value; //!< default value
173 
174  std::vector<JROOT::JRootReadableClass> buffer; // field count -> data member
175  };
176 
177 
178  /**
179  * Read ROOT object from result set.
180  *
181  * \param rs result set
182  * \param object object
183  * \return true if read; else false
184  */
185  template<class T>
186  inline bool operator>>(ResultSet& rs, T& object)
187  {
189 
190  if (rs.Next()) {
191 
192  object = reader(rs);
193 
194  return true;
195  }
196 
197  return false;
198  }
199 
200 
201  /**
202  * Read vector of ROOT objects from result set.
203  *
204  * \param rs result set
205  * \param buffer
206  * \return true if read; else false
207  */
208  template<class JElement_t, class JAllocator_t>
209  inline bool operator>>(ResultSet& rs, std::vector<JElement_t, JAllocator_t>& buffer)
210  {
211  for (JElement_t object; rs >> object; ) {
212  buffer.push_back(object);
213  }
214 
215  return true;
216  }
217 }
218 
219 #endif
Simple singleton class.
Definition: JSingleton.hh:18
JROOT::JRootReader reader
Definition: JDBReader.hh:172
static void reset()
Reset internal lookup table.
Definition: JDBReader.hh:106
const T & operator()(ResultSet &rs)
Read ROOT object from result set.
Definition: JDBReader.hh:131
T object
internal value
Definition: JDBReader.hh:170
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:171
do set_variable OUTPUT_DIRECTORY $WORKDIR T
bool is_valid(const json &js)
Check validity of JSon data.
std::vector< JROOT::JRootReadableClass > buffer
Definition: JDBReader.hh:174
std::string to_upper(const std::string &value)
Convert all character to upper case.
static const std::string SPECIAL_CHARACTERS
Special characters.
Definition: JDBReader.hh:36
static void set(const std::vector< Selector > &selection)
Set default value corresponding to given selection.
Definition: JDBReader.hh:119
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:97
Auxiliary class to read ROOT object from database.
Definition: JDBReader.hh:91
esac $JPP_BIN JLogger sh $LOGGER until pgrep JGetMessage</dev/null > dev null