... | ... | @@ -8,9 +8,7 @@ For Python to do that, we'll need a few things. |
|
|
|
|
|
unixODBC (`yum install unixODBC`), or use ACS.
|
|
|
|
|
|
If you plan on doing local development, and I highly recommend that you do!
|
|
|
You'll need to install the ODBC driver IBM ships for your operating system.
|
|
|
_Currently, Win, Mac, Linux and IBM i naturally_
|
|
|
If you plan on doing local development, and I highly recommend that you do! You'll need to install the ODBC driver IBM ships for your operating system. _Currently, Win, Mac, Linux and IBM i naturally_
|
|
|
|
|
|
You can download that, from the same place you downloaded ACS, [currently that is here](https://www.ibm.com/support/pages/ibm-i-access-client-solutions), but as you know, it changes...
|
|
|
|
... | ... | @@ -18,4 +16,84 @@ Finally, we'll need [PyODBC](https://pypi.org/project/pyodbc/) and [IBM_Db](http |
|
|
|
|
|
For an in-depth guide, [review the documentation](https://ibmi-oss-docs.readthedocs.io/en/latest/odbc/installation.html)
|
|
|
|
|
|
Most of this, is configured on your chroot environment, so we'll just `pip install pyodbc`... |
|
|
\ No newline at end of file |
|
|
Most of this, is configured on your chroot environment, so we'll just `pip install pyodbc`...
|
|
|
|
|
|
## Secrets, everyone has them.
|
|
|
|
|
|
We'll also need some credentials, for now we'll settle for having a single connection.
|
|
|
|
|
|
Still, we shouldn't distribute valid credentials, so create a file called _config.py_ and enter the following variables into it
|
|
|
|
|
|
In vim type \`:e config.py\` - it will create a new empty buffer to wok with.
|
|
|
|
|
|
```
|
|
|
DB2_HOST=
|
|
|
DB2_USER=
|
|
|
DB2_PASS=
|
|
|
```
|
|
|
|
|
|
The values of these, can be filled out with your credentials for now.
|
|
|
|
|
|
Now, `:w` to save.
|
|
|
|
|
|
The config.py file is .gitignore'd, so it wont be added to the repository unless you force it.\
|
|
|
Instead, we'll distribute a config.py.template file, that contains the variables that we want, but without any value associated.
|
|
|
|
|
|
Usually, we would then simply copy that file, and edit it as needed.\
|
|
|
Alternatively, we could create a DSN, outside the project, which could be referenced in the connection string.
|
|
|
|
|
|
For now, we'll go DSN'less.
|
|
|
|
|
|
All that is left, is to use the above, to create a connection in our service, in vim type \`:e db2.py\` - it will create a new empty buffer to work with.
|
|
|
|
|
|
`i`nsert the following code in the file
|
|
|
|
|
|
```
|
|
|
# Get the variables defined in config.py
|
|
|
from config import DB2_HOST, DB2_USER, DB2_PASS
|
|
|
|
|
|
import pyodbc
|
|
|
|
|
|
|
|
|
# Name of the ODBC driver
|
|
|
driver = '{IBM i Access ODBC Driver}'
|
|
|
|
|
|
|
|
|
# Create a connection, using this information
|
|
|
__internalConn = pyodbc.connect(
|
|
|
DRIVER=driver,
|
|
|
SYSTEM=DB2_HOST,
|
|
|
UID=DB2_USER,
|
|
|
PWD=DB2_PASS
|
|
|
)
|
|
|
|
|
|
|
|
|
# Define a function, using that connection.
|
|
|
def getProducts():
|
|
|
c1 = __internalConn.cursor()
|
|
|
return c1.execute('select * from ICEBREAK.PRODUCT')
|
|
|
```
|
|
|
|
|
|
Leave _insert-mode_ and save the file, then switch to the _api_router.py_ file
|
|
|
|
|
|
Here we added to following function
|
|
|
|
|
|
```
|
|
|
# Import the getProducts function
|
|
|
from db2 import getProducts
|
|
|
|
|
|
|
|
|
# And define an endpoint to use it.
|
|
|
@router.post("/query")
|
|
|
def read_item(query: Optional[str] = Form(...)):
|
|
|
cursor = getProducts()
|
|
|
return {
|
|
|
'results': [dict(zip([column[0] for column in cursor.description], row)) for row in cursor.fetchall()]
|
|
|
}
|
|
|
```
|
|
|
|
|
|
Save, and press **F5** in vim, just in case.
|
|
|
|
|
|
## Good things to those who wait.
|
|
|
|
|
|
Let's test if that works, switch to your browser, and head into the docs endpoint... |
|
|
\ No newline at end of file |