Jpp  17.3.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDatabaseObjectIterator.hh
Go to the documentation of this file.
1 #ifndef __JDB__JDATABASEOBJECTITERATOR__
2 #define __JDB__JDATABASEOBJECTITERATOR__
3 
4 #include <string>
5 
7 #include "JLang/JTypeList.hh"
8 #include "JLang/JSingleton.hh"
9 
10 #include "JDB/JDB.hh"
11 #include "JDB/JSelector.hh"
12 
13 #include "dbclient/KM3NeTDBClient.h"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace JDATABASE {}
21 namespace JPP { using namespace JDATABASE; }
22 
23 namespace JDATABASE {
24 
27  using JLANG::JTypeList;
28  using JLANG::JNullType;
29  using JLANG::JSingleton;
30  using KM3NeT::DB::ResultSet;
31 
32  /**
33  * Auxiliary class for invalid result set.
34  */
35  struct JNullResultSet :
36  public ResultSet,
37  public JSingleton<JNullResultSet>
38  {
39  virtual bool Next() override { return false; }
40  virtual unsigned int FieldCount() const override { return 0; }
41  virtual std::string& FieldName(unsigned int i) const override { static std::string buffer; return buffer; }
42  virtual std::string& GetString(unsigned int i) const override { static std::string buffer; return buffer; }
43  virtual void Close() override {}
44  };
45 
46 
47  /**
48  * Object iteration from database.
49  * This class implements the JLANG::JObjectIterator interface.
50  */
51  template<class T>
53  public JAbstractObjectIterator<T>
54  {
55  public:
56  /**
57  * Constructor.
58  *
59  * \param selection selection
60  */
61  JDatabaseObjectIterator(const JSelector& selection) :
62  rs(getResultSet(getTable<T>(), selection))
63  {
64  JDBReader<T>::set(selection);
65  }
66 
67 
68  /**
69  * Constructor.
70  *
71  * This constructor is used to select a table by name.
72  *
73  * \param query query / table name
74  * \param selection selection
75  */
76  JDatabaseObjectIterator(const std::string& query, const JSelector& selection) :
77  rs(query == getTable<T>() ? getResultSet(query, selection) : JNullResultSet::getInstance())
78  {
79  JDBReader<T>::set(selection);
80  }
81 
82 
83  /**
84  * Destructor.
85  */
87  {
88  //rs.Close();
89  }
90 
91 
92  /**
93  * Set object.
94  *
95  * \param object reference to object to be set
96  * \return true if set; else false
97  */
98  virtual bool setObject(T& object) override
99  {
100  return (rs >> object);
101  }
102 
103  private:
104  ResultSet& rs;
105  };
106 
107 
108  /**
109  * Implementation of object iterator for multiple data types.
110  *
111  * This class recursively defines the JLANG::JObjectIterator interface
112  * for all data types by deriving from:
113  * - JDatabaseObjectIterator<JHead_t>; and
114  * - JDatabaseObjectIterator<JTail_t>.
115  */
116  template<class JHead_t, class JTail_t>
117  struct JDatabaseObjectIterator< JTypeList<JHead_t, JTail_t> > :
118  public JDatabaseObjectIterator<JHead_t>,
119  public JDatabaseObjectIterator<JTail_t>,
120  public virtual JObjectIterator< JTypeList<JHead_t, JTail_t> >
121  {
122  /**
123  * Constructor.
124  *
125  * \param query query / table name
126  * \param selection selection
127  */
128  JDatabaseObjectIterator(const std::string& query, const JSelector& selection) :
129  JDatabaseObjectIterator<JHead_t>(query, selection),
130  JDatabaseObjectIterator<JTail_t>(query, selection)
131  {}
132  };
133 
134 
135  /**
136  * Terminator class of recursive JDatabaseObjectIterator class.
137  */
138  template<class JHead_t>
140  public JDatabaseObjectIterator<JHead_t>
141  {
142  /**
143  * Constructor.
144  *
145  * \param query query / table name
146  * \param selection selection
147  */
148  JDatabaseObjectIterator(const std::string& query, const JSelector& selection) :
149  JDatabaseObjectIterator<JHead_t>(query, selection)
150  {}
151  };
152 }
153 
154 #endif
Simple singleton class.
Definition: JSingleton.hh:18
const char * getTable()
Get table name.
Definition: JDB.hh:366
virtual std::string & FieldName(unsigned int i) const override
JDatabaseObjectIterator(const std::string &query, const JSelector &selection)
Constructor.
Object iteration from database.
virtual std::string & GetString(unsigned int i) const override
Interface of object iteration for a single data type.
Auxiliary class for specifying selection of database data.
Auxiliary class for invalid result set.
virtual bool setObject(T &object) override
Set object.
Type list.
Definition: JTypeList.hh:22
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:75
virtual bool Next() override
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Auxiliary class for no type definition.
Definition: JNullType.hh:19
then awk string
JDatabaseObjectIterator(const std::string &query, const JSelector &selection)
Constructor.
static void set(const std::vector< Selector > &selection)
Set default value corresponding to given selection.
Definition: JDBReader.hh:122
virtual void Close() override
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:431
JDatabaseObjectIterator(const JSelector &selection)
Constructor.
virtual unsigned int FieldCount() const override
JDatabaseObjectIterator(const std::string &query, const JSelector &selection)
Constructor.