Search Postgresql Archives

CAST function for user defined type

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

 



I've created my own type: y_octet_16.  Now I'm trying to create a CAST
function for this type, but I'm not quite getting it.

The input function for my type takes a 32 char hex string as input.

CREATE TABLE bt (
  name
    TEXT
    NOT NULL,
  val
    y_octet_16
    NOT NULL
);

CREATE INDEX
  bt_val_ndx
ON
  bt( val );

-- this works
INSERT INTO
  bt( name, val )
VALUES
  ( 'aaa', 'abcdef1234567890abcdef1234567890' );

-- this doesn't work, with or without the cast
INSERT INTO
  bt( name, val )
VALUES
  ( 'aaa', encode( y_uuid_generate(), 'hex' )::y_byte_16 );

% INSERT INTO bt( name, val ) VALUES
  ( 'aaa', encode( y_uuid_generate(), 'hex' )::y_byte_16 );
ERROR:  type "y_byte_16" does not exist
LINE 4:   ( 'aaa', encode( y_uuid_generate(), 'hex' )::y_byte_16 )



I think my question is: where do I define y_byte_16 as a type that is
recognized by my CAST function?  What are the requirements on this
definition?  I have:



CREATE CAST
  (text AS y_octet_16)
WITH FUNCTION
  text_cast_to_y_octet_16( text );

PG_FUNCTION_INFO_V1(text_cast_to_y_octet_16);
Datum
text_cast_to_y_octet_16(PG_FUNCTION_ARGS)
{
   text *txtstr;
   char *octstr;

   if( PG_ARGISNULL(0) ) {
      PG_RETURN_NULL();
   }
   txtstr = PG_GETARG_TEXT_P(0);

   octstr = hex2bin_palloc( VARDATA(txtstr), 16 );

   PG_RETURN_POINTER( octstr );
}


TIA

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


[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