tntdb 1.4
|
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 () |
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:
|
inline |
Create an empty Connection object.
|
inline |
Create a Connection object from conn.
void tntdb::Connection::beginTransaction | ( | ) |
Start a transaction.
Normally this is not needed. It is better to use the class Transaction instead.
|
inline |
Clear the Statement cache used from prepareCached()
|
inline |
Remove a query from the statement cache.
The return value is true if the given key was found in the cache, false otherwise.
|
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.
|
inline |
Get the actual implementation object
|
inline |
Get the actual implementation object
|
inline |
Get the last inserted insert id.
|
inline |
Check if a connection is established (true if not)
|
inline |
Check whether the connection is alive.
Statement tntdb::Connection::prepare | ( | const std::string & | query | ) |
Create a new Statement object with the given 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, | ||
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.
|
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::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.