CREATE CAST question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

CREATE CAST question

Robert Wimmer

I have the following problem

I want to define a schema qualified domain and create a cast to cast a
string in the new domain.
This is what i did.

***

CREATE DOMAIN tree.ident AS TEXT;

CREATE OR REPLACE FUNCTION tree.ident_cast(TEXT) RETURNS tree.ident AS '
DECLARE regex TEXT; ret TEXT; l1 INT; l2 INT;
BEGIN
  regex := ''^[a-zA-Z0-9][^/%_]*'';
  ret := SUBSTRING($1 FROM regex);
  l1 := LENGTH($1); l2 := LENGTH(ret);
  IF l1 <> l2 THEN RAISE EXCEPTION  ''syntax error in "%" at % (%)
'',$1,l2,ret; END IF;
  RETURN $1;
END; '
LANGUAGE plpgsql;

CREATE CAST (TEXT AS tree.ident) WITH FUNCTION tree.ident_cast(TEXT) AS
IMPLICIT;

***
the cast function works as expected ...

tree_cal=# SELECT tree.ident_cast('path/node');
ERROR:  syntax error in "path/node" at 9 (path)

but if i do the following no error will occur

tree_cal=# SELECT CAST('path/node' AS tree.ident);
path/node

this looks like as if the cast function never whas used ...

what do i have to do that the cast function is called during the cast ?

regards

i am using postgre 7.4 on debian

_________________________________________________________________
Behalten Sie Ihre Hotmails, den Messenger und die Suchfunktionen stets im
Auge! http://toolbar.msn.at?DI=43&XAPID=2170


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
Reply | Threaded
Open this post in threaded view
|

Re: CREATE CAST question

Tom Lane-2
"Robert Wimmer" <[hidden email]> writes:
> I want to define a schema qualified domain and create a cast to cast a
> string in the new domain.

Casts between domains and their underlying types are hard-wired into the
system: you don't get to muck with the semantics by doing CREATE CAST.

The correct way to do this is to define a constraint on the domain,
along the lines of

        create function is_ok_ident(text) returns bool as '
                ... return true if you like the string ...
        ' language plpgsql;

        create domain ident as text check (is_ok_ident(value));

                        regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match