Skip to content
Open
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
88 changes: 88 additions & 0 deletions Connect-with-Exasol/openresty-odbc-connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# OpenResty Exasol ODBC connection

## Background

Connecting to Exasol from OpenResty lets you build fast APIs or dynamic web endpoints directly inside Nginx using Lua.
This enables you to query Exasol on the fly and return results to clients without an extra backend layer.

## Steps

Below are steps to connect to Exasol from Openresty.

* Install openresty [Openresty Installation manual](https://openresty.org/en/linux-packages.html#ubuntu)
* install Luarocks
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* install Luarocks
* Install LuaRocks


```shell
wget https://github.com/luarocks/luarocks/archive/v3.0.0.tar.gz

tar zxvf v3.0.0.tar.gz

./configure --prefix=/usr/local/openresty/luajit/ --with-lua=/usr/local/openresty/luajit/ --lua-suffix=jit --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1

sudo make build

sudo make install
```
Comment on lines +15 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```shell
wget https://github.com/luarocks/luarocks/archive/v3.0.0.tar.gz
tar zxvf v3.0.0.tar.gz
./configure --prefix=/usr/local/openresty/luajit/ --with-lua=/usr/local/openresty/luajit/ --lua-suffix=jit --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
sudo make build
sudo make install
```
```shell
wget https://github.com/luarocks/luarocks/archive/v3.0.0.tar.gz
tar zxvf v3.0.0.tar.gz
./configure --prefix=/usr/local/openresty/luajit/ --with-lua=/usr/local/openresty/luajit/ --lua-suffix=jit --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
sudo make build
sudo make install
```


* install ODBC package in luarocks
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* install ODBC package in luarocks
* Install ODBC package in LuaRocks


```shell
luarocks install odbc
```
Comment on lines +29 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```shell
luarocks install odbc
```
```shell
luarocks install odbc
```


* install Exasol ODBC driver, create DSN for Exasol DB [ODBC Drivers for Linux/FreeBSD | Exasol Documentation](https://docs.exasol.com/db/latest/connect_exasol/drivers/odbc/odbc_linux.htm)

* Execute connection test scripts

odbctest.sh

```shell
# set -x
# ORHOME is the path to the OpenResty install
export ORHOME="/usr/local/openresty"
export ODBCINI="${ORHOME}/odbc/.odbc.ini"
export LD_LIBRARY_PATH="${ORHOME}/luajit/lib"
export PATH="$PATH:${ORHOME}/bin/"
resty odbctest.lua $1
echo "Retun code from resty $?"
```
Comment on lines +39 to +48
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```shell
# set -x
# ORHOME is the path to the OpenResty install
export ORHOME="/usr/local/openresty"
export ODBCINI="${ORHOME}/odbc/.odbc.ini"
export LD_LIBRARY_PATH="${ORHOME}/luajit/lib"
export PATH="$PATH:${ORHOME}/bin/"
resty odbctest.lua $1
echo "Retun code from resty $?"
```
```shell
# set -x
# ORHOME is the path to the OpenResty install
export ORHOME="/usr/local/openresty"
export ODBCINI="${ORHOME}/odbc/.odbc.ini"
export LD_LIBRARY_PATH="${ORHOME}/luajit/lib"
export PATH="$PATH:${ORHOME}/bin/"
resty odbctest.lua $1
echo "Retun code from resty $?"
```


odbctest.lua

```lua
local odbc = require("odbc")
local call = [[select * from sys.exa_all_tables]]
print("Using dsn " .. arg[1])
local DSN = arg[1]
print("About to connect")
local cnn,err = odbc.connect(DSN)
if cnn == nil then
print("Failed to connect" .. err.message)
else
print("Connected")
end
local stmt,err = cnn:statement()
if stmt == nil then
print("Failed to prepare Statement" ..err.message)
else
print("Prepared statement" )
end
local ret,err = stmt:execute(call)
if ret == nil then
print("Failed to execute statement" .. err.message)
else
print("Executed satement")
end
local cols = ret:colnames()
if string.upper(cols[1]) == [[ERR]] then
-- There has been some sort of error
print("Some sort of error was raised " .. ret:fetch())
end
```
Comment on lines +52 to +81
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```lua
local odbc = require("odbc")
local call = [[select * from sys.exa_all_tables]]
print("Using dsn " .. arg[1])
local DSN = arg[1]
print("About to connect")
local cnn,err = odbc.connect(DSN)
if cnn == nil then
print("Failed to connect" .. err.message)
else
print("Connected")
end
local stmt,err = cnn:statement()
if stmt == nil then
print("Failed to prepare Statement" ..err.message)
else
print("Prepared statement" )
end
local ret,err = stmt:execute(call)
if ret == nil then
print("Failed to execute statement" .. err.message)
else
print("Executed satement")
end
local cols = ret:colnames()
if string.upper(cols[1]) == [[ERR]] then
-- There has been some sort of error
print("Some sort of error was raised " .. ret:fetch())
end
```
```lua
local odbc = require("odbc")
local call = [[select * from sys.exa_all_tables]]
print("Using dsn " .. arg[1])
local DSN = arg[1]
print("About to connect")
local cnn,err = odbc.connect(DSN)
if cnn == nil then
print("Failed to connect" .. err.message)
else
print("Connected")
end
local stmt,err = cnn:statement()
if stmt == nil then
print("Failed to prepare Statement" ..err.message)
else
print("Prepared statement" )
end
local ret,err = stmt:execute(call)
if ret == nil then
print("Failed to execute statement" .. err.message)
else
print("Executed satement")
end
local cols = ret:colnames()
if string.upper(cols[1]) == [[ERR]] then
-- There has been some sort of error
print("Some sort of error was raised " .. ret:fetch())
end
```


## Additional References

* [Openresty Installation manual](https://openresty.org/en/linux-packages.html#ubuntu)
* [ODBC Drivers for Linux/FreeBSD | Exasol Documentation](https://docs.exasol.com/db/latest/connect_exasol/drivers/odbc/odbc_linux.htm)

*We appreciate your input! Share your knowledge by contributing to the Knowledge Base directly in [GitHub](https://github.com/exasol/public-knowledgebase).*