Re: convert binary string to datum

From: Ron Peterson <ron(dot)peterson(at)yellowbank(dot)com>
To: Gregory Stark <stark(at)enterprisedb(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: convert binary string to datum
Date: 2007-10-13 23:53:18
Message-ID: 20071013235318.GF18834@yellowbank.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2007-10-13_15:22:34-0400 Gregory Stark <stark(at)enterprisedb(dot)com>:
> "Ron Peterson" <ron(dot)peterson(at)yellowbank(dot)com> writes:
>
> > My first thought was to just do something like:
> >
> > CREATE TYPE __full_key AS ( n bytea, e bytea, d bytea );
> >
> > CREATE OR REPLACE FUNCTION
> > generate_rsa_key( )
> > RETURNS
> > __full_key
>
> Oh, incidentally you probably don't want to name your type starting with an _.
> Postgres names array types starting with _ so that's likely to confuse
> something and if not something then at least someone.

Thanks.

I got it working, but returning a composite type of text values, rather
than bytea. I think that's better for me anyway, because I'd like my
type's input and output functions to take hex values instead of the
crazy bytea octet representation. I ended up doing

CREATE TYPE full_key AS ( n TEXT, e TEXT, d TEXT );

-

vals = (char**)palloc( sizeof(char*) * 3 );

// convert key parts to strings
len = mpz_sizeinbase( Y_FULL_KEY_MOD(&akey), 16 ) + 1;
vals[0] = (char *)palloc( len );
gmp_snprintf( vals[0], len, "%Zx", Y_FULL_KEY_MOD(&akey) );

...etc

if( get_call_result_type( fcinfo, NULL, &td ) != TYPEFUNC_COMPOSITE ) {
ereport( ERROR,
( errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg( "function returning record called in context "
"that cannot accept type record" )));
PG_RETURN_NULL();
}

// Make a persistant copy.
td = CreateTupleDescCopy( td );

aim = TupleDescGetAttInMetadata( td );
ht = BuildTupleFromCStrings( aim, vals );

/* make the tuple into a datum */
result = HeapTupleGetDatum( ht );

Someday I'd still like to figure out how to return a composite type
containing bytea values...

--
Ron Peterson
https://www.yellowbank.com/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message syan tan 2007-10-14 04:49:21 atomic commit;begin for long running transactions , in combination with savepoint.
Previous Message Gregory Stark 2007-10-13 19:22:34 Re: convert binary string to datum