Skip to content

Commit b22b197

Browse files
Saumya Garggargsaumya
authored andcommitted
working
1 parent 70f6aa3 commit b22b197

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

mssql_python/pybind/connection/connection.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <vector>
1010
#include <pybind11/pybind11.h>
1111

12+
#define SQL_COPT_SS_ACCESS_TOKEN 1256 // Custom attribute ID for access token
13+
1214
//-------------------------------------------------------------------------------------------------
1315
// Implements the Connection class declared in connection.h.
1416
// This class wraps low-level ODBC operations like connect/disconnect,
@@ -21,8 +23,16 @@ Connection::~Connection() {
2123
close(); // Ensure the connection is closed when the object is destroyed.
2224
}
2325

24-
SQLRETURN Connection::connect() {
26+
SQLRETURN Connection::connect(const py::dict& attrs_before) {
2527
allocDbcHandle();
28+
// Apply access token before connect
29+
if (!attrs_before.is_none() && py::len(attrs_before) > 0) {
30+
LOG("Apply attributes before connect");
31+
applyAttrsBefore(attrs_before);
32+
if (_autocommit) {
33+
setAutocommit(_autocommit);
34+
}
35+
}
2636
return connectToDb();
2737
}
2838

@@ -91,11 +101,8 @@ SQLRETURN Connection::rollback() {
91101
}
92102

93103
SQLRETURN Connection::setAutocommit(bool enable) {
94-
if (!_dbc_handle) {
95-
throw std::runtime_error("Connection handle not allocated");
96-
}
97104
SQLINTEGER value = enable ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF;
98-
LOG("Set SQL Connection Attribute - Autocommit");
105+
LOG("Set SQL Connection Attribute - Autocommit");
99106
SQLRETURN ret = SQLSetConnectAttr_ptr(_dbc_handle->get(), SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)value, 0);
100107
if (!SQL_SUCCEEDED(ret)) {
101108
throw std::runtime_error("Failed to set autocommit mode.");
@@ -117,6 +124,9 @@ bool Connection::getAutocommit() const {
117124
}
118125

119126
SqlHandlePtr Connection::allocStatementHandle() {
127+
if (!_dbc_handle) {
128+
throw std::runtime_error("Connection handle not allocated");
129+
}
120130
LOG("Allocating statement handle");
121131
SQLHANDLE stmt = nullptr;
122132
SQLRETURN ret = SQLAllocHandle_ptr(SQL_HANDLE_STMT, _dbc_handle->get(), &stmt);
@@ -126,7 +136,7 @@ SqlHandlePtr Connection::allocStatementHandle() {
126136
return std::make_shared<SqlHandle>(SQL_HANDLE_STMT, stmt);
127137
}
128138

129-
SQLRETURN Connection::set_attribute(SQLINTEGER attribute, py::object value) {
139+
SQLRETURN Connection::setAttribute(SQLINTEGER attribute, py::object value) {
130140
LOG("Setting SQL attribute");
131141

132142
SQLPOINTER ptr = nullptr;
@@ -156,7 +166,7 @@ SQLRETURN Connection::set_attribute(SQLINTEGER attribute, py::object value) {
156166
return ret;
157167
}
158168

159-
void Connection::apply_attrs_before(const py::dict& attrs) {
169+
void Connection::applyAttrsBefore(const py::dict& attrs) {
160170
for (const auto& item : attrs) {
161171
int key;
162172
try {
@@ -166,7 +176,7 @@ void Connection::apply_attrs_before(const py::dict& attrs) {
166176
}
167177

168178
if (key == SQL_COPT_SS_ACCESS_TOKEN) {
169-
SQLRETURN ret = set_attribute(key, py::reinterpret_borrow<py::object>(item.second));
179+
SQLRETURN ret = setAttribute(key, py::reinterpret_borrow<py::object>(item.second));
170180
if (!SQL_SUCCEEDED(ret)) {
171181
throw std::runtime_error("Failed to set access token before connect");
172182
}

mssql_python/pybind/connection/connection.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ class Connection {
4646
std::wstring _conn_str;
4747
SqlHandlePtr _dbc_handle;
4848
bool _autocommit = false;
49-
std::shared_ptr<Connection> _conn;
50-
51-
SQLRETURN set_attribute(SQLINTEGER attribute, pybind11::object value);
52-
void apply_attrs_before(const pybind11::dict& attrs);
49+
50+
static SqlHandlePtr getSharedEnvHandle();
51+
SQLRETURN setAttribute(SQLINTEGER attribute, pybind11::object value);
52+
void applyAttrsBefore(const pybind11::dict& attrs);
5353
};
5454

5555
#endif // CONNECTION_H

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,14 +1922,11 @@ PYBIND11_MODULE(ddbc_bindings, m) {
19221922
.def(py::init<const std::wstring&, bool>(), py::arg("conn_str"), py::arg("autocommit") = false)
19231923
.def("connect", &Connection::connect, py::arg("attrs_before") = py::dict(), "Establish a connection to the database")
19241924
.def("close", &Connection::close, "Close the connection")
1925-
.def("commit", [](Connection& self) {
1926-
self.end_transaction(SQL_COMMIT);
1927-
})
1928-
.def("rollback", [](Connection& self) {
1929-
self.end_transaction(SQL_ROLLBACK);})
1930-
.def("set_autocommit", &Connection::set_autocommit)
1931-
.def("get_autocommit", &Connection::get_autocommit)
1932-
.def("alloc_statement_handle", &Connection::alloc_statement_handle);
1925+
.def("commit", &Connection::commit, "Commit the current transaction")
1926+
.def("rollback", &Connection::rollback, "Rollback the current transaction")
1927+
.def("set_autocommit", &Connection::setAutocommit)
1928+
.def("get_autocommit", &Connection::getAutocommit)
1929+
.def("alloc_statement_handle", &Connection::allocStatementHandle);
19331930
m.def("DDBCSQLExecDirect", &SQLExecDirect_wrap, "Execute a SQL query directly");
19341931
m.def("DDBCSQLExecute", &SQLExecute_wrap, "Prepare and execute T-SQL statements");
19351932
m.def("DDBCSQLRowCount", &SQLRowCount_wrap,

0 commit comments

Comments
 (0)