OCILIB Tutorials v5.0.0

empdir — the capstone source code

This page is the source companion to the capstone: a complete, single-file implementation of the empdir CLI in both languages.

Objectives

Schema setup

On top of assets/sql/tutorial-schema.sql, the app needs two extra objects — a JSON profile column and an AQ queue for jobs. Both live in assets/sql/empdir-schema.sql — run it once as the connecting user:

alter table employees add (profile json);

create or replace type job_t as object (title varchar2(64), content varchar2(200));
begin
    dbms_aqadm.create_queue_table('job_queue_table', 'job_t');
    dbms_aqadm.create_queue('job_queue', 'job_queue_table');
    dbms_aqadm.start_queue('job_queue');
end;
/

The C implementation

#include "ocilib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static OCI_Environment *g_env;
static OCI_Pool        *g_pool;

void err_handler(OCI_Error *err) { fprintf(stderr, "%s\n", OCI_ErrorGetString(err)); }

static OCI_Connection *checkout(void)
{
    return OCI_PoolGetConnection(g_env, g_pool, NULL);   /* free() releases back to the pool */
}

/* ------------------------------------------------------------------ list */

static int cmd_list(int dept)
{
    OCI_Connection *cn = checkout();
    OCI_Statement  *st = OCI_StatementCreate(cn);

    if (dept)
    {
        OCI_StatementPrepare(st, "select empno, ename, job, sal from employees "
                        "where deptno = :d order by empno");
        OCI_StatementBindInt32(st, ":d", &dept);
    }
    else
    {
        OCI_StatementPrepare(st, "select empno, ename, job, sal from employees order by empno");
    }

    OCI_StatementExecutePrepared(st);
    OCI_Resultset *rs = OCI_StatementGetResultset(st);

    while (OCI_ResultsetFetchNext(rs))
    {
        printf("%5i  %-10s  %-9s  %8.2f\n",
               OCI_ResultsetGetInt32(rs, 0), OCI_ResultsetGetString(rs, 1),
               OCI_ResultsetGetString(rs, 2), OCI_ResultsetGetDouble(rs, 3));
    }

    OCI_StatementFree(st);
    OCI_ConnectionFree(cn);
    return 0;
}

/* -------------------------------------------------------------------- get */

static int cmd_get(int empno)
{
    OCI_Connection *cn = checkout();
    OCI_Statement  *st = OCI_StatementCreate(cn);

    OCI_StatementPrepare(st, "select ename, job, sal, hiredate from employees where empno = :e");
    OCI_StatementBindInt32(st, ":e", &empno);
    OCI_StatementExecutePrepared(st);

    OCI_Resultset *rs = OCI_StatementGetResultset(st);
    if (OCI_ResultsetFetchNext(rs))
    {
        OCI_Date *d = OCI_ResultsetGetDate(rs, 3);
        otext buf[64];
        OCI_DateToText(d, OTEXT("YYYY-MM-DD"), 63, buf);
        printf("%s  %s  %.2f  %s\n",
               OCI_ResultsetGetString(rs, 0), OCI_ResultsetGetString(rs, 1),
               OCI_ResultsetGetDouble(rs, 2), buf);
    }
    else
    {
        printf("empno %d not found\n", empno);
    }

    OCI_StatementFree(st);
    OCI_ConnectionFree(cn);
    return 0;
}

/* -------------------------------------------------------------------- add */

static int cmd_add(int empno, const otext *name, const otext *job,
                   double sal, int deptno)
{
    OCI_Connection *cn = checkout();
    OCI_Statement  *st = OCI_StatementCreate(cn);

    OCI_StatementPrepare(st, "insert into employees (empno, ename, job, sal, deptno) "
                    "values (:e, :n, :j, :s, :d)");
    OCI_StatementBindInt32(st, ":e", &empno);
    OCI_StatementBindString(st, ":n", (otext *) name, 30);
    OCI_StatementBindString(st, ":j", (otext *) job, 20);
    OCI_StatementBindDouble(st, ":s", &sal);
    OCI_StatementBindInt32(st, ":d", &deptno);
    OCI_StatementExecutePrepared(st);

    printf("%u row(s) inserted\n", OCI_StatementGetAffectedRows(st));
    OCI_ConnectionCommit(cn);

    OCI_StatementFree(st);
    OCI_ConnectionFree(cn);
    return 0;
}

/* ------------------------------------------------------------------ raise */

static int cmd_raise(int deptno, double pct)
{
    OCI_Connection *cn = checkout();
    OCI_Statement  *st = OCI_StatementCreate(cn);

    OCI_StatementPrepare(st, "update employees set sal = sal * (1 + :p / 100) "
                    "where deptno = :d returning empno into :e");

    OCI_StatementBindDouble(st, ":p", &pct);
    OCI_StatementBindInt32(st, ":d", &deptno);
    OCI_StatementRegisterInt32(st, ":e");
    OCI_StatementExecutePrepared(st);

    printf("%u salary update(s)\n", OCI_StatementGetAffectedRows(st));
    OCI_ConnectionCommit(cn);

    OCI_StatementFree(st);
    OCI_ConnectionFree(cn);
    return 0;
}

/* ----------------------------------------------------------------- import */

static int cmd_import(const char *csv)
{
    FILE *f = fopen(csv, "r");
    if (!f)
    {
        fprintf(stderr, "cannot open %s\n", csv);
        return 1;
    }

    OCI_Connection *cn = checkout();
    OCI_Statement  *st = OCI_StatementCreate(cn);

    OCI_StatementPrepare(st, "insert into employees (empno, ename, sal) values (:e, :n, :s)");

    const int BATCH = 64;
    int      empno[64];
    otext    ename[64][31];
    double   sal[64];
    int      count = 0;

    OCI_StatementBindArraySetSize(st, BATCH);
    OCI_StatementBindInt32Array(st, ":e", empno, 0);
    OCI_StatementBindStringArray(st, ":n", (otext *) ename, 30, 0);
    OCI_StatementBindDoubleArray(st, ":s", sal, 0);

    char line[256];
    while (fgets(line, sizeof(line), f))
    {
        sscanf(line, "%d,%30[^,],%lf", &empno[count], ename[count], &sal[count]);
        if (++count == BATCH)
        {
            OCI_StatementBindArraySetSize(st, count);
            OCI_StatementExecutePrepared(st);
            count = 0;
        }
    }
    if (count)
    {
        OCI_StatementBindArraySetSize(st, count);
        OCI_StatementExecutePrepared(st);
    }

    printf("%u row(s) imported\n", OCI_StatementGetAffectedRows(st));
    OCI_ConnectionCommit(cn);

    OCI_StatementFree(st);
    OCI_ConnectionFree(cn);
    fclose(f);
    return 0;
}

/* ------------------------------------------------------------------ watch */

static void on_change(OCI_Event *event)
{
    printf("[cqn] type=%u object=%s rowid=%s\n",
           OCI_EventGetType(event), OCI_EventGetObject(event),
           OCI_EventGetRowid(event));
}

static int cmd_watch(void)
{
    OCI_Connection *cn = checkout();
    OCI_Statement  *st = OCI_StatementCreate(cn);
    OCI_Subscription *sub = OCI_SubscriptionCreate(
        cn, OTEXT("empdir-watch"), OCI_CNT_ALL, on_change, OCI_SRM_DEFAULT, 0);

    OCI_StatementPrepare(st, "select empno from employees");
    OCI_SubscriptionAddStatement(sub, st);     /* watch objects this stmt touches */
    OCI_StatementExecutePrepared(st);
    printf("watching employees — run DML in another session, Ctrl+C to stop\n");

    getchar();   /* stay subscribed until the user stops the program */

    OCI_SubscriptionUnregister(sub);
    OCI_StatementFree(st);
    OCI_ConnectionFree(cn);
    return 0;
}

/* ------------------------------------------------------------------- jobs */

static int cmd_jobs_submit(const otext *title, const otext *content)
{
    OCI_Connection *cn = checkout();
    OCI_TypeInfo *inf = OCI_TypeInfoGet(cn, OTEXT("job_t"), OCI_TIF_TYPE);
    OCI_Enqueue  *enq = OCI_EnqueueCreate(inf, OTEXT("job_queue"));
    OCI_Message      *msg = OCI_MessageCreate(inf);
    OCI_Object   *job = OCI_ObjectCreate(inf);

    OCI_ObjectSetString(job, OTEXT("title"),   (otext *) title);
    OCI_ObjectSetString(job, OTEXT("content"), (otext *) content);
    OCI_MessageSetObject(msg, job);
    OCI_EnqueuePut(enq, msg);
    OCI_ConnectionCommit(cn);

    OCI_ObjectFree(job);
    OCI_MessageFree(msg);
    OCI_EnqueueFree(enq);
    OCI_TypeInfoFree(inf);
    OCI_ConnectionFree(cn);
    return 0;
}

static int cmd_jobs_work(void)
{
    OCI_Connection *cn = checkout();
    OCI_TypeInfo *inf = OCI_TypeInfoGet(cn, OTEXT("job_t"), OCI_TIF_TYPE);
    OCI_Dequeue  *deq = OCI_DequeueCreate(inf, OTEXT("job_queue"));
    OCI_Message      *msg;

    while ((msg = OCI_DequeueGet(deq)) != NULL)
    {
        OCI_Object *job = OCI_MessageGetObject(msg);
        printf("job: %s — %s\n",
               OCI_ObjectGetString(job, OTEXT("title")),
               OCI_ObjectGetString(job, OTEXT("content")));
    }
    OCI_ConnectionCommit(cn);

    OCI_DequeueFree(deq);
    OCI_TypeInfoFree(inf);
    OCI_ConnectionFree(cn);
    return 0;
}

/* ---------------------------------------------------------------- profile */

static int cmd_profile(int empno)
{
    OCI_Connection *cn = checkout();
    OCI_Statement  *st = OCI_StatementCreate(cn);

    OCI_StatementPrepare(st, "select profile from employees where empno = :e");
    OCI_StatementBindInt32(st, ":e", &empno);
    OCI_StatementExecutePrepared(st);

    OCI_Resultset *rs = OCI_StatementGetResultset(st);
    if (OCI_ResultsetFetchNext(rs) && !OCI_ResultsetIsColumnNull(rs, 1))
    {
        OCI_Json *doc = OCI_ResultsetGetJson(rs, 0);
        OCI_JsonNode *root = OCI_JsonGetRoot(doc);

        const otext *skill = OTEXT("<none>");
        OCI_JsonNode *skills = OCI_JsonNodeGetFieldByName(root, OTEXT("skills"));
        if (skills && OCI_JsonNodeGetItemCount(skills))
        {
            skill = OCI_JsonNodeGetString(OCI_JsonNodeGetItem(skills, 0), NULL);
        }

        printf("emp %d — %u field(s), first skill: %s\n",
               empno, OCI_JsonNodeGetFieldCount(root), skill);
        OCI_JsonFree(doc);          /* frees root & children with it */
    }
    else
    {
        printf("no profile for empno %d\n", empno);
    }

    OCI_StatementFree(st);
    OCI_ConnectionFree(cn);
    return 0;
}

/* -------------------------------------------------------------------- main */

int main(int argc, char **argv)
{
    g_env = OCI_EnvironmentCreate(err_handler, NULL,
                                  OCI_ENV_THREADED | OCI_ENV_EVENTS);
    OCI_EnvironmentSetSubscriptionPort(g_env, 5468);
    g_pool = OCI_PoolCreate(g_env, getenv("OCILIB_DEMO_DB"),
                          getenv("OCILIB_DEMO_USR"), getenv("OCILIB_DEMO_PWD"),
                          OCI_POOL_CONNECTION, OCI_SESSION_DEFAULT, 2, 8, 1);

    int rc = 1;
    const char *cmd = argc > 1 ? argv[1] : "";

    if (!strcmp(cmd, "list"))
        rc = cmd_list(argc > 3 && !strcmp(argv[2], "--dept") ? atoi(argv[3]) : 0);
    else if (!strcmp(cmd, "get") && argc > 2)
        rc = cmd_get(atoi(argv[2]));
    else if (!strcmp(cmd, "add") && argc > 6)
        /* MBCS build: argv strings are already otext */
        rc = cmd_add(atoi(argv[2]), argv[3], argv[4],
                     atof(argv[5]), atoi(argv[6]));
    else if (!strcmp(cmd, "raise") && argc > 3)
        rc = cmd_raise(atoi(argv[2]), atof(argv[3]));
    else if (!strcmp(cmd, "import") && argc > 2)
        rc = cmd_import(argv[2]);
    else if (!strcmp(cmd, "watch"))
        rc = cmd_watch();
    else if (!strcmp(cmd, "jobs") && argc > 2 && !strcmp(argv[2], "work"))
        rc = cmd_jobs_work();
    else if (!strcmp(cmd, "jobs") && argc > 4 && !strcmp(argv[2], "submit"))
        rc = cmd_jobs_submit(argv[3], argv[4]);
    else if (!strcmp(cmd, "profile") && argc > 2)
        rc = cmd_profile(atoi(argv[2]));
    else
        fprintf(stderr, "usage: empdir list|get|add|raise|import|watch|jobs|profile ...\n");

    OCI_PoolFree(g_env, g_pool);
    OCI_EnvironmentFree(g_env);
    return rc;
}

What to notice in the code

Try it yourself

  1. Compile the C version (cl /std:c11 or gcc -std=c11 -locilib) and run list, get, add against tutorial-schema.sql.
  2. Extend import with a --mode=dirpath switch using OCI_DirectPath (page 815).
  3. Change watch to OCI_CNT_ROWS and observe rowids instead of objects.
  4. Add empdir export <empno> writing the employee as a JSON document.

Recap

Done. The appendices are your quick-reference shelf from here.