Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context manager support with connection object #947

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ibm_db_ctx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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, '', '')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put username and password here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put username and password here

Username and password will be there in DSN only.
For cataloged connection, we need to give database, username and password [ For this new test suite added for both cataloged and uncatalogued connection].

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Then why are we taking username and password in the constructor then. I don't see those values being use anywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of username and password depends on what type of connection we are trying to.
When we go for an uncatalogued connection, we will pass an empty string only for username and password field.
Please refer to: https://github.com/ibmdb/python-ibmdb/wiki/APIs#ibm_dbconnect

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

#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
61 changes: 61 additions & 0 deletions ibm_db_tests/test_context_ConnActive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#
# Licensed Materials - Property of IBM
#
# (c) Copyright IBM Corp. 2007-2008
#

from __future__ import print_function
import sys
import unittest
import ibm_db
import config
import platform
import ibm_db_ctx

from testfunctions import IbmDbTestFunctions

class IbmDbTestCase(unittest.TestCase):
@unittest.skipIf(platform.system() == 'z/OS',"Test fails with z/OS ODBC driver")
def test_context_ConnActive(self):
obj = IbmDbTestFunctions()
obj.assert_expect(self.run_test_context_ConnActive)

def run_test_context_ConnActive(self):
conn = None
is_alive = ibm_db.active(conn)
if is_alive:
print("Is active")
else:
print("Is not active")

with ibm_db_ctx.Db2connect(config.database, config.user, config.password) as conn:
is_alive = ibm_db.active(conn)
if is_alive:
print("Is active")
else:
print("Is not active")

is_alive = ibm_db.active(conn)
if is_alive:
print("Is active")
else:
print("Is not active")


#__END__
#__LUW_EXPECTED__
#Is not active
#Is active
#Is not active
#__ZOS_EXPECTED__
#Is not active
#Is active
#Is not active
#__SYSTEMI_EXPECTED__
#Is not active
#Is active
#Is not active
#__IDS_EXPECTED__
#Is not active
#Is active
#Is not active
34 changes: 34 additions & 0 deletions ibm_db_tests/test_context_ConnDb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Licensed Materials - Property of IBM
#
# (c) Copyright IBM Corp. 2007-2008
#

from __future__ import print_function
import sys
import unittest
import ibm_db
import config
from testfunctions import IbmDbTestFunctions
import ibm_db_ctx

class IbmDbTestCase(unittest.TestCase):

def test_context_ConnDb(self):
obj = IbmDbTestFunctions()
obj.assert_expect(self.run_test_context_ConnDb)

def run_test_context_ConnDb(self):
with ibm_db_ctx.Db2connect(config.database, config.user, config.password) as conn:
print("Connection succeeded.")


#__END__
#__LUW_EXPECTED__
#Connection succeeded.
#__ZOS_EXPECTED__
#Connection succeeded.
#__SYSTEMI_EXPECTED__
#Connection succeeded.
#__IDS_EXPECTED__
#Connection succeeded.
36 changes: 36 additions & 0 deletions ibm_db_tests/test_context_ConnDbUncatalogedConn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Licensed Materials - Property of IBM
#
# (c) Copyright IBM Corp. 2007-2008
#

from __future__ import print_function
import sys
import unittest
import ibm_db
import config
from testfunctions import IbmDbTestFunctions
import ibm_db_ctx

class IbmDbTestCase(unittest.TestCase):

def test_context_ConnDbUncatalogedConn(self):
obj = IbmDbTestFunctions()
obj.assert_expect(self.run_test_context_ConnDbUncatalogedConn)

def run_test_context_ConnDbUncatalogedConn(self):
conn_str = "DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % (config.database, config.hostname, config.port, config.user, config.password)
with ibm_db_ctx.Db2connect(conn_str,'','') as conn:
print("Connection succeeded.")



#__END__
#__LUW_EXPECTED__
#Connection succeeded.
#__ZOS_EXPECTED__
#Connection succeeded.
#__SYSTEMI_EXPECTED__
#Connection succeeded.
#__IDS_EXPECTED__
#Connection succeeded.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def print_exception( e, url):
(get_python_lib(), ['./LICENSE']),
(get_python_lib(), ['./config.py.sample'])]

modules = ['ibm_db_dbi', 'testfunctions', 'ibmdb_tests']
modules = ['ibm_db_dbi', 'testfunctions', 'ibmdb_tests', 'ibm_db_ctx']

if 'zos' == sys.platform:
ext_modules = _ext_modules(os.path.join(os.getcwd(), include_dir), library, ibm_db_lib, ibm_db_lib_runtime)
Expand Down
Loading