string_pad_size.cpp

The string_pad_size.cpp program defines a UDSF that returns the size of the stringpad created by the string_pad_create function.

/*
 * UDF string_pad_size() returns int
 * 
 * returns the size of the pad. -1 if no pad is found.
 *
 * USAGE:
 *
 * CREATE TABLE table1(c1 int);
 * INSERT INTO table1 VALUES(1);
 * SELECT string_pad_create(10, 'dashDB') FROM table1;
 * SELECT string_pad_size() FROM table1;  
 * 
 */

#include "udxinc.h"
#include <string.h>

using namespace nz::udx_ver2;

struct Root
{
  char* data;
  int size;
};


class StringPadSize: public Udf
{
private:

public:
    StringPadSize(UdxInit *pInit) : Udf(pInit) { }
    static Udf* instantiate(UdxInit *pInit);
    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(UdxInit *pInit)
{
    return new StringPadSize(pInit);
}