MIRA
SQLiteQuery.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_SQLITEQUERY_H_
48 #define _MIRA_SQLITEQUERY_H_
49 
50 #include <utils/ToString.h>
51 #include <serialization/adapters/std/pair>
52 #include <serialization/adapters/std/vector>
53 
54 struct sqlite3;
55 struct sqlite3_stmt;
56 
57 namespace mira {
58 
60 
67 {
68 public:
71 
73  SQLiteQuery();
74 
81  SQLiteQuery(sqlite3* db, sqlite3_stmt* statement, bool more);
82 
83  // no copy as copying statement would be illegal
84  SQLiteQuery(const SQLiteQuery& other) = delete;
85  SQLiteQuery& operator=(const SQLiteQuery& other) = delete;
86 
87  // move, moves statement
88  SQLiteQuery(SQLiteQuery&& other) noexcept;
89  SQLiteQuery& operator=(SQLiteQuery&& other) noexcept;
90 
92  ~SQLiteQuery();
93 
95 
96 public:
99 
101  bool valid() const
102  {
103  return mStatement != NULL;
104  }
105 
110  bool eof() const
111  {
112  return mMore == false;
113  }
114 
119  int getColumnCount() const
120  {
121  return mColumnCount;
122  }
123 
129  std::string getName(std::size_t column);
130 
137  template<typename T>
138  T getValue(std::size_t column)
139  {
140  const char* data = getTxtValue(column);
141  if ( data == NULL )
142  MIRA_THROW(XBadCast, "Value of column '" << column << "' is NULL");
143  return fromString<T>(data);
144  }
145 
153  template<typename T>
154  T getValue(std::size_t column, const T& defaultValue)
155  {
156  const char* data = getTxtValue(column);
157  if ( data == NULL )
158  return defaultValue;
159  return fromString<T>(data);
160  }
161 
173  int getDataType(std::size_t column);
174 
180  bool isNull(std::size_t column);
181 
182 
193  void next();
194 
199  {
200  next();
201  return *this;
202  }
203 
204 private:
205  const char* getTxtValue(std::size_t column);
206 
207  sqlite3* mDB;
208  sqlite3_stmt* mStatement;
209  bool mMore;
210  int mColumnCount;
211 };
212 
214 
223 {
224 public:
225  typedef std::vector<std::string> HeaderVector;
226  typedef std::vector<std::vector<std::pair<int, std::string>>> ValueVector;
227 
228 public:
231 
234 
237 
239 
240 public:
243 
248  bool eof() const
249  {
250  return mCurrent >= mCache.size();
251  }
252 
257  int getColumnCount() const
258  {
259  return mHeader.size();
260  }
261 
267  std::string getName(std::size_t column)
268  {
269  if(column>=mHeader.size())
270  MIRA_THROW(XAccessViolation, "Column " << column << " does not exist");
271 
272  return mHeader[column];
273  }
274 
281  template<typename T>
282  T getValue(std::size_t column)
283  {
284  if (isNull(column))
285  MIRA_THROW(XBadCast, "Value of column '" << column << "' is NULL");
286  return fromString<T>(mCache[mCurrent][column].second);
287  }
288 
296  template<typename T>
297  T getValue(std::size_t column, const T& defaultValue)
298  {
299  if (isNull(column))
300  return defaultValue;
301  try
302  {
303  return fromString<T>(mCache[mCurrent][column].second);
304  }
305  catch(...)
306  {
307  }
308  return defaultValue;
309  }
310 
321  int getDataType(std::size_t column);
322 
327  bool isNull(std::size_t column);
328 
329 
340  void next()
341  {
342  validate();
343  ++mCurrent;
344  }
345 
350  {
351  next();
352  return *this;
353  }
354 
360  {
361  return mCache;
362  }
363 
365 
366 public:
368  template<typename Reflector>
369  void reflect(Reflector& r)
370  {
371  r.member("Header", mHeader, "The header of the query");
372  r.member("Values", mCache, "The values of the query");
373  }
374 
375 protected:
376 
377  void validate()
378  {
379  if(eof())
380  MIRA_THROW(XAccessViolation, "Access to invalid query object.");
381  }
382 
383  void validate(std::size_t column)
384  {
385  validate();
386  if(column>=mCache[mCurrent].size())
387  MIRA_THROW(XAccessViolation, "Column " << column << " does not exist");
388  }
389 
392  std::size_t mCurrent;
393 };
394 
396 
397 }
398 
399 #endif
400 
void next()
Advance to next row.
Definition: SQLiteQuery.h:340
ValueVector mCache
Definition: SQLiteQuery.h:391
void validate()
Definition: SQLiteQuery.h:377
std::size_t mCurrent
Definition: SQLiteQuery.h:392
A cached SQLite query object that supports serialization.
Definition: SQLiteQuery.h:222
std::vector< std::string > HeaderVector
Definition: SQLiteQuery.h:225
std::vector< std::vector< std::pair< int, std::string > > > ValueVector
Definition: SQLiteQuery.h:226
int getColumnCount() const
Return the column count of the query.
Definition: SQLiteQuery.h:257
HeaderVector mHeader
Definition: SQLiteQuery.h:390
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Contains toString and fromString functions for converting data types to strings and the other way rou...
SQLiteQuery & operator++()
Preincrement operator.
Definition: SQLiteQuery.h:198
T getValue(std::size_t column)
Return a typecasted value for a given column in the current row.
Definition: SQLiteQuery.h:138
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
T getValue(std::size_t column)
Returns a typecasted value for a given column in the current row.
Definition: SQLiteQuery.h:282
std::string getName(std::size_t column)
Returns the name of a given column.
Definition: SQLiteQuery.h:267
bool eof() const
Return if the query contains data.
Definition: SQLiteQuery.h:248
void validate(std::size_t column)
Definition: SQLiteQuery.h:383
bool eof() const
Return if the query contains data.
Definition: SQLiteQuery.h:110
int getColumnCount() const
Return the column count of the query.
Definition: SQLiteQuery.h:119
T getValue(std::size_t column, const T &defaultValue)
Returns a typecasted value for a given column in the current row.
Definition: SQLiteQuery.h:297
void reflect(Reflector &r)
Reflect method for serialization.
Definition: SQLiteQuery.h:369
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
T getValue(std::size_t column, const T &defaultValue)
Return a typecasted value for a given column in the current row.
Definition: SQLiteQuery.h:154
ValueVector getValues() const
Return the cached values.
Definition: SQLiteQuery.h:359
SQLiteCachedQuery & operator++()
Preincrement operator.
Definition: SQLiteQuery.h:349
Class representing a result of a SQLite database query.
Definition: SQLiteQuery.h:66
bool valid() const
SQLiteQuery is valid if statement is not null.
Definition: SQLiteQuery.h:101