OpenMS
SqliteConnector.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Hannes Roest $
32 // $Authors: Hannes Roest $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
40 
41 #include <type_traits> // for is_same
42 
43 // forward declarations
44 struct sqlite3;
45 struct sqlite3_stmt;
46 
47 namespace OpenMS
48 {
56  class OPENMS_DLLAPI SqliteConnector
57  {
58  public:
59 
61  enum class SqlOpenMode
62  {
63  READONLY,
64  READWRITE,
65  READWRITE_OR_CREATE
66  };
67 
69  SqliteConnector() = delete;
70 
73  explicit SqliteConnector(const String& filename, const SqlOpenMode mode = SqlOpenMode::READWRITE_OR_CREATE);
74 
77 
87  sqlite3* getDB()
88  {
89  return db_;
90  }
91 
99  bool tableExists(const String& tablename)
100  {
101  return tableExists(db_, tablename);
102  }
103 
106  Size countTableRows(const String& table_name);
107 
116  bool columnExists(const String& tablename, const String& colname)
117  {
118  return columnExists(db_, tablename, colname);
119  }
120 
130  void executeStatement(const String& statement)
131  {
132  executeStatement(db_, statement);
133  }
134 
151  void executeBindStatement(const String& prepare_statement, const std::vector<String>& data)
152  {
153  executeBindStatement(db_, prepare_statement, data);
154  }
155 
167  void prepareStatement(sqlite3_stmt** stmt, const String& prepare_statement)
168  {
169  prepareStatement(db_, stmt, prepare_statement);
170  }
171 
180  static bool tableExists(sqlite3* db, const String& tablename);
181 
191  static bool columnExists(sqlite3* db, const String& tablename, const String& colname);
192 
203  static void executeStatement(sqlite3* db, const std::stringstream& statement);
204 
215  static void executeStatement(sqlite3* db, const String& statement);
216 
236  static void prepareStatement(sqlite3* db, sqlite3_stmt** stmt, const String& prepare_statement);
237 
238 
256  static void executeBindStatement(sqlite3* db, const String& prepare_statement, const std::vector<String>& data);
257 
258  protected:
259 
267  void openDatabase_(const String& filename, const SqlOpenMode mode);
268 
269  protected:
270  sqlite3* db_ = nullptr;
271 
272  };
273 
274  namespace Internal
275  {
276  namespace SqliteHelper
277  {
281  template <typename T>
282  UInt64 clearSignBit(T /*value*/)
283  {
284  static_assert(std::is_same<T, std::false_type>::value, "Wrong input type to clearSignBit(). Please pass unsigned 64bit ints!");
285  return 0;
286  };
288  template <>
289  inline UInt64 clearSignBit(UInt64 value) {
290  return value & ~(1ULL << 63);
291  }
292 
293 
294  enum class SqlState
295  {
296  SQL_ROW,
297  SQL_DONE,
298  SQL_ERROR
299  };
300 
313  SqlState nextRow(sqlite3_stmt* stmt, SqlState current = SqlState::SQL_ROW);
314 
315 
338  template <typename ValueType>
339  bool extractValue(ValueType* /* dst */, sqlite3_stmt* /* stmt */, int /* pos */)
340  {
341  throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
342  "Not implemented");
343  }
344 
345  template <> bool extractValue<double>(double* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
346 
347  template <> bool extractValue<int>(int* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
348  template <> bool extractValue<Int64>(Int64* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
349 
350  template <> bool extractValue<String>(String* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
351 
352  template <> bool extractValue<std::string>(std::string* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
353 
355  bool extractValueIntStr(String* dst, sqlite3_stmt* stmt, int pos);
356 
362  double extractDouble(sqlite3_stmt* stmt, int pos);
363  float extractFloat(sqlite3_stmt* stmt, int pos);
364  int extractInt(sqlite3_stmt* stmt, int pos);
365  Int64 extractInt64(sqlite3_stmt* stmt, int pos);
366  String extractString(sqlite3_stmt* stmt, int pos);
367  char extractChar(sqlite3_stmt* stmt, int pos);
368  bool extractBool(sqlite3_stmt* stmt, int pos); // end of sqlThrowingGetters
370  }
371  }
372 
373 
374 } // namespace OpenMS
A method or algorithm argument contains illegal values.
Definition: Exception.h:650
File adapter for Sqlite files.
Definition: SqliteConnector.h:57
bool tableExists(const String &tablename)
Checks whether the given table exists.
Definition: SqliteConnector.h:99
~SqliteConnector()
Destructor.
sqlite3 * getDB()
Returns the raw pointer to the database.
Definition: SqliteConnector.h:87
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:151
SqlOpenMode
how an sqlite db should be opened
Definition: SqliteConnector.h:62
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:167
bool columnExists(const String &tablename, const String &colname)
Checks whether the given table contains a certain column.
Definition: SqliteConnector.h:116
void executeStatement(const String &statement)
Executes a given SQL statement (insert statement)
Definition: SqliteConnector.h:130
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:60
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
OPENMS_INT64_TYPE Int64
Signed integer type (64bit)
Definition: Types.h:70
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
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:295
@ 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:282
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:339
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48