00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef OPENMS_FORMAT_DB_DBCONNECTION_H
00028 #define OPENMS_FORMAT_DB_DBCONNECTION_H
00029
00030 #include <OpenMS/config.h>
00031
00032
00033 #include <QtSql/QSqlDatabase>
00034
00035
00036 #include <OpenMS/CONCEPT/Exception.h>
00037 #include <OpenMS/DATASTRUCTURES/String.h>
00038 #include <OpenMS/CONCEPT/Types.h>
00039
00040 #include <iostream>
00041
00042 namespace OpenMS
00043 {
00051 class DBConnection
00052 {
00053 public:
00061 class InvalidQuery
00062 : public Exception::Base
00063 {
00064 public:
00065 InvalidQuery(const char* file, Int line, const char* function, std::string sql_query, std::string sql_error) throw();
00066 ~InvalidQuery() throw();
00067 };
00068
00076 class NotConnected
00077 : public Exception::Base
00078 {
00079 public:
00080 NotConnected(const char* file, Int line, const char* function) throw();
00081 ~NotConnected() throw();
00082 };
00083
00084
00085
00087 DBConnection();
00088
00090 ~DBConnection();
00091
00103 void connect(const std::string& db, const std::string& user, const std::string& password, const std::string& host = "localhost", UInt port=3306, const std::string& QTDBDriver = DB_PLUGIN, const std::string& connection_name="defaultConnection") throw(InvalidQuery);
00104
00106 bool isConnected() const;
00107
00109 void disconnect();
00110
00119 void executeQuery(const std::string& query, QSqlQuery& result) throw(InvalidQuery, NotConnected);
00120
00130 Int getIntValue(const std::string& table, const std::string& column, const std::string& id) throw (InvalidQuery,NotConnected,Exception::ConversionError);
00131
00141 double getDoubleValue(const std::string& table, const std::string& column, const std::string& id) throw (InvalidQuery,NotConnected,Exception::ConversionError);
00142
00152 String getStringValue(const std::string& table, const std::string& column, const std::string& id) throw (InvalidQuery,NotConnected,Exception::ConversionError);
00153
00163 UInt getId(const std::string& table, const std::string& column, const std::string& value) throw (InvalidQuery,NotConnected);
00164
00166 UInt getAutoId();
00167
00169 std::string DBName() const;
00170
00182 void render(QSqlQuery& result, std::ostream& out=std::cout , const std::string& separator=" | " , const std::string& line_begin="" , const std::string& line_end="\n");
00183
00184
00192 template <class StringListType>
00193 void executeQueries(const StringListType& queries) throw(InvalidQuery,NotConnected);
00194
00195 private:
00196
00202 QSqlQuery& executeQuery_(const std::string& query) throw(InvalidQuery,NotConnected);
00203
00205 QSqlDatabase db_handle_;
00206
00212 QSqlQuery* lir_;
00213
00214 };
00215
00216
00217
00218
00219
00220
00221
00222 template <class StringListType>
00223 void DBConnection::executeQueries(const StringListType& queries) throw(InvalidQuery,NotConnected)
00224 {
00225 String line;
00226 for( typename StringListType::const_iterator it = queries.begin(); it != queries.end(); ++it)
00227 {
00228 line = *it;
00229 line.trim();
00230 if (line != "")
00231 {
00232 executeQuery_(line);
00233 }
00234 }
00235 }
00236 }
00237
00238 #endif