17.10 - Using 64-bit Implementation-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  */
/* (D8XILMNT and D8XILPTR) defined for 64-bit 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 D8CAIRXSIZ      24
#define FIXED_ELM_LEN_8 42
#define D8XILMNTSIZE     4
#define SPOPTIONSSIZE    2


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

char  *psLogon;
Int32 result;
char   cnta[4];


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

#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


/******************************************************************
 *         function to print error message                        *
 ******************************************************************/

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,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);
				 if (SuccPcl->ActivityType != 0)
                 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);
				 if (OKPcl->ActivityType != 0)
                 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()
{
  char   *ext_ptr;
  D8CAIRX  dbcxptr;
  D8XILMNT dbxilmnt;
  D8XILPTR dbxilptr;
  char* SplFlag = "PY";
  int temp;

  dbcarea.dbriSeg='L'; /* Only one TDSP segment to send */

  /* Allocate Memory for the extension pointer */
  ext_ptr = (char *)(malloc(sizeof(D8CAIRX) +
	        sizeof(struct D8XILMNT) + sizeof(D8XILPTR)));

  /* Initialize the extension area in DBCAREA */
  dbcarea.extension_pointer = (char *)ext_ptr;

/*******************************/
/* Build the extension header  */
/*******************************/

/* Set the Eyecatcher         */
 strncpy(dbcxptr.d8xiId, "IRX8", 4);

  /* Set the first reserved field */
#if (defined(_LP64) || defined(__LP64__) || defined(__64BIT__) || defined(_WIN64))
  memset(dbcxptr.d8xiNxt4, 0, 4);
#else
  dbcxptr.d8xiNext = NULL;
#endif

  /* Set Length of DBCAREA extension to */
  /* size of Extension header + Size of Extension Elements */
  dbcxptr.d8xiSize = D8CAIRXSIZ + FIXED_ELM_LEN_8;

  /* set level to zero to indicate use of 64-bit */
  /* compatible elements of IRX8                 */
  dbcxptr.d8xiLvl = D8XIL64;

  /* Set the second reserved field */
  memset(dbcxptr.d8xiFil1, 0, 3);

#if (defined(_LP64) || defined(__LP64__) || defined(__64BIT__) || defined(_WIN64))
   dbcxptr.d8xiNext = NULL;
#else
   memset(dbcxptr.d8xiNxt8, 0, 8);
#endif

  /* Place the extension header at begining of extension_pointer */
  memcpy(ext_ptr, (char*)(&dbcxptr), sizeof(D8CAIRX));

  /* Move to the end of Extension header */
  ext_ptr = (char*)ext_ptr + D8CAIRXSIZ;

   /* Build the element */
   /* Initialize the SPOptions parcel header for TDSP */
   dbxilmnt.d8xilTyp = PclSPOPTIONSTYPE;
   dbxilmnt.d8xilLen = FIXED_ELM_LEN_8;

   /* Set the high end bit of the element type to 1             */
   /* inorder to notify CLI that app, uses pointer type element */
   dbxilmnt.d8xilTyp = (dbxilmnt.d8xilTyp | 0x8000);

   memcpy(ext_ptr, (char*)(&dbxilmnt), D8XILMNTSIZE);
   ext_ptr = (char*)ext_ptr + D8XILMNTSIZE;

   /* Initialize the pointer Element */
   /* Set first reserved field to zero */
   memset(dbxilptr.d8xilpF2, 0, 2 * sizeof(Byte));
   memcpy(ext_ptr, &dbxilptr.d8xilpF2, 2 * sizeof(Byte));
   ext_ptr = (char *)ext_ptr +  2 * sizeof(Byte);

#if (defined(_LP64) || defined(__LP64__) || defined(__64BIT__) || defined(_WIN64))
   /* Set second reserved field to zero */
   memset(dbxilptr.d8xilpP4, 0, 4 * sizeof(Byte));
   memcpy(ext_ptr, &dbxilptr.d8xilpP4, 4 * sizeof(Byte));
   ext_ptr = (char *)ext_ptr +  4 * sizeof(Byte);

#else
   /* Set  address of the parcel body */
   temp = (Int32)SplFlag;
   memcpy(dbxilptr.d8xilpPt, &temp, 4);
   memcpy(ext_ptr, &dbxilptr.d8xilpPt, 4 * sizeof(char));
   ext_ptr = (char *)ext_ptr +  4 * sizeof(char);

#endif
   /* Set third  reserved field to zero */
   memset(dbxilptr.d8xilpF3, 0, 4 * sizeof(UInt32));
   memcpy(ext_ptr, &dbxilptr.d8xilpF3, 4 * sizeof(UInt32));
   ext_ptr = (char *)ext_ptr +  4 * sizeof(UInt32);


   /* Set Parcel Body Length field  */
   dbxilptr.d8xilpLn = SPOPTIONSSIZE;
   memcpy(ext_ptr, &dbxilptr.d8xilpLn, sizeof(UInt32));
   ext_ptr = (char *)ext_ptr +  sizeof(UInt32);

   /* Set Fourth  reserved field to zero */
   memset(dbxilptr.d8xilpA, 0, 4 * sizeof(Byte));
   memcpy(ext_ptr, &dbxilptr.d8xilpA, 4 * sizeof(Byte));
   ext_ptr = (char *)ext_ptr +  4 * sizeof(Byte);

/* 64 bit support on SPARC, HPUX, AIX, Windows */
#if (defined(_LP64) || defined(__LP64__) || defined(__64BIT__) || \
     defined(_WIN64))
     /* Set  address of the parcel body */
     memcpy(dbxilptr.d8xilpPt, &SplFlag, 8);
     memcpy(ext_ptr, &dbxilptr.d8xilpPt, 8 * sizeof(char));
     ext_ptr = (char *)ext_ptr +  8 * sizeof(char);

#else
    /* Set reserved field to zero */
    memset(dbxilptr.d8xilpP8, 0, 8 * sizeof(Byte));
    memcpy(ext_ptr, &dbxilptr.d8xilpP8, 8 * sizeof(Byte));
    ext_ptr = (char *)ext_ptr +  8 * sizeof(Byte);

#endif

}


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

main(int argc,  char **argv)
{
  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;";
  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();

  MaxSegSize=GetSegmentSize();
  Setdbcareax();

  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);
}