BALL  1.4.1
DBInterface.h
Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_FORMAT_DBINTERFACE_H
00006 #define BALL_FORMAT_DBINTERFACE_H
00007 
00008 #include <BALL/KERNEL/system.h>
00009 #include <BALL/KERNEL/molecule.h>
00010 #include <BALL/FORMAT/MOLFile.h>
00011 #include <BALL/COMMON/exception.h>
00012 
00013 #include <QSqlRecord>
00014 #include <QSqlDatabase>
00015 #include <QSqlQuery>
00016 #include <QtCore/QVariant>
00017 
00018 namespace BALL
00019 {
00020 
00037   class BALL_EXPORT DBInterface
00038   {
00039     public:
00040     
00041     class InvalidQuery
00042       : public Exception::GeneralException
00043     {
00044       public:
00045       InvalidQuery(const char* file, int line, const String& s = "<unknown query>") ;
00046       virtual ~InvalidQuery() throw() {}
00047 
00048       protected:
00049       std::string query_;
00050     };
00051 
00052     class NotConnected
00053       : public Exception::GeneralException
00054     {
00055       public:
00056       NotConnected(const char* file, int line, const String& s = "<not connected>") ;
00057       virtual ~NotConnected() throw() {}
00058 
00059       protected:
00060       std::string query_;
00061     };
00062 
00064     class InconsistentTopology
00065       : public Exception::GeneralException
00066     {
00067       public:
00068       InconsistentTopology(const char* file, int line, const String& s = "<inconsistent topology>") ;
00069       virtual ~InconsistentTopology() throw() {}
00070 
00071       protected:
00072       std::string query_;
00073     };
00074 
00077       
00079     typedef qlonglong ID;
00080 
00082     typedef std::vector<ID> IDVector;
00083 
00087     typedef std::pair<String, String> ConformationMethod;
00088 
00092     typedef std::pair<String, String> ChargeMethod;
00094 
00097 
00099     enum ErrorCodes
00100     {
00101       NO_ERROR,
00102       NO_CONNECTION // the database was not connected/initialized (good() = false)
00103     };
00104 
00106     static const String BALL_DEFAULT_DBRCFILE;
00108     static const String BALL_DEFAULT_DATABASE_HOST;
00110     static const Size   BALL_DEFAULT_DATABASE_PORT;
00112     static const String BALL_DEFAULT_DATABASE_DRIVER;
00114     static const String BALL_DEFAULT_DATABASE_NAME;
00116     static const String BALL_DEFAULT_DATABASE_SECTIONNAME;
00118     
00119 
00122 
00123     DBInterface();
00125     virtual ~DBInterface() {}
00127 
00130 
00131     void getTopology(ID topology, System& system);
00136     ID newTopology(const System& system, const String& name, const String& source_id, ID id = 0);
00138     void setTopology(ID topology, const System& system);
00139 
00141     IDVector getConformationList(ID topology_od);
00143     IDVector getConformationList(ID topology_id, ID method_id);
00145     void loadConformation(const ID conformation, System& system);
00146 
00148     ID storeConformation(ID topology_id, ID method_id, const System& system,double energy);
00150     ID storeConformation(ID topology, ID method_ID, const System& system);
00151 
00153     IDVector getConformationMethods();
00155     ConformationMethod getConformationMethod(ID method_id);
00157     ID getConformationMethod(const String& method, const String& parameters);
00159     ID newConformationMethod(const String& method, const String& parameters);
00160         
00162     ID storeCharges(ID topology_id, ID method_id, const System& system);
00164     void loadCharges(const ID charge_id, System& system);
00166     IDVector getChargeMethods();
00168     ChargeMethod getChargeMethod(DBInterface::ID method_id);
00170     ID getChargeMethod(const String& method, const String& parameters);
00172     ID newChargeMethod(const String& method, const String& parameters);
00173 
00175     ErrorCode getError() const { return error_; }
00177     void setError(ErrorCode error) { error_ = error; }
00179     bool good() const { return error_ == 0; }
00181 
00191     bool connect
00192       (const String& user, const String& password, 
00193        const String& database_name = "structures", const String& host = "diclofenac.informatik.uni-tuebingen.de",
00194        Size port = 3306, const String& driver = "QMYSQL3");
00195 
00215     bool connect();
00216     
00221     QSqlQuery& executeQuery(const String& query_string);
00222     
00227     QSqlQuery& executeQuery();
00228 
00230     QSqlQuery& query() { return *query_; }
00231 
00233     String executedQuery() { return query_->executedQuery().toStdString();}
00234 
00236     bool first() { return query_->first(); }
00237 
00239     bool last() { return query_->last(); }
00240 
00242     bool next() { return query_->next(); }
00243 
00247     IDVector extractIDs();
00248 
00250     bool prev() { return query_->previous(); }
00251 
00253     ID lastInsertedID();
00254     
00256     Size size() { return (Size)query_->size(); }
00257 
00259     QVariant value(Position k) { return query_->value(k); }
00260 
00262     void prepare(const String& s) { query_->prepare(s.c_str()); }
00263 
00265     void addBindValue(const QVariant& v) { query_->addBindValue(v); }
00266     void addBindValue(const QVariant& v1, const QVariant& v2) { addBindValue(v1); addBindValue(v2);}
00267     void addBindValue(const QVariant& v1, const QVariant& v2, const QVariant& v3) { addBindValue(v1); addBindValue(v2); addBindValue(v3);}
00268     void addBindValue(const QVariant& v1, const QVariant& v2, const QVariant& v3, const QVariant& v4) { addBindValue(v1); addBindValue(v2); addBindValue(v3); addBindValue(v4);}
00270 
00273 
00274     bool getMOLFile(ID id, String& file_text, String& name, String& source_id);
00276     void addMOLFileToSystem(const String& molfile, System& system);
00278 
00279     protected:  
00280 
00282     static void assignCoordinates_(System& system, const QByteArray& data);
00284     static void extractCoordinates_(const System& system, QByteArray& data);
00286     static void assignCharges_(System& system, const QByteArray& data);
00288     static void extractCharges_(const System& system, QByteArray& data);
00290     static void encodeArray_(const std::vector<float>& v, QByteArray& a);
00292     static void decodeArray_(const QByteArray& a, std::vector<float>& v);
00293 
00294     // Database conection
00295     ErrorCode       error_;
00297     QSqlDatabase    db_;
00299     QSqlQuery*      query_;
00300     
00301     // Connection details
00302     String          database_name_;
00303     Size            port_;
00304     String          host_;
00305     String          username_;
00306     String          password_;
00307 
00308     private:
00309     // No copy construction allowed. Don't know what QSqlDatabase would
00310     // do in that case. It can't be copied anyhow...
00311     DBInterface(const DBInterface& /* interface */) {}
00312   };
00313 
00314 } // namespace BALL
00315 #endif // BALL_FORMAT_DBINTERFACE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines