VSQLManager_impl_SQLite.h
Go to the documentation of this file.
1 #ifndef _det_VSQLManager_impl_h_
2 #define _det_VSQLManager_impl_h_
3 
4 #include <iostream>
5 
6 using namespace std;
7 
8 template<typename T>
10 VSQLManager::FetchRow(T& data, const bool freeResult)
11  const
12 {
13  if (fResult && (FetchRow() == SQLITE_ROW)) {
14  if (sqlite3_column_count(fResult) >= 1) {
15 
16  // NULL entry should be interpreted as result not found
17  if (sqlite3_column_type(fResult, 0) == SQLITE_NULL) {
18  if (freeResult)
19  FreeResult();
20  return eNotFound;
21  }
22  try {
23  data = boost::lexical_cast<T>(sqlite3_column_text(fResult, 0));
24  }
25  catch (const boost::bad_lexical_cast& e) {
26  ERROR(e.what());
27  }
28  }
29  if (freeResult)
30  FreeResult();
31 
32  return eFound;
33  }
34  FreeResult();
35  return eNotFound;
36 }
37 
38 template<typename T>
40 VSQLManager::FetchRow(std::vector<T>& data, const bool freeResult)
41  const
42 {
43  // FetchRow returns SQLITE_DONE when no more result
44  if ( fResult && (FetchRow() == SQLITE_ROW) ) {
45  data.clear();
46  for (int i = 0; i < sqlite3_column_count(fResult); ++i) {
47  //in case of NULL column-count will be 1 but column_text returns null-pointer, which causes seg-fault in cast below
48  if ( sqlite3_column_bytes(fResult, i) ) {
49  try {
50  data.push_back(
51  boost::lexical_cast<T>(sqlite3_column_text(fResult, i)));
52  }
53  catch (const boost::bad_lexical_cast& e) {
54  ERROR(e.what());
55  }
56  }
57  }
58  if (freeResult)
59  FreeResult();
60 
61  if (data.size()) {
62  return eFound;
63  } else {
64  return eNotFound;
65  }
66  }
67 
68  FreeResult();
69  return eNotFound;
70 }
71 
72 template<typename TH, typename TT>
74 VSQLManager::FetchRowTuple(boost::tuples::cons<TH, TT>& tuple, const int idx)
75  const
76 {
77  if (idx < sqlite3_column_count(fResult)) {
78  try {
79  tuple.get_head() = boost::lexical_cast<TH>(
80  sqlite3_column_text(fResult, idx));
81  }
82  catch (const boost::bad_lexical_cast& e) {
83  ERROR(e.what());
84  }
85  return FetchRowTuple(tuple.get_tail(), idx + 1);
86  }
87  else {
88  return eNotFound;
89  }
90 }
91 
92 template<typename TH, typename TT>
94 VSQLManager::FetchRowMany(boost::tuples::cons<TH, TT>& tuple, const bool freeResult)
95  const
96 {
97  if (fResult && (FetchRow() == SQLITE_ROW)) {
98  const Status status = FetchRowTuple(tuple);
99  if (status == eNotFound || freeResult)
100  FreeResult();
101 
102  return status;
103  }
104  FreeResult();
105  return eNotFound;
106 }
107 
108 template<typename T>
110 VSQLManager::FetchColumn(std::vector<T>& data)
111  const
112 {
113  if (fResult) {
114  data.clear();
115  while (FetchRow() == SQLITE_ROW) {
116  if (sqlite3_column_count(fResult) >= 1) {
117  try {
118  data.push_back(
119  boost::lexical_cast<T>(sqlite3_column_text(fResult, 0)));
120  }
121  catch (const boost::bad_lexical_cast& e) {
122  ERROR(e.what());
123  }
124  }
125  }
126  FreeResult();
127  return eFound;
128  }
129  FreeResult();
130  return eNotFound;
131 }
132 
133 #endif // _det_VSQLManager_impl_h_
134 
Status
Return code for seek operation.
Definition: IoCodes.h:24
uint16_t * data
Definition: dump1090.h:228
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127

, generated on Tue Sep 26 2023.