Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

DBConnection.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_FORMAT_DB_DBCONNECTION_H
00028 #define OPENMS_FORMAT_DB_DBCONNECTION_H
00029 
00030 #include <OpenMS/config.h>
00031 
00032 //QT includes
00033 #include <QtSql/QSqlDatabase>
00034 
00035 //OpenMS includes
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   //  Implementation of the inline / template functions
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

Generated Tue Apr 1 15:36:33 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1