/* vatin--0.0.sql */

-- complain if script is sources in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION vat" to load this file. \quit

-- Example:
--  create table test ( id vatin );
--  insert into tast values('ES40404040D');
--
-- select vatin('ES40404040-D');
--

--
-- Input and output functions and data types:
--
---------------------------------------------
CREATE FUNCTION vatin_in(cstring)
	RETURNS vatin
	AS 'MODULE_PATHNAME'
	LANGUAGE C
	IMMUTABLE STRICT
	PARALLEL SAFE
;
CREATE FUNCTION vatin_out(vatin)
	RETURNS cstring
	AS 'textout'
	LANGUAGE 'internal'
	IMMUTABLE STRICT
	PARALLEL SAFE
;
CREATE TYPE vatin (
	INPUT = vatin_in,
	OUTPUT = vatin_out,
	LIKE = pg_catalog.text
);
COMMENT ON TYPE vatin
	IS 'Value added tax identification number (VATIN)'
;

--
-- Operator functions:
--
---------------------------------------------
CREATE FUNCTION vatinlt(vatin, vatin)
	RETURNS boolean
	AS 'text_lt'
	LANGUAGE 'internal'
	IMMUTABLE STRICT
	PARALLEL SAFE
;
CREATE FUNCTION vatinle(vatin, vatin)
	RETURNS boolean
	AS 'text_le'
	LANGUAGE 'internal'
	IMMUTABLE STRICT
	PARALLEL SAFE
;
CREATE FUNCTION vatineq(vatin, vatin)
	RETURNS boolean
	AS 'texteq'
	LANGUAGE 'internal'
	IMMUTABLE STRICT
	PARALLEL SAFE;
CREATE FUNCTION vatinge(vatin, vatin)
	RETURNS boolean
	AS 'text_ge'
	LANGUAGE 'internal'
	IMMUTABLE STRICT
	PARALLEL SAFE
;
CREATE FUNCTION vatingt(vatin, vatin)
	RETURNS boolean
	AS 'text_gt'
	LANGUAGE 'internal'
	IMMUTABLE STRICT
	PARALLEL SAFE
;
CREATE FUNCTION vatinne(vatin, vatin)
	RETURNS boolean
	AS 'textne'
	LANGUAGE 'internal'
	IMMUTABLE STRICT
	PARALLEL SAFE
;

--
-- Operators:
--
---------------------------------------------
CREATE OPERATOR < (
	PROCEDURE = vatinlt,
	LEFTARG = vatin,
	RIGHTARG = vatin,
	COMMUTATOR = >,
	NEGATOR = >=,
	RESTRICT = scalarltsel,
	JOIN = scalarltjoinsel
);
CREATE OPERATOR <= (
	PROCEDURE = vatinle,
	LEFTARG = vatin,
	RIGHTARG = vatin,
	COMMUTATOR = >=,
	NEGATOR = >,
	RESTRICT = scalarltsel,
	JOIN = scalarltjoinsel
);
CREATE OPERATOR = (
	PROCEDURE = vatineq,
	LEFTARG = vatin,
	RIGHTARG = vatin,
	COMMUTATOR = =,
	NEGATOR = <>,
	RESTRICT = eqsel,
	JOIN = eqjoinsel,
	MERGES,
	HASHES
);
CREATE OPERATOR >= (
	PROCEDURE = vatinge,
	LEFTARG = vatin,
	RIGHTARG = vatin,
	COMMUTATOR = <=,
	NEGATOR = <,
	RESTRICT = scalargtsel,
	JOIN = scalargtjoinsel
);
CREATE OPERATOR > (
	PROCEDURE = vatingt,
	LEFTARG = vatin,
	RIGHTARG = vatin,
	COMMUTATOR = <,
	NEGATOR = <=,
	RESTRICT = scalargtsel,
	JOIN = scalargtjoinsel
);
CREATE OPERATOR <> (
	PROCEDURE = vatinne,
	LEFTARG = vatin,
	RIGHTARG = vatin,
	COMMUTATOR = <>,
	NEGATOR = =,
	RESTRICT = neqsel,
	JOIN = neqjoinsel
);

--
-- Operator families for the various operator classes:
--
---------------------------------------------
CREATE OPERATOR FAMILY vatin_ops USING btree;
CREATE OPERATOR FAMILY vatin_ops USING hash;

--
-- Operator classes:
--
---------------------------------------------
CREATE FUNCTION btvatincmp(vatin, vatin)
	RETURNS int4
	AS 'bttextcmp'
	LANGUAGE 'internal'
	IMMUTABLE STRICT
	PARALLEL SAFE
;

CREATE OPERATOR CLASS vatin_ops DEFAULT
	FOR TYPE vatin USING btree FAMILY vatin_ops AS
	OPERATOR 1  <,
	OPERATOR 2  <=,
	OPERATOR 3  =,
	OPERATOR 4  >=,
	OPERATOR 5  >,
	FUNCTION 1  btvatincmp(vatin, vatin)
;

CREATE FUNCTION hashvatin(vatin)
	RETURNS int4
	AS 'hashtext'
	LANGUAGE 'internal' IMMUTABLE STRICT
	PARALLEL SAFE
;

CREATE OPERATOR CLASS vatin_ops DEFAULT
	FOR TYPE vatin USING hash FAMILY vatin_ops AS
	OPERATOR 1  =,
	FUNCTION 1  hashvatin(vatin)
;
