CAST function for user defined type

From: Ron Peterson <ron(dot)peterson(at)yellowbank(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: CAST function for user defined type
Date: 2007-01-22 14:44:52
Message-ID: 20070122144452.GA14241@yellowbank.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Moritz Bayer 2007-01-22 14:58:32 Re: Loop in loop
Previous Message Moritz Bayer 2007-01-22 14:43:42 Loop in loop