Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

Connection Class Reference

#include <tntdb/bits/connection.h>

List of all members.

Public Types

typedef unsigned size_type

Public Member Functions

 Connection ()
 Connection (IConnection *conn_)
void close ()
void beginTransaction ()
void commitTransaction ()
void rollbackTransaction ()
size_type execute (const std::string &query)
Result select (const std::string &query)
Row selectRow (const std::string &query)
Value selectValue (const std::string &query)
Statement prepare (const std::string &query)
Statement prepareCached (const std::string &query)
void clearStatementCache ()
bool ping ()
bool operator! () const
const IConnection * getImpl () const


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

Instantiate a empty connection-object.

Connection ( IConnection *  conn_  ) 

Initialize this class with a connection.


Member Function Documentation

void close (  ) 

Remove the reference to the connected database. If this was the last reference, the connection is actually closed.

void beginTransaction (  ) 

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

void commitTransaction (  ) 

Commits the current transaction.

void rollbackTransaction (  ) 

Rolls back the current transaction.

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

Result select ( const std::string &  query  ) 

Executes a static query, which returns a result. The query is normally a SELECT-statement.

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

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

Statement prepare ( const std::string &  query  ) 

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

Statement prepareCached ( const std::string &  query  ) 

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.

void clearStatementCache (  ) 

Clears the cache, built with prepareCache.

bool ping (  ) 

returns true, if connection is alive

bool operator! (  )  const

Returns true, when there is no connection established.

const IConnection* getImpl (  )  const

Returns the actual implementation-class.

Copyright © 2008 The Tntnet Development Team
Tntnet 1.6