tntdb  1.3
tntdb::Connection Class Reference

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

#include <connection.h>

Public Types

typedef unsigned size_type
 

Public Member Functions

 Connection ()
 Instantiate a empty connection-object. More...
 
 Connection (IConnection *conn_)
 Initialize this class with a connection. More...
 
void close ()
 Remove the reference to the connected database. More...
 
void beginTransaction ()
 Starts a transaction. More...
 
void commitTransaction ()
 Commits the current transaction. More...
 
void rollbackTransaction ()
 Rolls back the current transaction. More...
 
size_type execute (const std::string &query)
 Executes a static query, without returning results. More...
 
Result select (const std::string &query)
 Executes a static query, which returns a result. More...
 
Row selectRow (const std::string &query)
 Executes a static query, which returns a result. More...
 
Value selectValue (const std::string &query)
 Executes a static query, which returns a result. More...
 
Statement prepare (const std::string &query)
 Creates a new Statement-object, with the given query. More...
 
Statement prepareCached (const std::string &query, const std::string &key)
 Like prepareCached but use the passed string as a cache key. More...
 
Statement prepareCached (const std::string &query)
 Creates a new Statement-object, with the given query and stores the statement in a cache. More...
 
void clearStatementCache ()
 Clears the cache, built with prepareCache. More...
 
bool clearStatementCache (const std::string &key)
 Remove a query from the statement cache. More...
 
bool ping ()
 returns true, if connection is alive. More...
 
long lastInsertId (const std::string &name=std::string())
 returns the last inserted insert id. More...
 
bool operator! () const
 Returns true, when there is no connection established. More...
 
const IConnection * getImpl () const
 Returns the actual implementation-class. More...
 
IConnection * getImpl ()
 Returns the actual implementation-class. More...
 

Detailed Description

This class holds a connection to a database.

Normally you will create a connection with tntdb::connect(url). The actual connection is referencecounted. 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::Result res = conn.select("select col1, col2 from mytable");
for (tntdb::Result::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;
}

Constructor & Destructor Documentation

◆ Connection() [1/2]

tntdb::Connection::Connection ( )
inline

Instantiate a empty connection-object.

◆ Connection() [2/2]

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

Initialize this class with a connection.

Member Function Documentation

◆ beginTransaction()

void tntdb::Connection::beginTransaction ( )

Starts a transaction.

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

◆ clearStatementCache() [1/2]

void tntdb::Connection::clearStatementCache ( )
inline

Clears the cache, built with prepareCache.

◆ clearStatementCache() [2/2]

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

Remove a query from the statement cache.

The return value indicates, if the query was found in the cache. When the query was found, true is returned.

◆ close()

void tntdb::Connection::close ( )
inline

Remove the reference to the connected database.

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

◆ commitTransaction()

void tntdb::Connection::commitTransaction ( )

Commits the current transaction.

◆ execute()

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

Executes a static query, without returning results.

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

◆ getImpl() [1/2]

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

Returns the actual implementation-class.

◆ getImpl() [2/2]

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

Returns the actual implementation-class.

◆ lastInsertId()

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

returns the last inserted insert id.

◆ operator!()

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

Returns true, when there is no connection established.

◆ ping()

bool tntdb::Connection::ping ( )
inline

returns true, if connection is alive.

◆ prepare()

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

Creates a new Statement-object, with the given query.

◆ prepareCached() [1/2]

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

Like prepareCached but use the passed string as a cache key.

Since queries are normally quite lengthy, they are not that fast as a lookup key. For better performance of the cache it is feasible to give a shorter key for the query. But it is the user, who is in charge to use unique keys.

◆ prepareCached() [2/2]

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

Creates a new Statement-object, with the given query and stores the statement in a cache.

When called again with the same query, the cached result is returned.

◆ rollbackTransaction()

void tntdb::Connection::rollbackTransaction ( )

Rolls back the current transaction.

◆ select()

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

Executes a static query, which returns a result.

The query is normally a SELECT-statement.

◆ selectRow()

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

Executes a static query, which returns a result.

The first row is returned. If the query does not return rows, the exception tntdb::NotFound is thrown.

◆ selectValue()

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

Executes a static query, which returns a result.

The first value of the first row is returned. If the query does not return rows, the exception tntdb::NotFound is thrown.


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