Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

DBConnection Class Reference
[Database IO]

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

List of all members.


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.

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

Constructor & Destructor Documentation

DBConnection (  ) 

Default constructor.

~DBConnection (  ) 

Destructor.


Member Function Documentation

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.

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

Note:
Make sure that the result was created after connecting to the DB! Otherwise the query fails!
Parameters:
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.

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

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

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

Parameters:
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,"&lt;/td&gt;&lt;td&gt;","&lt;tr&gt;&lt;td&gt;","&lt;/td&gt;&lt;/tr&gt;");

Parameters:
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]

Executes all SQL queries from an container.

Each line has to be a query or empty.

Parameters:
queries A STL-compliant container of OpenMS String objects

QSqlQuery& executeQuery_ ( const std::string &  query  )  throw (InvalidQuery,NotConnected) [private]

Executes internal queries.

This method does not change the last query and last result


Member Data Documentation

QSqlDatabase db_handle_ [private]

The real database handle.

QSqlQuery* lir_ [private]

A pointer to the result of the last query.

Used for internal subqueries.


The documentation for this class was generated from the following file:
Generated Tue Apr 1 15:36:44 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1