your Oracle connection deserves a name: Go edition
An example of assigning a name to a Oracle Database connection in Go.
Get it running
-
Install the ODPI-C
driver.
-
Start the Oracle docker container:
$ docker run --rm \
--publish 1521:1521 \
--name ycdan-oracle \
--env ORACLE_PASSWORD=secret \
--env ORACLE_DATABASE=connection_showcase \
--env APP_USER=demo \
--env APP_USER_PASSWORD=showcase \
--detach \
gvenzl/oracle-xe:18.4.0-slim
- Compile the example program:
$ go build -o your-connection-deserves-a-name
- Start the example program:
$ ORACLE_LIB_DIR=/Users/agrunwald/Downloads/instantclient_19_8 ./your-connection-deserves-a-name
You should see something like
2021/08/20 16:16:49 Connecting to Oracle on 127.0.0.1:1521/connection_showcase (with library path /Users/agrunwald/Downloads/instantclient_19_8)
2021/08/20 16:16:49 Connecting to Oracle on 127.0.0.1:1521/connection_showcase (with library path /Users/agrunwald/Downloads/instantclient_19_8) ... Successful
2021/08/20 16:16:49
2021/08/20 16:16:49 Keeping the connection open ...
2021/08/20 16:16:49 You can connect to the Oracle database and execute the query:
2021/08/20 16:16:49 SELECT username, client_identifier, module, action FROM v$session WHERE username='DEMO';
2021/08/20 16:16:49
2021/08/20 16:16:49 or with current query:
2021/08/20 16:16:49 SELECT sess.username, sess.client_identifier, sess.module, sess.action, area.sql_text
2021/08/20 16:16:49 FROM v$session sess, v$sqlarea area
2021/08/20 16:16:49 WHERE
2021/08/20 16:16:49 sess.sql_address = area.address
2021/08/20 16:16:49 AND sess.username = 'DEMO';
2021/08/20 16:16:49 Hit CTRL + C or cancel the process to stop.
2021/08/20 16:16:49
- Login into your database and execute the query (e.g., with sqlplus):
$ sqlplus sys/secret@127.0.0.1:1521/connection_showcase as sysdba
This will kick off the SQL prompt, and you can proceed with ...
SQL> COLUMN username FORMAT a15 WORD_WRAPPED
SQL> COLUMN client_identifier FORMAT a25 WORD_WRAPPED
SQL> COLUMN module FORMAT a15 WORD_WRAPPED
SQL> COLUMN action FORMAT a15 WORD_WRAPPED
SQL> COLUMN sql_text FORMAT a40 WORD_WRAPPED
SQL> SELECT sess.username, sess.client_identifier, sess.module, sess.action, area.sql_text
FROM v$session sess, v$sqlarea area
WHERE
sess.sql_address = area.address
AND sess.username = 'DEMO';
USERNAME CLIENT_IDENTIFIER MODULE ACTION SQL_TEXT
--------------- ------------------------- --------------- --------------- ---------------------------
DEMO currency-conversion-app oracle/go main SELECT sysdate FROM dual
Don't know what this is all about?
Read the original blog post your database connection deserves a name @ andygrunwald.com.
Additionally, you can check out the projects README.