padcounter.cpp

The padcounter.cpp sample program contains two UDXs (padcounter and getpadcount) that use a CPad to obtain a row count of a table.

#include "udxinc.h"

/*
* These functions obtain a row count of a table.
*
* This will return the row count in table <TBL>:
*  select sum(getpadcount()) from one_per where exists 
*    (select count(padcounter()) from <TBL>);
*/

using namespace nz::udx_ver2;

struct Root
{
  int64 myCount;
};


class PadCounter: public Udf
{
private:

public:
    PadCounter(UdxInit *pInit) : Udf(pInit) { }
    static Udf* instantiate(UdxInit *pInit);
    virtual ReturnValue evaluate()
    {
        CPad* pad = getPad("PadCount");
        Root *ro = (Root*) pad->getRootObject(sizeof(Root));
        setReturnNull(false);
        if(!ro)
        {
            ro=PAD_NEW(pad,Root);
            ro->myCount = 1;
            pad->setRootObject(ro, sizeof(Root));
            NZ_UDX_RETURN_INT32(1);
        }
        else
        {
            if (isUserQuery())
                ro->myCount += 1;
            NZ_UDX_RETURN_INT32(1);
        }
     }
};

Udf* PadCounter::instantiate(UdxInit *pInit)
{
    return new PadCounter(pInit);
}


class GetPadCount: public Udf
{
private:

public:
    GetPadCount(UdxInit *pInit) : Udf(pInit) { }
    static Udf* instantiate(UdxInit *pInit);
    virtual ReturnValue evaluate()
    {
        CPad* pad = getPad("PadCount");
        Root *ro = (Root*) pad->getRootObject(sizeof(Root));
        setReturnNull(false);
        if(!ro)
        {
            NZ_UDX_RETURN_INT64(0);
        }
        else
        {
            NZ_UDX_RETURN_INT64(ro->myCount);
        }
     }
};

Udf* GetPadCount::instantiate(UdxInit *pInit)
{
    return new GetPadCount(pInit);
}