OpenMS
SqliteConnector.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Hannes Roest $
6 // $Authors: Hannes Roest $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
14 
15 #include <type_traits> // for is_same
16 
17 // forward declarations
18 struct sqlite3;
19 struct sqlite3_stmt;
20 
21 namespace OpenMS
22 {
30  class OPENMS_DLLAPI SqliteConnector
31  {
32  public:
33 
35  enum class SqlOpenMode
36  {
37  READONLY,
38  READWRITE,
39  READWRITE_OR_CREATE
40  };
41 
43  SqliteConnector() = delete;
44 
47  explicit SqliteConnector(const String& filename, const SqlOpenMode mode = SqlOpenMode::READWRITE_OR_CREATE);
48 
51 
61  sqlite3* getDB()
62  {
63  return db_;
64  }
65 
73  bool tableExists(const String& tablename)
74  {
75  return tableExists(db_, tablename);
76  }
77 
80  Size countTableRows(const String& table_name);
81 
90  bool columnExists(const String& tablename, const String& colname)
91  {
92  return columnExists(db_, tablename, colname);
93  }
94 
104  void executeStatement(const String& statement)
105  {
106  executeStatement(db_, statement);
107  }
108 
125  void executeBindStatement(const String& prepare_statement, const std::vector<String>& data)
126  {
127  executeBindStatement(db_, prepare_statement, data);
128  }
129 
141  void prepareStatement(sqlite3_stmt** stmt, const String& prepare_statement)
142  {
143  prepareStatement(db_, stmt, prepare_statement);
144  }
145 
154  static bool tableExists(sqlite3* db, const String& tablename);
155 
165  static bool columnExists(sqlite3* db, const String& tablename, const String& colname);
166 
177  static void executeStatement(sqlite3* db, const std::stringstream& statement);
178 
189  static void executeStatement(sqlite3* db, const String& statement);
190 
210  static void prepareStatement(sqlite3* db, sqlite3_stmt** stmt, const String& prepare_statement);
211 
212 
230  static void executeBindStatement(sqlite3* db, const String& prepare_statement, const std::vector<String>& data);
231 
232  protected:
233 
242  void openDatabase_(const String& filename, const SqlOpenMode mode);
243 
244  protected:
245  sqlite3* db_ = nullptr;
246 
247  };
248 
249  namespace Internal
250  {
251  namespace SqliteHelper
252  {
256  template <typename T>
257  UInt64 clearSignBit(T /*value*/)
258  {
259  static_assert(std::is_same<T, std::false_type>::value, "Wrong input type to clearSignBit(). Please pass unsigned 64bit ints!");
260  return 0;
261  };
263  template <>
264  inline UInt64 clearSignBit(UInt64 value) {
265  return value & ~(1ULL << 63);
266  }
267 
268 
269  enum class SqlState
270  {
271  SQL_ROW,
272  SQL_DONE,
273  SQL_ERROR
274  };
275 
289  SqlState nextRow(sqlite3_stmt* stmt, SqlState current = SqlState::SQL_ROW);
290 
291 
314  template <typename ValueType>
315  bool extractValue(ValueType* /* dst */, sqlite3_stmt* /* stmt */, int /* pos */)
316  {
317  throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
318  "Not implemented");
319  }
320 
321  template <> bool extractValue<double>(double* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
322 
323  template <> bool extractValue<int>(int* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
324  template <> bool extractValue<Int64>(Int64* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
325 
326  template <> bool extractValue<String>(String* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
327 
328  template <> bool extractValue<std::string>(std::string* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
329 
331  bool extractValueIntStr(String* dst, sqlite3_stmt* stmt, int pos);
332 
338  double extractDouble(sqlite3_stmt* stmt, int pos);
339  float extractFloat(sqlite3_stmt* stmt, int pos);
340  int extractInt(sqlite3_stmt* stmt, int pos);
341  Int64 extractInt64(sqlite3_stmt* stmt, int pos);
342  String extractString(sqlite3_stmt* stmt, int pos);
343  char extractChar(sqlite3_stmt* stmt, int pos);
344  bool extractBool(sqlite3_stmt* stmt, int pos); // end of sqlThrowingGetters
346  }
347  }
348 
349 
350 } // namespace OpenMS
A method or algorithm argument contains illegal values.
Definition: Exception.h:629
File adapter for Sqlite files.
Definition: SqliteConnector.h:31
bool tableExists(const String &tablename)
Checks whether the given table exists.
Definition: SqliteConnector.h:73
~SqliteConnector()
Destructor.
sqlite3 * getDB()
Returns the raw pointer to the database.
Definition: SqliteConnector.h:61
Size countTableRows(const String &table_name)
static bool columnExists(sqlite3 *db, const String &tablename, const String &colname)
Checks whether the given table contains a certain column.
void executeBindStatement(const String &prepare_statement, const std::vector< String > &data)
Executes raw data SQL statements (insert statements)
Definition: SqliteConnector.h:125
SqlOpenMode
how an sqlite db should be opened
Definition: SqliteConnector.h:36
SqliteConnector()=delete
Default constructor.
static bool tableExists(sqlite3 *db, const String &tablename)
Checks whether the given table exists.
SqliteConnector(const String &filename, const SqlOpenMode mode=SqlOpenMode::READWRITE_OR_CREATE)
void openDatabase_(const String &filename, const SqlOpenMode mode)
Opens a new SQLite database.
static void executeBindStatement(sqlite3 *db, const String &prepare_statement, const std::vector< String > &data)
Executes raw data SQL statements (insert statements)
static void prepareStatement(sqlite3 *db, sqlite3_stmt **stmt, const String &prepare_statement)
Converts an SQL statement into a prepared statement.
void prepareStatement(sqlite3_stmt **stmt, const String &prepare_statement)
Prepares a SQL statement.
Definition: SqliteConnector.h:141
bool columnExists(const String &tablename, const String &colname)
Checks whether the given table contains a certain column.
Definition: SqliteConnector.h:90
void executeStatement(const String &statement)
Executes a given SQL statement (insert statement)
Definition: SqliteConnector.h:104
static void executeStatement(sqlite3 *db, const std::stringstream &statement)
Executes a given SQL statement (insert statement)
static void executeStatement(sqlite3 *db, const String &statement)
Executes a given SQL statement (insert statement)
A more convenient string class.
Definition: String.h:34
int64_t Int64
Signed integer type (64bit)
Definition: Types.h:40
uint64_t UInt64
Unsigned integer type (64bit)
Definition: Types.h:47
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
bool extractBool(sqlite3_stmt *stmt, int pos)
float extractFloat(sqlite3_stmt *stmt, int pos)
convenience function; note: in SQL there is no float, just double. So this might be narrowing.
char extractChar(sqlite3_stmt *stmt, int pos)
Int64 extractInt64(sqlite3_stmt *stmt, int pos)
int extractInt(sqlite3_stmt *stmt, int pos)
String extractString(sqlite3_stmt *stmt, int pos)
double extractDouble(sqlite3_stmt *stmt, int pos)
bool extractValue< String >(String *dst, sqlite3_stmt *stmt, int pos)
SqlState nextRow(sqlite3_stmt *stmt, SqlState current=SqlState::SQL_ROW)
retrieves the next row from a prepared statement
SqlState
Definition: SqliteConnector.h:270
@ SQL_ERROR
includes SQLITE_BUSY, SQLITE_ERROR, SQLITE_MISUSE
bool extractValue< double >(double *dst, sqlite3_stmt *stmt, int pos)
bool extractValue< int >(int *dst, sqlite3_stmt *stmt, int pos)
UInt64 clearSignBit(T)
Definition: SqliteConnector.h:257
bool extractValueIntStr(String *dst, sqlite3_stmt *stmt, int pos)
Special case where an integer should be stored in a String field.
bool extractValue< Int64 >(Int64 *dst, sqlite3_stmt *stmt, int pos)
bool extractValue(ValueType *, sqlite3_stmt *, int)
Extracts a specific value from an SQL column.
Definition: SqliteConnector.h:315
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19