Oninit® SQLMorph — add_one
Source: examples/sp_addone.sql. Original SPL above, sqlmorph-generated Informix C UDR in the middle, and the CREATE FUNCTION script that registers the UDR under the same procedure name at the bottom.
SPL
-- @test EXECUTE FUNCTION add_one(5) -- @test EXECUTE FUNCTION add_one(0) -- @test EXECUTE FUNCTION add_one(-1) create procedure add_one(x INT) returning int; return x+1 with resume; end procedure;
C UDR — add_one.c
/* sqlmorph — Informix C UDR emitter. Built by Oninit LLC. */
#include <mi.h>
#include <miami.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "standard.h"
typedef struct {
mi_integer phase;
} iter_state_t;
UDREXPORT mi_integer add_one(mi_integer x, MI_FPARAM *fparam)
{
iter_state_t *state;
switch (mi_fp_request(fparam))
{
case SET_INIT:
DBDK_TRACE_ENTER(__FUNCTION__);
state = (iter_state_t *)mi_alloc((mi_integer)sizeof(iter_state_t), PER_COMMAND);
if (state == NULL) ONIERR_002("iterator state alloc failed");
memset(state, 0, sizeof(iter_state_t));
mi_fp_setfuncstate(fparam, (void *)state);
return 0;
case SET_RETONE:
state = (iter_state_t *)mi_fp_funcstate(fparam);
if (state->phase == 0)
{
state->phase = 1;
return (x + 1);
}
mi_fp_setisdone(fparam, MI_TRUE);
return 0;
case SET_END:
DBDK_TRACE_EXIT(__FUNCTION__);
return 0;
default:
ONIERR_002("unknown iterator request flag");
return 0;
}
}
Install SQL — add_one_install.sql
-- sqlmorph — Informix install SQL for add_one. Built by Oninit LLC. -- -- Drops the existing SPL of the same name (if any) and registers the -- C UDR shared library in its place. All UDRs in a directory link into -- one shared object (sqlmorph.so by default). Fetch the canonical -- Makefile via: -- wget https://www.oninit.com/sqlmorph/Makefile -- then run: -- make # builds sqlmorph.so -- make IFXDB=yourdb install # registers this procedure -- -- Override the library name with LIBNAME on the make command line if you -- want a different .so name; if you do, update the EXTERNAL NAME below -- to match. DROP PROCEDURE IF EXISTS add_one; CREATE PROCEDURE add_one(x INT) RETURNING INT WITH (ITERATOR) EXTERNAL NAME '$INFORMIXDIR/extend/sqlmorph/sqlmorph.so(add_one)' LANGUAGE C NOT VARIANT;