The string_pad_size.cpp sample program
The string_pad_size.cpp program defines a UDF that returns the size of the sample stringpad created by the string_pad_create function.
/**
* UDF string_pad_size() -> int4
*
* Returns the size of the pad. -1 if no pad is found.
*
* COMPILATION:
* nzudxcompile UDX_StringPadSize.cpp -o UDX_StringPadSize.o
*
* REGISTRATION:
* CREATE OR REPLACE FUNCTION string_pad_size()
* RETURNS int4 LANGUAGE CPP PARAMETER STYLE NPSGENERIC NOT FENCED
* CALLED ON NULL INPUT NOT DETERMINISTIC
* EXTERNAL CLASS NAME 'StringPadSize'
* EXTERNAL HOST OBJECT '/tmp/test/UDX_StringPadSize.o_x86'
* EXTERNAL SPU OBJECT '/tmp/test/UDX_StringPadSize.o_spu10';
*
* -->>Do not register any spu-pad related UDFs as 'deterministic'
*
* USAGE:
*
* CREATE TABLE one_dslice(c1 int4);
* INSERT INTO one_dslice VALUES(1);
* SELECT string_pad_create(10, 'netezza') FROM one_dslice;
* SELECT string_pad_size() FROM one_dslice;
*
* Copyright (c) 2007-2010 Netezza Corporation, and IBM Company
* All rights reserved.
*/
#include "udxinc.h"
#include <string.h>
using namespace nz::udx;
struct Root
{
char* data;
int size;
};
class StringPadSize: public Udf
{
private:
public:
static Udf* instantiate();
virtual ReturnValue evaluate()
{
CPad* pad = getPad("stringpad");
Root *ro = (Root*) pad->getRootObject(sizeof(Root));
if(!ro)
{
NZ_UDX_RETURN_INT32(-1);
}
else
{
NZ_UDX_RETURN_INT32(ro->size);
}
}
};
Udf* StringPadSize::instantiate()
{
return new StringPadSize;
}