-- $Id: pgc_casts.sql.in 1022 2010-08-06 07:28:07Z fabien $

LOAD 'MODULE_PATHNAME';

CREATE OR REPLACE FUNCTION varbit(BYTEA, INT, BOOL)
RETURNS VARBIT
LANGUAGE C
IMMUTABLE STRICT
AS 'MODULE_PATHNAME', 'varbitfrombytea';

-- bit and varbit are binary compatible...
CREATE OR REPLACE FUNCTION tobit(BYTEA, INT, BOOL)
RETURNS BIT
LANGUAGE C
IMMUTABLE STRICT
AS 'MODULE_PATHNAME', 'varbitfrombytea';

CREATE OR REPLACE FUNCTION bytea(VARBIT, INT, BOOL)
RETURNS BYTEA
LANGUAGE C
IMMUTABLE STRICT
AS 'MODULE_PATHNAME', 'varbittobytea';

CREATE OR REPLACE FUNCTION bytea(BIT, INT, BOOL)
RETURNS BYTEA
LANGUAGE C
IMMUTABLE STRICT
AS 'MODULE_PATHNAME', 'varbittobytea';

CREATE OR REPLACE FUNCTION varbit2int2(VARBIT, INT, BOOL)
RETURNS INT2
LANGUAGE C
IMMUTABLE STRICT
AS 'MODULE_PATHNAME', 'varbittoint2';

CREATE OR REPLACE FUNCTION bit2int2(BIT, INT, BOOL)
RETURNS INT2
LANGUAGE C
IMMUTABLE STRICT
AS 'MODULE_PATHNAME', 'varbittoint2';


-- no data loss, very similar types
-- AS IMPLICIT?
DROP CAST IF EXISTS (BYTEA AS VARBIT);
CREATE CAST (BYTEA AS VARBIT)
WITH FUNCTION varbit(BYTEA, INT, BOOL);

DROP CAST IF EXISTS (BYTEA AS BIT);
CREATE CAST (BYTEA AS BIT)
WITH FUNCTION tobit(BYTEA, INT, BOOL);

-- data loss: the bit length is rounded up
-- AS ASSIGNMENT?

DROP CAST IF EXISTS (VARBIT AS BYTEA);
CREATE CAST (VARBIT AS BYTEA)
WITH FUNCTION bytea(VARBIT, INT, BOOL);

DROP CAST IF EXISTS (BIT AS BYTEA);
CREATE CAST (BIT AS BYTEA)
WITH FUNCTION bytea(BIT, INT, BOOL);

DROP CAST IF EXISTS (VARBIT AS INT2);
CREATE CAST (VARBIT AS INT2)
WITH FUNCTION varbit2int2(VARBIT, INT, BOOL);

DROP CAST IF EXISTS (BIT AS INT2);
CREATE CAST (BIT AS INT2)
WITH FUNCTION bit2int2(BIT, INT, BOOL);
