

CREATE DATABASE mythconverg
  WITH OWNER = mythtv
       ENCODING = 'UNICODE'
       TABLESPACE = pg_default;



CREATE TRUSTED LANGUAGE "plpgsql" HANDLER "plpgsql_call_handler";



CREATE OR REPLACE FUNCTION int4_to_bool(int4)
  RETURNS bool AS
'select case when $1 = 0 then false else true end'
  LANGUAGE 'sql' VOLATILE;
ALTER FUNCTION int4_to_bool(int4) OWNER TO mythtv;

CREATE OR REPLACE FUNCTION int2_to_bool(int2)
  RETURNS bool AS
'select case when $1 = 0 then false else true end'
  LANGUAGE 'sql' VOLATILE;
ALTER FUNCTION int2_to_bool(int2) OWNER TO mythtv;

CREATE OR REPLACE FUNCTION bool_to_int2(bool)
  RETURNS int2 AS
'select case when $1 then 1::int2 else 0::int2 end'
  LANGUAGE 'sql' VOLATILE;
ALTER FUNCTION bool_to_int2(bool) OWNER TO mythtv;



CREATE CAST (int2 AS bool)
  WITH FUNCTION int2_to_bool(int2)
  AS IMPLICIT;

CREATE CAST (bool AS int2)
  WITH FUNCTION bool_to_int2(bool)
  AS IMPLICIT;
