Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
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
20namespace JDATABASE {}
21namespace JPP { using namespace JDATABASE; }
22
23namespace JDATABASE {
24
27 using JLANG::JTypeList;
28 using JLANG::JNullType;
30 using KM3NeT::DB::ResultSet;
31
32 /**
33 * Auxiliary class for invalid result set.
34 */
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>
54 {
55 public:
56 /**
57 * Constructor.
58 *
59 * \param selection selection
60 */
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
Object iteration from database.
JDatabaseObjectIterator(const std::string &query, const JSelector &selection)
Constructor.
JDatabaseObjectIterator(const JSelector &selection)
Constructor.
virtual bool setObject(T &object) override
Set object.
Auxiliary class for specifying selection of database data.
Interface of object iteration for a single data type.
Auxiliary classes and methods for database I/O.
Definition JAHRS.hh:14
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition JDB.hh:438
const char * getTable()
Get table name.
Definition JDB.hh:373
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static void set(const std::vector< Selector > &selection)
Set default value corresponding to given selection.
Definition JDBReader.hh:122
JDatabaseObjectIterator(const std::string &query, const JSelector &selection)
Constructor.
JDatabaseObjectIterator(const std::string &query, const JSelector &selection)
Constructor.
Auxiliary class for invalid result set.
virtual std::string & GetString(unsigned int i) const override
virtual std::string & FieldName(unsigned int i) const override
virtual unsigned int FieldCount() const override
Auxiliary class for no type definition.
Definition JNullType.hh:19
Simple singleton class.
Definition JSingleton.hh:18
Type list.
Definition JTypeList.hh:23