1-- 2-- CREATE_CAST 3-- 4-- Create some types to test with 5CREATE TYPE casttesttype; 6CREATE FUNCTION casttesttype_in(cstring) 7 RETURNS casttesttype 8 AS 'textin' 9 LANGUAGE internal STRICT IMMUTABLE; 10NOTICE: return type casttesttype is only a shell 11CREATE FUNCTION casttesttype_out(casttesttype) 12 RETURNS cstring 13 AS 'textout' 14 LANGUAGE internal STRICT IMMUTABLE; 15NOTICE: argument type casttesttype is only a shell 16CREATE TYPE casttesttype ( 17 internallength = variable, 18 input = casttesttype_in, 19 output = casttesttype_out, 20 alignment = int4 21); 22-- a dummy function to test with 23CREATE FUNCTION casttestfunc(casttesttype) RETURNS int4 LANGUAGE SQL AS 24$$ SELECT 1; $$; 25SELECT casttestfunc('foo'::text); -- fails, as there's no cast 26ERROR: function casttestfunc(text) does not exist 27LINE 1: SELECT casttestfunc('foo'::text); 28 ^ 29HINT: No function matches the given name and argument types. You might need to add explicit type casts. 30-- Try binary coercion cast 31CREATE CAST (text AS casttesttype) WITHOUT FUNCTION; 32SELECT casttestfunc('foo'::text); -- doesn't work, as the cast is explicit 33ERROR: function casttestfunc(text) does not exist 34LINE 1: SELECT casttestfunc('foo'::text); 35 ^ 36HINT: No function matches the given name and argument types. You might need to add explicit type casts. 37SELECT casttestfunc('foo'::text::casttesttype); -- should work 38 casttestfunc 39-------------- 40 1 41(1 row) 42 43DROP CAST (text AS casttesttype); -- cleanup 44-- Try IMPLICIT binary coercion cast 45CREATE CAST (text AS casttesttype) WITHOUT FUNCTION AS IMPLICIT; 46SELECT casttestfunc('foo'::text); -- Should work now 47 casttestfunc 48-------------- 49 1 50(1 row) 51 52-- Try I/O conversion cast. 53SELECT 1234::int4::casttesttype; -- No cast yet, should fail 54ERROR: cannot cast type integer to casttesttype 55LINE 1: SELECT 1234::int4::casttesttype; 56 ^ 57CREATE CAST (int4 AS casttesttype) WITH INOUT; 58SELECT 1234::int4::casttesttype; -- Should work now 59 casttesttype 60-------------- 61 1234 62(1 row) 63 64DROP CAST (int4 AS casttesttype); 65-- Try cast with a function 66CREATE FUNCTION int4_casttesttype(int4) RETURNS casttesttype LANGUAGE SQL AS 67$$ SELECT ('foo'::text || $1::text)::casttesttype; $$; 68CREATE CAST (int4 AS casttesttype) WITH FUNCTION int4_casttesttype(int4) AS IMPLICIT; 69SELECT 1234::int4::casttesttype; -- Should work now 70 casttesttype 71-------------- 72 foo1234 73(1 row) 74 75