OpenMS
Loading...
Searching...
No Matches
SqliteConnector.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- 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
18struct sqlite3;
19struct sqlite3_stmt;
20
21namespace OpenMS
22{
30 class OPENMS_DLLAPI SqliteConnector
31 {
32 public:
33
35 enum class SqlOpenMode
36 {
37 READ_ONLY,
38#ifndef READONLY
39 READONLY = READ_ONLY,
40#endif
41 READWRITE,
42 READWRITE_OR_CREATE
43 };
44
46 SqliteConnector() = delete;
47
50 explicit SqliteConnector(const std::string& filename, const SqlOpenMode mode = SqlOpenMode::READWRITE_OR_CREATE);
51
54
64 sqlite3* getDB()
65 {
66 return db_;
67 }
68
76 bool tableExists(const std::string& tablename)
77 {
78 return tableExists(db_, tablename);
79 }
80
83 Size countTableRows(const std::string& table_name);
84
93 bool columnExists(const std::string& tablename, const std::string& colname)
94 {
95 return columnExists(db_, tablename, colname);
96 }
97
107 void executeStatement(const std::string& statement)
108 {
109 executeStatement(db_, statement);
110 }
111
128 void executeBindStatement(const std::string& prepare_statement, const std::vector<std::string>& data)
129 {
130 executeBindStatement(db_, prepare_statement, data);
131 }
132
144 void prepareStatement(sqlite3_stmt** stmt, const std::string& prepare_statement)
145 {
146 prepareStatement(db_, stmt, prepare_statement);
147 }
148
157 static bool tableExists(sqlite3* db, const std::string& tablename);
158
168 static bool columnExists(sqlite3* db, const std::string& tablename, const std::string& colname);
169
180 static void executeStatement(sqlite3* db, const std::stringstream& statement);
181
192 static void executeStatement(sqlite3* db, const std::string& statement);
193
213 static void prepareStatement(sqlite3* db, sqlite3_stmt** stmt, const std::string& prepare_statement);
214
215
233 static void executeBindStatement(sqlite3* db, const std::string& prepare_statement, const std::vector<std::string>& data);
234
235 protected:
236
245 void openDatabase_(const std::string& filename, const SqlOpenMode mode);
246
247 protected:
248 sqlite3* db_ = nullptr;
249
250 };
251
252 namespace Internal
253 {
254 namespace SqliteHelper
255 {
259 template <typename T>
260 UInt64 clearSignBit(T /*value*/)
261 {
262 static_assert(std::is_same<T, std::false_type>::value, "Wrong input type to clearSignBit(). Please pass unsigned 64bit ints!");
263 return 0;
264 };
266 template <>
267 inline UInt64 clearSignBit(UInt64 value) {
268 return value & ~(1ULL << 63);
269 }
270
271
272 enum class SqlState
273 {
274 SQL_ROW,
275 SQL_DONE,
276 SQL_ERROR
277 };
278
292 SqlState nextRow(sqlite3_stmt* stmt, SqlState current = SqlState::SQL_ROW);
293
294
317 template <typename ValueType>
318 bool extractValue(ValueType* /* dst */, sqlite3_stmt* /* stmt */, int /* pos */)
319 {
320 throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
321 "Not implemented");
322 }
323
324 template <> bool extractValue<double>(double* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
325
326 template <> bool extractValue<int>(int* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
327 template <> bool extractValue<Int64>(Int64* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
328
329 template <> bool extractValue<std::string>(std::string* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
330
331 template <> bool extractValue<std::string>(std::string* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
332
334 bool extractValueIntStr(std::string* dst, sqlite3_stmt* stmt, int pos);
335
341 double extractDouble(sqlite3_stmt* stmt, int pos);
342 float extractFloat(sqlite3_stmt* stmt, int pos);
343 int extractInt(sqlite3_stmt* stmt, int pos);
344 Int64 extractInt64(sqlite3_stmt* stmt, int pos);
345 std::string extractString(sqlite3_stmt* stmt, int pos);
346 char extractChar(sqlite3_stmt* stmt, int pos);
347 bool extractBool(sqlite3_stmt* stmt, int pos);
// end of sqlThrowingGetters
349 }
350 }
351
352
353} // namespace OpenMS
A method or algorithm argument contains illegal values.
Definition Exception.h:630
File adapter for Sqlite files.
Definition SqliteConnector.h:31
bool columnExists(const std::string &tablename, const std::string &colname)
Checks whether the given table contains a certain column.
Definition SqliteConnector.h:93
static void prepareStatement(sqlite3 *db, sqlite3_stmt **stmt, const std::string &prepare_statement)
Converts an SQL statement into a prepared statement.
~SqliteConnector()
Destructor.
void executeBindStatement(const std::string &prepare_statement, const std::vector< std::string > &data)
Executes raw data SQL statements (insert statements)
Definition SqliteConnector.h:128
static bool tableExists(sqlite3 *db, const std::string &tablename)
Checks whether the given table exists.
SqliteConnector(const std::string &filename, const SqlOpenMode mode=SqlOpenMode::READWRITE_OR_CREATE)
SqlOpenMode
how an sqlite db should be opened
Definition SqliteConnector.h:36
SqliteConnector()=delete
Default constructor.
bool tableExists(const std::string &tablename)
Checks whether the given table exists.
Definition SqliteConnector.h:76
static void executeStatement(sqlite3 *db, const std::string &statement)
Executes a given SQL statement (insert statement)
void prepareStatement(sqlite3_stmt **stmt, const std::string &prepare_statement)
Prepares a SQL statement.
Definition SqliteConnector.h:144
void openDatabase_(const std::string &filename, const SqlOpenMode mode)
Opens a new SQLite database.
void executeStatement(const std::string &statement)
Executes a given SQL statement (insert statement)
Definition SqliteConnector.h:107
sqlite3 * getDB()
Returns the raw pointer to the database.
Definition SqliteConnector.h:64
static bool columnExists(sqlite3 *db, const std::string &tablename, const std::string &colname)
Checks whether the given table contains a certain column.
static void executeBindStatement(sqlite3 *db, const std::string &prepare_statement, const std::vector< std::string > &data)
Executes raw data SQL statements (insert statements)
static void executeStatement(sqlite3 *db, const std::stringstream &statement)
Executes a given SQL statement (insert statement)
Size countTableRows(const std::string &table_name)
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)
char extractChar(sqlite3_stmt *stmt, int pos)
double extractDouble(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.
std::string extractString(sqlite3_stmt *stmt, int pos)
int extractInt(sqlite3_stmt *stmt, int pos)
Int64 extractInt64(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:273
@ SQL_ERROR
includes SQLITE_BUSY, SQLITE_ERROR, SQLITE_MISUSE
bool extractValue< int >(int *dst, sqlite3_stmt *stmt, int pos)
bool extractValue< std::string >(std::string *dst, sqlite3_stmt *stmt, int pos)
bool extractValue< Int64 >(Int64 *dst, sqlite3_stmt *stmt, int pos)
UInt64 clearSignBit(T)
Definition SqliteConnector.h:260
bool extractValue< double >(double *dst, sqlite3_stmt *stmt, int pos)
bool extractValueIntStr(std::string *dst, sqlite3_stmt *stmt, int pos)
Special case where an integer should be stored in a std::string field.
bool extractValue(ValueType *, sqlite3_stmt *, int)
Extracts a specific value from an SQL column.
Definition SqliteConnector.h:318
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19