Document a UDX
As a best practice, you should develop a documentation and comments convention for each UDX. One method is to include all the necessary DDL commands and sample usage at the top of the C++ file as a comment. This can help you to reconstruct the purpose, compilation code, registration arguments, and any additional information such as table format for your functions.
The following example shows a complete listing of the customername.cpp file
with both the code and documentation comments at the beginning of
the 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;
}