

CREATE EXTENSION plpythonu;

CREATE OR REPLACE FUNCTION get_cstring(OUT cs cstring) LANGUAGE plpythonu AS $$
return 'this_is_a_cstring'
$$;

SELECT get_cstring();

SELECT pg_typeof(get_cstring());

CREATE OR REPLACE FUNCTION cstring_to_text(IN cs cstring, OUT tx text) LANGUAGE plpythonu AS $$
return cs
$$;

SELECT cstring_to_text('a cstring argument'::cstring);

SELECT pg_typeof(cstring_to_text('a cstring argument'::cstring));

/*
 * CREATE TYPE pydata , the python equivalent of text
 */

DROP TYPE pydata CASCADE;

CREATE TYPE pydata;

CREATE OR REPLACE FUNCTION pydata_input(IN cs_pydata cstring, OUT raw_pydata pydata) LANGUAGE plpythonu AS $$
#plpy.notice('pydata_input: %s, type %s' % (repr(cs_pydata), type(cs_pydata)))
return cs_pydata
$$;

CREATE OR REPLACE FUNCTION pydata_output(IN raw_pydata pydata, OUT cs_pydata cstring) LANGUAGE plpythonu AS $$
#plpy.notice('pydata_output: %s, type %s' % (repr(raw_pydata)), type(raw_pydata)))
return raw_pydata
$$;

CREATE TYPE pydata (
    INPUT = pydata_input,
    OUTPUT = pydata_output,
    INTERNALLENGTH = VARIABLE
);

select 'pydatasample'::pydata;

/*
 * CREATE TYPE pyint4 , the python equivalent of int4
 */
 
DROP TYPE IF EXISTS pyint4 CASCADE;

CREATE TYPE pyint4;

CREATE OR REPLACE FUNCTION pyint4_input(IN cs_pydata cstring, OUT raw_pydata pyint4) LANGUAGE plpythonu AS $$
#plpy.notice('pydata_input: %s, type %s' % (repr(cs_pydata), type(cs_pydata)))
import struct
return struct.pack('i',int(cs_pydata));
$$;

CREATE OR REPLACE FUNCTION pyint4_output(IN raw_pydata pyint4, OUT cs_pydata cstring) LANGUAGE plpythonu AS $$
#plpy.notice('pydata_output: %s, type %s' % (repr(raw_pydata), type(raw_pydata)))
import struct
return struct.unpack('i',raw_pydata)[0]
$$;

CREATE TYPE pyint4 (
    INPUT = pyint4_input,
    OUTPUT = pyint4_output,
    INTERNALLENGTH = 4,
    PASSEDBYVALUE
);

SELECT '42'::pyint4;




