SET search_path = public;
CREATE DOMAIN unitext text;
BEGIN;

CREATE OR REPLACE FUNCTION lower(unitext) RETURNS unitext AS $$
	utf8::decode($_[0]);
	return lc($_[0]);
$$ LANGUAGE plperlu IMMUTABLE;

CREATE OR REPLACE FUNCTION upper(unitext) RETURNS unitext AS $$
	utf8::decode($_[0]);
	return uc($_[0]);
$$ LANGUAGE plperlu IMMUTABLE;


CREATE OR REPLACE FUNCTION unitext_lt(unitext,unitext) RETURNS boolean AS $$
	utf8::decode($_[0]); utf8::decode($_[1]);

	use POSIX qw(setlocale LC_ALL);
	my $loc = setlocale(LC_ALL);
	setlocale(LC_ALL, 'pl_PL.utf8');
	use locale;

	my $ret = ($_[0] lt $_[1]) ? 't' : 'f';

	setlocale(LC_ALL, $loc);

	return $ret;
$$ LANGUAGE plperlu;

CREATE OR REPLACE FUNCTION unitext_le(unitext,unitext) RETURNS boolean AS $$
	utf8::decode($_[0]); utf8::decode($_[1]);

	use POSIX qw(setlocale LC_ALL);
	my $loc = setlocale(LC_ALL);
	setlocale(LC_ALL, 'pl_PL.utf8');
	use locale;

	my $ret = ($_[0] le $_[1]) ? 't' : 'f';

	setlocale(LC_ALL, $loc);

	return $ret;
$$ LANGUAGE plperlu STABLE;

CREATE OR REPLACE FUNCTION unitext_gt(unitext,unitext) RETURNS boolean AS $$
	utf8::decode($_[0]); utf8::decode($_[1]);

	use POSIX qw(setlocale LC_ALL);
	my $loc = setlocale(LC_ALL);
	setlocale(LC_ALL, 'pl_PL.utf8');
	use locale;

	my $ret = ($_[0] gt $_[1]) ? 't' : 'f';

	setlocale(LC_ALL, $loc);

	return $ret;
$$ LANGUAGE plperlu STABLE;

CREATE OR REPLACE FUNCTION unitext_ge(unitext,unitext) RETURNS boolean AS $$
	utf8::decode($_[0]); utf8::decode($_[1]);

	use POSIX qw(setlocale LC_ALL);
	my $loc = setlocale(LC_ALL);
	setlocale(LC_ALL, 'pl_PL.utf8');
	use locale;

	my $ret = ($_[0] ge $_[1]) ? 't' : 'f';

	setlocale(LC_ALL, $loc);

	return $ret;
$$ LANGUAGE plperlu STABLE;

CREATE OR REPLACE FUNCTION unitext_eq(unitext,unitext) RETURNS boolean AS $$
	utf8::decode($_[0]); utf8::decode($_[1]);

	use POSIX qw(setlocale LC_ALL);
	my $loc = setlocale(LC_ALL);
	setlocale(LC_ALL, 'pl_PL.utf8');
	use locale;

	my $ret = ($_[0] eq $_[1]) ? 't' : 'f';

	setlocale(LC_ALL, $loc);

	return $ret;
$$ LANGUAGE plperlu STABLE;

CREATE OR REPLACE FUNCTION unitext_ne(unitext,unitext) RETURNS boolean AS $$
	utf8::decode($_[0]); utf8::decode($_[1]);

	use POSIX qw(setlocale LC_ALL);
	my $loc = setlocale(LC_ALL);
	setlocale(LC_ALL, 'pl_PL.utf8');
	use locale;

	my $ret = ($_[0] ne $_[1]) ? 't' : 'f';

	setlocale(LC_ALL, $loc);

	return $ret;
$$ LANGUAGE plperlu;

CREATE OR REPLACE FUNCTION unitext_cmp(unitext,unitext) RETURNS integer AS $$
	utf8::decode($_[0]); utf8::decode($_[1]);

	use POSIX qw(setlocale LC_ALL);
	my $loc = setlocale(LC_ALL);
	setlocale(LC_ALL, 'pl_PL.utf8');
	use locale;

	my $ret = $_[0] cmp $_[1];

	setlocale(LC_ALL, $loc);

	return $ret;
$$ LANGUAGE plperlu;


COMMIT;

CREATE OPERATOR < (
	LEFTARG = unitext,
	RIGHTARG = unitext,
--	COMMUTATOR = >,
--	NEGATOR = >=,
	PROCEDURE = unitext_lt,
	RESTRICT = scalarltsel,
	JOIN = scalarltjoinsel
);

CREATE OPERATOR <= (
	LEFTARG = unitext,
	RIGHTARG = unitext,
--	COMMUTATOR = >=,
--	NEGATOR = >,
	PROCEDURE = unitext_le,
	RESTRICT = scalarltsel,
	JOIN = scalarltjoinsel
);

CREATE OPERATOR >= (
	LEFTARG = unitext,
	RIGHTARG = unitext,
	COMMUTATOR = <=,
	NEGATOR = <,
	PROCEDURE = unitext_ge,
	RESTRICT = scalargtsel,
	JOIN = scalargtjoinsel
);

CREATE OPERATOR > (
	LEFTARG = unitext,
	RIGHTARG = unitext,
	COMMUTATOR = <,
	NEGATOR = <=,
	PROCEDURE = unitext_gt,
	RESTRICT = scalargtsel,
	JOIN = scalargtjoinsel
);

CREATE OPERATOR <> (
	LEFTARG = unitext,
	RIGHTARG = unitext,
	COMMUTATOR = <>,
--	NEGATOR = =,
	PROCEDURE = unitext_ne,
	RESTRICT = neqsel,
	JOIN = neqjoinsel
);
CREATE OPERATOR = (
	LEFTARG = unitext,
	RIGHTARG = unitext,
	COMMUTATOR = =,
	NEGATOR = <>,
	HASHES, MERGES,
	RESTRICT = eqsel,
	JOIN = eqjoinsel,
	SORT1 = <,
	SORT2 = <,
	LTCMP = <,
	GTCMP = >,
	PROCEDURE = unitext_eq,
	RESTRICT = eqsel,
	JOIN = eqjoinsel
);


CREATE OPERATOR CLASS unitext_ops
DEFAULT FOR TYPE unitext USING btree 
AS
        OPERATOR        1       < ,
        OPERATOR        2       <= ,
        OPERATOR        3       = ,
        OPERATOR        4       >= ,
        OPERATOR        5       > ,
        FUNCTION        1       unitext_cmp(unitext, unitext);


