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

Feature db2 transport #6

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions pykit/tests/test_db2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest
import os
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unused import

from pykit.transport.db2 import connect


class TestDb2Connection(unittest.TestCase):
def setUp(self):
self.connection = connect()

def test_execute_payload(self):
payload = {
'pgm': [
{'name': 'HELLO', 'lib': 'DB2JSON'},
{'s': {'name': 'char', 'type': '128a', 'value': 'Hi there'}}
]
}
toolkit = self.connection.toolkit()
toolkit.add(payload)
response = toolkit.execute()


self.assertTrue(response)
if __name__ == '__main__':
unittest.main()
61 changes: 61 additions & 0 deletions pykit/transport/db2/Db2Connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import requests
import json
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unused imports

from ...Toolkit import Toolkit
import ibm_db_dbi as dbi




class Db2Connection:
"""
Represents a REST HTTP connection that can be used to get the Python
Copy link
Collaborator

Choose a reason for hiding this comment

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

REST HTTP -> Database

Toolkit object.
"""

def __init__(self, database, username, password):

self.connection = dbi.connect(database=database, \
user=username, password=password)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's possible to just wrap ibm_db_dbi.connect:

def __init__(self, *args, **kwargs):
    self.connection = dbi.connect(args, kwargs)

https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3

This would allow users to pass any connection options they want, though is that something we actually want them to be able to do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, probably. Thoughts on having db, username, and password explicit (for comprehension), and adding **kwargs on the end?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, you can have as many named parameters as you like followed by *args and **kwargs, however this would mess up positional argument handling unless you had the same named arguments and in the same order as ibm_db_dbi.connect. So I'd suggest only supporting kwargs.

Does that make sense?


def toolkit(self):
"""
Return an instance of the toolkit with a connection defined.

:return: Toolkit
"""
return Toolkit(self)

def execute(self, payload):
"""
Execute the payload and then clear the payload.

:return: dict
"""
payload_string = str(payload)

cur = self.connection.cursor()
cur.callproc('DB2JSON.DB2PROCJR', (payload_string,))

result = cur.fetchone()

"""
if not response.ok:
raise TransportError("There was an error while executing the payload.")
"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Residual REST code

return json.loads(result[0])

def __test_connection(self):
"""
Test that the DB2Sock REST transport exists and works properly
Copy link
Collaborator

Choose a reason for hiding this comment

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

s/REST/DB2/g

with the given configuration. Raise an error if not.

:return: void
"""
toolkit = self.toolkit()
toolkit.add({
'pgm': [
{'name': 'HELLO', 'lib': 'DB2JSON'},
{'s': {'name': 'char', 'type': '128a', 'value': 'Hi there'}}
]
})
response = toolkit.execute()
Copy link
Collaborator

Choose a reason for hiding this comment

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

No assertion? Looks like the test would never fail.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We had a _test_connection funciton in the HTTP transport, because the HTTP transport didn
t actually "connect". Am removing the _test_connection function here.

5 changes: 5 additions & 0 deletions pykit/transport/db2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .Db2Connection import Db2Connection


def connect(database='*LOCAL', username=None, password=None):
return Db2Connection(database, username, password)