Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
DBConnection Class Reference

A class for connecting to a SQL database. More...

#include <OpenMS/FORMAT/DB/DBConnection.h>

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...
 

Public Member Functions

 DBConnection ()
 Default constructor. More...
 
 ~DBConnection ()
 Destructor. More...
 
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")
 Connects to a SQL database. More...
 
bool isConnected () const
 returns if a connection is established. More...
 
void disconnect ()
 disconnects from the SQL database More...
 
QSqlQuery executeQuery (const String &query, bool first=false)
 Executes a query and returns the result. More...
 
Int getIntValue (const String &table, const String &column, const String &id)
 Returns a single field of a table as an integer. More...
 
double getDoubleValue (const String &table, const String &column, const String &id)
 Returns a single field of a table as a double. More...
 
String getStringValue (const String &table, const String &column, const String &id)
 Returns a single field of a table as string. More...
 
UInt getId (const String &table, const String &column, const String &value)
 Looks up the ID for a specific entry in an table. More...
 
UInt getAutoId ()
 Returns the last auto_increment ID of the SQL database. More...
 
String DBName () const
 Returns the name of the connected DB. More...
 
void render (QSqlQuery &result, std::ostream &out=std::cout, const String &separator=" | ", const String &line_begin="", const String &line_end="\n")
 Dumps a query result in table format into a stream. More...
 
template<class StringListType >
void executeQueries (const StringListType &queries)
 Executes all SQL queries from an container. More...
 

Private Member Functions

QSqlDatabase getDB_ () const
 Retruns the current database connection defined by connection_name_. More...
 

Private Attributes

QString connection_name_
 Name (handle) of the connection. More...
 

Detailed Description

A class for connecting to a SQL database.

Note
Do not use '*' in SELECT statments. The order of result columns is not definded then! Read the QT documentation for details.

Constructor & Destructor Documentation

Default constructor.

Destructor.

Member Function Documentation

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" 
)

Connects to a SQL database.

Parameters
dbthe database name
userthe login of the server
passwordthe password for the user
hostthe host where the server is running (default: "localhost")
portthe port where the server is listening (default: 3306)
QTDBDriverthe QT database driver used for the connection (default: "QMYSQL", unless you canged it in configure. See "OpenMS/include/OpenMS/config.h")
connection_nameName of the connection (needed for several concurrent connections only)
Exceptions
InvalidQueryis thrown if the database connection could not be opened

Referenced by TOPPViewBase::connectToDB_().

String DBName ( ) const

Returns the name of the connected DB.

void disconnect ( )

disconnects from the SQL database

Note
In order to disconnect, all queries running on a database must be closed. See the QSqlDatabase::removeDatabase(...) documentation.

Referenced by TOPPViewBase::openDatabaseDialog().

void executeQueries ( const StringListType &  queries)

Executes all SQL queries from an container.

Each line has to be a query or empty.

Parameters
queriesA STL-compliant container of OpenMS String objects
Exceptions
InvalidQueryis thrown if an invalid SQL query was given
NotConnectedif there is no database connection

References DBConnection::executeQuery(), and String::trim().

QSqlQuery executeQuery ( const String query,
bool  first = false 
)

Executes a query and returns the result.

The internal pointer of the returned result is positioned before the first row.

Parameters
queryan SQL query
firstif true, the internal pointer of the returned result is positioned to the first record.
Exceptions
InvalidQueryis thrown if an invalid SQL query was given
NotConnectedif there is no database connection

Referenced by TOPPViewBase::addDataDB(), DBConnection::executeQueries(), DBAdapter::loadExperiment(), DBAdapter::loadSpectrum(), and DBAdapter::storeExperiment().

UInt getAutoId ( )

Returns the last auto_increment ID of the SQL database.

Referenced by DBAdapter::storeExperiment().

QSqlDatabase getDB_ ( ) const
inlineprivate

Retruns the current database connection defined by connection_name_.

double getDoubleValue ( const String table,
const String column,
const String id 
)

Returns a single field of a table as a double.

The table has to contain an id column.

Parameters
tableThe table to look the field up
columnThe column of the table
idThe id of the dataset
Exceptions
InvalidQueryis thrown if an invalid SQL query was given
NotConnectedif there is no database connection
Exception::ConversionErroris thrown if the value could not be converted to the requested type
UInt getId ( const String table,
const String column,
const String value 
)

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.

Parameters
tableThe table to look the field up
columnThe column of the table
valueThe value the selected column has
Exceptions
InvalidQueryis thrown if an invalid SQL query was given
NotConnectedif there is no database connection
Int getIntValue ( const String table,
const String column,
const String id 
)

Returns a single field of a table as an integer.

The table has to contain an id column.

Parameters
tableThe table to look the field up
columnThe column of the table
idThe id of the dataset
Exceptions
InvalidQueryis thrown if an invalid SQL query was given
NotConnectedif there is no database connection
Exception::ConversionErroris thrown if the value could not be converted to the requested type
String getStringValue ( const String table,
const String column,
const String id 
)

Returns a single field of a table as string.

The table has to contain an id column.

Parameters
tableThe table to look the field up
columnThe column of the table
idThe id of the dataset
Exceptions
InvalidQueryis thrown if an invalid SQL query was given
NotConnectedif there is no database connection
Exception::ConversionErroris thrown if the value could not be converted to the requested type
bool isConnected ( ) const

returns if a connection is established.

Referenced by TOPPViewBase::addDataDB(), TOPPViewBase::metadataDatabaseDialog(), and TOPPViewBase::openDatabaseDialog().

void render ( QSqlQuery &  result,
std::ostream &  out = std::cout,
const String separator = " | ",
const String line_begin = "",
const 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,"&lt;/td&gt;&lt;td&gt;","&lt;tr&gt;&lt;td&gt;","&lt;/td&gt;&lt;/tr&gt;");

Parameters
resultThe result to render
outThe output stream to use
separatorThe string between the fields
line_beginThe string at the beginning of each line
line_endThe string at the end of each line

Member Data Documentation

QString connection_name_
private

Name (handle) of the connection.


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