Jpp
 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 "JROOT/JRootClass.hh"
13 #include "JROOT/JRootDictionary.hh"
14 #include "JROOT/JRootStreamer.hh"
15 
16 #include "JDB/JDBDictionary.hh"
17 
18 
19 
20 /**
21  * \author mdejong
22  */
23 namespace JDATABASE {}
24 namespace JPP { using namespace JDATABASE; }
25 
26 namespace JDATABASE {
27 
28  using KM3NeT::DB::ResultSet;
29 
30  /**
31  * Special characters.
32  */
33  static const std::string SPECIAL_CHARACTERS = "!@#$%^&*()-+=[]{};:'\"\\|,.<>/?";
34 
35 
36  /**
37  * Auxiliary class to read ROOT object from database.
38  *
39  * Note that the list of columns associated to the template parameter is considered to be permanent.\n
40  * If not, the method JDBReader::reset should be called before the next operation.
41  */
42  template<class T>
43  struct JDBReader {
44  /**
45  * Default constructor.
46  */
48  object(),
49  reader(JLANG::null, JLANG::JEquationParameters(), JDBDictionary::getInstance())
50  {}
51 
52 
53  /**
54  * Reset.
55  */
56  void reset()
57  {
58  buffer.clear();
59  }
60 
61 
62  /**
63  * Read ROOT object from result set.
64  *
65  * \param rs result set
66  * \return object
67  */
68  const T& operator()(ResultSet& rs)
69  {
70  using namespace std;
71  using namespace JPP;
72 
73  object = T();
74 
75  if (buffer.empty()) {
76 
77  JRootReadableClass cls(object);
78 
79  for (size_t i = 0; i != rs.FieldCount(); ++i) {
80 
81  string parameter = rs.FieldName(i);
82 
83  for (string::const_iterator i = SPECIAL_CHARACTERS.begin(); i != SPECIAL_CHARACTERS.end(); ++i) {
84  for (string::size_type pos; (pos = parameter.find(*i)) != string::npos; ) {
85  parameter.erase(pos);
86  }
87  }
88 
89  buffer.push_back(cls.find(parameter.c_str()));
90  }
91  }
92 
93  for (unsigned int i = 0; i != rs.FieldCount(); ++i) {
94 
95  if (buffer[i].is_valid()) {
96 
97  JRedirectString redirect(reader, rs.GetString(i));
98 
100  }
101  }
102 
103  return object;
104  }
105 
106  private:
109 
110  std::vector<JROOT::JRootReadableClass> buffer; // field count -> data member
111  };
112 
113 
114  /**
115  * Read ROOT object from result set.
116  *
117  * \param rs result set
118  * \param object object
119  * \return true if read; else false
120  */
121  template<class T>
122  inline bool operator>>(ResultSet& rs, T& object)
123  {
124  static JDBReader<T> reader;
125 
126  if (rs.Next()) {
127 
128  object = reader(rs);
129 
130  return true;
131  }
132 
133  return false;
134  }
135 
136 
137  /**
138  * Read vector of ROOT objects from result set.
139  *
140  * \param rs result set
141  * \param buffer
142  * \return true if read; else false
143  */
144  template<class JElement_t, class JAllocator_t>
146  {
147  for (JElement_t object; rs >> object; ) {
148  buffer.push_back(object);
149  }
150 
151  return true;
152  }
153 }
154 
155 #endif
JROOT::JRootReader reader
Definition: JDBReader.hh:108
static JRootReader & getObject(JRootReader &reader, T &object)
Read object.
void reset()
Reset.
Definition: JDBReader.hh:56
const T & operator()(ResultSet &rs)
Read ROOT object from result set.
Definition: JDBReader.hh:68
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
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:110
static const std::string SPECIAL_CHARACTERS
Special characters.
Definition: JDBReader.hh:33
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1278
JDBReader()
Default constructor.
Definition: JDBReader.hh:47
Auxiliary class to read ROOT object from database.
Definition: JDBReader.hh:43
esac $JPP_BIN JLogger sh $LOGGER until pgrep JGetMessage</dev/null > dev null