#include "ocilib_demo.h"
void print_version(void);
void cleanup(void);
void disconnect(void);
void create_tables(void);
void drop_tables(void);
void test_format(void);
void test_immediate(void);
void test_immediate_format(void);
void test_fetch(void);
void test_bind1(void);
void test_bind2(void);
void test_piecewise_insert(void);
void test_piecewise_fetch(void);
void test_lob(void);
void test_nested_table(void);
void test_ref_cursor(void);
void test_plsql(void);
void test_dates(void);
void test_timestamp(void);
void test_describe(void);
void test_returning(void);
void test_returning_array(void);
void test_object_insert(void);
void test_object_fetch(void);
void test_object_fetch_string(void);
void test_scrollable_cursor(void);
void test_collection(void);
void test_ref(void);
void test_directpath(void);
void test_bigint(void);
void test_number(void);
test_t tab_test[] =
{
{test_format, TRUE},
{test_immediate, TRUE},
{test_immediate_format, TRUE},
{test_fetch, TRUE},
{test_bind1, TRUE},
{test_bind2, TRUE},
{test_piecewise_insert, TRUE},
{test_piecewise_fetch, TRUE},
{test_lob, TRUE},
{test_nested_table, TRUE},
{test_ref_cursor, TRUE},
{test_plsql, TRUE},
{test_dates, TRUE},
{test_timestamp, TRUE},
{test_describe, TRUE},
{test_returning, TRUE},
{test_returning_array, TRUE},
{test_object_insert, TRUE},
{test_object_fetch, TRUE},
{test_object_fetch_string, TRUE},
{test_scrollable_cursor, TRUE},
{test_collection, TRUE},
{test_ref, TRUE},
{test_directpath, TRUE},
{test_bigint, TRUE},
{test_number, TRUE}
};
static otext str[SIZE_STR+1];
static otext temp[SIZE_STR+1];
static int nb_err = 0;
static int nb_warn = 0;
{
print_text("\n");
if (err_type == OCI_ERR_WARNING)
{
print_text("> WARNING : ");
nb_warn++;
}
else
{
print_text("> ERROR : ");
nb_err++;
}
print_text("\n");
}
int omain(int argc, oarg* argv[])
{
otext home[SIZE_STR+1] = OTEXT("");
otext dbs [SIZE_STR+1] = OTEXT("");
otext usr [SIZE_STR+1] = OTEXT("");
otext pwd [SIZE_STR+1] = OTEXT("");
size_t i;
if (argc < (ARG_COUNT-1))
{
return EXIT_FAILURE;
}
GET_ARG(dbs, ARG_DB);
GET_ARG(usr, ARG_USER);
GET_ARG(pwd, ARG_PWD);
if(argc == ARG_COUNT)
GET_ARG(home, ARG_HOME);
if (!
OCI_Initialize(err_handler, home, OCI_ENV_DEFAULT | OCI_ENV_THREADED))
return EXIT_FAILURE;
print_text("Connecting to ");
print_args(usr);
print_text("/");
print_args(pwd);
print_text("@");
print_args(dbs);
print_text("\n\n");
if (cn)
{
print_version();
create_tables();
for (i = 0; i < ARRAY_COUNT(tab_test); i++)
{
if (tab_test[i].execute)
tab_test[i].proc();
}
drop_tables();
disconnect();
}
else
{
}
cleanup();
print_text("\npress any key to exit...");
return getchar() != 0;
}
void cleanup(void)
{
print_frmt("\n%i errors \n", nb_err);
print_frmt("\n%i warnings \n\n", nb_warn);
}
void disconnect(void)
{
}
void print_version(void)
{
print_text("\n>>>>> OCILIB BUILD INFORMATION \n\n");
print_text("OCI import mode : LINKAGE\n");
else
print_text("OCI import mode : RUNTIME\n");
print_text("char type : ANSI\n");
else
print_text("char type : WIDE\n");
print_text("\n>>>>> VERSIONS INFORMATION \n\n");
print_frmt("OCILIB major version : %i\n", OCILIB_MAJOR_VERSION);
print_frmt("OCILIB minor version : %i\n", OCILIB_MINOR_VERSION);
print_frmt("OCILIB revision version : %i\n\n", OCILIB_REVISION_VERSION);
print_text("\n>>>>> SERVER VERSION BANNER \n\n");
print_text("\n\n");
}
void create_tables(void)
{
print_text("\n>>>>> CREATE TABLES FOR DEMO \n\n");
OCI_ExecuteStmt(st, OTEXT(
"create type type_t as OBJECT (id int, name varchar2(50))"));
OTEXT("( ")
OTEXT(" val_int number, ")
OTEXT(" val_flt float, ")
OTEXT(" val_dbl float, ")
OTEXT(" val_str varchar2(30), ")
OTEXT(" val_date date, ")
OTEXT(" val_lob clob, ")
OTEXT(" val_file bfile, ")
OTEXT(" val_obj type_t, ")
OTEXT(" val_raw raw(10) ")
OTEXT(")"));
OCI_ExecuteStmt(st, OTEXT(
"create type t_tab1_emp as VARRAY(100) of varchar2(50)"));
OCI_ExecuteStmt(st, OTEXT(
"create type t_tab2_emp as table of varchar2(50)"));
OTEXT("varchar2(30), price float, creation date)"));
OCI_ExecuteStmt(st, OTEXT(
"create table test_long_raw(code int, content long raw)"));
OCI_ExecuteStmt(st, OTEXT(
"create table test_long_str(code int, content long)"));
OCI_ExecuteStmt(st, OTEXT(
"create table test_lob(code int, content clob)"));
OTEXT("( ")
OTEXT(" val_int number, ")
OTEXT(" val_dbl float, ")
OTEXT(" val_flt float, ")
OTEXT(" val_str varchar2(30), ")
OTEXT(" val_date date, ")
OTEXT(" val_lob clob, ")
OTEXT(" val_file bfile ")
OTEXT(")")
);
OTEXT("( ")
OTEXT(" departement number, ")
OTEXT(" employees t_tab1_emp ")
OTEXT(")")
);
OTEXT("( ")
OTEXT(" departement number, ")
OTEXT(" employees t_tab2_emp ")
OTEXT(") nested table employees store as test_table_emp")
);
OCI_ExecuteStmt(st, OTEXT(
"create table test_directpath(val_int number(8,4), ")
OTEXT(" val_str varchar2(30), val_date date)"));
OCI_ExecuteStmt(st, OTEXT(
"create type test_num_coll_t as varray(10) of number"));
OCI_ExecuteStmt(st, OTEXT(
"create type test_num_t as object (value number)"));
OTEXT("(code, article, price, creation) ")
OTEXT("values (1, 'shoes', 3.14, to_date('1978-12-23', 'YYYY-MM-DD'))"));
OTEXT("(code, article, price, creation) ")
OTEXT("values (2, 'shirt', 5.99, to_date('1999-09-12', 'YYYY-MM-DD'))"));
OTEXT("values (1, EMPTY_CLOB())"));
OTEXT("values (1, 'Rugby rocks !')"));
OCI_ExecuteStmt(st, OTEXT(
"insert into test_coll_varray(departement,employees) ")
OTEXT("values (1, t_tab1_emp('Peter', 'John', 'Paula', 'Gina'))"));
OCI_ExecuteStmt(st, OTEXT(
"insert into test_coll_varray(departement,employees) ")
OTEXT("values (2, t_tab1_emp('Ben', 'Alice', 'Joel', 'Maria'))"));
OCI_ExecuteStmt(st, OTEXT(
"insert into test_coll_nested(departement,employees) ")
OTEXT("values (1, t_tab2_emp('Vince', 'Richard', 'Rita', 'Sophia'))"));
OCI_ExecuteStmt(st, OTEXT(
"insert into test_coll_nested(departement,employees) ")
OTEXT("values (2, t_tab2_emp('Paul', 'Sarah', 'Robert', 'Zoe'))"));
OCI_ExecuteStmt(st, OTEXT(
"insert into test_table_obj values(type_t(1, 'shoes'))"));
OCI_ExecuteStmt(st, OTEXT(
"insert into test_table_obj values(type_t(2, 'pen'))"));
}
void drop_tables(void)
{
print_text("\n>>>>> DROPPING TABLES AND TYPES \n\n");
}
void test_format(void)
{
int code = 1;
print_text("\n>>>>> TEST FORMATTING \n\n");
{
print_text("\n");
}
}
void test_immediate(void)
{
int code = 1;
print_text("\n>>>>> TEST IMMEDIATE \n\n");
OCI_Immediate(cn, OTEXT(
"select code, article from test_fetch where code = 1"),
OCI_ARG_INT, &code,
OCI_ARG_TEXT, temp);
print_frmt("> code : %i ", code);
print_text("- article : "); print_ostr(temp);
print_text("\n");
}
void test_immediate_format(void)
{
int code = 1;
print_text("\n>>>>> TEST IMMEDIATE FORMATTED\n\n");
code, OCI_ARG_TEXT, temp);
print_text("> article : "); print_ostr(temp); print_text("\n");
}
void test_fetch(void)
{
int i, n;
print_text("\n>>>>> SIMPLE TEST FETCH WITH META DATA\n\n");
for(i = 1; i <= n; i++)
{
print_frmt("> Field : #%i ", i);
print_text("\n");
}
print_text("\n");
{
print_text("\n");
}
}
void test_bind1(void)
{
int code = 1;
print_text("\n>>>>> TEST BINDING \n\n");
OCI_Prepare(st, OTEXT(
"select * from test_fetch where code = :code"));
{
print_text("\n");
}
}
void test_bind2(void)
{
int i;
double dbl;
float flt;
print_text("\n>>>>> SINGLE BINDING \n\n");
OTEXT("( ")
OTEXT(" val_int, val_dbl, val_flt, val_str, val_date, ")
OTEXT(" val_lob, val_file ")
OTEXT(") " )
OTEXT("values ")
OTEXT("( ")
OTEXT(" :val_int, :val_dbl, :val_flt, :val_str, :val_date, ")
OTEXT(" :val_lob, :val_file ")
OTEXT(") "));
osprintf(temp, SIZE_STR, OTEXT("lob value00"));
i = 1;
dbl = 3.14;
flt = (float) 3.14;
osprintf(temp, 30, OTEXT("Name00"));
}
void test_piecewise_insert(void)
{
FILE *f;
print_text("\n>>>>> TEST PIECEWISE INSERTING\n\n");
f = fopen(OCI_SHARED_LIB, "rb");
if (f)
{
int n;
unsigned char buffer[SIZE_BUF];
fseek (f , 0 , SEEK_END);
n = ftell(f);
rewind (f);
print_frmt("\n%d bytes to write\n", n);
OCI_Prepare(st, OTEXT(
"insert into test_long_raw(code, content) ")
OTEXT("values (1, :data)"));
while ((n = (int) fread(buffer, 1, sizeof(buffer), f)))
{
}
fclose(f);
}
}
void test_piecewise_fetch(void)
{
unsigned char buffer[SIZE_BUF];
int n;
print_text("\n>>>>> TEST PIECEWISE FETCHING\n\n");
OTEXT("where code = 1"));
{
}
print_text("\n>>>>> TEST LONG MAPPED TO STRING\n\n");
OTEXT("where code = 1"));
{
print_ostr(long_str);
print_frmt("\n%d bytes read\n", (int) ostrlen(long_str));
}
}
void test_lob(void)
{
print_text("\n>>>>> TEST LOB MANIPULATION\n\n");
OTEXT("where code=1 for update"));
{
print_text(", content : "); print_ostr(temp);
print_text("\n");
}
}
void test_nested_table(void)
{
print_text("\n>>>>> TEST NESTED TABLE \n\n");
OTEXT("from dual) from test_fetch"));
{
{
print_text("\n");
}
}
}
void test_ref_cursor(void)
{
print_text("\n>>>>> TEST REF CURSOR \n\n");
OCI_Prepare(st, OTEXT(
"begin open :c for select * from test_fetch; end;"));
{
print_text("\n");
}
}
void test_plsql(void)
{
int res = 0;
const otext *p=NULL;
print_text("\n>>>>> TEST PL/SQL OUTPUT BIND\n\n");
OCI_Prepare(st, OTEXT(
"begin :res := trunc(sysdate+1)-trunc(sysdate-1); end;"));
print_text("\nPL/SQL : trunc(sysdate+1)-trunc(sysdate-1)\n");
print_frmt("\nResult : %i\n", res);
#ifndef OCI_CHARSET_ANSI
{
if (version < OCI_9_0)
return;
}
#endif
print_text("\n>>>>> TEST PL/SQL SERVER OUTPUT\n\n");
OTEXT(" dbms_output.put_line('First line'); ")
OTEXT(" dbms_output.put_line('Second line'); ")
OTEXT(" dbms_output.put_line('Third line'); ")
OTEXT("end;")
);
{
print_ostr(p);
print_text("\n");
}
}
void test_dates(void)
{
print_text("\n>>>>> TEST DATETIME MANIPULATION\n\n");
OCI_DateFromText(date, OTEXT(
"1978-04-13 20:20:12"), OTEXT(
"YYYY-MM-DD HH24:MI:SS"));
print_text("\nDate : "); print_ostr(str);
print_text("\nSysdate : "); print_ostr(str);
print_text("\nDate + 5 days + 2 months : "); print_ostr(str);
print_text("\nLast day of the month : "); print_ostr(str);
print_frmt("\nEnd of the month - date : %i days",
print_text("\n");
}
void test_timestamp(void)
{
#ifndef OCI_CHARSET_ANSI
if ((version >= OCI_9_0) || (version < OCI_10_1))
return;
#endif
{
print_text("\n>>>>> TEST TIMESTAMP\n\n");
print_text("Current timestamp : "); print_ostr(str);
{
print_text("\n>>>>> TEST INTERVAL \n\n");
print_text("Interval : "); print_ostr(str);
print_text("\n");
print_text("Current timestamp + Interval : "); print_ostr(str);
}
}
}
void test_describe(void)
{
int i, n;
print_text("\n>>>>> TEST DESCRIBING TABLE \n\n");
if (tbl)
{
print_text("Column Name NULL ? Type \n");
print_text("--------------------------------------------------------\n");
for(i = 1; i <= n; i++)
{
#if defined(OCI_CHARSET_WIDE)
#if !defined(_WINDOWS)
printf("%-20ls%-8ls%-30ls\n",
#else
wprintf(OTEXT("%-20s%-8s%-30s\n"),
#endif
#else
printf("%-20s%-8s%-30s\n",
#endif
str);
}
}
print_text("\n>>>>> TEST DESCRIBING TYPE \n\n");
if (tbl)
{
print_text("Column Name Type \n");
print_text("---------------------------------------------------\n");
for(i = 1; i <= n; i++)
{
#if defined(OCI_CHARSET_WIDE)
#if !defined(_WINDOWS)
printf("%-20ls%-30ls\n",
#else
wprintf(OTEXT("%-20s%-30s\n"),
#endif
#else
printf("%-20s%-30s\n",
#endif
}
}
}
void test_returning(void)
{
print_text("\n>>>>> TEST RETURNING CLAUSE \n\n");
OCI_Prepare(st, OTEXT(
"update test_lob set code = code + 1 returning code, content into :i, :l"));
{
print_ostr(temp);
print_text("\n");
}
}
void test_returning_array(void)
{
int i;
int tab_int [SIZE_TAB];
otext tab_str [SIZE_TAB][31];
float tab_flt [SIZE_TAB];
double tab_dbl [SIZE_TAB];
print_text("\n>>>>> TEST ARRAY BINDING WITH RETURNING CLAUSE \n\n");
OTEXT("( ")
OTEXT(" val_int, val_dbl, val_flt, val_str, val_date, ")
OTEXT(" val_lob, val_file ")
OTEXT(") " )
OTEXT("values ")
OTEXT("( ")
OTEXT(" :val_int, :val_dbl, :val_flt, :val_str, :val_date, ")
OTEXT(" :val_lob, :val_file ")
OTEXT(") ")
OTEXT("returning")
OTEXT(" val_int, val_dbl, val_flt, val_str, val_date, ")
OTEXT(" val_lob, val_file ")
OTEXT("into " )
OTEXT(" :out_int, :out_dbl, :out_flt, :out_str, :out_date, ")
OTEXT(" :out_lob, :out_file "));
for(i = 0; i < SIZE_TAB; i++)
{
tab_int[i] = i+1;
tab_dbl[i] = 3.14*(double)(i+1);
tab_flt[i] = (float) 3.14*(float)(i+1);
osprintf(tab_str[i], 30, OTEXT("Name%02i"), i+1);
osprintf(temp, SIZE_STR, OTEXT("lob value%02i"), i+1);
OCI_LobWrite(tab_lob[i], temp, (
unsigned int) ostrlen(temp));
osprintf(str, SIZE_STR, OTEXT("file%02i.txt"), i+1);
}
i = 1;
while (rs != NULL)
{
{
print_frmt("Row # %d-------------------\n", i);
print_frmt(
".... val_int : %i\n",
OCI_GetInt2(rs, OTEXT(
":OUT_INT")));
print_frmt(
".... val_dbl : %g\n",
OCI_GetDouble2(rs, OTEXT(
":OUT_DBL")));
print_frmt(
".... val_flt : %g\n",
OCI_GetFloat2(rs, OTEXT(
":OUT_FLT")));
print_text(
".... val_str : "); print_ostr(
OCI_GetString2(rs, OTEXT(
":OUT_STR")));
print_text("\n");
print_text(".... val_date : "); print_ostr(str);
print_text("\n");
print_text(".... val_lob : "); print_ostr(temp);
print_text("\n");
print_text(".... val_file : ");
print_text("/");
print_text("\n");
print_text("\n");
i++;
}
}
}
void test_object_insert(void)
{
char rawbuf[11];
strcpy(rawbuf, "0123456789");
print_text("\n>>>>> TEST OBJECT BINDING \n\n");
OCI_Prepare(st, OTEXT(
"insert into test_object values(:obj)"));
}
void test_object_fetch(void)
{
char rawbuf[11] = "";
print_text("\n>>>>> TEST OBJECT FETCHING \n\n");
{
print_text("\n");
print_text("val_raw : "); print_text(rawbuf);
print_text("\n");
print_text("val_date : "); print_ostr(str);
print_text("\n");
print_text("val_lob : "); print_ostr(temp);
print_text("\n");
print_text("\n");
print_text("\n");
print_text("\n");
print_text("\n");;
}
}
void test_object_fetch_string(void)
{
print_text("\n>>>>> TEST OBJECT FETCHING AS STRING \n\n");
{
print_text("\n");
}
}
void test_scrollable_cursor(void)
{
{
print_text("\n>>>>> TEST SCROLLABLE CURSORS \n\n");
OTEXT("table_name like 'TEST_%' order by table_name"));
print_text("... Go to row 1\n");
{
print_text("\n");
}
{
print_text("\n");
}
print_frmt(
"... Enumerate from row %u back to row 1\n",
OCI_GetRowCount(rs)-1);
{
print_text("\n");
}
print_text("... Go to the 3th row\n");
{
print_text("\n");
}
print_text("... Fetch the next 2 rows\n");
{
print_text("\n");
}
}
}
void test_collection(void)
{
unsigned int i, n;
print_text("\n>>>>> TEST VARRAY BINDING \n\n");
OTEXT(" select employees into :tab_emp ")
OTEXT(" from test_coll_varray ")
OTEXT(" where departement = :id; ")
OTEXT("end;"));
i = 1;
print_frmt("Department ID #%u\n\n", i);
while (elem != NULL)
{
print_text("... Employee : ");
print_text("\n");
}
print_text("\n>>>>> TEST VARRAY FETCHING WITH ITERATOR \n\n");
{
print_frmt(
"\nDepartment ID #%d\n\n",
OCI_GetInt(rs, 1));
while (elem != NULL)
{
print_text("... Employee : ");
print_text("\n");
}
}
print_text("\n>>>>> TEST VARRAY PRINTING \n\n");
{
otext *tmp_str = (otext* ) calloc(n+1, sizeof(otext));
if (tmp_str)
{
print_ostr(tmp_str);
print_text("\n");
free(tmp_str);
}
}
print_text("\n>>>>> TEST NESTED TABLE FETCHING WITH INDEX ACCESS \n\n");
{
print_frmt(
"\nDepartment ID #%d\n\n",
OCI_GetInt(rs, 1));
for(i = 1; i <= n; i++)
{
print_text("... Employee : ");
print_text("\n");
}
}
}
void test_ref(void)
{
print_text("\n>>>>> TEST REF FETCHING \n\n");
{
print_text("\n");
}
print_text("\n>>>>> TEST REF PL/SQL BINDING \n\n");
OTEXT(" select ref(e) into :r from test_table_obj e where e.id = 1; ")
OTEXT("end; "));
print_text("\n");
}
void test_directpath(void)
{
{
boolean res = TRUE;
otext val1[SIZE_COL1+1];
otext val2[SIZE_COL2+1];
otext val3[SIZE_COL3+1];
int i = 0, j = 0, nb_rows = SIZE_ARRAY;
int state;
print_text("\n>>>>> TEST DIRECT PATH (10 loads of 100 rows) \n\n");
{
}
if (res)
{
for (i = 0; i < NB_LOAD ; i++)
{
for (j = 1; j <= nb_rows && res; j++)
{
osprintf(val1, SIZE_COL1+1, OTEXT("%04d"), i + (i*100));
osprintf(val2, SIZE_COL2+1, OTEXT("value %05d"), j + (i*100));
osprintf(val3, SIZE_COL3+1, OTEXT("%04d%02d%02d"), (j%23)+1 + 2000,
(j%11)+1,
(j%23)+1);
}
while (res == TRUE)
{
if ((state == OCI_DPR_FULL) || (state == OCI_DPR_COMPLETE))
if (state == OCI_DPR_COMPLETE)
break;
}
}
if (res)
{
}
}
}
}
void test_bigint(void)
{
big_int value1 = 12345, value2 = 0;
print_text("\n>>>>> TEST BINDING BIG INT \n\n");
OCI_Prepare(st, OTEXT(
"begin :value2 := :value1 * :value1; end;"));
printf("\n%lld * %lld = %lld \n", value1, value1, value2);
}
void test_number()
{
unsigned int i, n;
print_text("\n>>>>> TEST ORACLE NUMBER \n\n");
OCI_Prepare(st, OTEXT(
"select value from test_number"));
{
print_ostr(str);
print_text(" - ");
print_ostr(buf);
print_text("\n");
}
print_ostr(str);
print_text("\n");
print_ostr(str);
print_text("\n");
OCI_Prepare(st, OTEXT(
"update test_number set value = value *2 returning value into :1"));
{
print_text("\n");
}
{
print_ostr(str);
print_text("\n");
}
print_ostr(str);
print_text("\n");
print_ostr(str);
print_text("\n");
OCI_Prepare(st, OTEXT(
"SELECT utl_raw.cast_to_number('FF65'), utl_raw.cast_to_number('00') from dual"));
{
print_text("pos infinite = ");
print_text("\n");
print_text("neg infinite = ");
print_text("\n");
}
print_ostr(str);
print_text("\n");
print_ostr(str);
print_text("\n");
print_ostr(str);
print_text("\n");
print_ostr(str);
print_text("\n");
print_ostr(str);
print_text("\n");
print_ostr(str);
print_text("\n");
}