forked from ibmdb/python-ibmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibm_db_ctx.py
25 lines (21 loc) · 805 Bytes
/
ibm_db_ctx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import ibm_db
class Db2connect:
"""Context manager to handle connections to DB2."""
#__cxn: Optional['IBM_DBConnection']
def __init__(self, dsn: str, username: str, password: str) -> None:
"""Instantiate a DB2 connection."""
#print("init method called")
self.__dsn = dsn
self.__username = username
self.__password = password
self.__cxn = None
def __enter__(self) -> 'IBM_DBConnection':
"""Connect to DB2."""
self.__cxn = ibm_db.connect(self.__dsn, '', '')
#print("enter method called")
return self.__cxn
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
"""Disconnect from DB2."""
#print("exit method called")
ibm_db.close(self.__cxn)
self.__cxn = None