#include <OpenMS/FORMAT/DB/DBConnection.h>
Public Member Functions | |
DBConnection () | |
Default constructor. | |
~DBConnection () | |
Destructor. | |
void | connect (const std::string &db, const std::string &user, const std::string &password, const std::string &host="localhost", UInt port=3306, const std::string &QTDBDriver=DB_PLUGIN, const std::string &connection_name="defaultConnection") throw (InvalidQuery) |
Connects to a SQL database. | |
bool | isConnected () const |
returns if a connection is established. | |
void | disconnect () |
disconnects from the SQL database. | |
void | executeQuery (const std::string &query, QSqlQuery &result) throw (InvalidQuery, NotConnected) |
Executes a query. | |
Int | getIntValue (const std::string &table, const std::string &column, const std::string &id) throw (InvalidQuery,NotConnected,Exception::ConversionError) |
Returns a single field of a table as an integer. | |
double | getDoubleValue (const std::string &table, const std::string &column, const std::string &id) throw (InvalidQuery,NotConnected,Exception::ConversionError) |
Returns a single field of a table as a double. | |
String | getStringValue (const std::string &table, const std::string &column, const std::string &id) throw (InvalidQuery,NotConnected,Exception::ConversionError) |
Returns a single field of a table as string. | |
UInt | getId (const std::string &table, const std::string &column, const std::string &value) throw (InvalidQuery,NotConnected) |
Looks up the ID for a specific entry in an table. | |
UInt | getAutoId () |
Returns the last auto_increment ID of the SQL database. | |
std::string | DBName () const |
Returns the name of the connected DB. | |
void | render (QSqlQuery &result, std::ostream &out=std::cout, const std::string &separator=" | ", const std::string &line_begin="", const std::string &line_end="\n") |
Dumps a query result in table format into a stream. | |
template<class StringListType> | |
void | executeQueries (const StringListType &queries) throw (InvalidQuery,NotConnected) |
Executes all SQL queries from an container. | |
Private Member Functions | |
QSqlQuery & | executeQuery_ (const std::string &query) throw (InvalidQuery,NotConnected) |
Executes internal queries. | |
Private Attributes | |
QSqlDatabase | db_handle_ |
The real database handle. | |
QSqlQuery * | lir_ |
A pointer to the result of the last query. | |
Classes | |
class | InvalidQuery |
Exception in case of an invalid Query. More... | |
class | NotConnected |
Exception in case of trying to execute a query without having established a database connection. More... |
DBConnection | ( | ) |
Default constructor.
~DBConnection | ( | ) |
Destructor.
void connect | ( | const std::string & | db, | |
const std::string & | user, | |||
const std::string & | password, | |||
const std::string & | host = "localhost" , |
|||
UInt | port = 3306 , |
|||
const std::string & | QTDBDriver = DB_PLUGIN , |
|||
const std::string & | connection_name = "defaultConnection" | |||
) | throw (InvalidQuery) |
Connects to a SQL database.
db | the database name | |
user | the login of the server | |
password | the password for the user | |
host | the host where the server is running (default: "localhost") | |
port | the port where the server is listening (default: 3306) | |
QTDBDriver | the QT database driver used for the connection (default: "QMYSQL", unless you canged it in configure. See "OpenMS/include/OpenMS/config.h") | |
connection_name | Name of the connection (needed for several concurrent connections only) |
bool isConnected | ( | ) | const |
returns if a connection is established.
void disconnect | ( | ) |
disconnects from the SQL database.
void executeQuery | ( | const std::string & | query, | |
QSqlQuery & | result | |||
) | throw (InvalidQuery, NotConnected) |
Executes a query.
query | the query itself | |
result | the results are written to this object |
Int getIntValue | ( | const std::string & | table, | |
const std::string & | column, | |||
const std::string & | id | |||
) | throw (InvalidQuery,NotConnected,Exception::ConversionError) |
Returns a single field of a table as an integer.
The table has to contain an id
column.
table | The table to look the field up | |
column | The column of the table | |
id | The id of the dataset |
double getDoubleValue | ( | const std::string & | table, | |
const std::string & | column, | |||
const std::string & | id | |||
) | throw (InvalidQuery,NotConnected,Exception::ConversionError) |
Returns a single field of a table as a double.
The table has to contain an id
column.
table | The table to look the field up | |
column | The column of the table | |
id | The id of the dataset |
String getStringValue | ( | const std::string & | table, | |
const std::string & | column, | |||
const std::string & | id | |||
) | throw (InvalidQuery,NotConnected,Exception::ConversionError) |
Returns a single field of a table as string.
The table has to contain an id
column.
table | The table to look the field up | |
column | The column of the table | |
id | The id of the dataset |
UInt getId | ( | const std::string & | table, | |
const std::string & | column, | |||
const std::string & | value | |||
) | throw (InvalidQuery,NotConnected) |
Looks up the ID for a specific entry in an table.
If several entries in the table have the desired value in the column, the first one is returned.
table | The table to look the field up | |
column | The column of the table | |
value | The value the selected column has |
UInt getAutoId | ( | ) |
Returns the last auto_increment ID of the SQL database.
std::string DBName | ( | ) | const |
Returns the name of the connected DB.
void render | ( | QSqlQuery & | result, | |
std::ostream & | out = std::cout , |
|||
const std::string & | separator = " | " , |
|||
const std::string & | line_begin = "" , |
|||
const std::string & | line_end = "\n" | |||
) |
Dumps a query result in table format into a stream.
To dump a result as HTML table, use render(result, cout,"</td><td>","<tr><td>","</td></tr>");
result | The result to render | |
out | The output stream to use | |
separator | The string between the fields | |
line_begin | The string at the beginning of each line | |
line_end | The string at the end of each line |
void executeQueries | ( | const StringListType & | queries | ) | throw (InvalidQuery,NotConnected) [inline] |
QSqlQuery& executeQuery_ | ( | const std::string & | query | ) | throw (InvalidQuery,NotConnected) [private] |
Executes internal queries.
This method does not change the last query and last result
QSqlDatabase db_handle_ [private] |
The real database handle.
QSqlQuery* lir_ [private] |
A pointer to the result of the last query.
Used for internal subqueries.
Generated Tue Apr 1 15:36:44 2008 -- using doxygen 1.5.4 | OpenMS / TOPP 1.1 |