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 ();
2936 }
3037 SQLRETURN ret = SQLAllocHandle_ptr (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
3138 if (!SQL_SUCCEEDED (ret)) {
32- LOG (" Failed to allocate environment handle" );
3339 throw std::runtime_error (" Failed to allocate environment handle" );
3440 }
3541 _env_handle = std::make_shared<SqlHandle>(SQL_HANDLE_ENV, env);
3642
37- ret = SQLSetEnvAttr_ptr (env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3_80, 0 );
43+ }
44+
45+ // Sets environment attributes
46+ void Connection::setEnvAttributes () {
47+ LOG (" Setting environment attributes" );
48+ SQLRETURN ret = SQLSetEnvAttr_ptr (_env_handle->get (), SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3_80, 0 );
3849 if (!SQL_SUCCEEDED (ret)) {
39- LOG (" Failed to set environment attribute" );
4050 throw std::runtime_error (" Failed to set environment attribute" );
4151 }
52+ }
4253
54+ // Allocates DBC handle
55+ void Connection::allocDbcHandle () {
56+ SQLHANDLE dbc = nullptr ;
4357 LOG (" Allocate SQL Connection Handle" );
44- ret = SQLAllocHandle_ptr (SQL_HANDLE_DBC, env , &dbc);
58+ SQLRETURN ret = SQLAllocHandle_ptr (SQL_HANDLE_DBC, _env_handle-> get () , &dbc);
4559 if (!SQL_SUCCEEDED (ret)) {
46- LOG (" Failed to allocate connection handle" );
4760 throw std::runtime_error (" Failed to allocate connection handle" );
4861 }
4962 _dbc_handle = std::make_shared<SqlHandle>(SQL_HANDLE_DBC, dbc);
63+ }
5064
51- ret = SQLDriverConnect_ptr (dbc, nullptr ,
65+ // Connects to the database
66+ SQLRETURN Connection::connectToDb () {
67+ LOG (" Connecting to database" );
68+ SQLRETURN ret = SQLDriverConnect_ptr (_dbc_handle->get (), nullptr ,
5269 (SQLWCHAR*)_conn_str.c_str (), SQL_NTS,
5370 nullptr , 0 , nullptr , SQL_DRIVER_NOPROMPT);
5471 if (!SQL_SUCCEEDED (ret)) {
55- LOG (" Failed to connect to database" );
56- }
57- else {
58- LOG (" Connected to database successfully" );
72+ throw std::runtime_error (" Failed to connect to database" );
5973 }
74+ LOG (" Connected to database successfully" );
6075 return ret;
6176}
6277
@@ -70,13 +85,22 @@ SQLRETURN Connection::close() {
7085 return SQLDisconnect_ptr (_dbc_handle->get ());
7186}
7287
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 ( );
88+ SQLRETURN Connection::commit ( ) {
89+ LOG (" Commit transaction " );
90+ SQLRETURN ret = SQLEndTran_ptr (SQL_HANDLE_DBC, _dbc_handle-> get (), SQL_COMMIT);
91+ if (! SQL_SUCCEEDED (ret)) {
92+ throw std::runtime_error ( " Failed to commit transaction " );
7893 }
79- return SQLEndTran_ptr (_dbc_handle->type (), _dbc_handle->get (), completion_type);
94+ return ret;
95+ }
96+
97+ SQLRETURN Connection::rollback () {
98+ LOG (" Rollback transaction" );
99+ SQLRETURN ret = SQLEndTran_ptr (SQL_HANDLE_DBC, _dbc_handle->get (), SQL_ROLLBACK);
100+ if (!SQL_SUCCEEDED (ret)) {
101+ throw std::runtime_error (" Failed to rollback transaction" );
102+ }
103+ return ret;
80104}
81105
82106SQLRETURN Connection::set_autocommit (bool enable) {
@@ -98,7 +122,7 @@ bool Connection::get_autocommit() const {
98122 return value == SQL_AUTOCOMMIT_ON;
99123}
100124
101- SqlHandlePtr Connection::alloc_statement_handle () {
125+ SqlHandlePtr Connection::allocStatementHandle () {
102126 LOG (" Allocating statement handle" );
103127 SQLHANDLE stmt = nullptr ;
104128 SQLRETURN ret = SQLAllocHandle_ptr (SQL_HANDLE_STMT, _dbc_handle->get (), &stmt);
0 commit comments