|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +XMLSERVICE odbc call |
| 4 | +
|
| 5 | +License: |
| 6 | + BSD (LICENSE) |
| 7 | + -- or -- |
| 8 | + http://yips.idevcloud.com/wiki/index.php/XMLService/LicenseXMLService |
| 9 | +
|
| 10 | +Import: |
| 11 | + from itoolkit import * |
| 12 | + from itoolkit.odbc.iodbccall import * |
| 13 | + itransport = iODBCCall(user,password) |
| 14 | + -- or -- |
| 15 | + conn = ibm_db.connect(database, user, password) |
| 16 | + itransport = iODBCCall(conn) |
| 17 | +
|
| 18 | +Note: |
| 19 | + XMLSERVICE library search order: |
| 20 | + 1) lib parm -- iODBCCall(...,'XMLSERVICE') |
| 21 | + 2) environment variable 'XMLSERVICE' (export XMLSERVICE=XMLSERVICE) |
| 22 | + 3) QXMLSERV -- IBM PTF library (DG1 PTFs) |
| 23 | +
|
| 24 | +""" |
| 25 | +import sys |
| 26 | +import os |
| 27 | +import re |
| 28 | +import urllib |
| 29 | +if sys.version_info >= (3,0): |
| 30 | + """ |
| 31 | + urllib has been split up in Python 3. |
| 32 | + The urllib.urlencode() function is now urllib.parse.urlencode(), |
| 33 | + and the urllib.urlopen() function is now urllib.request.urlopen(). |
| 34 | + """ |
| 35 | + import urllib.request |
| 36 | + import urllib.parse |
| 37 | +import xml.dom.minidom |
| 38 | +# import inspect |
| 39 | +import pyodbc |
| 40 | + |
| 41 | +class iODBCCall: |
| 42 | + """ |
| 43 | + Transport XMLSERVICE calls over ODBC connection. |
| 44 | +
|
| 45 | + Args: |
| 46 | + idsn (str): 'DRIVER={IBM i Access ODBC Driver};SYSTEM=system.mydomain.com;UID=MYUSER;PASSWORD=PASSWORD' |
| 47 | + -- or -- |
| 48 | + active odbc connection |
| 49 | + ictl (str): optional - XMLSERVICE control ['*here','*sbmjob'] |
| 50 | + ipc (str): optional - XMLSERVICE xToolkit job route for *sbmjob ['/tmp/myunique42'] |
| 51 | + ilib (str): optional - XMLSERVICE library compiled (default QXMLSERV) |
| 52 | +
|
| 53 | + Example: |
| 54 | + from itoolkit.odbc.iodbccall import * |
| 55 | + itransport = iODBCCall(user,password) |
| 56 | + -- or -- |
| 57 | + conn = ibm_db.connect(database, user, password) |
| 58 | + itransport = iODBCCall(conn) |
| 59 | +
|
| 60 | + Returns: |
| 61 | + (obj) |
| 62 | + """ |
| 63 | + def __init__(self, idsn=0, ictl=0, ipc=0, ilib=0): |
| 64 | + # manditory |
| 65 | + self.dsn = idsn |
| 66 | + if ictl == 0: |
| 67 | + self.ctl = '*here *cdata' |
| 68 | + else: |
| 69 | + self.ctl = ictl |
| 70 | + if ipc == 0: |
| 71 | + self.ipc = '*na' |
| 72 | + else: |
| 73 | + self.ipc = ipc |
| 74 | + if ilib == 0: |
| 75 | + self.lib = os.getenv('XMLSERVICE','QXMLSERV'); |
| 76 | + else: |
| 77 | + self.lib = ilib |
| 78 | + |
| 79 | + def trace_data(self): |
| 80 | + """Return trace driver data. |
| 81 | +
|
| 82 | + Args: |
| 83 | + none |
| 84 | +
|
| 85 | + Returns: |
| 86 | + initialization data |
| 87 | + """ |
| 88 | + data = "" |
| 89 | + data += " dsn (" + str(self.dsn) + ")" |
| 90 | + data += " ctl (" + str(self.ctl) + ")" |
| 91 | + data += " ipc (" + str(self.ipc) + ")" |
| 92 | + data += " lib (" + str(self.lib) + ")" |
| 93 | + return data |
| 94 | + |
| 95 | + def call(self, itool): |
| 96 | + """Call xmlservice with accumulated input XML. |
| 97 | +
|
| 98 | + Args: |
| 99 | + itool - iToolkit object |
| 100 | +
|
| 101 | + Returns: |
| 102 | + xml |
| 103 | + """ |
| 104 | + if isinstance(self.dsn, str): |
| 105 | + conn = pyodbc.connect(self.dsn) |
| 106 | + else: |
| 107 | + conn = self.dsn |
| 108 | + # create cursor |
| 109 | + csr = conn.cursor() |
| 110 | + # call xmlservice |
| 111 | + csr.execute("call " + self.lib + ".iPLUGR512K(?,?,?)", ipc, clt, itool.xml_in()) |
| 112 | + # fetch all rows (xml document) |
| 113 | + xml_out = "" |
| 114 | + rows = csr.fetchall() |
| 115 | + for row in rows: |
| 116 | + xml_out += row |
| 117 | + # close |
| 118 | + csr.close() |
| 119 | + del csr |
| 120 | + conn.close() |
| 121 | + # return xml |
| 122 | + return xml_out |
| 123 | + |
| 124 | + |
0 commit comments