DBInterface.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: DBInterface.h,v 1.7.10.2 2007/08/07 12:31:11 oliver Exp $
00005 //
00006 // Author:
00007 //   Oliver Kohlbacher
00008 //
00009 
00010 #ifndef BALL_FORMAT_DBINTERFACE_H
00011 #define BALL_FORMAT_DBINTERFACE_H
00012 
00013 #include <BALL/KERNEL/system.h>
00014 #include <BALL/KERNEL/molecule.h>
00015 #include <BALL/FORMAT/MOLFile.h>
00016 #include <BALL/COMMON/exception.h>
00017 
00018 #include <QtSql/qsqlrecord.h>
00019 #include <QtSql/qsqldatabase.h>
00020 #include <QtSql/qsqlquery.h>
00021 #include <QtCore/qvariant.h>
00022 
00023 namespace BALL
00024 {
00025 
00042   class BALL_EXPORT DBInterface
00043   {
00044     public:
00045     
00046     class InvalidQuery
00047       : public Exception::GeneralException
00048     {
00049       public:
00050       InvalidQuery(const char* file, int line, const String& s = "<unknown query>") ;
00051       virtual ~InvalidQuery() throw() {}
00052 
00053       protected:
00054       std::string query_;
00055     };
00056 
00057     class NotConnected
00058       : public Exception::GeneralException
00059     {
00060       public:
00061       NotConnected(const char* file, int line, const String& s = "<not connected>") ;
00062       virtual ~NotConnected() throw() {}
00063 
00064       protected:
00065       std::string query_;
00066     };
00067 
00069     class InconsistentTopology
00070       : public Exception::GeneralException
00071     {
00072       public:
00073       InconsistentTopology(const char* file, int line, const String& s = "<inconsistent topology>") ;
00074       virtual ~InconsistentTopology() throw() {}
00075 
00076       protected:
00077       std::string query_;
00078     };
00079 
00082       
00084     typedef qlonglong ID;
00085 
00087     typedef std::vector<ID> IDVector;
00088 
00092     typedef std::pair<String, String> ConformationMethod;
00093 
00097     typedef std::pair<String, String> ChargeMethod;
00099 
00102 
00104     enum ErrorCodes
00105     {
00106       NO_ERROR,
00107       NO_CONNECTION // the database was not connected/initialized (good() = false)
00108     };
00109 
00111     static const String BALL_DEFAULT_DBRCFILE;
00113     static const String BALL_DEFAULT_DATABASE_HOST;
00115     static const Size   BALL_DEFAULT_DATABASE_PORT;
00117     static const String BALL_DEFAULT_DATABASE_DRIVER;
00119     static const String BALL_DEFAULT_DATABASE_NAME;
00121     static const String BALL_DEFAULT_DATABASE_SECTIONNAME;
00123     
00124 
00127 
00128     DBInterface();
00130     virtual ~DBInterface() {}
00132 
00135 
00136     void getTopology(ID topology, System& system);
00141     ID newTopology(const System& system, const String& name, const String& source_id, ID id = 0);
00143     void setTopology(ID topology, const System& system);
00144 
00146     IDVector getConformationList(ID topology_od);
00148     IDVector getConformationList(ID topology_id, ID method_id);
00150     void loadConformation(const ID conformation, System& system);
00151 
00153     ID storeConformation(ID topology_id, ID method_id, const System& system,double energy);
00155     ID storeConformation(ID topology, ID method_ID, const System& system);
00156 
00158     IDVector getConformationMethods();
00160     ConformationMethod getConformationMethod(ID method_id);
00162     ID getConformationMethod(const String& method, const String& parameters);
00164     ID newConformationMethod(const String& method, const String& parameters);
00165         
00167     ID storeCharges(ID topology_id, ID method_id, const System& system);
00169     void loadCharges(const ID charge_id, System& system);
00171     IDVector getChargeMethods();
00173     ChargeMethod getChargeMethod(DBInterface::ID method_id);
00175     ID getChargeMethod(const String& method, const String& parameters);
00177     ID newChargeMethod(const String& method, const String& parameters);
00178 
00180     ErrorCode getError() const { return error_; }
00182     void setError(ErrorCode error) { error_ = error; }
00184     bool good() const { return error_ == 0; }
00186 
00196     bool connect
00197       (const String& user, const String& password, 
00198        const String& database_name = "structures", const String& host = "diclofenac.informatik.uni-tuebingen.de",
00199        Size port = 3306, const String& driver = "QMYSQL3");
00200 
00220     bool connect();
00221     
00223     QSqlQuery& executeQuery(const String& query_string) 
00224       throw(InvalidQuery, NotConnected);
00225     
00227     QSqlQuery& executeQuery() 
00228       throw(InvalidQuery, NotConnected);
00229 
00231     QSqlQuery& query() { return *query_; }
00232 
00234     String executedQuery() { return query_->executedQuery().toStdString();}
00235 
00237     bool first() { return query_->first(); }
00238 
00240     bool last() { return query_->last(); }
00241 
00243     bool next() { return query_->next(); }
00244 
00248     IDVector extractIDs();
00249 
00251     bool prev() { return query_->previous(); }
00252 
00254     ID lastInsertedID();
00255     
00257     Size size() { return (Size)query_->size(); }
00258 
00260     QVariant value(Position k) { return query_->value(k); }
00261 
00263     void prepare(const String& s) { query_->prepare(s.c_str()); }
00264 
00266     void addBindValue(const QVariant& v) { query_->addBindValue(v); }
00267     void addBindValue(const QVariant& v1, const QVariant& v2) { addBindValue(v1); addBindValue(v2);}
00268     void addBindValue(const QVariant& v1, const QVariant& v2, const QVariant& v3) { addBindValue(v1); addBindValue(v2); addBindValue(v3);}
00269     void addBindValue(const QVariant& v1, const QVariant& v2, const QVariant& v3, const QVariant& v4) { addBindValue(v1); addBindValue(v2); addBindValue(v3); addBindValue(v4);}
00271 
00274 
00275     bool getMOLFile(ID id, String& file_text, String& name, String& source_id);
00277     void addMOLFileToSystem(const String& molfile, System& system);
00279 
00280     protected:  
00281 
00283     static void assignCoordinates_(System& system, const QByteArray& data);
00285     static void extractCoordinates_(const System& system, QByteArray& data);
00287     static void assignCharges_(System& system, const QByteArray& data);
00289     static void extractCharges_(const System& system, QByteArray& data);
00291     static void encodeArray_(const std::vector<float>& v, QByteArray& a);
00293     static void decodeArray_(const QByteArray& a, std::vector<float>& v);
00294 
00295     // Database conection
00296     ErrorCode       error_;
00298     QSqlDatabase    db_;
00300     QSqlQuery*      query_;
00301     
00302     // Connection details
00303     String          database_name_;
00304     Size            port_;
00305     String          host_;
00306     String          username_;
00307     String          password_;
00308 
00309     private:
00310     // No copy construction allowed. Don't know what QSqlDatabase would
00311     // do in that case. It can't be copied anyhow...
00312     DBInterface(const DBInterface& /* interface */) {}
00313   };
00314 
00315 } // namespace BALL
00316 #endif // BALL_FORMAT_DBINTERFACE_H