Skip to content

Commit c89cfbe

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

2 files changed

Lines changed: 56 additions & 27 deletions

File tree

mssql_python/pybind/connection/connection.cpp

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,66 @@
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();
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

82106
SQLRETURN 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);

mssql_python/pybind/connection/connection.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,28 @@ 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);
3235

3336
// Check whether autocommit is enabled.
3437
bool get_autocommit() const;
3538

36-
SqlHandlePtr alloc_statement_handle(); // Will later be moved to cursor c++ class
37-
39+
SqlHandlePtr allocStatementHandle();
3840
private:
41+
void allocEnvHandle();
42+
void setEnvAttributes();
43+
void allocDbcHandle();
44+
SQLRETURN connectToDb();
3945

4046
std::wstring _conn_str; // Connection string
4147
SqlHandlePtr _env_handle; // Environment handle
4248
SqlHandlePtr _dbc_handle; // Connection handle
43-
4449
bool _autocommit = false;
4550
};
4651
#endif // CONNECTION_H

0 commit comments

Comments
 (0)