16.20 - C Function Definition - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
16.20
Release Date
April 2020
Content Type
Programming Reference
Publication ID
B035-1147-162K
Language
English (United States)
#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>

void add_int (UDT_HANDLE *dynUDT,
              INTEGER    *result,
              int        *indicator_udt,
              int        *indicator_result,
              char        sqlstate[6],
              SQL_TEXT    extname[129],     
              SQL_TEXT    specific_name[129],
              SQL_TEXT    error_message[257])
{
   int Indicator=0;
   int  length = 0;
   int  sumValue=0;
   int AttributeIntValue = 0;
   char AttributeVCBuf[41];
   int AttributeVCInt=0;
   char AttributeLobBuf[41];
   int AttributeLobInt=0;
   LOB_LOCATOR AttributeLob;
   FNC_LobLength_t  StartLobLength;
   FNC_LobLength_t  MaxLobLength;
   FNC_LobLength_t  ActualLobLength;
   LOB_CONTEXT_ID LobCtx;
   UDT_HANDLE *UDT;
   int Attribute_count=0;
   int i;
   attribute_info_t dyn_attrinfo;

   strcpy((char *)sqlstate, "00000");
   memset(&(error_message[0]), 0, sizeof(error_message));

   /* This function takes a single Dynamic UDT parameter which is */
   /* permitted to have an arbitrary number of expressions passed */
   /* into it. Each expression is required to be one of:         */
   /*   - BYTEINT, SHORTINT, INTEGER value.                       */
   /*   - CHAR() string representing an integer value.            */
   /*   - VARCHAR() string representing an integer value.         */
   /*   - CLOB() string representing an integer value.            */

   /* Determine number of expressions passed into  */
   /* NEW VARIANT_TYPE Dynamic UDT constructor     */
   FNC_GetStructuredAttributeCount(*dynUDT, &Attribute_count);

   for(i=0; i<Attribute_count; i++)
   {
      /* Get data type information about the attribute being processed */
      FNC_GetStructuredAttributeInfo(*dynUDT, i,
         sizeof(attribute_info_t), &dyn_attrinfo);

      /* Process the value based upon the data type */
      switch(dyn_attrinfo.data_type)
      {
         case BYTEINT_DT:
         case SMALLINT_DT:
         case INTEGER_DT:
            AttributeIntValue = 0;          
            Indicator =0;
            /* Acquire the attribute value */
            FNC_GetStructuredAttribute(*dynUDT,
               &(dyn_attrinfo.attribute_name[0]), &AttributeIntValue,
               sizeof(INTEGER), &Indicator, &length);
            if (Indicator != 0)           // If NULL
               AttributeIntValue = 0;
            /* Process the value */
            sumValue = sumValue + AttributeIntValue;
            break;
         case VARCHAR_DT:
         case CHAR_DT:
            memset(&(AttributeVCBuf[0]), 0, sizeof(AttributeVCBuf));
            AttributeVCInt = 0;
            Indicator = 0;
            FNC_GetStructuredAttribute(*dynUDT,
               &(dyn_attrinfo.attribute_name[0]), &(AttributeVCBuf[0]),
               sizeof(AttributeVCBuf), &Indicator, &length);
            if (Indicator != 0)           // If NULL
               AttributeVCInt = 0;
            else
               AttributeVCInt = atoi(&(AttributeVCBuf[0]));
            /* Process the value */
            sumValue = sumValue + AttributeVCInt;
            break;
         case CLOB_REFERENCE_DT:
            memset(&(AttributeLobBuf[0]), 0, sizeof(AttributeLobBuf));
            AttributeLobInt = 0;
            Indicator = 0;
            FNC_GetStructuredInputLobAttribute(*dynUDT,
               &(dyn_attrinfo.attribute_name[0]),
               &Indicator, &AttributeLob );
            if (Indicator != 0)           // If NULL
               AttributeLobInt = 0;
            else
            {
               MaxLobLength = 41;
               StartLobLength = 0;
               FNC_LobOpen(AttributeLob, &LobCtx, StartLobLength,
                  MaxLobLength);
               FNC_LobRead(LobCtx, &(AttributeLobBuf[0]), MaxLobLength,
                  &ActualLobLength);
               FNC_LobClose(LobCtx);
               AttributeLobInt = atoi(&(AttributeLobBuf[0]));
            }

            sumValue = sumValue + AttributeLobInt;
            break;
         default:
            /* Invalid data type */
            strcpy((char *)sqlstate, "00023");
            sprintf(error_message,
               "\nBad data type passed in to Dynamic UDT");
            return;
            break;
      } /* end of the switch statement */
   }  /* end boundary for the for-attribute-processing-loop */
   *indicator_result = 0;    
   *result = sumValue;
   return;
}