string_pad_get.cpp (Beispielprogramm)
Das Beispielprogramm string_pad_get.cpp verarbeitet den Positionswert einer Eingabezeichenfolge und gibt das Zeichen zurück, das im Zeichenfolgepad an dieser Position eines Zeichenbereichs gespeichert ist. Das Zeichenfolgepad muss mithilfe der Funktion string_pad_create erstellt und mit Daten gefüllt werden.
/**
* UDF string_pad_get(int4) -> char(1)
* Gets a character at index from the pad "stringpad".
* The pad must be created first by using string_pad_create.
*
* argument1 = index at which to return the character
*
* returns = the character at index
*
* throws error if the spu pad "stringpad" is not found or if index
* is out of range.
*
* COMPILATION:
* nzudxcompile UDX_StringPadGet.cpp -o /tmp/test/UDX_StringPadGet.o
*
* REGISTRATION:
* CREATE OR REPLACE FUNCTION string_pad_get (int4)
* RETURNS char(1)
* LANGUAGE CPP
* PARAMETER STYLE NPSGENERIC NOT FENCED
* CALLED ON NULL INPUT
* NOT DETERMINISTIC
* EXTERNAL CLASS NAME 'StringPadGet'
* EXTERNAL HOST OBJECT '/tmp/test/UDX_StringPadGet.o_x86'
* EXTERNAL SPU OBJECT '/tmp/test/UDX_StringPadGet.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_get(1) FROM one_dslice;
*
* Copyright (c) 2007-2010 Netezza Corporation, an IBM Company
* All rights reserved.
*/
#include "udxinc.h"
#include <string.h>
using namespace nz::udx;
struct Root
{
char* data;
int size;
};
class StringPadGet: public Udf
{
private:
public:
static Udf* instantiate();
virtual ReturnValue evaluate()
{
if(isArgNull(0))
{
throwUdxException("must not accept null arguments.");
}
if(argType(0)!=UDX_INT32)
{
throwUdxException("1st argument must be int4 (int32).");
}
CPad* pad = getPad("stringpad");
Root *ro = (Root*) pad->getRootObject(sizeof(Root));
if(!ro)
{
throwUdxException("Pad does not exist");
}
else
{
int index = int32Arg(0);
if(index<0||index>=ro->size)
{
throwUdxException("Index out of bounds");
}
StringReturn *ret = stringReturnInfo();
ret->size=1;
ret->data[0]=ro->data[index];
NZ_UDX_RETURN_STRING(ret);
}
}
};
Udf* StringPadGet::instantiate()
{
return new StringPadGet;
}