|
| 1 | +-- |
| 2 | +-- CREATE_CAST |
| 3 | +-- |
| 4 | +-- Create some types to test with |
| 5 | +CREATE TYPE casttesttype; |
| 6 | +CREATE FUNCTION casttesttype_in(cstring) |
| 7 | + RETURNS casttesttype |
| 8 | + AS 'textin' |
| 9 | + LANGUAGE internal STRICT; |
| 10 | +NOTICE: return type casttesttype is only a shell |
| 11 | +CREATE FUNCTION casttesttype_out(casttesttype) |
| 12 | + RETURNS cstring |
| 13 | + AS 'textout' |
| 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, |
| 20 | + alignment = int4 |
| 21 | +); |
| 22 | +-- a dummy function to test with |
| 23 | +CREATE FUNCTION casttestfunc(casttesttype) RETURNS int4 LANGUAGE SQL AS |
| 24 | +$$ SELECT 1; $$; |
| 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); |
| 28 | + ^ |
| 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); |
| 35 | + ^ |
| 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 |
| 38 | + casttestfunc |
| 39 | +-------------- |
| 40 | + 1 |
| 41 | +(1 row) |
| 42 | + |
| 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 |
| 47 | + casttestfunc |
| 48 | +-------------- |
| 49 | + 1 |
| 50 | +(1 row) |
| 51 | + |
| 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; |
| 56 | + ^ |
| 57 | +CREATE CAST (int4 AS casttesttype) WITH INOUT; |
| 58 | +SELECT 1234::int4::casttesttype; -- Should work now |
| 59 | + casttesttype |
| 60 | +-------------- |
| 61 | + 1234 |
| 62 | +(1 row) |
| 63 | + |
| 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 |
| 70 | + casttesttype |
| 71 | +-------------- |
| 72 | + foo1234 |
| 73 | +(1 row) |
| 74 | + |
0 commit comments