-
Notifications
You must be signed in to change notification settings - Fork 203
Add asyncio support, cursor enhancements, test runner refactor and documentation #1060
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
Open
Earammak
wants to merge
1
commit into
ibmdb:master
Choose a base branch
from
Earammak:asynciosupportpr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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,50 @@ | ||
| from __future__ import print_function | ||
| import asyncio | ||
| import sys | ||
| import unittest | ||
| import ibm_db_dbi | ||
| import config | ||
| from testfunctions import IbmDbTestFunctions | ||
|
|
||
| class IbmDbTestCase(unittest.TestCase): | ||
|
|
||
| def test_01_async_connect(self): | ||
| obj = IbmDbTestFunctions() | ||
| obj.assert_expect(self.run_test_01) | ||
|
|
||
| def run_test_01(self): | ||
| async def main(): | ||
| # Cataloged connection | ||
| conn = await ibm_db_dbi.connect_async(config.database, config.user, config.password) | ||
| if conn: | ||
| print("Cataloged connection succeeded.") | ||
| conn.close() | ||
| else: | ||
| print("Cataloged connection failed.") | ||
|
|
||
| # Uncataloged connection | ||
| conn = await ibm_db_dbi.connect_async( | ||
| "DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % ( | ||
| config.database, config.hostname, config.port, | ||
| config.user, config.password), | ||
| '', '') | ||
| if conn: | ||
| print("Uncataloged connection succeeded.") | ||
| conn.close() | ||
| else: | ||
| print("Uncataloged connection failed.") | ||
| asyncio.run(main()) | ||
|
|
||
| #__END__ | ||
| #__LUW_EXPECTED__ | ||
| #Cataloged connection succeeded. | ||
| #Uncataloged connection succeeded. | ||
| #__ZOS_EXPECTED__ | ||
| #Cataloged connection succeeded. | ||
| #Uncataloged connection succeeded. | ||
| #__SYSTEMI_EXPECTED__ | ||
| #Cataloged connection succeeded. | ||
| #Uncataloged connection succeeded. | ||
| #__IDS_EXPECTED__ | ||
| #Cataloged connection succeeded. | ||
| #Uncataloged connection succeeded. | ||
This file contains hidden or 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,46 @@ | ||
| from __future__ import print_function | ||
| import asyncio | ||
| import sys | ||
| import unittest | ||
| import ibm_db_dbi | ||
| import config | ||
| from testfunctions import IbmDbTestFunctions | ||
|
|
||
|
|
||
| class IbmDbTestCase(unittest.TestCase): | ||
|
|
||
| def test_02_async_pconnect(self): | ||
| obj = IbmDbTestFunctions() | ||
| obj.assert_expectf(self.run_test_02) | ||
|
|
||
| def run_test_02(self): | ||
| async def main(): | ||
| conn = await ibm_db_dbi.pconnect_async( | ||
| "DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % ( | ||
| config.database, config.hostname, config.port, | ||
| config.user, config.password), | ||
| '', '') | ||
| print("Persistent connection:", conn) | ||
| info = conn.server_info() | ||
| print("Server info:", info) | ||
| conn.close() | ||
| print("Connection closed.") | ||
| asyncio.run(main()) | ||
|
|
||
| #__END__ | ||
| #__LUW_EXPECTED__ | ||
| #Persistent connection: <ibm_db_dbi.Connection object at %s> | ||
| #Server info: (%s) | ||
| #Connection closed. | ||
| #__ZOS_EXPECTED__ | ||
| #Persistent connection: <ibm_db_dbi.Connection object at %s> | ||
| #Server info: (%s) | ||
| #Connection closed. | ||
| #__SYSTEMI_EXPECTED__ | ||
| #Persistent connection: <ibm_db_dbi.Connection object at %s> | ||
| #Server info: (%s) | ||
| #Connection closed. | ||
| #__IDS_EXPECTED__ | ||
| #Persistent connection: <ibm_db_dbi.Connection object at %s> | ||
| #Server info: (%s) | ||
| #Connection closed. |
This file contains hidden or 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,44 @@ | ||
| from __future__ import print_function | ||
| import asyncio | ||
| import sys | ||
| import unittest | ||
| import config | ||
| from ibm_db_dbi import AsyncConnection | ||
| from testfunctions import IbmDbTestFunctions | ||
|
|
||
|
|
||
| class IbmDbTestCase(unittest.TestCase): | ||
|
|
||
| def test_03_async_connection_class(self): | ||
| obj = IbmDbTestFunctions() | ||
| obj.assert_expectf(self.run_test_03) | ||
|
|
||
| def run_test_03(self): | ||
| async def main(): | ||
| conn = await AsyncConnection.connect( | ||
| "DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % ( | ||
| config.database, config.hostname, config.port, | ||
| config.user, config.password), | ||
| '', '') | ||
| print("Connected via AsyncConnection.connect():", conn) | ||
|
|
||
| cursor = await conn.cursor() | ||
| print("Cursor created:", cursor) | ||
|
|
||
| await cursor.close() | ||
| await conn.close() | ||
| asyncio.run(main()) | ||
|
|
||
| #__END__ | ||
| #__LUW_EXPECTED__ | ||
| #Connected via AsyncConnection.connect(): <ibm_db_dbi.AsyncConnection object at %s> | ||
| #Cursor created: <ibm_db_dbi.AsyncCursor object at %s> | ||
| #__ZOS_EXPECTED__ | ||
| #Connected via AsyncConnection.connect(): <ibm_db_dbi.AsyncConnection object at %s> | ||
| #Cursor created: <ibm_db_dbi.AsyncCursor object at %s> | ||
| #__SYSTEMI_EXPECTED__ | ||
| #Connected via AsyncConnection.connect(): <ibm_db_dbi.AsyncConnection object at %s> | ||
| #Cursor created: <ibm_db_dbi.AsyncCursor object at %s> | ||
| #__IDS_EXPECTED__ | ||
| #Connected via AsyncConnection.connect(): <ibm_db_dbi.AsyncConnection object at %s> | ||
| #Cursor created: <ibm_db_dbi.AsyncCursor object at %s> |
This file contains hidden or 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,59 @@ | ||
| from __future__ import print_function | ||
| import asyncio | ||
| import sys | ||
| import unittest | ||
| import config | ||
| from ibm_db_dbi import AsyncConnection | ||
| from testfunctions import IbmDbTestFunctions | ||
|
|
||
|
|
||
| class IbmDbTestCase(unittest.TestCase): | ||
|
|
||
| def test_04_async_context_manager(self): | ||
| obj = IbmDbTestFunctions() | ||
| obj.assert_expect(self.run_test_04) | ||
|
|
||
| def run_test_04(self): | ||
| async def main(): | ||
| conn = await AsyncConnection.connect( | ||
| "DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % ( | ||
| config.database, config.hostname, config.port, | ||
| config.user, config.password), | ||
| '', '') | ||
|
|
||
| async with conn: | ||
| cursor = await conn.cursor() | ||
| async with cursor: | ||
| await cursor.execute("SELECT ID, NAME FROM STAFF FETCH FIRST 3 ROWS ONLY") | ||
| rows = await cursor.fetchall() | ||
| for row in rows: | ||
| print(row) | ||
| print("Cursor auto-closed via __aexit__") | ||
| print("Connection auto-closed via __aexit__") | ||
| asyncio.run(main()) | ||
|
|
||
| #__END__ | ||
| #__LUW_EXPECTED__ | ||
| #(10, 'Sanders') | ||
| #(20, 'Pernal') | ||
| #(30, 'Marenghi') | ||
| #Cursor auto-closed via __aexit__ | ||
| #Connection auto-closed via __aexit__ | ||
| #__ZOS_EXPECTED__ | ||
| #(10, 'Sanders') | ||
| #(20, 'Pernal') | ||
| #(30, 'Marenghi') | ||
| #Cursor auto-closed via __aexit__ | ||
| #Connection auto-closed via __aexit__ | ||
| #__SYSTEMI_EXPECTED__ | ||
| #(10, 'Sanders') | ||
| #(20, 'Pernal') | ||
| #(30, 'Marenghi') | ||
| #Cursor auto-closed via __aexit__ | ||
| #Connection auto-closed via __aexit__ | ||
| #__IDS_EXPECTED__ | ||
| #(10, 'Sanders') | ||
| #(20, 'Pernal') | ||
| #(30, 'Marenghi') | ||
| #Cursor auto-closed via __aexit__ | ||
| #Connection auto-closed via __aexit__ |
This file contains hidden or 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,59 @@ | ||
| from __future__ import print_function | ||
| import asyncio | ||
| import sys | ||
| import unittest | ||
| import config | ||
| from ibm_db_dbi import AsyncConnection | ||
| from testfunctions import IbmDbTestFunctions | ||
|
|
||
|
|
||
| class IbmDbTestCase(unittest.TestCase): | ||
|
|
||
| def test_05_async_fetch(self): | ||
| obj = IbmDbTestFunctions() | ||
| obj.assert_expect(self.run_test_05) | ||
|
|
||
| def run_test_05(self): | ||
| async def main(): | ||
| conn = await AsyncConnection.connect( | ||
| "DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % ( | ||
| config.database, config.hostname, config.port, | ||
| config.user, config.password), | ||
| '', '') | ||
| cursor = await conn.cursor() | ||
|
|
||
| await cursor.execute("SELECT ID, NAME, JOB FROM STAFF FETCH FIRST 10 ROWS ONLY") | ||
|
|
||
| # fetchone | ||
| row = await cursor.fetchone() | ||
| print("fetchone:", row) | ||
|
|
||
| # fetchmany | ||
| rows = await cursor.fetchmany(size=3) | ||
| print("fetchmany(3):", rows) | ||
|
|
||
| # fetchall (remaining) | ||
| remaining = await cursor.fetchall() | ||
| print("fetchall remaining:", remaining) | ||
|
|
||
| await cursor.close() | ||
| await conn.close() | ||
| asyncio.run(main()) | ||
|
|
||
| #__END__ | ||
| #__LUW_EXPECTED__ | ||
| #fetchone: (10, 'Sanders', 'Mgr ') | ||
| #fetchmany(3): [(20, 'Pernal', 'Sales'), (30, 'Marenghi', 'Mgr '), (40, 'OBrien', 'Sales')] | ||
| #fetchall remaining: [(50, 'Hanes', 'Mgr '), (60, 'Quigley', 'Sales'), (70, 'Rothman', 'Sales'), (80, 'James', 'Clerk'), (90, 'Koonitz', 'Sales'), (100, 'Plotz', 'Mgr ')] | ||
| #__ZOS_EXPECTED__ | ||
| #fetchone: (10, 'Sanders', 'Mgr ') | ||
| #fetchmany(3): [(20, 'Pernal', 'Sales'), (30, 'Marenghi', 'Mgr '), (40, 'OBrien', 'Sales')] | ||
| #fetchall remaining: [(50, 'Hanes', 'Mgr '), (60, 'Quigley', 'Sales'), (70, 'Rothman', 'Sales'), (80, 'James', 'Clerk'), (90, 'Koonitz', 'Sales'), (100, 'Plotz', 'Mgr ')] | ||
| #__SYSTEMI_EXPECTED__ | ||
| #fetchone: (10, 'Sanders', 'Mgr ') | ||
| #fetchmany(3): [(20, 'Pernal', 'Sales'), (30, 'Marenghi', 'Mgr '), (40, 'OBrien', 'Sales')] | ||
| #fetchall remaining: [(50, 'Hanes', 'Mgr '), (60, 'Quigley', 'Sales'), (70, 'Rothman', 'Sales'), (80, 'James', 'Clerk'), (90, 'Koonitz', 'Sales'), (100, 'Plotz', 'Mgr ')] | ||
| #__IDS_EXPECTED__ | ||
| #fetchone: (10, 'Sanders', 'Mgr ') | ||
| #fetchmany(3): [(20, 'Pernal', 'Sales'), (30, 'Marenghi', 'Mgr '), (40, 'OBrien', 'Sales')] | ||
| #fetchall remaining: [(50, 'Hanes', 'Mgr '), (60, 'Quigley', 'Sales'), (70, 'Rothman', 'Sales'), (80, 'James', 'Clerk'), (90, 'Koonitz', 'Sales'), (100, 'Plotz', 'Mgr ')] |
This file contains hidden or 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,65 @@ | ||
| from __future__ import print_function | ||
| import asyncio | ||
| import sys | ||
| import unittest | ||
| import config | ||
| from ibm_db_dbi import AsyncConnection | ||
| from testfunctions import IbmDbTestFunctions | ||
|
|
||
|
|
||
| class IbmDbTestCase(unittest.TestCase): | ||
|
|
||
| def test_06_async_iteration(self): | ||
| obj = IbmDbTestFunctions() | ||
| obj.assert_expect(self.run_test_06) | ||
|
|
||
| def run_test_06(self): | ||
| async def main(): | ||
| conn = await AsyncConnection.connect( | ||
| "DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % ( | ||
| config.database, config.hostname, config.port, | ||
| config.user, config.password), | ||
| '', '') | ||
| cursor = await conn.cursor() | ||
|
|
||
| await cursor.execute("SELECT ID, NAME FROM STAFF FETCH FIRST 5 ROWS ONLY") | ||
|
|
||
| count = 0 | ||
| async for row in cursor: | ||
| print(row) | ||
| count += 1 | ||
| print("Iterated over %d rows" % count) | ||
|
|
||
| await cursor.close() | ||
| await conn.close() | ||
| asyncio.run(main()) | ||
|
|
||
| #__END__ | ||
| #__LUW_EXPECTED__ | ||
| #(10, 'Sanders') | ||
| #(20, 'Pernal') | ||
| #(30, 'Marenghi') | ||
| #(40, 'OBrien') | ||
| #(50, 'Hanes') | ||
| #Iterated over 5 rows | ||
| #__ZOS_EXPECTED__ | ||
| #(10, 'Sanders') | ||
| #(20, 'Pernal') | ||
| #(30, 'Marenghi') | ||
| #(40, 'OBrien') | ||
| #(50, 'Hanes') | ||
| #Iterated over 5 rows | ||
| #__SYSTEMI_EXPECTED__ | ||
| #(10, 'Sanders') | ||
| #(20, 'Pernal') | ||
| #(30, 'Marenghi') | ||
| #(40, 'OBrien') | ||
| #(50, 'Hanes') | ||
| #Iterated over 5 rows | ||
| #__IDS_EXPECTED__ | ||
| #(10, 'Sanders') | ||
| #(20, 'Pernal') | ||
| #(30, 'Marenghi') | ||
| #(40, 'OBrien') | ||
| #(50, 'Hanes') | ||
| #Iterated over 5 rows |
Oops, something went wrong.
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.
@Earammak If user or password are not in config file, check env vars DB2_USER and DB2_PASSWD for these values and if set, use it. Having password in config file is not recommended but it is an option based on users risk. Recommended way is to set DB2_PASSWD env var and ibm_db should be able to check that env var value with high priority. If env var is not set, then only use the value from config.json file.