OpenMS
Loading...
Searching...
No Matches
SqliteConnector_impl.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
11// -----------------------------------------------------------------------------
12// PRIVATE (non-installed) implementation header.
13//
14// This header exposes the raw SQLite C API (sqlite3 / sqlite3_stmt) that backs
15// OpenMS::SqliteConnector. It is deliberately NOT installed, NOT exported and
16// NOT part of the public API, so that SQLite can remain a fully private link
17// dependency of libOpenMS (no SQLite type ever appears in an installed header).
18//
19// It must only be included from .cpp files that live inside libOpenMS. Public
20// (installed) headers must never include it.
21// -----------------------------------------------------------------------------
22
26
27#include <sqlite3.h>
28
29#include <sstream>
30#include <string>
31#include <type_traits> // for is_same
32#include <vector>
33
34namespace OpenMS
35{
36 namespace Internal
37 {
42 {
43 static sqlite3* handle(SqliteConnector& conn)
44 {
45 return static_cast<sqlite3*>(conn.db_);
46 }
47 };
48
49 namespace SqliteHelper
50 {
52 inline sqlite3* getNativeHandle(SqliteConnector& conn)
53 {
55 }
56
65 bool tableExists(sqlite3* db, const std::string& tablename);
66
76 bool columnExists(sqlite3* db, const std::string& tablename, const std::string& colname);
77
88 void executeStatement(sqlite3* db, const std::string& statement);
89
101 void prepareStatement(sqlite3* db, sqlite3_stmt** stmt, const std::string& prepare_statement);
102
104 inline void prepareStatement(SqliteConnector& conn, sqlite3_stmt** stmt, const std::string& prepare_statement)
105 {
106 prepareStatement(getNativeHandle(conn), stmt, prepare_statement);
107 }
108
120 void executeBindStatement(sqlite3* db, const std::string& prepare_statement, const std::vector<std::string>& data);
121
125 template <typename T>
126 UInt64 clearSignBit(T /*value*/)
127 {
128 static_assert(std::is_same<T, std::false_type>::value, "Wrong input type to clearSignBit(). Please pass unsigned 64bit ints!");
129 return 0;
130 };
132 template <>
133 inline UInt64 clearSignBit(UInt64 value) {
134 return value & ~(1ULL << 63);
135 }
136
137
138 enum class SqlState
139 {
140 SQL_ROW,
141 SQL_DONE,
142 SQL_ERROR
143 };
144
158 SqlState nextRow(sqlite3_stmt* stmt, SqlState current = SqlState::SQL_ROW);
159
160
183 template <typename ValueType>
184 bool extractValue(ValueType* /* dst */, sqlite3_stmt* /* stmt */, int /* pos */)
185 {
186 throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
187 "Not implemented");
188 }
189
190 template <> bool extractValue<double>(double* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
191
192 template <> bool extractValue<int>(int* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
193 template <> bool extractValue<Int64>(Int64* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
194
195 template <> bool extractValue<std::string>(std::string* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
196
198 bool extractValueIntStr(std::string* dst, sqlite3_stmt* stmt, int pos);
199
205 double extractDouble(sqlite3_stmt* stmt, int pos);
206 float extractFloat(sqlite3_stmt* stmt, int pos);
207 int extractInt(sqlite3_stmt* stmt, int pos);
208 Int64 extractInt64(sqlite3_stmt* stmt, int pos);
209 std::string extractString(sqlite3_stmt* stmt, int pos);
210 char extractChar(sqlite3_stmt* stmt, int pos);
211 bool extractBool(sqlite3_stmt* stmt, int pos);
// end of sqlThrowingGetters
213 }
214 }
215} // namespace OpenMS
A method or algorithm argument contains illegal values.
Definition Exception.h:633
File adapter for Sqlite files.
Definition SqliteConnector.h:41
void * db_
Definition SqliteConnector.h:132
int64_t Int64
Signed integer type (64bit)
Definition Types.h:40
uint64_t UInt64
Unsigned integer type (64bit)
Definition Types.h:47
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
void executeBindStatement(sqlite3 *db, const std::string &prepare_statement, const std::vector< std::string > &data)
Executes raw data SQL statements (insert statements)
SqlState
Definition SqliteConnector_impl.h:139
@ 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)
void prepareStatement(sqlite3 *db, sqlite3_stmt **stmt, const std::string &prepare_statement)
Converts an SQL statement into a prepared statement.
sqlite3 * getNativeHandle(SqliteConnector &conn)
Retrieve the typed native SQLite handle behind a SqliteConnector.
Definition SqliteConnector_impl.h:52
void executeStatement(sqlite3 *db, const std::string &statement)
Executes a given SQL statement (insert statement)
bool tableExists(sqlite3 *db, const std::string &tablename)
Checks whether the given table exists.
bool extractValue< Int64 >(Int64 *dst, sqlite3_stmt *stmt, int pos)
UInt64 clearSignBit(T)
Definition SqliteConnector_impl.h:126
bool extractValue< double >(double *dst, sqlite3_stmt *stmt, int pos)
bool columnExists(sqlite3 *db, const std::string &tablename, const std::string &colname)
Checks whether the given table contains a certain column.
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_impl.h:184
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Definition SqliteConnector_impl.h:42
static sqlite3 * handle(SqliteConnector &conn)
Definition SqliteConnector_impl.h:43