Skip to content

Commit 7a4b014

Browse files
author
Saumya Garg
committed
refining
1 parent 015c430 commit 7a4b014

2 files changed

Lines changed: 55 additions & 17 deletions

File tree

mssql_python/pybind/connection/connection.cpp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@
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

1718
Connection::~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

2122
SQLRETURN 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

82114
SQLRETURN Connection::set_autocommit(bool enable) {

mssql_python/pybind/connection/connection.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ class Connection {
2424
// Close the connection and free resources.
2525
SQLRETURN close();
2626

27-
// End the transaction with the specified completion type.
28-
SQLRETURN end_transaction(SQLSMALLINT completion_type);
27+
// Commit the current transaction.
28+
SQLRETURN commit();
29+
30+
// Rollback the current transaction.
31+
SQLRETURN rollback();
2932

3033
// Enable or disable autocommit mode.
3134
SQLRETURN set_autocommit(bool value);
@@ -36,11 +39,14 @@ class Connection {
3639
SqlHandlePtr alloc_statement_handle(); // Will later be moved to cursor c++ class
3740

3841
private:
42+
void allocEnvHandle();
43+
void setEnvAttributes();
44+
void allocDbcHandle();
45+
SQLRETURN connectToDb();
3946

4047
std::wstring _conn_str; // Connection string
4148
SqlHandlePtr _env_handle; // Environment handle
4249
SqlHandlePtr _dbc_handle; // Connection handle
43-
4450
bool _autocommit = false;
4551
};
4652
#endif // CONNECTION_H

0 commit comments

Comments
 (0)