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

SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Published
July 2021
Language
English (United States)
Last Update
2021-07-27
dita:mapPath
rin1593638965306.ditamap
dita:ditavalPath
rin1593638965306.ditaval
dita:id
B035-1147
lifecycle
previous
Product Category
Teradata Vantageā„¢

The following example implements transform functionality that allows Vantage to export a circle_t type as a VARCHAR(80). 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_fromsql.c */

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

void circle_t_FromSql( 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);
}