BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DBInterface.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_FORMAT_DBINTERFACE_H
6 #define BALL_FORMAT_DBINTERFACE_H
7 
8 #include <BALL/KERNEL/system.h>
9 #include <BALL/KERNEL/molecule.h>
10 #include <BALL/FORMAT/MOLFile.h>
11 #include <BALL/COMMON/exception.h>
12 
13 #include <QSqlRecord>
14 #include <QSqlDatabase>
15 #include <QSqlQuery>
16 #include <QtCore/QVariant>
17 
18 namespace BALL
19 {
20 
37  class BALL_EXPORT DBInterface
38  {
39  public:
40 
41  class InvalidQuery
42  : public Exception::GeneralException
43  {
44  public:
45  InvalidQuery(const char* file, int line, const String& s = "<unknown query>") ;
46  virtual ~InvalidQuery() throw() {}
47 
48  protected:
49  std::string query_;
50  };
51 
52  class NotConnected
53  : public Exception::GeneralException
54  {
55  public:
56  NotConnected(const char* file, int line, const String& s = "<not connected>") ;
57  virtual ~NotConnected() throw() {}
58 
59  protected:
60  std::string query_;
61  };
62 
64  class InconsistentTopology
65  : public Exception::GeneralException
66  {
67  public:
68  InconsistentTopology(const char* file, int line, const String& s = "<inconsistent topology>") ;
69  virtual ~InconsistentTopology() throw() {}
70 
71  protected:
72  std::string query_;
73  };
74 
77 
79  typedef qlonglong ID;
80 
82  typedef std::vector<ID> IDVector;
83 
87  typedef std::pair<String, String> ConformationMethod;
88 
92  typedef std::pair<String, String> ChargeMethod;
94 
97 
99  enum ErrorCodes
100  {
101  NO_ERROR,
102  NO_CONNECTION // the database was not connected/initialized (good() = false)
103  };
104 
106  static const String BALL_DEFAULT_DBRCFILE;
108  static const String BALL_DEFAULT_DATABASE_HOST;
110  static const Size BALL_DEFAULT_DATABASE_PORT;
112  static const String BALL_DEFAULT_DATABASE_DRIVER;
114  static const String BALL_DEFAULT_DATABASE_NAME;
116  static const String BALL_DEFAULT_DATABASE_SECTIONNAME;
118 
119 
122  DBInterface();
125  virtual ~DBInterface() {}
127 
130  void getTopology(ID topology, System& system);
136  ID newTopology(const System& system, const String& name, const String& source_id, ID id = 0);
138  void setTopology(ID topology, const System& system);
139 
141  IDVector getConformationList(ID topology_od);
143  IDVector getConformationList(ID topology_id, ID method_id);
145  void loadConformation(const ID conformation, System& system);
146 
148  ID storeConformation(ID topology_id, ID method_id, const System& system,double energy);
150  ID storeConformation(ID topology, ID method_ID, const System& system);
151 
153  IDVector getConformationMethods();
155  ConformationMethod getConformationMethod(ID method_id);
157  ID getConformationMethod(const String& method, const String& parameters);
159  ID newConformationMethod(const String& method, const String& parameters);
160 
162  ID storeCharges(ID topology_id, ID method_id, const System& system);
164  void loadCharges(const ID charge_id, System& system);
166  IDVector getChargeMethods();
168  ChargeMethod getChargeMethod(DBInterface::ID method_id);
170  ID getChargeMethod(const String& method, const String& parameters);
172  ID newChargeMethod(const String& method, const String& parameters);
173 
175  ErrorCode getError() const { return error_; }
177  void setError(ErrorCode error) { error_ = error; }
179  bool good() const { return error_ == 0; }
181 
191  bool connect
192  (const String& user, const String& password,
193  const String& database_name = "structures", const String& host = "diclofenac.informatik.uni-tuebingen.de",
194  Size port = 3306, const String& driver = "QMYSQL3");
195 
215  bool connect();
216 
221  QSqlQuery& executeQuery(const String& query_string);
222 
227  QSqlQuery& executeQuery();
228 
230  QSqlQuery& query() { return *query_; }
231 
233  String executedQuery() { return query_->executedQuery().toStdString();}
234 
236  bool first() { return query_->first(); }
237 
239  bool last() { return query_->last(); }
240 
242  bool next() { return query_->next(); }
243 
247  IDVector extractIDs();
248 
250  bool prev() { return query_->previous(); }
251 
253  ID lastInsertedID();
254 
256  Size size() { return (Size)query_->size(); }
257 
259  QVariant value(Position k) { return query_->value(k); }
260 
262  void prepare(const String& s) { query_->prepare(s.c_str()); }
263 
265  void addBindValue(const QVariant& v) { query_->addBindValue(v); }
266  void addBindValue(const QVariant& v1, const QVariant& v2) { addBindValue(v1); addBindValue(v2);}
267  void addBindValue(const QVariant& v1, const QVariant& v2, const QVariant& v3) { addBindValue(v1); addBindValue(v2); addBindValue(v3);}
268  void addBindValue(const QVariant& v1, const QVariant& v2, const QVariant& v3, const QVariant& v4) { addBindValue(v1); addBindValue(v2); addBindValue(v3); addBindValue(v4);}
270 
273  bool getMOLFile(ID id, String& file_text, String& name, String& source_id);
276  void addMOLFileToSystem(const String& molfile, System& system);
278 
279  protected:
280 
282  static void assignCoordinates_(System& system, const QByteArray& data);
284  static void extractCoordinates_(const System& system, QByteArray& data);
286  static void assignCharges_(System& system, const QByteArray& data);
288  static void extractCharges_(const System& system, QByteArray& data);
290  static void encodeArray_(const std::vector<float>& v, QByteArray& a);
292  static void decodeArray_(const QByteArray& a, std::vector<float>& v);
293 
294  // Database conection
295  ErrorCode error_;
297  QSqlDatabase db_;
299  QSqlQuery* query_;
300 
301  // Connection details
302  String database_name_;
303  Size port_;
304  String host_;
305  String username_;
306  String password_;
307 
308  private:
309  // No copy construction allowed. Don't know what QSqlDatabase would
310  // do in that case. It can't be copied anyhow...
311  DBInterface(const DBInterface& /* interface */) {}
312  };
313 
314 } // namespace BALL
315 #endif // BALL_FORMAT_DBINTERFACE_H
BALL_INDEX_TYPE ErrorCode
BALL_EXTERN_VARIABLE const double k
Definition: constants.h:93
BALL_SIZE_TYPE Size
#define BALL_EXPORT
Definition: COMMON/global.h:50
BALL_SIZE_TYPE Position