-- Create the user-defined type for the 1-D integer arrays (_int4)
-- 
BEGIN TRANSACTION;

-- Adjust this setting to control where the operators, functions, and
-- opclasses get created.
SET search_path = public;

-- Query type
CREATE FUNCTION bqarr_in(opaque)
RETURNS opaque
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE FUNCTION bqarr_out(opaque)
RETURNS opaque
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE TYPE query_int (
internallength = -1,
input = bqarr_in,
output = bqarr_out
);

--only for debug
CREATE FUNCTION querytree(query_int)
RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);


CREATE FUNCTION boolop(_int4, query_int) RETURNS bool
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION boolop(_int4, query_int) IS 'boolean operation with array';

CREATE FUNCTION rboolop(query_int, _int4) RETURNS bool
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION rboolop(query_int, _int4) IS 'boolean operation with array';

CREATE OPERATOR @@ (
   LEFTARG = _int4, RIGHTARG = query_int, PROCEDURE = boolop,
   COMMUTATOR = '~~', RESTRICT = contsel, JOIN = contjoinsel
);

CREATE OPERATOR ~~ (
   LEFTARG = query_int, RIGHTARG = _int4, PROCEDURE = rboolop,
   COMMUTATOR = '@@', RESTRICT = contsel, JOIN = contjoinsel
);


--
-- External C-functions for R-tree methods
--

-- Comparison methods

CREATE FUNCTION _int_contains(_int4, _int4) RETURNS bool
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION _int_contains(_int4, _int4) IS 'contains';

CREATE FUNCTION _int_contained(_int4, _int4) RETURNS bool
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION _int_contained(_int4, _int4) IS 'contained in';

CREATE FUNCTION _int_overlap(_int4, _int4) RETURNS bool
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION _int_overlap(_int4, _int4) IS 'overlaps';

CREATE FUNCTION _int_same(_int4, _int4) RETURNS bool
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION _int_same(_int4, _int4) IS 'same as';

CREATE FUNCTION _int_different(_int4, _int4) RETURNS bool
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION _int_different(_int4, _int4) IS 'different';

-- support routines for indexing

CREATE FUNCTION _int_union(_int4, _int4) RETURNS _int4
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

CREATE FUNCTION _int_inter(_int4, _int4) RETURNS _int4
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

--
-- OPERATORS
--

CREATE OPERATOR && (
   LEFTARG = _int4, RIGHTARG = _int4, PROCEDURE = _int_overlap,
   COMMUTATOR = '&&',
   RESTRICT = contsel, JOIN = contjoinsel
);

--CREATE OPERATOR = (
--   LEFTARG = _int4, RIGHTARG = _int4, PROCEDURE = _int_same,
--   COMMUTATOR = '=', NEGATOR = '<>',
--   RESTRICT = eqsel, JOIN = eqjoinsel,
--   SORT1 = '<', SORT2 = '<'
--);

CREATE OPERATOR <> (
   LEFTARG = _int4, RIGHTARG = _int4, PROCEDURE = _int_different,
   COMMUTATOR = '<>', NEGATOR = '=',
   RESTRICT = neqsel, JOIN = neqjoinsel
);

CREATE OPERATOR @ (
   LEFTARG = _int4, RIGHTARG = _int4, PROCEDURE = _int_contains,
   COMMUTATOR = '~', RESTRICT = contsel, JOIN = contjoinsel
);

CREATE OPERATOR ~ (
   LEFTARG = _int4, RIGHTARG = _int4, PROCEDURE = _int_contained,
   COMMUTATOR = '@', RESTRICT = contsel, JOIN = contjoinsel
);


-- define the GiST support methods
CREATE FUNCTION g_int_consistent(opaque,_int4,int4) RETURNS bool
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_int_compress(opaque) RETURNS opaque 
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_int_decompress(opaque) RETURNS opaque 
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_int_penalty(opaque,opaque,opaque) RETURNS opaque
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

CREATE FUNCTION g_int_picksplit(opaque, opaque) RETURNS opaque
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_int_union(bytea, opaque) RETURNS _int4 
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_int_same(_int4, _int4, opaque) RETURNS opaque 
	AS 'MODULE_PATHNAME' LANGUAGE 'c';


-- Create the operator class for indexing

CREATE OPERATOR CLASS gist__int_ops
    DEFAULT FOR TYPE _int4 USING gist AS
	OPERATOR	3	&&,
	OPERATOR	6	=	RECHECK,
	OPERATOR	7	@,
	OPERATOR	8	~,
	OPERATOR	20	@@ (_int4, query_int),
	FUNCTION	1	g_int_consistent (opaque, _int4, int4),
	FUNCTION	2	g_int_union (bytea, opaque),
	FUNCTION	3	g_int_compress (opaque),
	FUNCTION	4	g_int_decompress (opaque),
	FUNCTION	5	g_int_penalty (opaque, opaque, opaque),
	FUNCTION	6	g_int_picksplit (opaque, opaque),
	FUNCTION	7	g_int_same (_int4, _int4, opaque);


---------------------------------------------
-- intbig
---------------------------------------------
-- define the GiST support methods
CREATE FUNCTION g_intbig_consistent(opaque,_int4,int4) RETURNS bool
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_intbig_compress(opaque) RETURNS opaque 
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_intbig_decompress(opaque) RETURNS opaque 
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_intbig_penalty(opaque,opaque,opaque) RETURNS opaque
	AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

CREATE FUNCTION g_intbig_picksplit(opaque, opaque) RETURNS opaque
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_intbig_union(bytea, opaque) RETURNS _int4 
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION g_intbig_same(_int4, _int4, opaque) RETURNS opaque 
	AS 'MODULE_PATHNAME' LANGUAGE 'c';

-- register the opclass for indexing (not as default)

CREATE OPERATOR CLASS gist__intbig_ops
    FOR TYPE _int4 USING gist AS
	OPERATOR	3	&&	RECHECK,
	OPERATOR	6	=	RECHECK,
	OPERATOR	7	@	RECHECK,
	OPERATOR	8	~	RECHECK,
	OPERATOR	20	@@ (_int4, query_int)	RECHECK,
	FUNCTION	1	g_intbig_consistent (opaque, _int4, int4),
	FUNCTION	2	g_intbig_union (bytea, opaque),
	FUNCTION	3	g_intbig_compress (opaque),
	FUNCTION	4	g_intbig_decompress (opaque),
	FUNCTION	5	g_intbig_penalty (opaque, opaque, opaque),
	FUNCTION	6	g_intbig_picksplit (opaque, opaque),
	FUNCTION	7	g_intbig_same (_int4, _int4, opaque);

END TRANSACTION;
