VSQLManager_SQLite.h
Go to the documentation of this file.
1 #ifndef _det_VSQLManager_h_
2 #define _det_VSQLManager_h_
3 
4 #include <stddef.h>
5 #include <ctype.h>
6 #ifdef __GNUC__
7 # include <cxxabi.h>
8 #endif
9 #include <sstream>
10 #include <fstream>
11 
12 #include <boost/tuple/tuple.hpp>
13 #include <boost/lexical_cast.hpp>
14 
15 #include <det/VManager.h>
16 #include <utl/RealTimeStopwatch.h>
17 
18 #include <sqlite3.h>
19 
20 
21 /* Use this macro to call the generic GetDBData() method of the VSQLManager */
22 #define VSQLMANAGER_GETDATA_GETDBDATA(_T_...) \
23  VMANAGER_GETDATA_CALL(GetDBData, _T_)
24 
25 
26 namespace det {
27 
43  class VSQLManager : public det::VManager {
44 
45  public:
47  virtual ~VSQLManager() { Close(); }
48 
49  protected:
50  void Close();
51 
52  // to be modified to get file name
53  virtual void Init(const std::string& configLink);
54 
56  Status Query(const std::ostringstream& os, const std::string& what = "") const
57  { return Query(os.str(), what); }
58 
59  Status Query(const std::string& query, const std::string& what = "") const;
60 
61  Status Query(const std::ostringstream& os, const std::ostringstream& what) const
62  { return Query(os.str(), what.str()); }
63 
65  template<typename T>
66  Status FetchRow(T& data, const bool freeResult = true) const;
67 
69  template<typename T>
70  Status FetchRow(std::vector<T>& data, const bool freeResult = true) const;
71 
73  template<typename TH, typename TT>
74  Status FetchRowMany(boost::tuples::cons<TH, TT>& tuple,
75  const bool freeResult = true) const;
76 
78  template<typename T>
79  Status FetchColumn(std::vector<T>& data) const;
80 
82  template<typename T> Status FetchNextRow(T& result) const
83  { return FetchRow(result, false); }
84 
86  template<typename TH, typename TT>
87  Status FetchNextRowMany(boost::tuples::cons<TH, TT>& result) const
88  { return FetchRowMany(result, false); }
89 
90  unsigned int NumFields() const
91  { return sqlite3_column_count(fResult); }
92 
94  virtual Status GetDBResFundamental(const std::string&,
95  const std::string&,
96  const IndexMap&) const
97  { ERROR("Should not be used!"); return eNotFound; }
98 
99  template<typename T>
100  Status
101  GetDBData(T& returnData,
102  const std::string& tableName,
103  const std::string& columnName,
104  const IndexMap& componentIndex)
105  const
106  {
107  if (GetDBResFundamental(tableName, columnName, componentIndex) == eNotFound)
108  return eNotFound;
109  return FetchRow(returnData);
110  }
111 
113  virtual Status GetDBResVector(const std::string&,
114  const std::string&,
115  const IndexMap&) const
116  { ERROR("Should not be used!"); return eNotFound; }
117 
118  template<typename T>
119  Status
120  GetDBData(std::vector<T>& returnData,
121  const std::string& tableName,
122  const std::string& columnName,
123  const IndexMap& componentIndex)
124  const
125  {
126  if (GetDBResVector(tableName, columnName, componentIndex) == eNotFound)
127  return eNotFound;
128  return FetchColumn(returnData);
129  }
130 
132  int FreeResult() const;
133 
134  std::string fDatabaseSoftwareVersion;
135 
136  static std::string MergeIndexMap(const IndexMap& componentIndex);
137 
138  private:
140  Status FetchRowTuple(const boost::tuples::null_type&, const int) const
141  { return eFound; }
142 
145  template<typename TH, typename TT>
146  Status FetchRowTuple(boost::tuples::cons<TH, TT>& tuple, const int idx = 0) const;
147 
148  std::string fDatabaseName;
149  sqlite3* fDb = nullptr;
150  mutable sqlite3_stmt* fResult = nullptr;
151 
153  int FetchRow() const;
154 
155  mutable utl::RealTimeStopwatch fTime{false};
156  /* int fLogQueries; */
157  /* std::ofstream* fLogQueryStream; */
158 
159  // disable copying
160  VSQLManager(const VSQLManager&);
162 
163  };
164 
165 #include <det/VSQLManager_impl_SQLite.h>
166 
167 }
168 
169 
170 #endif
Status GetDBData(T &returnData, const std::string &tableName, const std::string &columnName, const IndexMap &componentIndex) const
virtual Status GetDBResVector(const std::string &, const std::string &, const IndexMap &) const
overload this in all derived managers that need it
Status GetDBData(std::vector< T > &returnData, const std::string &tableName, const std::string &columnName, const IndexMap &componentIndex) const
Status FetchRowTuple(const MYSQL_ROW, const boost::tuples::null_type &, const int) const
stop taking tuples
Status FetchRowMany(boost::tuples::cons< TH, TT > &tuple, const bool freeResult=true) const
Get a tuple of values from the first row of an SQLite query result.
sqlite3 * fDb
SQLite database object pointer.
Status FetchRowTuple(const boost::tuples::null_type &, const int) const
Helper function that stops the recursion of FetchRowTuple.
Status FetchNextRow(T &result) const
Fetch first item (may be a tuple) from current row.
VSQLManager & operator=(const VSQLManager &)
std::string fDatabaseSoftwareVersion
Interface for detector managers.
Definition: VManager.h:115
Status Query(const std::ostringstream &os, const std::ostringstream &what) const
virtual void Init(const std::string &configLink)
Manager Initialization. configLink is the CentralConfig hook for the configuration file...
Interface for detector managers that use MySQL.
const Data result[]
sqlite3_stmt * fResult
Storage for SQLite query results.
std::string fDatabaseName
Database filename.
Status FetchColumn(std::vector< T > &data) const
Get the first item from many rows of an SQLite query result.
MYSQL_ROW FetchRow() const
Status FetchNextRowMany(boost::tuples::cons< TH, TT > &result) const
workaround
virtual Status GetDBResFundamental(const std::string &, const std::string &, const IndexMap &) const
overload this in all derived managers that need it
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
uint16_t * data
Definition: dump1090.h:228
Status Query(const std::ostringstream &os, const std::string &what="") const
query MySQL
static std::string MergeIndexMap(const IndexMap &componentIndex)
utl::RealTimeStopwatch fTime
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127
unsigned int NumFields() const

, generated on Tue Sep 26 2023.