Documentare un UDX
Come best practice, si dovrebbe sviluppare una convenzione per la documentazione e i commenti per ogni UDX. Un metodo consiste nell'includere tutti i comandi DDL necessari e un esempio di utilizzo all'inizio del file C++ come commento. Questo può aiutare a ricostruire lo scopo, il codice di compilazione, gli argomenti di registrazione e qualsiasi informazione aggiuntiva, come il formato delle tabelle per le funzioni.
L'esempio seguente mostra un elenco completo del file " customername.cpp con i commenti al codice e alla documentazione all'inizio del file.
/*
Copyright (c) 2007-2013 IBM Corp.
All rights reserved.
Function CustomerName takes a string and returns an integer 1 if it
begins with 'Customer A' and 0 otherwise.
REGISTRATION:
create or replace function
CustomerName(varchar(64000))
returns INT4
language cpp
parameter style npsgeneric
EXTERNAL CLASS NAME 'CCustomerName'
EXTERNAL HOST OBJECT '/home/nz/udx_files/customername.o_x86'
EXTERNAL SPU OBJECT '/home/nz/udx_files/customername.o_spu10';
USAGE:
create table customers (a int, b varchar(200));
insert into customers values (1,'Customer A');
insert into customers values (2,'Customer B');
insert into customers values (3,'Customer CBA');
insert into customers values (4,'Customer ABC');
select * from customers where CustomerName(b) = 1;
*/
#include "udxinc.h"
#include <string.h>
using namespace nz::udx;
class CCustomerName: public Udf
{
public:
static Udf* instantiate();
virtual ReturnValue evaluate()
{
StringArg *str;
str = stringArg(0);
int lengths = str->length;
char *datas = str->data;
int32 retval = 0;
if (lengths >= 10)
if (memcmp("Customer A",datas,10) == 0)
retval = 1;
NZ_UDX_RETURN_INT32(retval);
}
};
Udf* CCustomerName::instantiate()
{
return new CCustomerName;
}