--
--	PostgreSQL code for uniqueidentifierss.
--
--	$Id: uniqueidentifier.sql.in,v 1.8 2003/12/02 12:45:20 dimagog Exp $
--

BEGIN TRANSACTION;

SET SEARCH_PATH='pg_catalog';
--
--	Input and output functions and the type itself:
--

CREATE OR REPLACE FUNCTION uniqueidentifier_in(cstring)
	returns uniqueidentifier
	as '$libdir/uniqueidentifier', 'uniqueidentifier_in'
	language 'c';

CREATE OR REPLACE FUNCTION uniqueidentifier_out(uniqueidentifier)
	returns cstring
	as '$libdir/uniqueidentifier', 'uniqueidentifier_out'
	language 'c';

create type uniqueidentifier (
	internallength = 16,
	externallength = 36,
	input =  uniqueidentifier_in,
	output = uniqueidentifier_out
);

--
--	The various boolean tests:
--

CREATE OR REPLACE FUNCTION uniqueidentifier_lt(uniqueidentifier, uniqueidentifier)
	returns bool
	as '$libdir/uniqueidentifier'
	language 'c';

CREATE OR REPLACE FUNCTION uniqueidentifier_le(uniqueidentifier, uniqueidentifier)
	returns bool
	as '$libdir/uniqueidentifier'
	language 'c';

CREATE OR REPLACE FUNCTION uniqueidentifier_eq(uniqueidentifier, uniqueidentifier)
	returns bool
	as '$libdir/uniqueidentifier'
	language 'c';

CREATE OR REPLACE FUNCTION uniqueidentifier_ge(uniqueidentifier, uniqueidentifier)
	returns bool
	as '$libdir/uniqueidentifier'
	language 'c';

CREATE OR REPLACE FUNCTION uniqueidentifier_gt(uniqueidentifier, uniqueidentifier)
	returns bool
	as '$libdir/uniqueidentifier'
	language 'c';

CREATE OR REPLACE FUNCTION uniqueidentifier_ne(uniqueidentifier, uniqueidentifier)
	returns bool
	as '$libdir/uniqueidentifier'
	language 'c';

--
--	Now the operators.  Note how some of the parameters to some
--	of the 'create operator' commands are commented out.  This
--	is because they reference as yet undefined operators, and
--	will be implicitly defined when those are, further down.
--

create operator < (
	leftarg = uniqueidentifier,
	rightarg = uniqueidentifier,
	commutator = >,
	negator = >=,
	procedure = uniqueidentifier_lt
);

create operator <= (
	leftarg = uniqueidentifier,
	rightarg = uniqueidentifier,
	commutator = >=,
	negator = >,
	procedure = uniqueidentifier_le
);

create operator = (
	leftarg = uniqueidentifier,
	rightarg = uniqueidentifier,
	commutator = =,
	negator = <>,
	procedure = uniqueidentifier_eq,
	hashes,
	restrict = eqsel,
	join = eqjoinsel,
	sort1 = <,
	sort2 = <
);

create operator >= (
	leftarg = uniqueidentifier,
	rightarg = uniqueidentifier,
	commutator = <=,
	negator = <,
	procedure = uniqueidentifier_ge
);

create operator > (
	leftarg = uniqueidentifier,
	rightarg = uniqueidentifier,
	commutator = <,
	negator = <=,
	procedure = uniqueidentifier_gt
);

create operator <> (
	leftarg = uniqueidentifier,
	rightarg = uniqueidentifier,
	negator = =,
	procedure = uniqueidentifier_ne
);

--
--	Create new uniqueidentifier:
--

CREATE OR REPLACE FUNCTION newid()
	returns uniqueidentifier
	as '$libdir/uniqueidentifier'
	language 'c';

--
--	Convert from uniqueidentifier to text:
--

CREATE OR REPLACE FUNCTION text(uniqueidentifier)
	returns text
	immutable
	as '$libdir/uniqueidentifier', 'uniqueidentifier_text'
	language 'c';
	
--
--	Convert from text to uniqueidentifier:
--

CREATE OR REPLACE FUNCTION newid(text)
	returns uniqueidentifier
	immutable
	as '$libdir/uniqueidentifier', 'uniqueidentifier_from_text'
	language 'c';
	
--
--	Create cast from uniqueidentifier to text:
--

create cast (uniqueidentifier AS text)
	with function text(uniqueidentifier)
	as implicit;

--
--	Create cast from text to uniqueidentifier:
--

create cast (text AS uniqueidentifier)
	with function newid(text)
	as implicit;

---
---	Index related stuff
---

CREATE OR REPLACE FUNCTION uniqueidentifier_cmp(uniqueidentifier, uniqueidentifier)
	RETURNS int4
	AS '$libdir/uniqueidentifier'
	LANGUAGE 'C';

CREATE OPERATOR CLASS uniqueidentifier_ops_btree
    DEFAULT FOR TYPE uniqueidentifier USING btree AS
	OPERATOR	1	<,
	OPERATOR	2	<=,
	OPERATOR	3	=,
	OPERATOR	4	>=,
	OPERATOR	5	>,
	FUNCTION	1	uniqueidentifier_cmp(uniqueidentifier, uniqueidentifier);
    

CREATE OR REPLACE FUNCTION uniqueidentifier_hash(uniqueidentifier)
	RETURNS int4
	AS '$libdir/uniqueidentifier'
	LANGUAGE 'C';

CREATE OPERATOR CLASS uniqueidentifier_ops_hash
    DEFAULT FOR TYPE uniqueidentifier USING hash AS
	OPERATOR	1	=,
	FUNCTION	1	uniqueidentifier_hash(uniqueidentifier);
    
CREATE DOMAIN public.uniqueidentifier uniqueidentifier;
END TRANSACTION;
	
--
--	eof
--
	
