17.10 - Using Multiple Using Data Records - Call-Level Interface Version 2

Teradata® Call-Level Interface Version 2 Reference for Workstation-Attached Systems

Product
Call-Level Interface Version 2
Release Number
17.10
Release Date
October 2021
Content Type
Programming Reference
Publication ID
B035-2418-061K
Language
English (United States)
/* Copyright 2013, by Teradata Corporation. All rights reserved. */
/* Teradata PROPRIETARY AND CONFIDENTIAL-RESTRICTED */
/* This program demonstrates use of "using_data_ptr_array" in DBCAREA*/
/* It shows how to set up pointer to an array of data items. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dbcarea.h"
#include "dbchqep.h"
#include <parcel.h>

struct DBCAREA dbcarea;
DBCHQEP dbchqep;
int rc, quit, row_count, i, count_pcl;
char *context;
char cont[4];
char result;
short epis;
char TDPid[30];

char create[] = "CREATE TABLE ARRAY_TAB"
                ",NO FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL "
                "(C1 CHAR(8)) "
                "UNIQUE PRIMARY INDEX (C1);";

char request_1[] = "USING (A CHAR(8)) INSERT INTO ARRAY_TAB VALUES (:A);";

#pragma pack(1)

struct var_req
{
    short length;
    char req[sizeof(request_1)];
} request_2;

#pragma pack()

char selectc1[] = "SELECT C1 FROM ARRAY_TAB ORDER BY C1;";

char dropit[] = "DROP TABLE ARRAY_TAB;";

#define ARRAY_COUNT 15

char *data_array[ARRAY_COUNT] =  {"Jim    ",
                        "Jane   ",
                        "Jack   ",
                        "Jill   ",
                        "Joe    ",
                        "Jesse  ",
                        "Juan   ",
                        "Jordan ",
                        "Jamal  ",
                        "Jerry  ",
                        "JoAnn  ",
                        "Jorgen ",
                        "Judd   ",
                        "Josiah ",
                        "Junior "};

int data_array_lens[ARRAY_COUNT] = {8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};

struct v_array
{
    short length;
    char name[8];
    } data_array_v[ARRAY_COUNT] = {{8,"Bill  "},
                        {8,"Ben    "},
                        {8,"Buddy  "},
                        {8,"Bertha "},
                        {8,"Booboo "},
                        {8,"Bambam "},
                        {8,"Brian  "},
                        {8,"Barry  "},
                        {8,"Beth   "},
                        {8,"Bo     "},
                        {8,"Banana "},
                        {8,"Boris  "},
                        {8,"Bessie "},
                        {8,"Bernie "},
                        {8,"Buster "}};

struct v_array * data_array_p[ARRAY_COUNT] =
{&data_array_v[0],
&data_array_v[1],
&data_array_v[2],
&data_array_v[3],
&data_array_v[4],
&data_array_v[5],
&data_array_v[6],
&data_array_v[7],
&data_array_v[8],
&data_array_v[9],
&data_array_v[10],
&data_array_v[11],
&data_array_v[12],
&data_array_v[13],
&data_array_v[14]};

struct CliSuccessType *success_ptr;
int x;

void main(int argc, char **argv)
{
    printf("array ops demo\n");
    context = cont;
    /* determine if the server supports this feature */
    dbchqep.qepLevel = 1;
    dbchqep.qepItem = QEPIAOP;
    dbchqep.qepTLen = strchr(argv[1], '/') - argv[1];
    memcpy(TDPid, argv[1], dbchqep.qepTLen);
    dbchqep.qepTDP = (int) TDPid;
    dbchqep.qepRArea = &result;
    dbchqep.qepRALen = 1;
    DBCHQE(&rc, context, &dbchqep);
    if (rc)
    {
        printf("DBCHQE 1 failed: %d\n", rc);
        exit(4);
    }
    if (result == 'Y')
        printf("array ops *IS* supported\n");
    else
    {
        printf("array ops *IS NOT* supported\n");
        exit(4);
    }
    /* Initialize CLIv2 */
    dbcarea.total_len = sizeof(struct DBCAREA);
    DBCHINI(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHINI failed: %d\n", rc);
        exit(4);
    }
    /* connect a single session */
    dbcarea.wait_for_resp = 'Y';
    dbcarea.loc_mode = 'Y';
    dbcarea.logon_ptr = argv[1];
    dbcarea.logon_len = strlen(argv[1]);
    dbcarea.connect_type = 'C';
    dbcarea.func = DBFCON;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFCON) failed: %d\n", rc);
        exit(4);
    }
    /* fetch the response */
    dbcarea.i_sess_id = dbcarea.o_sess_id;
    dbcarea.i_req_id = dbcarea.o_req_id;
    dbcarea.func = DBFFET;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFFET) failed: %d\n", rc);
        exit(4);
    }
    if (dbcarea.fet_parcel_flavor == PclSUCCESS)
        printf("*** connect was successful ***\n");
    else
    {
        printf("*** connect failed ***\n");
        exit(4);
    }
    /* end the request */
    dbcarea.func = DBFERQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFERQ) failed: %d\n", rc);
        exit(4);
    }
    /* create the test table */
    dbcarea.req_ptr = create;
    dbcarea.req_len = strlen(create);
    dbcarea.func = DBFIRQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFIRQ) failed: %d\n", rc);
        exit(4);
    }
    /* fetch the response */
    dbcarea.i_req_id = dbcarea.o_req_id;
    dbcarea.func = DBFFET;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFFET) failed: %d\n", rc);
        exit(4);
    }
    if (dbcarea.fet_parcel_flavor == PclSUCCESS)
        printf("*** create-table was successful ***\n");
    else
    {
        printf("*** create-table failed ***\n");
        exit(4);
    }
    /* end the request */
    dbcarea.func = DBFERQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFERQ) failed: %d\n", rc);
        exit(4);
    }
    /* insert using array ops feature */
    dbcarea.req_ptr = request_1;
    dbcarea.req_len = strlen(request_1);
    /*  dbcarea.use_presence_bits = 'Y';
    dbcarea.change_opts='Y'; */
    /*  array ops support code here  */
    dbcarea.using_data_ptr_array = (char *) &data_array;
    dbcarea.using_data_len_array = data_array_lens;
    dbcarea.using_data_count = ARRAY_COUNT;
    dbcarea.resp_mode = 'M';
    dbcarea.change_opts = 'Y';
    dbcarea.func = DBFIRQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFIRQ) failed: %d\n", rc);
        printf("%s\n", dbcarea.msg_text);
        exit(4);
    }
    /* fetch the response */
    dbcarea.i_req_id = dbcarea.o_req_id;
    dbcarea.func = DBFFET;
    count_pcl = 2 * ARRAY_COUNT;
    do
    {
        DBCHCL(&rc, context, &dbcarea);
        if (rc)
        {
            printf("DBCHCL(DBFFET) failed: %d\n", rc);
            exit(4);
        }
        if ((dbcarea.fet_parcel_flavor != PclSUCCESS) && (dbcarea.fet_parcel_flavor != PclENDSTATEMENT))
        {
            printf("*** insert-rows failed for var_len_req='N' ***\n");
            exit(4);
        }
    } while (--count_pcl);
    printf("*** insert-rows was successful for var_len_req='N' ***\n");
    /* end the request */
    dbcarea.func = DBFERQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFERQ) failed: %d\n", rc);
        exit(4);
    }
    /* verify the contents of the table */
    dbcarea.req_ptr = selectc1;
    dbcarea.req_len = strlen(selectc1);
    dbcarea.using_data_ptr_array = 0;
    dbcarea.using_data_len_array = 0;
    dbcarea.using_data_count = 0;
    dbcarea.func = DBFIRQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFIRQ) failed: %d\n", rc);
        exit(4);
    }
    /* fetch the response */
    dbcarea.i_req_id = dbcarea.o_req_id;
    dbcarea.func = DBFFET;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFFET) failed: %d\n", rc);
        exit(4);
    }
    if (dbcarea.fet_parcel_flavor == PclSUCCESS)
    {
        printf("*** select-rows was successful ***\n");
        success_ptr = (struct CliSuccessType *) dbcarea.fet_data_ptr;
#if (defined(I370) && defined(_O_BYTEALIGN))
        memcpy(&x, &success_ptr->ActivityCount, sizeof(int));
#else
        memcpy(&x, success_ptr->ActivityCount, sizeof(int));
#endif
        printf("*** %d rows were returned ***\n", x);
    }
    else
    {
        printf("*** select-rows failed ***\n");
        exit(4);
    }
    quit = 1;
    do
    {
        DBCHCL(&rc, context, &dbcarea);
        if (rc)
        {
            printf("DBCHCL(DBFFET) failed: %d\n", rc);
            exit(4);
        }

        switch (dbcarea.fet_parcel_flavor)
        {
        case PclRECORD:
        case PclSMR:
            printf("Row %2d: %s\n", ++row_count,
                (dbcarea.resp_mode == 'M' ? dbcarea.fet_data_ptr + 1 : dbcarea.fet_data_ptr));
            break;

        case PclXDIX:
        case PclSEMR:
        case PclENDSTATEMENT:
            break;

        case PclENDREQUEST:
            quit = 0;
            break;

        case PclERROR:
        case PclFAILURE:
            printf("an error or failure occurred\n");
            exit(4);

        default:
            printf("unexpected parcel received\n");
            exit(4);

        }
    } while (quit);
    /* end the request */
    dbcarea.func = DBFERQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFERQ) failed: %d\n", rc);
        exit(4);
    }
    /* drop the table */
    dbcarea.req_ptr = dropit;
    dbcarea.req_len = strlen(dropit);
    dbcarea.func = DBFIRQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFIRQ) failed: %d\n", rc);
        exit(4);
    }
    /* fetch the response */
    dbcarea.i_req_id = dbcarea.o_req_id;
    dbcarea.func = DBFFET;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFFET)failed: %d\n", rc);
        exit(4);
    }
    if (dbcarea.fet_parcel_flavor == PclSUCCESS)
        printf("*** drop-table was successful ***\n");
    else
    {
        printf("*** drop-table failed ***\n");
        exit(4);
    }
    /* end the request */
    dbcarea.func = DBFERQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFERQ) failed: %d\n", rc);
        exit(4);
    }
    /* create the test table */
    dbcarea.req_ptr = create;
    dbcarea.req_len = strlen(create);
    dbcarea.func = DBFIRQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFIRQ) failed: %d\n", rc);
        exit(4);
    }
    /* fetch the response */
    dbcarea.i_req_id = dbcarea.o_req_id;
    dbcarea.func = DBFFET;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFFET) failed: %d\n", rc);
        exit(4);
    }
    if (dbcarea.fet_parcel_flavor == PclSUCCESS)
        printf("*** create-table was successful ***\n");
    else
    {
        printf("*** create-table failed ***\n");
        exit(4);
    }
    /* end the request */
    dbcarea.func = DBFERQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFERQ) failed: %d\n", rc);
        exit(4);
    }
    /* insert using array ops feature */
    request_2.length = strlen(request_1);
    strcpy(request_2.req, request_1);
    dbcarea.req_ptr = (char *) &request_2;
    dbcarea.req_len = 0;
    dbcarea.var_len_req = 'Y';
    dbcarea.change_opts='Y';
    /*  array ops support code here  */
    dbcarea.using_data_ptr_array = (char *) &data_array_p;
    dbcarea.using_data_count = ARRAY_COUNT;
    dbcarea.func = DBFIRQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFIRQ) failed: %d\n", rc);
        exit(4);
    }
    /* fetch the response */
    dbcarea.var_len_req = 'N';
    dbcarea.change_opts='Y';
    dbcarea.i_req_id = dbcarea.o_req_id;
    dbcarea.func = DBFFET;
    count_pcl = 2 * ARRAY_COUNT;
    do
    {
        DBCHCL(&rc, context, &dbcarea);
        if (rc)
        {
            printf("DBCHCL(DBFFET) failed: %d\n", rc);
            exit(4);
        }
        if ((dbcarea.fet_parcel_flavor != PclSUCCESS) && (dbcarea.fet_parcel_flavor != PclENDSTATEMENT))
        {
            printf("*** insert-rows failed for var_len_req='Y' ***\n");
            exit(4);
        }
    } while (--count_pcl);
    printf("*** insert-rows was successful for var_len_req='Y' ***\n");
    /* end the request */
    dbcarea.func = DBFERQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFERQ) failed: %d\n", rc);
        exit(4);
    }
    /* verify the contents of the table */
    dbcarea.req_ptr = selectc1;
    dbcarea.req_len = strlen(selectc1);
    dbcarea.using_data_ptr_array = 0;
    dbcarea.using_data_len_array = 0;
    dbcarea.using_data_count = 0;
    dbcarea.func = DBFIRQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFIRQ) failed: %d\n", rc);
        exit(4);
    }
    /* fetch the response */
    dbcarea.i_req_id = dbcarea.o_req_id;
    dbcarea.func = DBFFET;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFFET) failed: %d\n", rc);
        exit(4);
    }
    if (dbcarea.fet_parcel_flavor == PclSUCCESS)
    {
        printf("*** select-rows was successful ***\n");
        success_ptr = (struct CliSuccessType *) dbcarea.fet_data_ptr;
#if (defined(I370) && defined(_O_BYTEALIGN))
        memcpy(&x, &success_ptr->ActivityCount, sizeof(int));
#else
        memcpy(&x, success_ptr->ActivityCount, sizeof(int));
#endif
        printf("*** %d rows were returned ***\n", x);
    }
    else
    {
        printf("*** select-rows failed ***\n");
        exit(4);
    }
    quit = 1;
    row_count = 0;
    do
    {
        DBCHCL(&rc, context, &dbcarea);
        if (rc)
        {
            printf("DBCHCL(DBFFET) failed: %d\n", rc);
            exit(4);
        }

       switch (dbcarea.fet_parcel_flavor)
        {
        case PclRECORD:
        case PclSMR:
            printf("Row %2d: %s\n", ++row_count,
                (dbcarea.resp_mode == 'M' ? dbcarea.fet_data_ptr + 1 : dbcarea.fet_data_ptr));
            break;

        case PclXDIX:
        case PclSEMR:
        case PclENDSTATEMENT:
            break;

        case PclENDREQUEST:
            quit = 0;
            break;

        case PclERROR:
        case PclFAILURE:
            printf("an error or failure occurred\n");
            exit(4);

        default:
            printf("unexpected parcel received\n");
            exit(4);

        }
    } while (quit);
    /* end the request */
    dbcarea.func = DBFERQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFERQ) failed: %d\n", rc);
        exit(4);
    }
    /* drop the table */
    dbcarea.req_ptr = dropit;
    dbcarea.req_len = strlen(dropit);
    dbcarea.func = DBFIRQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFIRQ) failed: %d\n", rc);
        exit(4);
    }
    /* fetch the response */
    dbcarea.i_req_id = dbcarea.o_req_id;
    dbcarea.func = DBFFET;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFFET)failed: %d\n", rc);
        exit(4);
    }
    if (dbcarea.fet_parcel_flavor == PclSUCCESS)
        printf("*** drop-table was successful ***\n");
    else
    {
        printf("*** drop-table failed ***\n");
        exit(4);
    }
    /* end the request */
    dbcarea.func = DBFERQ;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFERQ) failed: %d\n", rc);
        exit(4);
    }
    /* disconnect the session */
    dbcarea.func = DBFDSC;
    DBCHCL(&rc, context, &dbcarea);
    if (rc)
    {
        printf("DBCHCL(DBFDSC) failed: %d\n", rc);
        exit(4);
    }
    /* clean up CLIv2 */
    DBCHCLN(&rc, context);
    if (rc)
    {
        printf("DBCHCLN failed: %d\n", rc);
        exit(4);
    }
}