1
// -*- mode: C++; tab-width: 2; -*-
4
// --------------------------------------------------------------------------
5
// OpenMS Mass Spectrometry Framework
6
// --------------------------------------------------------------------------
7
// Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
9
// This library is free software; you can redistribute it and/or
10
// modify it under the terms of the GNU Lesser General Public
11
// License as published by the Free Software Foundation; either
12
// version 2.1 of the License, or (at your option) any later version.
14
// This library is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
// Lesser General Public License for more details.
19
// You should have received a copy of the GNU Lesser General Public
20
// License along with this library; if not, write to the Free Software
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
// --------------------------------------------------------------------------
25
// $Authors: Marc Sturm $
26
// --------------------------------------------------------------------------
30
#include <OpenMS/FORMAT/DB/DBConnection.h>
33
#include <QtSql/QSqlQuery>
34
#include <QtSql/QSqlError>
35
#include <QtCore/QVariant>
36
#include <QtSql/QSqlRecord>
43
DBConnection::DBConnection()
49
DBConnection::~DBConnection()
54
void DBConnection::connect(const String& db, const String& user, const String& password, const String& host,UInt port,const String& QTDBDriver, const String& connection_name)
56
connection_name_ = connection_name.toQString();
58
QSqlDatabase::addDatabase(QTDBDriver.c_str(), connection_name_);
60
getDB_().setHostName(host.c_str());
61
getDB_().setUserName(user.c_str());
62
getDB_().setDatabaseName(db.c_str());
63
getDB_().setPassword(password.c_str());
64
getDB_().setPort(port);
68
String query = String("Connecting to DB '") + db + "' ( host: '"+host+"', port: '"+String(port)+"', user: '"+user+"', password: '"+password+"')";
70
String error = getDB_().lastError().databaseText();
74
throw InvalidQuery(__FILE__, __LINE__, __PRETTY_FUNCTION__,query,error);
78
bool DBConnection::isConnected() const
80
return getDB_().isOpen();
83
void DBConnection::disconnect()
86
QSqlDatabase::removeDatabase(connection_name_);
89
QSqlQuery DBConnection::executeQuery(const String& query, bool first)
91
QSqlDatabase db_handle = getDB_();
93
if (!db_handle.isOpen())
95
throw NotConnected(__FILE__, __LINE__, __PRETTY_FUNCTION__);
98
QSqlQuery result(db_handle);
101
if (!result.exec(query.c_str()))
103
throw InvalidQuery(__FILE__, __LINE__, __PRETTY_FUNCTION__,query,db_handle.lastError().text() );
105
if (first) result.first();
110
UInt DBConnection::getId(const String& table, const String& column, const String& value)
112
String query = String("SELECT id FROM ") + table + " WHERE " + column + "='" + value + "' LIMIT 1";
113
return executeQuery(query, true).value(0).toInt();
116
String DBConnection::DBName() const
118
QSqlDatabase db_handle = getDB_();
119
if (!db_handle.isOpen()) return "";
120
return db_handle.databaseName();
123
void DBConnection::render(QSqlQuery& result, ostream& out, const String& separator, const String& line_begin, const String& line_end)
126
//if res is still empty, do nothing
127
if (result.size()!=0)
129
Int col_count = result.record().count();
134
for (Int i = 0; i < col_count; i++)
141
out << result.record().fieldName(i).toStdString();
145
//set back internal pointer
149
while(result.isValid())
152
for (Int j = 0; j < col_count; j++)
158
out << result.value(j).toString().toAscii().data();
166
Int DBConnection::getIntValue(const String& table, const String& column, const String& id)
168
String query = String("SELECT ") + column + " FROM " + table + " WHERE id='" + id + "'";
170
QSqlQuery result = executeQuery(query, true);
172
if (!result.value(0).canConvert(QVariant::Int))
174
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__,"Conversion of QVariant to int failed!");
176
return result.value(0).toInt();
179
double DBConnection::getDoubleValue(const String& table, const String& column, const String& id)
181
String query = String("SELECT ") + column + " FROM " + table + " WHERE id='" + id + "'";
183
QSqlQuery result = executeQuery(query, true);
185
if (!result.value(0).canConvert(QVariant::Double))
187
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__,"Conversion of QVariant to double failed!");
189
return result.value(0).toDouble();
192
String DBConnection::getStringValue(const String& table, const String& column, const String& id)
194
String query = String("SELECT ") + column + " FROM " + table + " WHERE id='" + id + "'";
196
QSqlQuery result = executeQuery(query, true);
198
if (!result.value(0).canConvert(QVariant::String))
200
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__,"Conversion of QVariant to double failed!");
202
return result.value(0).toString();
205
UInt DBConnection::getAutoId()
207
QSqlQuery result = executeQuery("SELECT LAST_INSERT_ID()", true);
208
if (!result.value(0).canConvert(QVariant::Int))
210
throw Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__,"Conversion of QVariant to int failed in DBConnection::getAutoId()!");
212
return result.value(0).toInt();
216
// ---------------------------------------------------------------
220
// ---------------------------------------------------------------
222
DBConnection::InvalidQuery::InvalidQuery(const char* file, Int line, const char* function, const String& sql_query, const String& sql_error)
223
: BaseException(file, line, function, "Invalid Query", "an SQL query failed")
225
what_ = String("Query '")+sql_query +"' failed: '"+sql_error+"'";
226
Exception::globalHandler.setMessage(what_);
229
DBConnection::InvalidQuery::~InvalidQuery() throw()
233
DBConnection::NotConnected::NotConnected(const char* file, Int line, const char* function)
234
: BaseException(file, line, function, "Not Connected", "the DBConnection was accessed but it is not connected to a SQL database")
238
DBConnection::NotConnected::~NotConnected() throw()