17.10 - Sending TDSP Options Parcel 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       */
/* Creates a Stored procedure using dbcarea extension pointers  */

/* This program demonstrates use of DBCAREA extension elements  */
/* (D8XIELEM and D8XIEP) defined for APH implementation in      */
/* CLI, to send MultiTSR parcels in Parcel mode                 */

/* Both 32-bit as well as  64-bit executables 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
#define D8CAIRXSIZ      24
#define FIXED_ELM_LEN_8 42
#define D8XILMNTSIZE     4  /* sizeof (D8XIELEM) */
#define SPOPTIONSSIZE    2
#define D8XIEPSIZE      38 /* sizeof(D8XIEP) */

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


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


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",logonstr);
  dbcarea.logon_len=strlen(logonstr);
  dbcarea.logon_ptr=logonstr;
  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,i,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 {
                 printf("Flavor Type : %d\n",dbcarea.fet_parcel_flavor);
 switch (dbcarea.fet_parcel_flavor){

  case PclSUCCESS :
                 SuccPcl = (struct CliSuccessType *) dbcarea.fet_data_ptr;
                 printf("Activity Type : %d\n",SuccPcl->ActivityType);
                 printf("Activity Count: %d\n",SuccPcl->ActivityCount);
                 act_count(SuccPcl->ActivityType);
                 break;


  case PclOK      :
                 printf("\nOK Parcel\n");
                 OKPcl = (struct CliOkType *) dbcarea.fet_data_ptr;
                 printf("Activity type : %d\n",OKPcl->ActivityType);
                 printf("Activity Count : %d\n",OKPcl->ActivityCount);
                 act_count(OKPcl->ActivityType);
                 if (OKPcl->ActivityType==49)
                    ShowFlag=1;
                 else
                    ShowFlag=0;
                 break;

  case PclFIELD  :
                for (i=0;i<dbcarea.fet_ret_data_len;i++){
                 if ((ShowFlag==0) ||
                     ((rowcount>2) && (dbcarea.fet_data_ptr[i] != ' ')))
	            printf("%c",dbcarea.fet_data_ptr[i]);
                 else
                   count++;
                 if ((count <8) && (dbcarea.fet_data_ptr[i]==' ')
                                && ((ShowFlag ==1) ))
                   if ((rowcount != 4) && (count !=2))
                    printf("%c",dbcarea.fet_data_ptr[i]);
		 }
                 if((ShowFlag==0)  || ((ShowFlag == 1) && (rowcount>5)))

                  printf("\n");
                 rowcount++;
                 break;

  case PclRECORD :                         /*Returned data */
                 printf( " %s\n",dbcarea.fet_data_ptr);
                 printf( "parcel length %d\n",(Int32)dbcarea.fet_ret_data_len);
                 memcpy(&ctc[0],&dbcarea.fet_data_ptr[0],1);
                 memcpy(&ctc[1],&dbcarea.fet_data_ptr[1],1);
                 memcpy(&ctc[2],&dbcarea.fet_data_ptr[2],1);
                 printf("first two bytes -%s\n",ctc);
                 printf("first two bytes -%d\n",atoi(ctc));
                 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 PclDropProcStmt :
                         printf("Procedure has been dropped\n");
                         break;

  case PclRenProcStmt  :
                         printf("Procedure has been Renamed\n");
                         break;
 case PclCallStmt      :
                         printf("Procedure has been Executed\n");
                         break;
 case PclCreateProcStmt:
                         printf("Procedure has been Created\n");
                         break;
 case PclShowStmt      :
                         printf("Show Procedure Successful\n\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);

}

/*********************************************************************
*      Function : Setdbcareax                                        *
*      Purpose  : Uses the Extension pointer to send MultiTSR parcels*
**********************************************************************/

void Setdbcareax()
{

D8CAIRX           *pExtArea;
D8XIEP            *pElem;
D8XIELEM          *pElem_Typ_and_Len;
void* memptr;
char * s ="PY";

dbcarea.dbriSeg='L';

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

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

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

pExtArea->d8xiId[2] = 'X';
pExtArea->d8xiId[3] = '8';
pExtArea->d8xiSize = sizeof( D8CAIRX ) + 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 = D8XILMNTSIZE + D8XIEPSIZE;
pElem_Typ_and_Len->d8xieTyp = 0;

memptr = (char*)memptr +  D8XILMNTSIZE;

pElem = (D8XIEP *)memptr;
pElem->d8xiepF = PclSPOPTIONSTYPE;        /* Flavor */
pElem->d8xiepLn = 2;
pElem->d8xiepPt = s;

}


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


main()
{
  unsigned int   MaxSegSize;
  char str1[] = "replace procedure tmp(IN p1 integer,IN p2 integer, OUT p3 integer)\n   begin\n    set p3=p1*p2;\n   end;\n";
  char str2[] = "CALL tmp(2,3,p3);";
  char str3[] = "RENAME PROCEDURE tmp TO tmp1;";
  char str4[] = "SHOW PROCEDURE tmp1;";
  char str5[] = "HELP PROCEDURE tmp1 attributes;";
  char str6[] = "HELP 'spl WHILE' ;";
  char str7[] = "DROP PROCEDURE tmp1;";

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

  MaxSegSize=GetSegmentSize();
  Setdbcareax();
  dbcarea.dbriSeg = 'L';
  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();
  dbc_irq(str3);
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();
  dbc_irq(str4);
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();
  dbc_irq(str5);
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();
  dbc_irq(str6);
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();
  dbc_irq(str7);
  fetch_request(dbcarea.o_req_id,dbcarea.o_sess_id);
  dbc_erq();
  exitout(0);
}