1-- 2-- CREATE_CAST 3-- 4 5-- Create some types to test with 6CREATE TYPE casttesttype; 7 8CREATE FUNCTION casttesttype_in(cstring) 9 RETURNS casttesttype 10 AS 'textin' 11 LANGUAGE internal STRICT IMMUTABLE; 12CREATE FUNCTION casttesttype_out(casttesttype) 13 RETURNS cstring 14 AS 'textout' 15 LANGUAGE internal STRICT IMMUTABLE; 16 17CREATE TYPE casttesttype ( 18 internallength = variable, 19 input = casttesttype_in, 20 output = casttesttype_out, 21 alignment = int4 22); 23 24-- a dummy function to test with 25CREATE FUNCTION casttestfunc(casttesttype) RETURNS int4 LANGUAGE SQL AS 26$$ SELECT 1; $$; 27 28SELECT casttestfunc('foo'::text); -- fails, as there's no cast 29 30-- Try binary coercion cast 31CREATE CAST (text AS casttesttype) WITHOUT FUNCTION; 32SELECT casttestfunc('foo'::text); -- doesn't work, as the cast is explicit 33SELECT casttestfunc('foo'::text::casttesttype); -- should work 34DROP CAST (text AS casttesttype); -- cleanup 35 36-- Try IMPLICIT binary coercion cast 37CREATE CAST (text AS casttesttype) WITHOUT FUNCTION AS IMPLICIT; 38SELECT casttestfunc('foo'::text); -- Should work now 39 40-- Try I/O conversion cast. 41SELECT 1234::int4::casttesttype; -- No cast yet, should fail 42 43CREATE CAST (int4 AS casttesttype) WITH INOUT; 44SELECT 1234::int4::casttesttype; -- Should work now 45 46DROP CAST (int4 AS casttesttype); 47 48-- Try cast with a function 49 50CREATE FUNCTION int4_casttesttype(int4) RETURNS casttesttype LANGUAGE SQL AS 51$$ SELECT ('foo'::text || $1::text)::casttesttype; $$; 52 53CREATE CAST (int4 AS casttesttype) WITH FUNCTION int4_casttesttype(int4) AS IMPLICIT; 54SELECT 1234::int4::casttesttype; -- Should work now 55