4 -- Create some types to test with
5 CREATE TYPE casttesttype;
6 CREATE FUNCTION casttesttype_in(cstring)
9 LANGUAGE internal STRICT;
10 NOTICE: return type casttesttype is only a shell
11 CREATE FUNCTION casttesttype_out(casttesttype)
14 LANGUAGE internal STRICT;
15 NOTICE: argument type casttesttype is only a shell
16 CREATE TYPE casttesttype (
17 internallength = variable,
18 input = casttesttype_in,
19 output = casttesttype_out,
22 -- a dummy function to test with
23 CREATE FUNCTION casttestfunc(casttesttype) RETURNS int4 LANGUAGE SQL AS
25 SELECT casttestfunc('foo'::text); -- fails, as there's no cast
26 ERROR: function casttestfunc(text) does not exist
27 LINE 1: SELECT casttestfunc('foo'::text);
29 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
30 -- Try binary coercion cast
31 CREATE CAST (text AS casttesttype) WITHOUT FUNCTION;
32 SELECT casttestfunc('foo'::text); -- doesn't work, as the cast is explicit
33 ERROR: function casttestfunc(text) does not exist
34 LINE 1: SELECT casttestfunc('foo'::text);
36 HINT: No function matches the given name and argument types. You might need to add explicit type casts.
37 SELECT casttestfunc('foo'::text::casttesttype); -- should work
43 DROP CAST (text AS casttesttype); -- cleanup
44 -- Try IMPLICIT binary coercion cast
45 CREATE CAST (text AS casttesttype) WITHOUT FUNCTION AS IMPLICIT;
46 SELECT casttestfunc('foo'::text); -- Should work now
52 -- Try I/O conversion cast.
53 SELECT 1234::int4::casttesttype; -- No cast yet, should fail
54 ERROR: cannot cast type integer to casttesttype
55 LINE 1: SELECT 1234::int4::casttesttype;
57 CREATE CAST (int4 AS casttesttype) WITH INOUT;
58 SELECT 1234::int4::casttesttype; -- Should work now
64 DROP CAST (int4 AS casttesttype);
65 -- Try cast with a function
66 CREATE FUNCTION int4_casttesttype(int4) RETURNS casttesttype LANGUAGE SQL AS
67 $$ SELECT ('foo'::text || $1::text)::casttesttype; $$;
68 CREATE CAST (int4 AS casttesttype) WITH FUNCTION int4_casttesttype(int4) AS IMPLICIT;
69 SELECT 1234::int4::casttesttype; -- Should work now