-
Notifications
You must be signed in to change notification settings - Fork 192
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
+157
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '', '') | ||
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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].
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok