Made to Order Software Corporation Logo

odbcpp: odbcpp::connection Class Reference

Handles a connection between you and the database server. More...

#include <connection.h>

Inheritance diagram for odbcpp::connection:
Collaboration diagram for odbcpp::connection:

List of all members.

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< environmentf_environment
 The parent environment of this connection.


Detailed Description

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:

  • Create an environment object
  • Create a connection object using that environment as the parent
  • Setup attributes as required
  • Call connect() with valid parameters
See also:
connection(environment& env)
set_attr(SQLINTEGER attr, SQLINTEGER integer)
set_attr(SQLINTEGER attr, SQLPOINTER ptr, SQLINTEGER length)
connect(const std::string& dsn, const std::string& login, const std::string& passwd)

Definition at line 31 of file connection.h.


Constructor & Destructor Documentation

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.

Parameters:
[in] env The parent environment of this connection
Exceptions:
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().


Member Function Documentation

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.

Exceptions:
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.

Parameters:
[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
Exceptions:
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.

Exceptions:
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.

Returns:
true if a call to connect() succeeded
See also:
connect()
disconnect()

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.

Exceptions:
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.

Parameters:
[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
Exceptions:
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.

Parameters:
[in] attr The attribute to be modified
[in] integer The integer attribute data
Exceptions:
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().


Member Data Documentation

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

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


The documentation for this class was generated from the following files:
Generated on Mon Sep 19 12:52:27 2011 for odbcpp by  doxygen 1.6.3