Handles a connection between you and the database server. More...
#include <connection.h>
Public Member Functions | |
void | commit () |
Immediately commit all the transactions. | |
void | connect (const std::string &dns, const std::string &login, const std::string &passwd) |
Connect to a database. | |
connection (environment &env) | |
The constructor allocates a connection handle. | |
void | disconnect () |
Disconnect from the server. | |
bool | is_connected () const |
Check whether the connection is currently active. | |
void | rollback () |
Immediately rollback all the transactions. | |
void | set_attr (SQLINTEGER attr, SQLPOINTER ptr, SQLINTEGER length) |
Set a pointer connection attribute. | |
void | set_attr (SQLINTEGER attr, SQLINTEGER integer) |
Set a integer connection attribute. | |
~connection () | |
Ensures that the connection is closed. | |
Private Attributes | |
bool | f_connected |
A flag indicating whether the connection object is connected. | |
smartptr< environment > | f_environment |
The parent environment of this connection. |
Handles a connection between you and the database server.
This class is the one you need to use to connect to a database.
To use this object follow the following steps:
Definition at line 31 of file connection.h.
odbcpp::connection::connection | ( | environment & | env | ) |
The constructor allocates a connection handle.
This function allocates a connection handle.
The connection is in the DISCONNECTED state until you call the connect() function.
[in] | env | The parent environment of this connection |
odbcpp_error | If the connection handle cannot be allocated, throw an error. |
Definition at line 82 of file connection.cpp.
References odbcpp::handle::check(), f_environment, odbcpp::handle::f_handle, and odbcpp::handle::f_handle_type.
Referenced by is_connected().
odbcpp::connection::~connection | ( | ) |
Ensures that the connection is closed.
The destructor calls the disconnect function to ensure that the connection is closed before freeing the connection.
Definition at line 99 of file connection.cpp.
References disconnect().
Referenced by is_connected().
void odbcpp::connection::commit | ( | ) |
Immediately commit all the transactions.
This function sends a commit to all the transactions running on this connection.
If there are no transactions to commit, nothing happens.
odbcpp_error | And odbcpp_error will be thrown if the SQL function returns an error. |
Definition at line 218 of file connection.cpp.
References odbcpp::handle::check(), odbcpp::handle::f_handle, and odbcpp::handle::f_handle_type.
void odbcpp::connection::connect | ( | const std::string & | dsn, | |
const std::string & | login, | |||
const std::string & | passwd | |||
) |
Connect to a database.
This function attempts to connect to a database.
If it is possible that the connection fails, make sure that you try/catch this function call.
[in] | dsn | The virtual name of the database to connect to |
[in] | login | The login name of the person connecting |
[in] | passwd | The password of the person connecting |
odbcpp_error | If the connection fails, or an invalid name is specified, this function will throw an odbcpp_error exception. |
Definition at line 164 of file connection.cpp.
References odbcpp::handle::check(), f_connected, and odbcpp::handle::f_handle.
void odbcpp::connection::disconnect | ( | ) |
Disconnect from the server.
This function ensures that the connection gets closed.
If the connection is already closed, then the function will most certainly generate an odbcpp_error.
odbcpp_error | If the connection fails, or an invalid name is specified, this function will throw an odbcpp_error exception. |
Definition at line 185 of file connection.cpp.
References odbcpp::handle::check(), f_connected, and odbcpp::handle::f_handle.
Referenced by ~connection().
odbcpp::connection::is_connected | ( | ) | const [inline] |
Check whether the connection is currently active.
This function returns true if the connection object was successfully connected. This does not mean that the connection is still valid. Only that it was properly connected.
Definition at line 32 of file connection.h.
References connection(), f_connected, set_attr(), and ~connection().
void odbcpp::connection::rollback | ( | ) |
Immediately rollback all the transactions.
This function sends a rollback to all the transactions running on this connection.
If there are no transactions to rollback, nothing happens.
odbcpp_error | And odbcpp_error will be thrown if the SQL function returns an error. |
Definition at line 235 of file connection.cpp.
References odbcpp::handle::check(), odbcpp::handle::f_handle, and odbcpp::handle::f_handle_type.
void odbcpp::connection::set_attr | ( | SQLINTEGER | attr, | |
SQLPOINTER | ptr, | |||
SQLINTEGER | length | |||
) |
Set a pointer connection attribute.
This function sets one attribute in the connection.
[in] | attr | The attribute to be modified |
[in] | ptr | The pointer to the attribute data |
[in] | length | The size of the data pointed by ptr , can be SQL_NTS for strings |
odbcpp_error | And odbcpp_error will be thrown if the SQL function returns an error. |
Definition at line 143 of file connection.cpp.
References odbcpp::handle::check(), and odbcpp::handle::f_handle.
void odbcpp::connection::set_attr | ( | SQLINTEGER | attr, | |
SQLINTEGER | integer | |||
) |
Set a integer connection attribute.
This function sets one attribute in the connection.
[in] | attr | The attribute to be modified |
[in] | integer | The integer attribute data |
odbcpp_error | And odbcpp_error will be thrown if the SQL function returns an error. |
Definition at line 115 of file connection.cpp.
References odbcpp::handle::check(), odbcpp::handle::f_handle, and odbcpp::odbcpp_error::ODBCPP_INTERNAL.
Referenced by is_connected().
odbcpp::connection::f_connected [private] |
A flag indicating whether the connection object is connected.
Because there is no way to tell whether a connection is active or not, we use a flag to let ourselves know. This is not 100% rock solid as a network connection to a backend server could very well break and we still would indicate that the connection is live.
Use the connection::is_connected() const function to check this flag.
Definition at line 42 of file connection.h.
Referenced by connect(), disconnect(), and is_connected().
odbcpp::connection::f_environment [private] |
The parent environment of this connection.
The environment in which the connection is made.
Definition at line 41 of file connection.h.
Referenced by connection().