Jpp
 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() { return false; }
40  virtual unsigned int FieldCount() { return 0; };
41  virtual std::string FieldName(unsigned int i) { return ""; }
42  virtual std::string GetString(unsigned int i) { return ""; }
43  virtual void Close() {}
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 
65 
66  /**
67  * Constructor.
68  *
69  * This constructor is used to select a table by name.
70  *
71  * \param query query / table name
72  * \param selection selection
73  */
74  JDatabaseObjectIterator(const std::string& query, const JSelector& selection) :
75  rs(query == getTable<T>() ? getResultSet(query, selection) : JNullResultSet::getInstance())
76  {}
77 
78 
79  /**
80  * Destructor.
81  */
83  {
84  //rs.Close();
85  }
86 
87 
88  /**
89  * Set object.
90  *
91  * \param object reference to object to be set
92  * \return true if set; else false
93  */
94  virtual bool setObject(T& object)
95  {
96  return (rs >> object);
97  }
98 
99  private:
100  ResultSet& rs;
101  };
102 
103 
104  /**
105  * Implementation of object iterator for multiple data types.
106  *
107  * This class recursively defines the JLANG::JObjectIterator interface
108  * for all data types by deriving from:
109  * - JDatabaseObjectIterator<JHead_t>; and
110  * - JDatabaseObjectIterator<JTail_t>.
111  */
112  template<class JHead_t, class JTail_t>
113  struct JDatabaseObjectIterator< JTypeList<JHead_t, JTail_t> > :
114  public JDatabaseObjectIterator<JHead_t>,
115  public JDatabaseObjectIterator<JTail_t>,
116  public virtual JObjectIterator< JTypeList<JHead_t, JTail_t> >
117  {
118  /**
119  * Constructor.
120  *
121  * \param query query / table name
122  * \param selection selection
123  */
124  JDatabaseObjectIterator(const std::string& query, const JSelector& selection) :
125  JDatabaseObjectIterator<JHead_t>(query, selection),
126  JDatabaseObjectIterator<JTail_t>(query, selection)
127  {}
128  };
129 
130 
131  /**
132  * Terminator class of recursive JDatabaseObjectIterator class.
133  */
134  template<class JHead_t>
136  public JDatabaseObjectIterator<JHead_t>
137  {
138  /**
139  * Constructor.
140  *
141  * \param query query / table name
142  * \param selection selection
143  */
144  JDatabaseObjectIterator(const std::string& query, const JSelector& selection) :
145  JDatabaseObjectIterator<JHead_t>(query, selection)
146  {}
147  };
148 }
149 
150 #endif
Simple singleton class.
Definition: JSingleton.hh:18
const char * getTable()
Get table name.
Definition: JDB.hh:200
virtual std::string GetString(unsigned int i)
JDatabaseObjectIterator(const std::string &query, const JSelector &selection)
Constructor.
Object iteration from database.
virtual std::string FieldName(unsigned int i)
Interface of object iteration for a single data type.
Auxiliary class for specifying selection of database data.
Auxiliary class for invalid result set.
Type list.
Definition: JTypeList.hh:22
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:75
virtual unsigned int FieldCount()
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Auxiliary class for no type definition.
Definition: JNullType.hh:19
JDatabaseObjectIterator(const std::string &query, const JSelector &selection)
Constructor.
virtual bool setObject(T &object)
Set object.
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:269
JDatabaseObjectIterator(const JSelector &selection)
Constructor.
JDatabaseObjectIterator(const std::string &query, const JSelector &selection)
Constructor.