00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #include        "odbcpp/connection.h"
00025 
00026 namespace odbcpp
00027 {
00028 
00029 
00082 connection::connection(environment& env) :
00083         handle(SQL_HANDLE_DBC),
00084         f_environment(&env),
00085         f_connected(false)
00086 {
00087         
00088         
00089         check(SQLAllocHandle(f_handle_type, f_environment->get_handle(), &f_handle), f_environment);
00090 }
00091 
00092 
00099 connection::~connection()
00100 {
00101         try { disconnect(); } catch(...) {}
00102 }
00103 
00104 
00115 void connection::set_attr(SQLINTEGER attr, SQLINTEGER integer)
00116 {
00117         
00118 #ifdef _MSC_VER
00119         SQLPOINTER ptr;
00120         if(sizeof(ptr) < sizeof(integer)) {
00121                 diagnostic d(odbcpp_error::ODBCPP_INTERNAL, std::string("the size of an SQLINTEGER is larger than SQLPOINTER?!"));
00122                 throw odbcpp_error(d);
00123         }
00124         memcpy(&ptr, &integer, sizeof(integer));
00125         check(SQLSetConnectAttr(f_handle, attr, ptr, 0));
00126 #else
00127         check(SQLSetConnectAttr(f_handle, attr, reinterpret_cast<SQLPOINTER>(integer), 0));
00128 #endif
00129 }
00130 
00131 
00143 void connection::set_attr(SQLINTEGER attr, SQLPOINTER ptr, SQLINTEGER length)
00144 {
00145         check(SQLSetConnectAttr(f_handle, attr, ptr, length));
00146 }
00147 
00148 
00164 void connection::connect(const std::string& dsn, const std::string& login, const std::string& passwd)
00165 {
00166         check(SQLConnect(f_handle,
00167                         (SQLCHAR *) dsn.c_str(), static_cast<SQLSMALLINT>(dsn.length()),
00168                         (SQLCHAR *) login.c_str(), static_cast<SQLSMALLINT>(login.length()),
00169                         (SQLCHAR *) passwd.c_str(), static_cast<SQLSMALLINT>(passwd.length())));
00170         f_connected = true;
00171 }
00172 
00173 
00185 void connection::disconnect()
00186 {
00187         f_connected = false;
00188         check(SQLDisconnect(f_handle));
00189 }
00190 
00191 
00218 void connection::commit()
00219 {
00220         check(SQLEndTran(f_handle_type, f_handle, SQL_COMMIT));
00221 }
00222 
00223 
00224 
00235 void connection::rollback()
00236 {
00237         check(SQLEndTran(f_handle_type, f_handle, SQL_ROLLBACK));
00238 }
00239 
00240 
00241 
00242 
00243 }