1212// This class wraps low-level ODBC operations like connect/disconnect,
1313// transaction control, and autocommit configuration.
1414// -------------------------------------------------------------------------------------------------
15- Connection::Connection (const std::wstring& conn_str, bool autocommit) : _conn_str(conn_str) , _autocommit(autocommit) {}
15+ Connection::Connection (const std::wstring& conn_str, bool autocommit)
16+ : _conn_str(conn_str) , _autocommit(autocommit) {}
1617
1718Connection::~Connection () {
18- close (); // Ensure the connection is closed when the object is destroyed.
19+ close (); // Ensure the connection is closed when the object is destroyed.
1920}
2021
2122SQLRETURN Connection::connect () {
22- SQLHANDLE env = nullptr ;
23- SQLHANDLE dbc = nullptr ;
23+ allocEnvHandle ();
24+ setEnvAttributes ();
25+ allocDbcHandle ();
26+ return connectToDb ();
27+ }
2428
25- LOG (" Allocate SQL Handle" );
29+ // Allocates environment handle
30+ void Connection::allocEnvHandle () {
31+ SQLHANDLE env = nullptr ;
32+ LOG (" Allocating Environment Handle" );
2633 if (!SQLAllocHandle_ptr) {
2734 LOG (" Function pointer not initialized. Loading the driver." );
2835 DriverLoader::getInstance ().loadDriver ();
@@ -34,21 +41,35 @@ SQLRETURN Connection::connect() {
3441 }
3542 _env_handle = std::make_shared<SqlHandle>(SQL_HANDLE_ENV, env);
3643
37- ret = SQLSetEnvAttr_ptr (env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3_80, 0 );
44+ }
45+
46+ // Sets environment attributes
47+ void Connection::setEnvAttributes () {
48+ LOG (" Setting environment attributes" );
49+ SQLRETURN ret = SQLSetEnvAttr_ptr (_env_handle->get (), SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3_80, 0 );
3850 if (!SQL_SUCCEEDED (ret)) {
3951 LOG (" Failed to set environment attribute" );
4052 throw std::runtime_error (" Failed to set environment attribute" );
4153 }
54+ }
4255
56+ // Allocates DBC handle
57+ void Connection::allocDbcHandle () {
58+ SQLHANDLE dbc = nullptr ;
4359 LOG (" Allocate SQL Connection Handle" );
44- ret = SQLAllocHandle_ptr (SQL_HANDLE_DBC, env , &dbc);
60+ SQLRETURN ret = SQLAllocHandle_ptr (SQL_HANDLE_DBC, _env_handle-> get () , &dbc);
4561 if (!SQL_SUCCEEDED (ret)) {
4662 LOG (" Failed to allocate connection handle" );
4763 throw std::runtime_error (" Failed to allocate connection handle" );
4864 }
4965 _dbc_handle = std::make_shared<SqlHandle>(SQL_HANDLE_DBC, dbc);
5066
51- ret = SQLDriverConnect_ptr (dbc, nullptr ,
67+ }
68+
69+ // Connects to the database
70+ SQLRETURN Connection::connectToDb () {
71+ LOG (" Connecting to database" );
72+ SQLRETURN ret = SQLDriverConnect_ptr (_dbc_handle->get (), nullptr ,
5273 (SQLWCHAR*)_conn_str.c_str (), SQL_NTS,
5374 nullptr , 0 , nullptr , SQL_DRIVER_NOPROMPT);
5475 if (!SQL_SUCCEEDED (ret)) {
@@ -70,13 +91,24 @@ SQLRETURN Connection::close() {
7091 return SQLDisconnect_ptr (_dbc_handle->get ());
7192}
7293
73- SQLRETURN Connection::end_transaction (SQLSMALLINT completion_type) {
74- LOG (completion_type == SQL_COMMIT ? " End SQL Transaction (Commit)" : " End SQL Transaction (Rollback)" );
75- if (!SQLEndTran_ptr) {
76- LOG (" Function pointer not initialized. Loading the driver." );
77- DriverLoader::getInstance ().loadDriver ();
94+ SQLRETURN Connection::commit () {
95+ LOG (" Commit transaction" );
96+ SQLRETURN ret = SQLEndTran_ptr (SQL_HANDLE_DBC, _dbc_handle->get (), SQL_COMMIT);
97+ if (!SQL_SUCCEEDED (ret)) {
98+ LOG (" Failed to commit transaction" );
99+ throw std::runtime_error (" Failed to commit transaction" );
78100 }
79- return SQLEndTran_ptr (_dbc_handle->type (), _dbc_handle->get (), completion_type);
101+ return ret;
102+ }
103+
104+ SQLRETURN Connection::rollback () {
105+ LOG (" Rollback transaction" );
106+ SQLRETURN ret = SQLEndTran_ptr (SQL_HANDLE_DBC, _dbc_handle->get (), SQL_ROLLBACK);
107+ if (!SQL_SUCCEEDED (ret)) {
108+ LOG (" Failed to rollback transaction" );
109+ throw std::runtime_error (" Failed to rollback transaction" );
110+ }
111+ return ret;
80112}
81113
82114SQLRETURN Connection::set_autocommit (bool enable) {
0 commit comments