--
-- adds new operators for inet: 
-- &<, &<=, &=, &>=, &>, &==, &>>, &<<
-- (left of, left of or overlaps, overlaps, right of or overlaps, right of, same, contains, contained by)
--
-- It's worth noting that rtree is currently broken, so the operators &<, &<=, &>=, and &> does not work,
-- when used with an index. Consequently, they are only usable for sort operations at this stage.
--

SET search_path = pg_catalog;

DROP FUNCTION network_left(inet,inet)		cascade;
DROP FUNCTION network_overleft(inet,inet)	cascade;
DROP FUNCTION network_overlap(inet,inet)	cascade;
DROP FUNCTION network_overright(inet,inet)	cascade;
DROP FUNCTION network_right(inet,inet)		cascade;
DROP FUNCTION network_same(inet,inet)		cascade;
DROP FUNCTION network_contain(inet,inet)	cascade;
DROP FUNCTION network_contained(inet,inet)	cascade;

DROP FUNCTION rt_network_union(inet,inet)	cascade;
DROP FUNCTION rt_network_inter(inet,inet)	cascade;
DROP FUNCTION rt_network_size(inet,internal)	cascade;

DROP OPERATOR CLASS cidr_ops USING rtree	cascade;
DROP OPERATOR CLASS inet_ops USING rtree	cascade;

DROP OPERATOR CLASS cidr_inet_ops USING rtree	cascade;
DROP OPERATOR CLASS inet_cidr_ops USING rtree	cascade;

CREATE OR REPLACE FUNCTION network_left(inet,inet) RETURNS boolean AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION network_overleft(inet,inet) RETURNS boolean AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION network_overlap(inet,inet) RETURNS boolean AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION network_overright(inet,inet) RETURNS boolean AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION network_right(inet,inet) RETURNS boolean AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION network_same(inet,inet) RETURNS boolean AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION network_contain(inet,inet) RETURNS boolean AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION network_contained(inet,inet) RETURNS boolean AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION rt_network_union(inet,inet) RETURNS inet AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION rt_network_inter(inet,inet) RETURNS void AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION rt_network_size(inet,internal) RETURNS void AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION network_union(inet,inet) RETURNS inet AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION network_inter(inet,inet) RETURNS inet AS '$libdir/rtree_inet.so'
LANGUAGE 'C' IMMUTABLE STRICT;

CREATE OPERATOR &< (
    PROCEDURE = network_left,
    LEFTARG = inet,
    RIGHTARG = inet,
    RESTRICT = positionsel,
    JOIN = positionjoinsel
);

CREATE OPERATOR &<= (
    PROCEDURE = network_overleft,
    LEFTARG = inet,
    RIGHTARG = inet,
    RESTRICT = positionsel,
    JOIN = positionjoinsel
);

CREATE OPERATOR &= (
    PROCEDURE = network_overlap,
    LEFTARG = inet,
    RIGHTARG = inet,
    COMMUTATOR = &=,
    RESTRICT = eqsel,
    JOIN = eqjoinsel
);

CREATE OPERATOR &>= (
    PROCEDURE = network_overright,
    LEFTARG = inet,
    RIGHTARG = inet,
    RESTRICT = positionsel,
    JOIN = positionjoinsel
);

CREATE OPERATOR &> (
    PROCEDURE = network_right,
    LEFTARG = inet,
    RIGHTARG = inet,
    RESTRICT = positionsel,
    JOIN = positionjoinsel
);

CREATE OPERATOR &== (
    PROCEDURE = network_same,
    LEFTARG = inet,
    RIGHTARG = inet,
    COMMUTATOR = &==,
    RESTRICT = eqsel,
    JOIN = eqjoinsel
);

CREATE OPERATOR &>> (
    PROCEDURE = network_contain,
    LEFTARG = inet,
    RIGHTARG = inet,
    COMMUTATOR = &<<,
    RESTRICT = contsel,
    JOIN = contjoinsel
);

CREATE OPERATOR &<< (
    PROCEDURE = network_contained,
    LEFTARG = inet,
    RIGHTARG = inet,
    COMMUTATOR = &>>,
    RESTRICT = contsel,
    JOIN = contjoinsel
);

CREATE OPERATOR &* (
    PROCEDURE = network_union,
    LEFTARG = inet,
    RIGHTARG = inet,
    COMMUTATOR = &%,
    RESTRICT = areasel,
    JOIN = areajoinsel
);

CREATE OPERATOR &# (
    PROCEDURE = network_inter,
    LEFTARG = inet,
    RIGHTARG = inet,
    COMMUTATOR = &#,
    RESTRICT = areasel,
    JOIN = areajoinsel
);

CREATE OPERATOR CLASS cidr_ops
    DEFAULT FOR TYPE cidr USING rtree AS
    OPERATOR 1 &<(inet,inet) ,
    OPERATOR 2 &<=(inet,inet) ,
    OPERATOR 3 &=(inet,inet) ,
    OPERATOR 4 &>=(inet,inet) ,
    OPERATOR 5 &>(inet,inet) ,
    OPERATOR 6 &==(inet,inet) ,
    OPERATOR 7 &>>(inet,inet) ,
    OPERATOR 8 &<<(inet,inet) ,
    FUNCTION 1 rt_network_union(inet,inet) ,
    FUNCTION 2 rt_network_inter(inet,inet) ,
    FUNCTION 3 rt_network_size(inet,internal);

CREATE OPERATOR CLASS inet_ops
    DEFAULT FOR TYPE inet USING rtree AS
    OPERATOR 1 &<(inet,inet) ,
    OPERATOR 2 &<=(inet,inet) ,
    OPERATOR 3 &=(inet,inet) ,
    OPERATOR 4 &>=(inet,inet) ,
    OPERATOR 5 &>(inet,inet) ,
    OPERATOR 6 &==(inet,inet) ,
    OPERATOR 7 &>>(inet,inet) ,
    OPERATOR 8 &<<(inet,inet) ,
    FUNCTION 1 rt_network_union(inet,inet) ,
    FUNCTION 2 rt_network_inter(inet,inet) ,
    FUNCTION 3 rt_network_size(inet,internal);

SET search_path = default;

