17.10 - Sending a Parameterized SQL Using APH Defined Extension Elements - 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 2006, by Teradata Corporation. All rights reserved.    */
/* Teradata PROPRIETARY AND CONFIDENTIAL-RESTRICTED            */
/* Sends a NON-APH datainfo and data parcel using dbcarea      */
/* extension for a parameterized SQL                           */
/* This program makes use of pointer elements in Extension     */
/* designed for APH support D8XIEP, D8XIELEM                   */

/* Both 32-bit as well as  64-bit execuatbles can be generated */

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

DBCAREA dbcarea;

Int16 exitout(int n1);
Int16 dbc_con();
Int16 dbc_init();
Int16 dbc_irq(char *);
Int16 dbc_erq();
void set_options();
void act_count(Word );
Int16 fetch_request(long ,long);
void Setdbcareax();
unsigned int GetSegmentSize();

#define CONNECTED     0

#define NOT_CONNECTED 1
#define OK            0
#define STOP          1
#define FAILED       -1
#define MAX_SESSIONS  1

char logon_str[] = "nag2n1/clitst,clitst";
char *logonstr=logon_str;
Int32 result;
char   cnta[4];
char  *psLogon;

struct ERROR_FAIL_Type {
      Word         StatementNo;
      Word         Info;
      Word         Code;
      Word         Length;
     char         Msg[255];
} *Error_Fail;

typedef struct
{
   UInt16               FieldCount;
   UInt16               Datatype;
   UInt16               Length;
}DATAINFO;

#ifdef WIN32
  __declspec(dllimport) char   COPCLIVersion[];
  __declspec(dllimport) char   COPMTDPVersion[];
  __declspec(dllimport) char   COPMOSIosVersion[];
  __declspec(dllimport) char   COPMOSIDEPVersion[];
  __declspec(dllimport) char   OSERRVersion[];

#else
  extern char   COPCLIVersion[];
  extern char   COPMTDPVersion[];
  extern char   COPMOSIosVersion[];
  extern char   COPMOSIDEPVersion[];
  extern char   OSERRVersion[];
#endif

write_error(parcel)
char *parcel;
{ int i;
  Error_Fail = ((struct ERROR_FAIL_Type *) (parcel));
  printf("%d : ",Error_Fail->Code);
  for(i=0;i < (int)Error_Fail->Length;i++)
    printf("%c",Error_Fail->Msg[i]);
  printf("\n");
  return(0);
}

/******************************************************************
 *         function to initialize dbcarea                         *
 ******************************************************************/


Int16 dbc_init()
{
  dbcarea.total_len = sizeof(struct DBCAREA);
  DBCHINI(&result,cnta,&dbcarea);
  if (result != EM_OK){
     printf("\nError in initialization --%s",dbcarea.msg_text);
     exitout(1);
  }
  return EM_OK;
}


/********************************************************************
 *                  function to logon                               *
 ********************************************************************/


Int16 dbc_con()
{
  printf("Logging on to :%s",psLogon);
  dbcarea.logon_len=strlen(psLogon);
  dbcarea.logon_ptr=psLogon;
  dbcarea.func=DBFCON;
  DBCHCL(&result,cnta,&dbcarea);
  if (result != EM_OK)
  {
     printf("\nError in Logon--%s\n",dbcarea.msg_text);
     exitout(1);
  }

  return EM_OK;
}



/*********************************************************************
 *                    function to initiate request                   *
 *********************************************************************/


Int16 dbc_irq(char *str)
{

  dbcarea.func = DBFIRQ;
  printf("\nSubmitting request :%s",str);
  dbcarea.req_ptr = str;
  dbcarea.req_len = strlen(str);
  DBCHCL(&result,cnta,&dbcarea);
  if (result != EM_OK){
    printf("Error in Initiating a request--%s",dbcarea.msg_text);
    exitout(1);
  }
  return EM_OK;
}


/*******************************************************************
 *       function to fetch parcels and check activity type         *
 *******************************************************************/


Int16 fetch_request(request,session)
long  request,session;
{
  int count,status,rowcount;
  int ShowFlag=0;
  char ctc[6400];
  struct CliSuccessType *SuccPcl;
  struct CliOkType *OKPcl;
  dbcarea.i_sess_id=session;
  dbcarea.i_req_id=request;
  dbcarea.func = DBFFET;
  status=OK;
  printf("\n");
   rowcount=1;
  while (status == OK){
   DBCHCL(&result,cnta,&dbcarea);
   count=1;
    if (result == REQEXHAUST) status = STOP;
    else if (result != EM_OK) status = FAILED;
    else {

 switch (dbcarea.fet_parcel_flavor){
           /*getch();*/
           printf ("%d",dbcarea.fet_parcel_flavor);

  case PclSUCCESS :
                 SuccPcl = (struct CliSuccessType *) dbcarea.fet_data_ptr;
                 act_count(SuccPcl->ActivityType);
                 break;


  case PclOK      :
                 OKPcl = (struct CliOkType *) dbcarea.fet_data_ptr;
                 act_count(OKPcl->ActivityType);
                 if (OKPcl->ActivityType==49)
                    ShowFlag=1;
                 else
                    ShowFlag=0;
                 break;

  case PclFIELD  :
                 break;

  case PclRECORD :                         /*Returned data */
                 break;

  case PclENDSTATEMENT:
                 break;

  case PclENDREQUEST :
                 break;

  case PclFAILURE :
                 printf("failure parcel  PCLFAILURE\n,length %d \n",
                        (Int32)dbcarea.fet_ret_data_len);    /*Failure parcel*/
                 break;

  case PclERROR :
                printf("PCLERROR");                    /*Error parcel  */
                status = STOP;
                write_error(dbcarea.fet_data_ptr);
                exitout(CONNECTED);
                break;

      } /*end of switch*/
    }  /*end of else*/
 } /*end of while*/

  if(status == FAILED)
  { printf("fetch failed -- %s\n",dbcarea.msg_text);
    exitout(CONNECTED);
  } /*if*/
  printf("\n");
   return EM_OK;
}

Int16 dbc_erq()
{
  dbcarea.i_sess_id = dbcarea.o_sess_id;
  dbcarea.i_req_id = dbcarea.o_req_id;
  dbcarea.func = DBFERQ;
  DBCHCL(&result,cnta,&dbcarea);
  if (result != EM_OK){
     printf("Error in EndRequest--%s\n",dbcarea.msg_text);
     exitout(1);
  }
  return EM_OK;
}


/*********************************************************************
 *                      function to disconnect                       *
 *********************************************************************/



Int16 exitout(int  n)
{
  dbcarea.func = DBFDSC;
  DBCHCL(&result,cnta,&dbcarea);

  DBCHCLN(&result,cnta);
  printf("\nExiting--%s",dbcarea.msg_text);
  exit(n);
  return(n);
}


/*********************************************************************
 *                      function to set options                      *
 *********************************************************************/


void set_options()
{
  dbcarea.change_opts = 'Y';
  dbcarea.resp_mode = 'F';
  dbcarea.use_presence_bits = 'N';
  dbcarea.keep_resp = 'N';
  dbcarea.wait_across_delay = 'N';
  dbcarea.tell_about_delay = 'Y';

  dbcarea.loc_mode = 'Y';
  dbcarea.var_len_req = 'N';
  dbcarea.var_len_fetch = 'N';
  dbcarea.save_resp_buf = 'N';
  dbcarea.two_resp_bufs = 'N';
  dbcarea.ret_time = 'N';

  dbcarea.parcel_mode='Y';
  dbcarea.wait_for_resp = 'Y';
  dbcarea.req_proc_opt = 'E';
  dbcarea.req_buf_len = 1024;
  dbcarea.resp_buf_len = 1024;

}


void act_count(Word Activitytype)
{
  switch(Activitytype){
  case PclDropTabStmt :
                         printf("\n-----------Table dropped--------------\n");
                         break;

  case PclCTStmt:
                         printf("\n-----------Table Created--------------\n");
                         break;

 case PclInsStmt     :
                         printf("\n-------------Insert Successful--------\n");
                         break;

 case PclRetStmt     : printf("\n-------------Select Successful----------\n");
                         break;

 default               :
                         break;

 }
 return;
}



/********************************************************************
*      Function : GetSegmentSize                                    *
*      Purpose  : Gets maximum segment size from server             *
********************************************************************/

unsigned int GetSegmentSize()
{
 DBCHQEP QEPParms ;
 unsigned int   MaxSegSize;
 memset( &QEPParms, 0, sizeof( QEPParms ) );
 QEPParms.qepLevel = QEPLEVEL;
 QEPParms.qepItem = QEPIDMSS ;
 QEPParms.qepRALen = sizeof( long );
 QEPParms.qepRArea = &MaxSegSize;
 QEPParms.qepTDP = ( long ) ( "nag2n1" );
 QEPParms.qepTLen =  strlen("nag2n1");
 DBCHQE( &result, cnta, &QEPParms );
 MaxSegSize = *(unsigned int *) QEPParms.qepRArea;
 printf(" MaxSegSize :%d", MaxSegSize);
 return(MaxSegSize);

}

void Setdbcareax()
{

D8CAIRX           *pExtArea;
D8XIEP            *pElem;
D8XIELEM          *pElem_Typ_and_Len;
void* memptr;
int* data;
DATAINFO*  datainfo;

datainfo = malloc (sizeof(DATAINFO));
data = malloc(sizeof(data));
*data =1;
datainfo->FieldCount =1;
datainfo->Datatype   =496;
datainfo->Length     =4;

dbcarea.extension_pointer = malloc ( sizeof( D8CAIRX ) + (2 *( sizeof(D8XIELEM) + sizeof(D8XIEP) )));
pExtArea = (D8CAIRX *)dbcarea.extension_pointer;
memset( pExtArea, 0, sizeof( D8CAIRX ) +
	     (2 * (( sizeof (D8XIELEM) + sizeof(D8XIEP) ))) );

pExtArea->d8xiId[0] = 'I';

pExtArea->d8xiId[1] = 'R';

pExtArea->d8xiId[2] = 'X';
pExtArea->d8xiId[3] = '8';

pExtArea->d8xiSize = sizeof( D8CAIRX ) +(2 * (( sizeof (D8XIELEM) + sizeof(D8XIEP) ))) ;
pExtArea->d8xiLvl = 1;
/* next the data parcel */

memptr = (char *)(pExtArea + 1);
pElem_Typ_and_Len = (D8XIELEM *)memptr;
pElem_Typ_and_Len->d8xieLen = sizeof (D8XIELEM) + sizeof(D8XIEP);
pElem_Typ_and_Len->d8xieTyp = 0;

memptr = (char*)memptr +  sizeof (D8XIELEM);

pElem = (D8XIEP *)memptr;
pElem->d8xiepF = PclDATAINFO;        /* Flavor */
pElem->d8xiepLn = sizeof(DATAINFO);
pElem->d8xiepPt = datainfo;
memptr = (char*)memptr +  sizeof (D8XIEP);


pElem_Typ_and_Len = (D8XIELEM *)memptr;
pElem_Typ_and_Len->d8xieLen = sizeof (D8XIELEM) + sizeof(D8XIEP);
pElem_Typ_and_Len->d8xieTyp = 0;
memptr = (char*)memptr +  sizeof (D8XIELEM);

pElem = (D8XIEP *)memptr;
pElem->d8xiepF = PclDATA;        /* Flavor */
pElem->d8xiepLn = sizeof(int);
pElem->d8xiepPt = data;

}


/*************************************************************
 *                 main                                      *
 ************************************************************/


main(int argc,  char **argv)
{
  char str0[]= "drop table samp";
  char str1[] = "create table samp ( i int)";
  char str2[] = "insert into samp (1)";
  char str3[] = "select * from samp where i=?";
  char  psDefaultLogon[] = "dbc/systemfe,service";

  psLogon = psDefaultLogon;
  if (argc >= 2)
  {
     psLogon = argv[1];
  }
  if (psLogon == NULL)
  {
     psLogon = psDefaultLogon;
  }
  if (!strncmp(psLogon, "-h", 2))
  {
     printf ("clisamp logonstring(dbcname/username,password)\n");
     exit(1);
 }

  printf("\nCLIv2   version is %s\n",   COPCLIVersion);
  printf("MTDP    version is %s\n",   COPMTDPVersion);
  printf("MOSIOS  version is %s\n",   COPMOSIosVersion);
  printf("MOSIDEP version is %s\n",   COPMOSIDEPVersion);
  printf("OSERR   version is %s\n\n", OSERRVersion);


  dbc_init();
  set_options();
  dbc_con();
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();

  dbc_irq(str0);
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();
  dbc_irq(str1);
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();
  dbcarea.extension_pointer=NULL;
  dbcarea.dbriSeg='N';
  dbc_irq(str2);
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();
  Setdbcareax();
  dbc_irq(str3);
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();
  exitout(0);
}