@@ -630,6 +630,20 @@ SqlHandle::SqlHandle(SQLSMALLINT type, SQLHANDLE rawHandle)
630630 : _type(type), _handle(rawHandle) {}
631631
632632SqlHandle::~SqlHandle () {
633+ if (_handle) {
634+ free ();
635+ }
636+ }
637+
638+ SQLHANDLE SqlHandle::get () const {
639+ return _handle;
640+ }
641+
642+ SQLSMALLINT SqlHandle::type () const {
643+ return _type;
644+ }
645+
646+ void SqlHandle::free () {
633647 if (_handle && SQLFreeHandle_ptr) {
634648 const char * type_str = nullptr ;
635649 switch (_type) {
@@ -647,14 +661,6 @@ SqlHandle::~SqlHandle() {
647661 }
648662}
649663
650- SQLHANDLE SqlHandle::get () const {
651- return _handle;
652- }
653-
654- SQLSMALLINT SqlHandle::type () const {
655- return _type;
656- }
657-
658664// Helper function to check for driver errors
659665ErrorInfo SQLCheckError_Wrap (SQLSMALLINT handleType, SqlHandlePtr handle, SQLRETURN retcode) {
660666 LOG (" Checking errors for retcode - {}" , retcode);
@@ -1917,7 +1923,8 @@ PYBIND11_MODULE(ddbc_bindings, m) {
19171923 .def_readwrite (" sqlState" , &ErrorInfo::sqlState)
19181924 .def_readwrite (" ddbcErrorMsg" , &ErrorInfo::ddbcErrorMsg);
19191925
1920- py::class_<SqlHandle, SqlHandlePtr>(m, " SqlHandle" );
1926+ py::class_<SqlHandle, SqlHandlePtr>(m, " SqlHandle" )
1927+ .def (" free" , &SqlHandle::free, " Free the handle" );
19211928 py::class_<Connection>(m, " Connection" )
19221929 .def (py::init<const std::wstring&, bool >(), py::arg (" conn_str" ), py::arg (" autocommit" ) = false )
19231930 .def (" connect" , &Connection::connect, py::arg (" attrs_before" ) = py::dict (), " Establish a connection to the database" )
0 commit comments