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 ()
| 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 IConnection * | getImpl () const
IConnection * | getImpl ()

## 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;
}
tntdb::Connection
This class holds a connection to a database.
Definition connection.h:67
tntdb::Connection::prepare
Statement prepare(const std::string &query)
Create a new Statement object with the given query.
tntdb::Statement::const_iterator
This class represents a database-cursor.
Definition statement_iterator.h:46
tntdb::Statement
This class represents an SQL statement.
Definition statement.h:73
tntdb::connect
Connection connect(const std::string &url, const std::string &username, const std::string &password)
Establish a connection to a database.

## Constructor & Destructor Documentation

## Connection() [1/2]

tntdb::Connection::Connection | ( |  | ) |

inline

Create an empty Connection object.

## Connection() [2/2]

tntdb::Connection::Connection | ( | IConnection * | conn | ) |

inline

Create a Connection object from conn.

## Member Function Documentation

## beginTransaction()

void tntdb::Connection::beginTransaction | ( |  | ) |

Start a transaction.

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

## clearStatementCache() [1/2]

void tntdb::Connection::clearStatementCache | ( |  | ) |

inline

Clear the Statement cache used from prepareCached().

## clearStatementCache() [2/2]

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.

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

Commit the current transaction.

## execute()

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.

## getImpl() [1/2]

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

inline

Get the actual implementation object

## getImpl() [2/2]

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

inline

Get the actual implementation object

## lastInsertId()

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

inline

Get the last inserted insert id.

## operator!()

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

inline

Check if a connection is established (true if not).

## ping()

bool tntdb::Connection::ping | ( |  | ) |

inline

Check whether the connection is alive.

## prepare()

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

Create a new Statement object with the given query.

## prepareCached() [1/2]

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

## prepareCached() [2/2]

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.

## prepareCachedWithLimit() [1/2]

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.

## prepareCachedWithLimit() [2/2]

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.

## prepareWithLimit()

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.

## rollbackTransaction()

void tntdb::Connection::rollbackTransaction | ( |  | ) |

Roll back the current transaction.

## select()

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

Execute a static query which returns a result.

The query normally is a SELECT statement.

## selectRow()

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.

## selectValue()

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:

- tntdb/bits/connection.h