KIM
KIM

Reputation: 1254

functions and operator for user define type

I have built User Define Type with function_in and function_out.

CREATE OR REPLACE FUNCTION MyOwnType_in(cstring)
   RETURNS MyOwnType
   AS '/home/postgres/ENCRIPTION/MyOwnType/MyOwnType.so','MyOwnType_in'
   LANGUAGE C IMMUTABLE STRICT;


CREATE OR REPLACE FUNCTION MyOwnType_out(MyOwnType)
   RETURNS cstring
   AS '/home/postgres/ENCRIPTION/MyOwnType/MyOwnType.so','MyOwnType_out'
   LANGUAGE C IMMUTABLE STRICT;

As you can see each function_in receives cstring and function_out returns cstring.

The thing is function overloading and operator overriding on which I don't want to waste my time.

Is there any way to declare DEFAULT TYPE for my MyOwnType type which tells postgres that functions and operators with MyOwnTypearguments can parse and rewrite it into text?

Upvotes: 0

Views: 75

Answers (2)

pozs
pozs

Reputation: 36234

The most you can do with your type is to mark it as a string type, with:

CREATE TYPE MyOwnType(
  INPUT = MyOwnType_in,
  OUTPUT = MyOwnType_out,
  CATEGORY = 'S'
  -- ...
);

The category parameter is especially useful when adding a user-defined type to an existing built-in category, such as the numeric or string types. However, it is also possible to create new entirely-user-defined type categories. Select any ASCII character other than an upper-case letter to name such a category ...

It may require additional CASTs, as @IgorRomanchenko suggested.

Upvotes: 1

Ihor Romanchenko
Ihor Romanchenko

Reputation: 28531

You need to CREATE CAST ... AS IMPLICIT from your datatype to text. This way your type will be automatically cast to text when a function/operator requires text.

Details here: http://www.postgresql.org/docs/current/static/sql-createcast.html

Upvotes: 1

Related Questions