tntdb::Connection Class Reference

This class holds a connection to a database. More...

#include <tntdb/bits/connection.h>

Public Types

typedef unsigned size_type

Public Member Functions

 Connection ()
 Create an empty Connection object.
 Connection (IConnection *conn)
 Create a Connection object from conn.
void close ()
 Remove the reference to the connected database.
void beginTransaction ()
 Start a transaction.
void commitTransaction ()
 Commit the current transaction.
void rollbackTransaction ()
 Roll back the current transaction.
size_type execute (const std::string &query)
 Execute a static query without returning results.
Result select (const std::string &query)
 Execute a static query which returns a result.
Row selectRow (const std::string &query)
 Execute a static query which returns a result.
Value selectValue (const std::string &query)
 Execute a static query which returns a result.
Statement prepare (const std::string &query)
 Create a new Statement object with the given query.
Statement prepareWithLimit (const std::string &query, const std::string &limit, const std::string &offset=std::string())
 Create a new Statement object with the given query added by a range limitation.
Statement prepareCached (const std::string &query)
 Create a new Statement object with the given query and store it in a cache.
Statement prepareCached (const std::string &query, const std::string &key)
 Create a new Statement object with the given query and caching key.
Statement prepareCachedWithLimit (const std::string &query, const std::string &limit, const std::string &offset=std::string())
 Create a new Statement object with the given query added by a range limitation.
Statement prepareCachedWithLimit (const std::string &query, const std::string &limit, const std::string &offset, const std::string &key)
 Create a new Statement object with the given query added by a range limitation.
void clearStatementCache ()
 Clear the Statement cache used from prepareCached()
bool clearStatementCache (const std::string &key)
 Remove a query from the statement cache.
bool ping ()
 Check whether the connection is alive.
long lastInsertId (const std::string &name=std::string())
 Get the last inserted insert id.
bool operator! () const
 Check if a connection is established (true if not)
const IConnectiongetImpl () const
IConnectiongetImpl ()

Detailed Description

This class holds a connection to a database.

Normally you would create a connection with tntdb::connect(url). The actual connection is reference counted. You can copy this class as you need. When the last copy of it is destroyed, the connection is closed.

Example:

try
{
tntdb::Connection conn = tntdb::connect("postgresql:dbname=mydatabase");
tntdb::Statement st = conn.prepare("SELECT col1, col2 FROM mytable");
for (tntdb::Statement::const_iterator it = res.begin(); it != res.end(); ++it)
std::cout << "col1 = " << it->getString() << ", col2 = " << it->getInt() << std::endl;
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}

Member Typedef Documentation

typedef unsigned tntdb::Connection::size_type

Constructor & Destructor Documentation

tntdb::Connection::Connection ( )
inline

Create an empty Connection object.

tntdb::Connection::Connection ( IConnection conn)
inline

Create a Connection object from conn.

Member Function Documentation

void tntdb::Connection::beginTransaction ( )

Start a transaction.

Normally this is not needed. It is better to use the class Transaction instead.

void tntdb::Connection::clearStatementCache ( )
inline

Clear the Statement cache used from prepareCached()

bool tntdb::Connection::clearStatementCache ( const std::string &  key)
inline

Remove a query from the statement cache.

The return value is true if the given key was found in the cache, false otherwise.

void tntdb::Connection::close ( )
inline

Remove the reference to the connected database.

If this was the last reference, the connection is actually closed.

void tntdb::Connection::commitTransaction ( )

Commit the current transaction.

size_type tntdb::Connection::execute ( const std::string &  query)

Execute a static query without returning results.

The query normally is an INSERT, UPDATE or DELETE statement. As with the other query execution methods this should be used only for static queries. If you need to pass parameters it is always better to use the Statement class.

const IConnection* tntdb::Connection::getImpl ( ) const
inline

Get the actual implementation object

IConnection* tntdb::Connection::getImpl ( )
inline

Get the actual implementation object

long tntdb::Connection::lastInsertId ( const std::string &  name = std::string())
inline

Get the last inserted insert id.

bool tntdb::Connection::operator! ( ) const
inline

Check if a connection is established (true if not)

bool tntdb::Connection::ping ( )
inline

Check whether the connection is alive.

Statement tntdb::Connection::prepare ( const std::string &  query)

Create a new Statement object with the given query.

Statement tntdb::Connection::prepareCached ( const std::string &  query)
inline

Create a new Statement object with the given query and store it in a cache.

When called again with the same query, the cached object is returned

Statement tntdb::Connection::prepareCached ( const std::string &  query,
const std::string &  key 
)

Create a new Statement object with the given query and caching key.

Per default, prepareCached() uses the query as key which identifies a Statement object in the cache. With this method you can provide your own key (because shorter keys mean faster lookup in the cache). Be aware though that when using this, you have to ensure the same key won't be used twice.

Statement tntdb::Connection::prepareCachedWithLimit ( const std::string &  query,
const std::string &  limit,
const std::string &  offset = std::string() 
)
inline

Create a new Statement object with the given query added by a range limitation.

This is like prepareWithLimit but the prepared statement cache is used.

Statement tntdb::Connection::prepareCachedWithLimit ( const std::string &  query,
const std::string &  limit,
const std::string &  offset,
const std::string &  key 
)

Create a new Statement object with the given query added by a range limitation.

This is like prepareWithLimit but the prepared statement cache is used. Like in prepareCached an optional key can be used.

Statement tntdb::Connection::prepareWithLimit ( const std::string &  query,
const std::string &  limit,
const std::string &  offset = std::string() 
)

Create a new Statement object with the given query added by a range limitation.

The limit and offset parameters are the names of the host variables, which receive the limits. The offset is optional. When an empty string is passed to offset, just the limit is used.

void tntdb::Connection::rollbackTransaction ( )

Roll back the current transaction.

Result tntdb::Connection::select ( const std::string &  query)

Execute a static query which returns a result.

The query normally is a SELECT statement.

Row tntdb::Connection::selectRow ( const std::string &  query)

Execute a static query which returns a result.

The first row is returned. If the query returns an empty result, a NotFound exception is thrown.

Value tntdb::Connection::selectValue ( const std::string &  query)

Execute a static query which returns a result.

The first value of the first row is returned. If the query returns an empty result, a NotFound exception is thrown.


The documentation for this class was generated from the following file: