Thread: Getting char * from timestamp in a composite type

Getting char * from timestamp in a composite type

From
Michael Akinde
Date:
Hi,

I need to extract a text string representation of a timestamp from a
user-defined row type; e.g., from the composite type ('1980-01-01
12:00:00', 'Random Comment'), I'd like to extract the C string
1980-01-01 12:00:00 in my user-defined C function.

As I understand it, I should be able to do something like (assuming
"time" is the attribute name):

    bool isNull;
    HeapTupleHeader t = DatumGetHeapTupleHeader(row);
    Datum var = GetAttributeByName( row, "time", & isNull );
    // Check for null
    char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );

However, the code above (and every other variant I've tried) invariable
segmentation faults the postmaster. so obviously I am doing something
wrong. Can someone explain (or point to an example that works) of how to
do this?

Regards,

Michael A.

Attachment

Re: Getting char * from timestamp in a composite type

From
Martijn van Oosterhout
Date:
On Wed, Dec 19, 2007 at 04:40:38PM +0100, Michael Akinde wrote:
> As I understand it, I should be able to do something like (assuming
> "time" is the attribute name):
>
>    bool isNull;
>    HeapTupleHeader t = DatumGetHeapTupleHeader(row);
>    Datum var = GetAttributeByName( row, "time", & isNull );
>    // Check for null
>    char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );

That's not going to work. textout wants a text datum. Try calling
timestamp_out instead...

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Those who make peaceful revolution impossible will make violent revolution inevitable.
>  -- John F Kennedy

Attachment

Re: Getting char * from timestamp in a composite type

From
Michael Akinde
Date:
Martijn van Oosterhout wrote:
On Wed, Dec 19, 2007 at 04:40:38PM +0100, Michael Akinde wrote: 
As I understand it, I should be able to do something like (assuming 
"time" is the attribute name):
  bool isNull;  HeapTupleHeader t = DatumGetHeapTupleHeader(row);  Datum var = GetAttributeByName( row, "time", & isNull );  // Check for null  char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );   
That's not going to work. textout wants a text datum. Try calling
timestamp_out instead..
Sigh... it's always the idiot bugs that are the hardest to see. Thanks a lot.

Regards,

Michael A.
Attachment

Re: Getting char * from timestamp in a composite type

From
Tom Lane
Date:
Michael Akinde <michael.akinde@met.no> writes:
>>> bool isNull;
>>> HeapTupleHeader t = DatumGetHeapTupleHeader(row);
>>> Datum var = GetAttributeByName( row, "time", & isNull );
>>> // Check for null
>>> char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );
>>
>> That's not going to work. textout wants a text datum. Try calling
>> timestamp_out instead..
> Sigh... it's always the idiot bugs that are the hardest to see. Thanks a
> lot.

The other problem is that you're passing row not t to
GetAttributeByName.  If you don't have your compiler configured to bleat
about type mismatches like that, your days of C programming will be
bleak and painful.

            regards, tom lane

Re: Getting char * from timestamp in a composite type

From
Michael Akinde
Date:
Thanks, though that was an error caused by my attempt to copy-paste some readable example of code from the source file.

Matching the correct _out functions in the DirectFunctionCall fixed the segmentation fault problems I was seeing.

Regards,

Michael A.

Tom Lane wrote:
Michael Akinde <michael.akinde@met.no> writes: 
bool isNull;
HeapTupleHeader t = DatumGetHeapTupleHeader(row);
Datum var = GetAttributeByName( row, "time", & isNull );
// Check for null
char * ret = DatumGetCString( DirectFunctionCall1(textout, var ) );       
That's not going to work. textout wants a text datum. Try calling
timestamp_out instead..     
Sigh... it's always the idiot bugs that are the hardest to see. Thanks a 
lot.   
The other problem is that you're passing row not t to
GetAttributeByName.  If you don't have your compiler configured to bleat
about type mismatches like that, your days of C programming will be
bleak and painful.
		regards, tom lane 

Attachment