Exemple #1
0
int main(int argc, char *argv[])
{
    OCI_Connection *con;
    OCI_Enqueue    *enq;
    OCI_Dequeue    *deq;
    OCI_Msg        *msg;
    OCI_TypeInfo   *inf;
    OCI_Object     *obj;
   
    OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT);
     
    con = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);

    inf = OCI_TypeInfoGet(con, "MY_MESSAGE", OCI_TIF_TYPE);
    
    enq = OCI_EnqueueCreate(inf, "my_queue");
    deq = OCI_DequeueCreate(inf, "my_queue");
    
    msg = OCI_MsgCreate(inf);
    obj = OCI_ObjectCreate(con, inf);

    OCI_ObjectSetString(obj, "TITLE", "NEXT MEETING");
    OCI_ObjectSetString(obj, "CONTENT", "12:00 PM IN STARBUCKS");

    OCI_MsgSetObject(msg, obj);

    OCI_EnqueuePut(enq, msg);

    OCI_MsgFree(msg);
    OCI_ObjectFree(obj);

    OCI_Commit(con);
    
    msg = OCI_DequeueGet(deq);
    obj = OCI_MsgGetObject(msg);

    printf("MSG '%s' => %s\n",  OCI_ObjectGetString(obj, "TITLE"),
                                OCI_ObjectGetString(obj, "CONTENT"));

    OCI_EnqueueFree(enq);
    OCI_DequeueFree(deq);
    OCI_ConnectionFree(con);

    OCI_Cleanup();

   return EXIT_SUCCESS;
}
Exemple #2
0
int main(void)
{
    OCI_Connection *cn;
    OCI_Statement *st;
    OCI_Object *racing_car;
    OCI_TypeInfo *vehicule_type_inf;
    OCI_TypeInfo *car_type_inf;
    OCI_TypeInfo *racing_car_type_inf;

    char buffer[512];
    unsigned int size = sizeof(buffer) - 1;

    if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
    {
        return EXIT_FAILURE;
    }

    cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
    st = OCI_StatementCreate(cn);

    racing_car = OCI_ObjectCreate(cn, OCI_TypeInfoGet(cn, "racing_car_type", OCI_TIF_TYPE));

    OCI_Prepare(st, "begin :obj := racing_car_type(1,'Formula1', 123456789, 300); end;");
    OCI_BindObject(st, "obj", racing_car);
    OCI_Execute(st);
    OCI_ObjectToText(racing_car, &size, buffer);
    printf("Object => %s\n", buffer);

    racing_car_type_inf = OCI_ObjectGetTypeInfo(racing_car);
    car_type_inf = OCI_TypeInfoGetSuperType(racing_car_type_inf);
    vehicule_type_inf = OCI_TypeInfoGetSuperType(car_type_inf);

    printf("Is type '%s' final => %d\n", OCI_TypeInfoGetName(racing_car_type_inf), OCI_TypeInfoIsFinalType(racing_car_type_inf));
    printf("Is type '%s' final => %d\n", OCI_TypeInfoGetName(car_type_inf), OCI_TypeInfoIsFinalType(car_type_inf));
    printf("Is type '%s' final => %d\n", OCI_TypeInfoGetName(vehicule_type_inf), OCI_TypeInfoIsFinalType(vehicule_type_inf));

    OCI_ObjectFree(racing_car);
    OCI_StatementFree(st);
    OCI_ConnectionFree(cn);
    
    OCI_Cleanup();

    return EXIT_SUCCESS;
}
Exemple #3
0
int main(void)
{
    OCI_Connection *cn;
    OCI_Statement  *st;
    OCI_Resultset  *rs;
    OCI_Ref       *ref;

    if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
        return EXIT_FAILURE;

    cn  = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
    st  = OCI_StatementCreate(cn);

    OCI_ExecuteStmt(st, "select ref(e) from table_obj e");
    rs = OCI_GetResultset(st);

    printf("\n\n=> fetch refs from object table\n\n");

    while (OCI_FetchNext(rs))
    {
        dump_ref(OCI_GetRef(rs, 1));
    }

    printf("\n\n=> bind a local ref object to a PL/SQL statement\n\n");

    ref = OCI_RefCreate(cn, OCI_TypeInfoGet(cn, "ARTICLE_T", OCI_TIF_TYPE));

    OCI_Prepare(st, "begin "
                    "  select ref(e) into :r from table_obj e where e.id = 1; "
                    "end; ");
    
    OCI_BindRef(st, ":r", ref);
    OCI_Execute(st);

    dump_ref(ref);

    OCI_RefFree(ref);

    OCI_Cleanup();

    return EXIT_SUCCESS;
}
int main(void)
{
    OCI_Connection *cn;
    OCI_TypeInfo *tbl;
    int i,n;

    if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
        return EXIT_FAILURE;

    cn  = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
    tbl = OCI_TypeInfoGet(cn, "products", OCI_TIF_TABLE);

    if (tbl != NULL)
    {
        printf ("Column Name         Type      Length  Prec.   Scale   Null ?\n");
        printf ("----------------------------  ------------------------------\n");

        n = OCI_TypeInfoGetColumnCount(tbl);
   
        for(i = 1; i <= n; i++)
        {
            OCI_Column *col = OCI_TypeInfoGetColumn(tbl, i);

            printf("%-20s%-10s%-8i%-8i%-8i%-s\n",
                    OCI_GetColumnName(col),
                    OCI_GetColumnSQLType(col),
                    OCI_GetColumnSize(col),
                    OCI_GetColumnPrecision(col),
                    OCI_GetColumnScale(col),
                    OCI_GetColumnNullable(col) == TRUE ? "Y" : "N");
        }
    }

    OCI_Cleanup();

    return EXIT_SUCCESS;
}
Exemple #5
0
int main(int argc, char **argv)
{
    OCI_Connection *cn;
    OCI_Statement  *st;
    OCI_Object     *obj_sdo;
    OCI_Coll       *coll_inf,  *coll_ord;
    OCI_Elem       *elem_inf,  *elem_ord;
    OCI_TypeInfo   *tif_inf,   *tif_ord, *tif_sdo;
 
    int i;
 
    /* check command line */
    if (argc < 3) 
    {
        fprintf(stderr, "Usage: %s user password\n", argv[0]);
        return EXIT_FAILURE;
    }
 
    /* init OCILIB */
    if (OCI_Initialize(error, NULL, OCI_ENV_DEFAULT))
    {
        /* connect to oracle */
        if (cn = OCI_ConnectionCreate(NULL, argv[1], argv[2], OCI_SESSION_DEFAULT))
        {
            printf ("\nConnected to Oracle.\n");
 
            /* retreive type info */
            tif_sdo = OCI_TypeInfoGet(cn, "MDSYS.SDO_GEOMETRY", OCI_TIF_TYPE);
            tif_inf = OCI_TypeInfoGet(cn, "MDSYS.SDO_ELEM_INFO_ARRAY", OCI_TIF_TYPE);
            tif_ord = OCI_TypeInfoGet(cn, "MDSYS.SDO_ORDINATE_ARRAY", OCI_TIF_TYPE);
 
            /* create sdo object */
            obj_sdo = OCI_ObjectCreate(cn, tif_sdo);
 
            /* create sub arrays */
            coll_inf = OCI_CollCreate(tif_inf);
            coll_ord = OCI_CollCreate(tif_ord);
 
            /* create sub array element accessors */
            elem_inf = OCI_ElemCreate(tif_inf);
            elem_ord = OCI_ElemCreate(tif_ord);
 
            /* build ordinates collection with test values */
            for (i = 0; i < NB_ELEM; i++)
            {
                OCI_ElemSetDouble(elem_ord, (double) i);
                OCI_CollAppend(coll_ord, elem_ord);
                OCI_CollAppend(coll_ord, elem_ord);
            }
 
            /* setup information collection attribute 'starting_offset' */
            OCI_ElemSetUnsignedInt(elem_inf, 1);
            OCI_CollAppend(coll_inf, elem_inf);
 
            /* setup information collection attribute 'element_type' */
            OCI_ElemSetUnsignedInt(elem_inf, 1);
            OCI_CollAppend(coll_inf, elem_inf);
 
            /* setup information collection attribute 'interpretation' */
            OCI_ElemSetUnsignedInt(elem_inf, 1);
            OCI_CollAppend(coll_inf, elem_inf);
 
 
            /* set sdo object member attributes */
            OCI_ObjectSetInt(obj_sdo,  "SDO_GTYPE", 4);
            OCI_ObjectSetNull(obj_sdo, "SDO_SRID");
            OCI_ObjectSetNull(obj_sdo, "SDO_POINT");
            OCI_ObjectSetColl(obj_sdo, "SDO_ELEM_INFO", coll_inf);
            OCI_ObjectSetColl(obj_sdo, "SDO_ORDINATES", coll_ord);
 
            /*create statement object */
            st = OCI_StatementCreate(cn);
 
            /* prepare, bind and excute statement then commit*/
            OCI_Prepare(st, "INSERT INTO test_insert (gid, geometry) VALUES (1, :sdo)");
            OCI_BindObject(st, "sdo", obj_sdo);
            OCI_Execute(st);
            OCI_Commit(cn);
 
            /* free local objects */
            OCI_ObjectFree(obj_sdo);
            OCI_CollFree(coll_inf);
            OCI_CollFree(coll_ord);
            OCI_ElemFree(elem_inf);
            OCI_ElemFree(elem_ord);
        }
    }
 
    /* disconnect from oracle and cleanup OCILIB */
    OCI_Cleanup();
 
    printf ("\nDisconnected from Oracle.\n");
 
    return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
    OCI_Connection *con;
    OCI_Enqueue    *enq;
    OCI_Dequeue    *deq1;
    OCI_Dequeue    *deq2;
    OCI_Msg        *msg;
    OCI_TypeInfo   *inf;
    OCI_Object     *obj;
    OCI_Agent      *agents[2];

    OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT | OCI_ENV_THREADED |OCI_ENV_EVENTS);

    con = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
    
    OCI_Immediate(con, "create type my_message as object (title varchar2(50), content varchar2(50)) ");

    OCI_QueueTableCreate(con, "my_queue_table", "my_message", NULL, NULL, TRUE, FALSE, NULL, 0,0,NULL);
    OCI_QueueCreate(con, "my_queue", "my_queue_table", OCIT_NORMAL, 0, 0, 0, 0, NULL);
    OCI_QueueStart(con, "my_queue", TRUE, TRUE);

    agents[0] = OCI_AgentCreate(con, "C1", NULL);
    agents[1] = OCI_AgentCreate(con, "C2", NULL);

    inf = OCI_TypeInfoGet(con, "MY_MESSAGE", OCI_TIF_TYPE);

    enq = OCI_EnqueueCreate(inf, "my_queue");
    deq1 = OCI_DequeueCreate(inf, "my_queue");
    deq2 = OCI_DequeueCreate(inf, "my_queue");

    OCI_DequeueSetConsumer(deq1, "C1");
    OCI_DequeueSetNavigation(deq1, OCI_ADN_FIRST_MSG);
    OCI_DequeueSetConsumer(deq2, "C2");
    OCI_DequeueSetNavigation(deq2, OCI_ADN_FIRST_MSG);

    OCI_DequeueSubscribe(deq1, 9998, 0, on_message);
    OCI_DequeueSubscribe(deq2, 9999, 0, on_message);

    msg = OCI_MsgCreate(inf);
    obj = OCI_ObjectCreate(con, inf);

    OCI_ObjectSetString(obj, "TITLE", "NEXT MEETING");
    OCI_ObjectSetString(obj, "CONTENT", "12:00 PM IN STARBUCKS");

    OCI_MsgSetObject(msg, obj);
    OCI_MsgSetConsumers(msg, agents, 2);

    OCI_EnqueuePut(enq, msg);

    OCI_MsgFree(msg);
    OCI_ObjectFree(obj);

    OCI_Commit(con);

    getchar();

    OCI_DequeueUnsubscribe(deq1);    
    OCI_DequeueUnsubscribe(deq2);    

    OCI_AgentFree(agents[0]);
    OCI_AgentFree(agents[1]);
    OCI_EnqueueFree(enq);
    OCI_DequeueFree(deq1);
    OCI_DequeueFree(deq2);
 
    OCI_QueueStop(con, "my_queue", TRUE, TRUE, 0);
    OCI_QueueDrop(con, "my_queue");
    OCI_QueueTableDrop(con, "my_queue_table", TRUE);
    OCI_Immediate(con, "drop type my_message");

    OCI_ConnectionFree(con);

    OCI_Cleanup();

    return EXIT_SUCCESS;
}
Exemple #7
0
int main(void)
{
    OCI_Connection *cn;
    OCI_Statement *st;
    OCI_Resultset *rs;
    OCI_Coll *coll;
    OCI_Iter *iter;
    OCI_Elem *elem;
    OCI_TypeInfo *type;
    OCI_Object *obj;
    int i, n;

    if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
       return EXIT_FAILURE;

    cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);


    /* Varray binding -------------------------------------------------------- */

    st = OCI_StatementCreate(cn);

    /* create the collection */

    type = OCI_TypeInfoGet(cn, "Varray_type", OCI_TIF_TYPE);
    coll = OCI_CollCreate(type);

    /* bind the local collection to a PL/SQL procedure */
    
    OCI_Prepare(st, "begin load_array(:array); end;");
    OCI_BindColl(st, ":array", coll);
    OCI_Execute(st);
 
    /* the procedure has filled the collection and 
       we can iterate it using an iterator */
    
    iter = OCI_IterCreate(coll);
    elem = OCI_IterGetNext(iter);

    while (elem != NULL)
    {
        printf("value %s\n", OCI_ElemGetString(elem));
        elem = OCI_IterGetNext(iter);
    }

    OCI_IterFree(iter);
    OCI_CollFree(coll);
 
    /* Varray SQL fetch ------------------------------------------------------- */
  
    /* query on a table with varray column */
 
    OCI_ExecuteStmt(st, "SELECT * from table_article");

    rs = OCI_GetResultset(st);

    while (OCI_FetchNext(rs))
    {
        /* iterate the collection using an iterator */

        coll = OCI_GetColl(rs, 2);

        iter = OCI_IterCreate(coll);
        elem = OCI_IterGetNext(iter);

        printf("article #%d\n", OCI_GetInt(rs, 1));

        while (elem != NULL)
        {
            obj = OCI_ElemGetObject(elem);
            printf(".... code %d, name%s \n", OCI_ObjectGetInt(obj, "ID"),
                                              OCI_ObjectGetString(obj, "NAME"));
            elem = OCI_IterGetNext(iter);
        }

        OCI_IterFree(iter);
    }

    /* Nested table fetch ------------------------------------------------------- */
    
    /* query on a table with nested table column */
 
    OCI_ExecuteStmt(st, "SELECT * from table_sales");

    rs = OCI_GetResultset(st);

    while (OCI_FetchNext(rs))
    {
        coll = OCI_GetColl(rs, 2);

        printf("Sale #%d\n", OCI_GetInt(rs, 1));

        /* iterate the collection by accessing element by index */
   
        n = OCI_CollGetSize(coll);

        for(i = 1; i <= n; i++)
        {
            elem = OCI_CollGetAt(coll, i);
            obj  = OCI_ElemGetObject(elem);

            printf(".... employee %s, amount %s \n", OCI_ObjectGetString(obj, "EMP"),
                                                     OCI_ObjectGetString(obj, "AMOUNT"));
        }
    }

    OCI_Cleanup();
    
    return EXIT_SUCCESS;
}
Exemple #8
0
void do_load(OCI_Connection *con, boolean gen_conv_error, boolean gen_load_error, boolean gen_buffer_error, boolean force, char *test_name)
{
    OCI_DirPath    *dp;

    char val1[SIZE_COL1 + 1];
    char val2[SIZE_COL2 + 1];
    char val3[SIZE_COL3 + 1];

    int i = 0, j = 0, nb_rows = SIZE_ARRAY;

    printf("%s : ", test_name);

    /* create table */
    create_table(con);

    /* create direct object */
    dp = OCI_DirPathCreate(OCI_TypeInfoGet(con, TABLE_NAME, OCI_TIF_TABLE), NULL, NUM_COLS, nb_rows);

    /* optional attributes to set */
    OCI_DirPathSetBufferSize(dp, gen_buffer_error ? 100 : 64000);
    OCI_DirPathSetConvertMode(dp, force ? OCI_DCM_FORCE : OCI_DCM_DEFAULT);

    /* describe the target table */
    OCI_DirPathSetColumn(dp, 1, "VAL_INT", SIZE_COL1, NULL);
    OCI_DirPathSetColumn(dp, 2, "VAL_STR", SIZE_COL2, NULL);
    OCI_DirPathSetColumn(dp, 3, "VAL_DATE", SIZE_COL3, "YYYYMMDD");

    /* prepare the load */
    OCI_DirPathPrepare(dp);

    /* perform the load - NB_LOAD iterations of SIZE_ARRAY rows */
    nb_rows = OCI_DirPathGetMaxRows(dp);

    for (i = 0; i < NB_LOAD; i++)
    {
        /* reset the load at each iteration */
        OCI_DirPathReset(dp);

        /* set the values for the current load iteration */
        for (j = 1; j <= nb_rows; j++)
        {
            /* conversion and loading errors are performed on the first load for rows 5 and 8 */
            if (i == 0 && (j == 5 || j == 8) && gen_conv_error)
            {
                /* generate a conversion error - not null clause violation on column val_int  */
                OCI_DirPathSetEntry(dp, j, 1, NULL, 0, TRUE);
                OCI_DirPathSetEntry(dp, j, 2, NULL, 0, TRUE);
                OCI_DirPathSetEntry(dp, j, 3, NULL, 0, TRUE);
            }
            else if (i == 0 && (j == 5 || j == 8) && gen_load_error)
            {
                /* generate a load error - if partitioning available, insert value out of declared partitions ranges */
                sprintf(val1, "%04d", 2500);
                sprintf(val2, "value %05d", j + (i*SIZE_ARRAY));
                sprintf(val3, "%04d%02d%02d", (j % 23) + 1 + 2000, (j % 11) + 1, (j % 23) + 1);

                OCI_DirPathSetEntry(dp, j, 1, val1, (unsigned int)strlen(val1), TRUE);
                OCI_DirPathSetEntry(dp, j, 2, val2, (unsigned int)strlen(val2), TRUE);
                OCI_DirPathSetEntry(dp, j, 3, val3, (unsigned int)strlen(val3), TRUE);
            }
            else
            {
                /* default loading */
                sprintf(val1, "%04d", j + (i*SIZE_ARRAY));
                sprintf(val2, "value %05d", j + (i*SIZE_ARRAY));
                sprintf(val3, "%04d%02d%02d", (j % 23) + 1 + 2000, (j % 11) + 1, (j % 23) + 1);

                OCI_DirPathSetEntry(dp, j, 1, val1, (unsigned int)strlen(val1), TRUE);
                OCI_DirPathSetEntry(dp, j, 2, val2, (unsigned int)strlen(val2), TRUE);
                OCI_DirPathSetEntry(dp, j, 3, val3, (unsigned int)strlen(val3), TRUE);
            }
        }

        /* load data to the server */
        while (TRUE)
        {
            int nb_conv = 0;
            int nb_error = 0;

            /* convert data*/
            int state = OCI_DirPathConvert(dp);

            /* retrieve the number of converted rows */
            nb_conv += OCI_DirPathGetAffectedRows(dp);

            /* conversion or loading errors are performed on the first load */
            if (i == 0)
            {
                if (gen_conv_error && !force && state == OCI_DPR_ERROR)
                {
                    /* on conversion error in default mode, correct values for next conversion */
                    int row = OCI_DirPathGetErrorRow(dp);

                    sprintf(val1, "%04d", row);
                    sprintf(val2, "value %05d", row);
                    sprintf(val3, "%04d%02d%02d", (row % 23) + 1 + 2000, (row % 11) + 1, (row % 23) + 1);

                    OCI_DirPathSetEntry(dp, row, 1, val1, (unsigned int)strlen(val1), TRUE);
                    OCI_DirPathSetEntry(dp, row, 2, val2, (unsigned int)strlen(val2), TRUE);
                    OCI_DirPathSetEntry(dp, row, 3, val3, (unsigned int)strlen(val3), TRUE);

                    nb_conv = 0;
                }

                if (gen_conv_error && force && state == OCI_DPR_COMPLETE)
                {
                    /* on conversion error in force mode, check we got the expected errors*/
                    int err_row = OCI_DirPathGetErrorRow(dp);
                    int err_col = OCI_DirPathGetErrorColumn(dp);

                    while (err_row != 0)
                    {
                        check_results(con, dp, nb_error == 0 ? 5 : 8, err_row, "erred converted row index (force mode)");
                        check_results(con, dp, err_col, 1, "erred column index in force mode");

                        err_row = OCI_DirPathGetErrorRow(dp);
                        err_col = OCI_DirPathGetErrorColumn(dp);

                        nb_error++;
                    }

                    check_results(con, dp, NB_ERROR, nb_error, "conversion errors in force mode");
                }
            }

            if (state == OCI_DPR_FULL)
            {
                /* buffer is too small - load the stream */

                state = OCI_DirPathLoad(dp);

                /* as the stream cannot accept all rows in one go,
                we need to do conversion+load until all rows are processed */

                while (nb_conv < SIZE_ARRAY)
                {
                    for (j = 1; j <= nb_rows - nb_conv; j++)
                    {
                        sprintf(val1, "%04d", (j + nb_conv) + (i*SIZE_ARRAY));
                        sprintf(val2, "value %05d", (j + nb_conv) + (i*SIZE_ARRAY));
                        sprintf(val3, "%04d%02d%02d", ((j + nb_conv) % 23) + 1 + 2000, ((j + nb_conv) % 11) + 1, ((j + nb_conv) % 23) + 1);

                        OCI_DirPathSetEntry(dp, j, 1, val1, (unsigned int)strlen(val1), TRUE);
                        OCI_DirPathSetEntry(dp, j, 2, val2, (unsigned int)strlen(val2), TRUE);
                        OCI_DirPathSetEntry(dp, j, 3, val3, (unsigned int)strlen(val3), TRUE);
                    }

                    /* convert again */

                    state = OCI_DirPathConvert(dp);

                    nb_conv = OCI_DirPathGetAffectedRows(dp);

                    /* load again */

                    state = OCI_DirPathLoad(dp);
                }

                break;
            }
            else if (state == OCI_DPR_COMPLETE)
            {
                /* conversion is complete */

                check_results(con, dp, (gen_conv_error && i == 0 && force) ? (SIZE_ARRAY - NB_ERROR) : SIZE_ARRAY, nb_conv, "converted rows");

                state = OCI_DirPathLoad(dp);

                if (gen_load_error && i == 0)
                {
                    /* on loading error in force mode, check we got the expected errors*/
                    int err_row = OCI_DirPathGetErrorRow(dp);

                    while (err_row != 0)
                    {
                        check_results(con, dp, nb_error == 0 ? 5 : 8, err_row, "erred loaded row index");

                        err_row = OCI_DirPathGetErrorRow(dp);

                        nb_error++;
                    }

                    check_results(con, dp, NB_ERROR, nb_error, "loading errors in force mode");
                }

                check_results(con, dp, (gen_load_error && i == 0) ? nb_conv - NB_ERROR : nb_conv, OCI_DirPathGetAffectedRows(dp), "loaded rows");

                break;
            }
        }
    }

    /* commit load */
    OCI_DirPathFinish(dp);

    /* verify loading results */
    check_results(con, dp, ((gen_conv_error && force) || gen_load_error) ? (NB_TOTAL - NB_ERROR) : NB_TOTAL, OCI_DirPathGetRowCount(dp), "total loaded rows");

    /* check data integrity in database */
    check_data(con, dp, gen_conv_error, gen_load_error, force);

    /* free direct path*/
    OCI_DirPathFree(dp);

    /* drop table */
    drop_table(con);

    printf("SUCCESS\n");
}
Exemple #9
0
int main(void)
{
    OCI_Connection *cn;
    OCI_DirPath    *dp;
    OCI_TypeInfo   *tbl;

    char val1[SIZE_COL1 + 1];
    char val2[SIZE_COL2 + 1];
    char val3[SIZE_COL3 + 1];

    int i = 0, j = 0, nb_rows = SIZE_ARRAY;
    boolean res = TRUE;

    if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
    {
        return EXIT_FAILURE;
    }

    cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
    tbl = OCI_TypeInfoGet(cn, "dirpath_data", OCI_TIF_TABLE);
    dp = OCI_DirPathCreate(tbl, NULL, NUM_COLS, nb_rows);

    /* optional attributes to set */

    OCI_DirPathSetBufferSize(dp, 64000);
    OCI_DirPathSetNoLog(dp, TRUE);
    OCI_DirPathSetParallel(dp, TRUE);

    /* describe the target table */

    OCI_DirPathSetColumn(dp, 1, "val_int", SIZE_COL1, NULL);
    OCI_DirPathSetColumn(dp, 2, "val_str", SIZE_COL2, NULL);
    OCI_DirPathSetColumn(dp, 3, "val_date", SIZE_COL3, "YYYYMMDD");

    /* prepare the load */

    OCI_DirPathPrepare(dp);

    nb_rows = OCI_DirPathGetMaxRows(dp);

    for (i = 0; i < NB_LOAD; i++)
    {
        OCI_DirPathReset(dp);

        for (j = 1; j <= nb_rows; j++)
        {
            /* fill test values */

            sprintf(val1, "%04d", i + (i * 100));
            sprintf(val2, "value %05d", j + (i * 100));
            sprintf(val3, "%04d%02d%02d", (j % 23) + 1 + 2000, (j % 11) + 1, (j % 23) + 1);

            OCI_DirPathSetEntry(dp, j, 1, val1, (unsigned int)strlen(val1), TRUE);
            OCI_DirPathSetEntry(dp, j, 2, val2, (unsigned int)strlen(val2), TRUE);
            OCI_DirPathSetEntry(dp, j, 3, val3, (unsigned int)strlen(val3), TRUE);
        }

        /* load data to the server */

        while (res)
        {
            int state = OCI_DirPathConvert(dp);

            if ((state == OCI_DPR_FULL) || (state == OCI_DPR_COMPLETE))
                res = OCI_DirPathLoad(dp);

            if (state == OCI_DPR_COMPLETE)
                break;
        }
    }

    /* commits changes */

    OCI_DirPathFinish(dp);

    printf("%04d row(s) loaded\n", OCI_DirPathGetRowCount(dp));

    /* free direct path object */

    OCI_DirPathFree(dp);

    OCI_Cleanup();

    return EXIT_SUCCESS;
}