Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
DBConnection.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2013.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: $
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FORMAT_DB_DBCONNECTION_H
36 #define OPENMS_FORMAT_DB_DBCONNECTION_H
37 
38 #include <OpenMS/config.h>
39 
40 //QT includes
41 #include <QtSql/QSqlDatabase>
42 #include <QtSql/QSqlQuery>
43 
44 //OpenMS includes
47 #include <OpenMS/CONCEPT/Types.h>
48 
49 #include <iostream>
50 
51 namespace OpenMS
52 {
60  class OPENMS_DLLAPI DBConnection
61  {
62 public:
70  class OPENMS_DLLAPI InvalidQuery :
72  {
73 public:
74  InvalidQuery(const char * file, Int line, const char * function, const String & sql_query, const String & sql_error);
75  ~InvalidQuery() throw();
76  };
77 
85  class OPENMS_DLLAPI NotConnected :
87  {
88 public:
89  NotConnected(const char * file, Int line, const char * function);
90  ~NotConnected() throw();
91  };
92 
93 
94 
96  DBConnection();
97 
99  ~DBConnection();
100 
114  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");
115 
117  bool isConnected() const;
118 
124  void disconnect();
125 
138  QSqlQuery executeQuery(const String & query, bool first = false);
139 
153  Int getIntValue(const String & table, const String & column, const String & id);
154 
169  double getDoubleValue(const String & table, const String & column, const String & id);
170 
184  String getStringValue(const String & table, const String & column, const String & id);
185 
198  UInt getId(const String & table, const String & column, const String & value);
199 
201  UInt getAutoId();
202 
204  String DBName() const;
205 
217  void render(QSqlQuery & result, std::ostream & out = std::cout, const String & separator = " | ", const String & line_begin = "", const String & line_end = "\n");
218 
219 
230  template <class StringListType>
231  void executeQueries(const StringListType & queries);
232 
233 private:
234 
237 
239  inline QSqlDatabase getDB_() const
240  {
241  return QSqlDatabase::database(connection_name_, false);
242  }
243 
244  };
245 
246 
247 
248  //---------------------------------------------------------------
249  // Implementation of the inline / template functions
250  //---------------------------------------------------------------
251 
252  template <class StringListType>
253  void DBConnection::executeQueries(const StringListType & queries)
254  {
255  String line;
256  for (typename StringListType::const_iterator it = queries.begin(); it != queries.end(); ++it)
257  {
258  line = *it;
259  line.trim();
260  if (line != "")
261  {
262  executeQuery(line);
263  }
264  }
265  }
266 
267 }
268 
269 #endif
QSqlQuery executeQuery(const String &query, bool first=false)
Executes a query and returns the result.
A more convenient string class.
Definition: String.h:56
QString connection_name_
Name (handle) of the connection.
Definition: DBConnection.h:236
void executeQueries(const StringListType &queries)
Executes all SQL queries from an container.
Definition: DBConnection.h:253
Exception in case of an invalid Query.
Definition: DBConnection.h:70
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
A class for connecting to a SQL database.
Definition: DBConnection.h:60
Exception base class.
Definition: Exception.h:90
QSqlDatabase getDB_() const
Retruns the current database connection defined by connection_name_.
Definition: DBConnection.h:239
int Int
Signed integer type.
Definition: Types.h:100
Exception in case of trying to execute a query without having established a database connection...
Definition: DBConnection.h:85

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:12 using doxygen 1.8.5