以下のサンプルは、Teradata Databaseが2つのcircle_t UDTを面積の順序で整列するための整列機能を実装しています。この関数は、指定された半径に基づいて面積を計算し、その結果をFLOATデータ型で戻します。円がNULLの場合や、円のx属性、y属性、または半径属性がNULLの場合、関数から戻される結果はNULLになります。
/* File: c_order.c */
#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>
void circle_t_Ordering( UDT_HANDLE *circleUdt,
FLOAT *result,
int *indicator_circle,
int *indicator_result,
char sqlstate[6],
SQL_TEXT extname[129],
SQL_TEXT specific_name[129],
SQL_TEXT error_message[257])
{
INTEGER x, y, r;
int nullIndicator;
int length;
/* If circle is null, return null. */
if (*indicator_circle == -1) {
*indicator_result = -1;
return;
}
/* Verify that the x attribute is not null. */
FNC_GetStructuredAttribute(*circleUdt, "x", &x, SIZEOF_INTEGER, &nullIndicator, &length);
if (nullIndicator == -1) {
*indicator_result = -1;
return;
}
/* Verify that the y attribute is not null. */
FNC_GetStructuredAttribute(*circleUdt, "y", &y, SIZEOF_INTEGER, &nullIndicator, &length);
if (nullIndicator == -1) {
*indicator_result = -1;
return;
}
/* Get the radius attribute. */
FNC_GetStructuredAttribute(*circleUdt, "radius", &r, SIZEOF_INTEGER, &nullIndicator, &length);
if (nullIndicator == -1) {
*indicator_result = -1;
return;
}
*result = 3.14 * r * r;
}
以下のサンプルでは、クライアントからのVARCHAR(80)をサーバー上のcircle_t型に変換するための変換機能を実装しています。その文字列の内容は、x、y、半径、および色属性をコロン( : )区切り文字で区切ったものです。
x:y:r:color
値がない場合、その属性はnullです。例えば、半径がnullの場合の形式は以下のとおりです。
x:y::color
/* File: c_tosql.c */
#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>
void circle_t_ToSql( VARCHAR_LATIN *p1,
UDT_HANDLE *resultCircleUdt,
char sqlstate[6])
{
char buf[81];
char *x_str, *y_str, *r_str, *color;
char *p;
INTEGER x, y, r;
int nullIndicator;
int len;
/* Make a copy of the input string since we want to modify it. */
strcpy(buf, (char *)p1);
/* Retrieve and set the x coordinate field */
x_str = buf;
p = strchr(x_str, ':');
*p = '\0';
if (x_str[0] == '\0') {
nullIndicator = -1;
}
else {
nullIndicator = 0;
x = atoi(x_str);
}
p++;
FNC_SetStructuredAttribute(*resultCircleUdt, "x", &x, nullIndicator, SIZEOF_INTEGER);
/* Retrieve and set the y coordinate field */
y_str = p;
p = strchr(y_str, ':');
*p = '\0';
if (y_str[0] == '\0') {
nullIndicator = -1;
}
else {
nullIndicator = 0;
y = atoi(y_str);
}
p++;
FNC_SetStructuredAttribute(*resultCircleUdt, "y", &y, nullIndicator, SIZEOF_INTEGER);
/* Retrieve and set the radius field */
r_str = p;
p = strchr(r_str, ':');
*p = '\0';
if (r_str[0] == '\0') {
nullIndicator = -1;
}
else {
nullIndicator = 0;
r = atoi(r_str);
}
p++;
FNC_SetStructuredAttribute(*resultCircleUdt, "radius", &r, nullIndicator, SIZEOF_INTEGER);
/* Retrieve and set the color field */
color = p;
if (color[0] == '\0') {
nullIndicator = -1;
}
else {
nullIndicator = 0;
}
len = strlen(color);
FNC_SetStructuredAttribute(*resultCircleUdt, "color", (VARCHAR_LATIN *)color, nullIndicator, SIZEOF_VARCHAR_LATIN(len));
}
以下のサンプルでは、Teradata Databaseがcircle_t型をVARCHAR(80)としてエクスポートするための変換機能を実装しています。
/* 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);
}