Re: convert binary string to datum - Mailing list pgsql-general

From Ron Peterson
Subject Re: convert binary string to datum
Date
Msg-id 20071013235318.GF18834@yellowbank.com
Whole thread Raw
In response to Re: convert binary string to datum  (Gregory Stark <stark@enterprisedb.com>)
List pgsql-general
2007-10-13_15:22:34-0400 Gregory Stark <stark@enterprisedb.com>:
> "Ron Peterson" <ron.peterson@yellowbank.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/

pgsql-general by date:

Previous
From: "Martin Gainty"
Date:
Subject: Re: can I define own variables?
Next
From: syan tan
Date:
Subject: atomic commit;begin for long running transactions , in combination with savepoint.