Oninit® SQLMorph — C to UDR: a small library
A small C library, mathx, with a header and a source file. Three of its functions cannot become UDRs, and
the report says which and why.
c2udr is a work in progress. These pages show real output of the current version, run against an Informix 15.0 test engine; they are examples, not a released product.
The header
/* Add two ints. */ int mx_add(int a, int b); /* Hypotenuse of a right triangle. */ double mx_hyp(double a, double b); long long mx_big(long long v); /* A greeting for the given name. */ char *mx_greet(const char *name); /* Upper-case a string into a caller buffer. */ void mx_upper(const char *in, char *out, size_t len); /* Square an int into an output parameter. */ void mx_square(int v, int *out); /* Returns NULL for an empty string. */ const char *mx_nonempty(const char *s); void mx_tick(int v);
The command
c2udr.py -n mathx -s mathx.c mathx.h make -f mathx.mk INFORMIXDIR=/opt/informix
The summary printed by c2udr:
c2udr: 12 converted, 3 skipped -> c2udr_out/ skipped mx_manhattan: parameter p: no SQL mapping for type 'point_t' (add a [type] to the map) skipped mx_vsum: parameter 2 (arg1): not supported (varargs or function pointer) skipped mx_notdefined: declared in the headers but not defined in the --src files
The registration SQL
One statement per function. The parameter types follow from the C types; the external name is the generated wrapper.
-- mx_add: Add two ints.
CREATE FUNCTION mx_add(INTEGER, INTEGER)
RETURNING INTEGER
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mathx/mathx.bld(udr_mx_add)'
LANGUAGE C;
-- mx_greet: A greeting for the given name.
CREATE FUNCTION mx_greet(LVARCHAR)
RETURNING LVARCHAR
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mathx/mathx.bld(udr_mx_greet)'
LANGUAGE C;
-- mx_upper: Upper-case a string into a caller buffer.
CREATE FUNCTION mx_upper(LVARCHAR)
RETURNING LVARCHAR
WITH (NOT VARIANT)
EXTERNAL NAME '$INFORMIXDIR/extend/mathx/mathx.bld(udr_mx_upper)'
LANGUAGE C;
A generated wrapper
mx_greet takes a const char * and returns a char *. The wrapper turns the
LVARCHAR argument into a C string, calls the original function, and returns NULL to SQL if the C function
returned a null pointer.
/* mx_greet: A greeting for the given name. */
mi_lvarchar *udr_mx_greet(mi_lvarchar *a0, MI_FPARAM *fp)
{
char *l0;
char * rv;
mi_lvarchar * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
l0 = mi_lvarchar_to_string(a0);
rv = (char *)mx_greet(l0);
if (!rv) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
r = mi_string_to_lvarchar(rv);
return r;
}
mx_upper(const char *in, char *out, size_t len) writes into a buffer the caller supplies. c2udr recognises the
char * plus size pair, gives the wrapper a 512-byte buffer of its own, and makes the buffer the SQL result:
/* mx_upper: Upper-case a string into a caller buffer. */
mi_lvarchar *udr_mx_upper(mi_lvarchar *a0, MI_FPARAM *fp)
{
char *l0;
char l1[512];
mi_lvarchar * r;
if (mi_fp_argisnull(fp, 0)) {
mi_fp_setreturnisnull(fp, 0, 1);
return NULL;
}
l0 = mi_lvarchar_to_string(a0);
l1[0] = '\0';
mx_upper(l0, l1, (size_t)512);
r = mi_string_to_lvarchar(l1);
return r;
}
Calling them
| SQL | Result | What it shows |
|---|---|---|
mx_add(2, 3) | 5 | INTEGER in and out |
mx_hyp(3, 4) | 5.000000000000 | double passed by reference |
mx_big(1099511627775::int8) | 1099511627776 | 64-bit integer |
mx_greet('Ann') | Hello, Ann | string in, string out |
mx_upper('abc def') | ABC DEF | output buffer becomes the result |
mx_square(12) | 144 | output parameter becomes the result |
mx_nonempty('') IS NULL | t | a C null pointer is SQL NULL |
mx_add(1, NULL) IS NULL | t | NULL in, NULL out, C function not called |
EXECUTE PROCEDURE mx_tick(1) | Routine executed. | a void function is a procedure |
See also a real date library and the type mapping.