| OpenMS 2.5.0

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-2020.
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
37 #include <OpenMS/KERNEL/StandardTypes.h>
38 #include <OpenMS/DATASTRUCTURES/String.h>
39 #include <OpenMS/CONCEPT/Exception.h>
40
41 #include <iostream>
42 #include <sstream>
43
44 // forward declarations
45 struct sqlite3;
46 struct sqlite3_stmt;
47
48 namespace OpenMS
49 {
57 class OPENMS_DLLAPI SqliteConnector
58 {
59 public:
60
62 SqliteConnector();
63
64 explicit SqliteConnector(const String& filename)
65 {
66 openDatabase(filename);
67 }
68
70 ~SqliteConnector();
71
81 sqlite3* getDB()
82 {
83 return db_;
84 }
85
93 bool tableExists(const String& tablename)
94 {
95 return tableExists(db_, tablename);
96 }
97
106 bool columnExists(const String& tablename, const String& colname)
107 {
108 return columnExists(db_, tablename, colname);
109 }
110
119 void executeStatement(const std::stringstream& statement)
120 {
121 executeStatement(db_, statement);
122 }
123
133 void executeStatement(const String& statement)
134 {
135 executeStatement(db_, statement);
136 }
137
154 void executeBindStatement(const String& prepare_statement, const std::vector<String>& data)
155 {
156 executeBindStatement(db_, prepare_statement, data);
157 }
158
170 void prepareStatement(sqlite3_stmt** stmt, const String& prepare_statement)
171 {
172 prepareStatement(db_, stmt, prepare_statement);
173 }
174
183 static bool tableExists(sqlite3* db, const String& tablename);
184
194 static bool columnExists(sqlite3* db, const String& tablename, const String& colname);
195
206 static void executeStatement(sqlite3* db, const std::stringstream& statement);
207
218 static void executeStatement(sqlite3* db, const String& statement);
219
239 static void prepareStatement(sqlite3* db, sqlite3_stmt** stmt, const String& prepare_statement);
240
241
259 static void executeBindStatement(sqlite3* db, const String& prepare_statement, const std::vector<String>& data);
260
261 protected:
262
270 void openDatabase(const String& filename);
271
272 protected:
273 sqlite3 *db_;
274
275 };
276
277 namespace Internal
278 {
279 namespace SqliteHelper
280 {
281
304 template <typename ValueType>
305 bool extractValue(ValueType* /* dst */, sqlite3_stmt* /* stmt */, int /* pos */)
306 {
307 throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
308 "Not implemented");
309 }
310
311 template <> bool extractValue<double>(double* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
312
313 template <> bool extractValue<int>(int* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
314
315 template <> bool extractValue<String>(String* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
316
317 template <> bool extractValue<std::string>(std::string* dst, sqlite3_stmt* stmt, int pos); //explicit specialization
318
320 bool extractValueIntStr(String* dst, sqlite3_stmt* stmt, int pos);
321
322 }
323 }
324
325
326 } // namespace OpenMS
327
328
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::SqliteConnector::SqliteConnector
SqliteConnector(const String &filename)
Definition: SqliteConnector.h:64
OpenMS::SqliteConnector::columnExists
bool columnExists(const String &tablename, const String &colname)
Checkes whether the given table contains a certain column.
Definition: SqliteConnector.h:106
OpenMS::SqliteConnector::getDB
sqlite3 * getDB()
Returns the raw pointer to the database.
Definition: SqliteConnector.h:81
OpenMS::SqliteConnector::executeStatement
void executeStatement(const std::stringstream &statement)
Executes a given SQL statement (insert statement)
Definition: SqliteConnector.h:119
Exception.h
StandardTypes.h
OpenMS::Internal::SqliteHelper::extractValue< String >
bool extractValue< String >(String *dst, sqlite3_stmt *stmt, int pos)
OpenMS::Internal::SqliteHelper::extractValue< int >
bool extractValue< int >(int *dst, sqlite3_stmt *stmt, int pos)
OpenMS::Internal::SqliteHelper::extractValueIntStr
bool extractValueIntStr(String *dst, sqlite3_stmt *stmt, int pos)
Special case where an integer should be stored in a String field.
OpenMS::Internal::SqliteHelper::extractValue
bool extractValue(ValueType *, sqlite3_stmt *, int)
Extracts a specific value from an SQL column.
Definition: SqliteConnector.h:305
OpenMS::SqliteConnector::executeStatement
void executeStatement(const String &statement)
Executes a given SQL statement (insert statement)
Definition: SqliteConnector.h:133
String.h
OpenMS::SqliteConnector::prepareStatement
void prepareStatement(sqlite3_stmt **stmt, const String &prepare_statement)
Prepares a SQL statement.
Definition: SqliteConnector.h:170
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::SqliteConnector::db_
sqlite3 * db_
Definition: SqliteConnector.h:273
OpenMS::Exception::IllegalArgument
A method or algorithm argument contains illegal values.
Definition: Exception.h:648
OpenMS::Internal::SqliteHelper::extractValue< double >
bool extractValue< double >(double *dst, sqlite3_stmt *stmt, int pos)
OpenMS::SqliteConnector
File adapter for Sqlite files.
Definition: SqliteConnector.h:57
OpenMS::SqliteConnector::tableExists
bool tableExists(const String &tablename)
Checks whether the given table exists.
Definition: SqliteConnector.h:93
OpenMS::SqliteConnector::executeBindStatement
void executeBindStatement(const String &prepare_statement, const std::vector< String > &data)
Executes raw data SQL statements (insert statements)
Definition: SqliteConnector.h:154