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

DBConnection.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: $
00032 // $Authors: Marc Sturm $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_FORMAT_DB_DBCONNECTION_H
00036 #define OPENMS_FORMAT_DB_DBCONNECTION_H
00037 
00038 #include <OpenMS/config.h>
00039 
00040 //QT includes
00041 #include <QtSql/QSqlDatabase>
00042 #include <QtSql/QSqlQuery>
00043 
00044 //OpenMS includes
00045 #include <OpenMS/CONCEPT/Exception.h>
00046 #include <OpenMS/DATASTRUCTURES/String.h>
00047 #include <OpenMS/CONCEPT/Types.h>
00048 
00049 #include <iostream>
00050 
00051 namespace OpenMS
00052 {
00060   class OPENMS_DLLAPI DBConnection
00061   {
00062 public:
00070     class OPENMS_DLLAPI InvalidQuery :
00071       public Exception::BaseException
00072     {
00073 public:
00074       InvalidQuery(const char * file, Int line, const char * function, const String & sql_query, const String & sql_error);
00075       ~InvalidQuery() throw();
00076     };
00077 
00085     class OPENMS_DLLAPI NotConnected :
00086       public Exception::BaseException
00087     {
00088 public:
00089       NotConnected(const char * file, Int line, const char * function);
00090       ~NotConnected() throw();
00091     };
00092 
00093 
00094 
00096     DBConnection();
00097 
00099     ~DBConnection();
00100 
00114     void connect(const String & db, const String & user, const String & password, const String & host = "localhost", UInt port = 3306, const String & QTDBDriver = DB_PLUGIN, const String & connection_name = "OpenMS_default_connection");
00115 
00117     bool isConnected() const;
00118 
00124     void disconnect();
00125 
00138     QSqlQuery executeQuery(const String & query, bool first = false);
00139 
00153     Int getIntValue(const String & table, const String & column, const String & id);
00154 
00169     double getDoubleValue(const String & table, const String & column, const String & id);
00170 
00184     String getStringValue(const String & table, const String & column, const String & id);
00185 
00198     UInt getId(const String & table, const String & column, const String & value);
00199 
00201     UInt getAutoId();
00202 
00204     String DBName() const;
00205 
00217     void render(QSqlQuery & result, std::ostream & out = std::cout, const String & separator = " | ", const String & line_begin = "", const String & line_end = "\n");
00218 
00219 
00230     template <class StringListType>
00231     void executeQueries(const StringListType & queries);
00232 
00233 private:
00234 
00236     QString connection_name_;
00237 
00239     inline QSqlDatabase getDB_() const
00240     {
00241       return QSqlDatabase::database(connection_name_, false);
00242     }
00243 
00244   };
00245 
00246 
00247 
00248   //---------------------------------------------------------------
00249   //  Implementation of the inline / template functions
00250   //---------------------------------------------------------------
00251 
00252   template <class StringListType>
00253   void DBConnection::executeQueries(const StringListType & queries)
00254   {
00255     String line;
00256     for (typename StringListType::const_iterator it = queries.begin(); it != queries.end(); ++it)
00257     {
00258       line = *it;
00259       line.trim();
00260       if (line != "")
00261       {
00262         executeQuery(line);
00263       }
00264     }
00265   }
00266 
00267 }
00268 
00269 #endif

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:38 using doxygen 1.7.1