CAST function for user defined type

Поиск
Список
Период
Сортировка
От Ron Peterson
Тема CAST function for user defined type
Дата
Msg-id 20070122144452.GA14241@yellowbank.com
обсуждение исходный текст
Ответы Re: CAST function for user defined type  (Martijn van Oosterhout <kleptog@svana.org>)
Re: CAST function for user defined type  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
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/

В списке pgsql-general по дате отправления:

Предыдущее
От: Ron Johnson
Дата:
Сообщение: Re: More grist for the PostgreSQL vs MySQL mill
Следующее
От: "Moritz Bayer"
Дата:
Сообщение: Re: Loop in loop