Search Postgresql Archives

Re: Understanding Datum

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2011/3/24 Nick Raj <nickrajjain@xxxxxxxxx>:
> If Datum contains only the value (not having type specific info.), then
> Suppose i want to print the Datum V value (already defined in postgres)
> then printf("%??", V);
>
> Because V is assigned by PG_GETARG_POINTER(1);
> I don't having the information of type Datum.
>
> How to print the value of Datum in postgres?

you have to find a adequate out function and you have to call it. A
out functions transform a binary Datum value to CString value.

#include "executor/spi.h"               /* this is what you need to
work with SPI */
#include "utils/lsyscache.h"

PG_MODULE_MAGIC;

extern Datum quote_literal(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(quote_literal);

Datum
quote_literal(PG_FUNCTION_ARGS)
{
        text *result;
        Oid         valtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
        Datum       value = PG_GETARG_DATUM(0);
        Oid                     typoutput;
        bool            typIsVarlena;
        StringInfoData buf;

        if (!OidIsValid(valtype))
                elog(ERROR, "could not determine data type of input");

        initStringInfo(&buf);

        appendStringInfoChar(&buf, '\'');

        getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
        appendStringInfoString(&buf, OidOutputFunctionCall(typoutput, value));

        appendStringInfoChar(&buf, '\'');

        result = DatumGetTextP(DirectFunctionCall1(textin,
CStringGetDatum(buf.data)));

        PG_RETURN_TEXT_P(result);
}

CREATE OR REPLACE FUNCTION quote_literal(anyelement)
RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION quote_literal(text)
RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;

Regards

Pavel Stehule

p.s. this is simple variant of this function. For production usage is
using cache necessary.

>
>
> On Thu, Mar 24, 2011 at 2:35 AM, Tom Lane <tgl@xxxxxxxxxxxxx> wrote:
>>
>> Nick Raj <nickrajjain@xxxxxxxxx> writes:
>> > In postgres, typedef uintptr_t Datum
>> > Datum is getting value from PG_GETARG_POINTER(1);
>> > But, now problem is how would i know the type of PG_GETARG_POINTER(1)
>> > (postgres internally pass this argument) to figure out datum type?
>>
>> Datum does not carry any type information, only a value. ÂFunctions are
>> typically coded to know their input types a priori. ÂIf you want to
>> write code that is not type-specific then you'd better be passing around
>> type OIDs as well as values.
>>
>> Â Â Â Â Â Â Â Â Â Â Â Âregards, tom lane
>>
>> --
>> Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-general
>
>

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux