17.10 - C Function Definition for the Cast Method - Advanced SQL Engine - Teradata Database

Teradata Vantageā„¢ - SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B035-1147-171K
Language
English (United States)

The following example implements cast functionality for converting a circle_t UDT to a VARCHAR(80) predefined data type. The format of the VARCHAR string is:

x:y:r:color

Null attributes are indicated by an absence of a value. For example, the format for a null radius is:

x:y::color

/* File: c_cast.c */

#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>

void circle_t_Cast( UDT_HANDLE    *circleUdt,
                    VARCHAR_LATIN *result,
                    char           sqlstate[6])
{
    INTEGER x, y, r;
    char y_str[11], r_str[11];
    VARCHAR_LATIN color[81];
    int nullIndicator;
    int length;

    /* Get each of the attributes and write them to the result string. */
    FNC_GetStructuredAttribute(*circleUdt, "x", &x, SIZEOF_INTEGER, &nullIndicator, &length);
    if (nullIndicator == 0)
        sprintf((char *)result, "%d:", x);
    else
        strcpy((char *)result, ":");

    FNC_GetStructuredAttribute(*circleUdt, "y", &y, SIZEOF_INTEGER, &nullIndicator, &length);
    if (nullIndicator == 0) {
        sprintf(y_str, "%d", y);
        strcat((char *)result, y_str);
    }
    strcat((char *)result, ":");

    FNC_GetStructuredAttribute(*circleUdt, "radius", &r, SIZEOF_INTEGER, &nullIndicator, &length);
    if (nullIndicator == 0) {
        sprintf(r_str, "%d", r);
        strcat((char *)result, r_str);
    }
    strcat((char *)result, ":");

    FNC_GetStructuredAttribute(*circleUdt, "color", color, SIZEOF_VARCHAR_LATIN_WITH_NULL(80), &nullIndicator, &length);
    if (nullIndicator == 0)
        strcat((char *)result, (char *)color);
}