#include <odbcDatabase.hpp>

Public Member Functions | |
| ODBCDatabase (const std::string &databaseName, const std::string &userName, const std::string &passWord) throw (Error) | |
| virtual | ~ODBCDatabase (void) |
| Destructor. | |
| virtual StatementPtr | execQuery (const std::string &query) throw (Error) |
Protected Attributes | |
| SQLHENV | odbcEnvironment |
| ODBC environment handle. | |
| SQLHDBC | connection |
| Connection handle. | |
Definition at line 27 of file odbcDatabase.hpp.
| ODBCDatabase::ODBCDatabase | ( | const std::string & | databaseName, | |
| const std::string & | userName, | |||
| const std::string & | passWord | |||
| ) | throw (Error) |
Constructor establishing connection to the data source.
| databaseName | Name of unixODBC's data source. | |
| userName | Name of database user. | |
| passWord | Password of user. The password will be passed to the SQLConnect command. |
Definition at line 24 of file odbcDatabase.cpp.
References odbcWrap().
00026 : 00027 Database( userName ), odbcEnvironment( NULL ), connection( NULL ) 00028 { 00029 00030 bool connected = false; 00031 00032 try { 00033 // 1. allocate Environment handle and register version 00034 odbcWrap( SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, 00035 &odbcEnvironment ), SQL_NULL_HANDLE, NULL ); 00036 00037 odbcWrap( SQLSetEnvAttr( odbcEnvironment, SQL_ATTR_ODBC_VERSION, 00038 (void*)SQL_OV_ODBC3, 0 ), 00039 SQL_HANDLE_ENV, odbcEnvironment ); 00040 00041 odbcWrap( SQLAllocHandle( SQL_HANDLE_DBC, odbcEnvironment, &connection ), 00042 SQL_HANDLE_ENV, odbcEnvironment ); 00043 00044 // 2. allocate connection handle, set timeout 00045 odbcWrap( SQLSetConnectAttr( connection, SQL_LOGIN_TIMEOUT, 00046 (SQLPOINTER *)5, 0 ), 00047 SQL_HANDLE_DBC, connection ); 00048 00049 // 3. Connect to the datasource "web" 00050 odbcWrap( SQLConnect( connection, (SQLCHAR*)databaseName.c_str(), SQL_NTS, 00051 (SQLCHAR*)userName.c_str(), SQL_NTS, 00052 (SQLCHAR*)passWord.c_str(), SQL_NTS ), 00053 SQL_HANDLE_DBC, connection ); 00054 00055 connected = true; 00056 00057 } catch ( Error &e ) { 00058 00059 if ( connected ) { 00060 assert( connection ); 00061 SQLDisconnect( connection ); 00062 }; 00063 if ( connection ) 00064 SQLFreeHandle( SQL_HANDLE_DBC, connection ); 00065 if ( odbcEnvironment ) 00066 SQLFreeHandle( SQL_HANDLE_ENV, odbcEnvironment ); 00067 throw e; 00068 00069 }; 00070 00071 };
| ODBCDatabase::~ODBCDatabase | ( | void | ) | [virtual] |
Destructor.
Definition at line 73 of file odbcDatabase.cpp.
References connection, and odbcEnvironment.
00074 { 00075 assert( connection ); 00076 SQLDisconnect( connection ); 00077 SQLFreeHandle( SQL_HANDLE_DBC, connection ); 00078 assert( odbcEnvironment ); 00079 SQLFreeHandle( SQL_HANDLE_ENV, odbcEnvironment ); 00080 }
| StatementPtr ODBCDatabase::execQuery | ( | const std::string & | query | ) | throw (Error) [virtual] |
Execute single query. The returned Statement object allows requesting the results of the query.
Implements Database.
Definition at line 82 of file odbcDatabase.cpp.
References connection.
00083 { 00084 StatementPtr retVal( new ODBCStatement( connection, query ) ); 00085 return retVal; 00086 }
SQLHENV ODBCDatabase::odbcEnvironment [protected] |
ODBC environment handle.
Definition at line 44 of file odbcDatabase.hpp.
Referenced by ~ODBCDatabase().
SQLHDBC ODBCDatabase::connection [protected] |
Connection handle.
Definition at line 46 of file odbcDatabase.hpp.
Referenced by execQuery(), and ~ODBCDatabase().