\echo Use "CREATE EXTENSION timeuuid" to load this file. \quit

create type timeuuid;

create or replace function timeuuid_in(cstring) returns timeuuid
    as 'uuid_in' language internal immutable strict;
create or replace function timeuuid_out(timeuuid) returns cstring
    as 'uuid_out' language internal immutable strict;

create or replace function timeuuid_recv(internal) returns timeuuid
    as 'uuid_recv' language internal immutable strict;
create or replace function timeuuid_send(timeuuid) returns bytea
    as 'uuid_send' language internal immutable strict;

create type timeuuid (
    input = timeuuid_in,
    output = timeuuid_out,
    receive = timeuuid_recv,
    send = timeuuid_send,
    like = uuid
);

create or replace function timeuuid_eq(timeuuid, timeuuid) returns boolean
    as 'uuid_eq' language internal immutable strict;
create or replace function timeuuid_neq(timeuuid, timeuuid) returns boolean
    as 'uuid_ne' language internal immutable strict;

create or replace function timeuuid_hash(timeuuid) returns int4
    as 'uuid_hash' language internal immutable strict;

create function timeuuid_lt(timeuuid, timeuuid) returns boolean
    as 'MODULE_PATHNAME' language c strict immutable;
create function timeuuid_gt(timeuuid, timeuuid) returns boolean
    as 'MODULE_PATHNAME' language c strict immutable;
create function timeuuid_le(timeuuid, timeuuid) returns boolean
    as 'MODULE_PATHNAME' language c strict immutable;
create function timeuuid_ge(timeuuid, timeuuid) returns boolean
    as 'MODULE_PATHNAME' language c strict immutable;

create function timeuuid_cmp(timeuuid, timeuuid) returns int4
    as 'MODULE_PATHNAME' language c strict immutable;

create operator = (
    leftarg = timeuuid,
    rightarg = timeuuid,
    commutator = =,
    negator = <>,
    procedure = timeuuid_eq,
    restrict = eqsel,
    join = eqjoinsel,
    hashes,
    merges
);

create operator <> (
    leftarg = timeuuid,
    rightarg = timeuuid,
    commutator = <>,
    negator = =,
    procedure = timeuuid_neq,
    restrict = neqsel,
    join = neqjoinsel
);

create operator < (
    leftarg = timeuuid,
    rightarg = timeuuid,
    commutator = >,
    negator = >=,
    procedure = timeuuid_lt,
    restrict = scalarltsel,
    join = scalarltjoinsel
);

create operator > (
    leftarg = timeuuid,
    rightarg = timeuuid,
    commutator = <,
    negator = <=,
    procedure = timeuuid_gt,
    restrict = scalargtsel,
    join = scalargtjoinsel
);

create operator <= (
    leftarg = timeuuid,
    rightarg = timeuuid,
    commutator = >=,
    negator = >,
    procedure = timeuuid_le,
    restrict = scalarltsel,
    join = scalarltjoinsel
);

create operator >= (
    leftarg = timeuuid,
    rightarg = timeuuid,
    commutator = <=,
    negator = <,
    procedure = timeuuid_ge,
    restrict = scalargtsel,
    join = scalargtjoinsel
);

create operator class timeuuid_ops
default for type timeuuid using btree as
    operator 1 <,
    operator 2 <=,
    operator 3 =,
    operator 4 >=,
    operator 5 >,
    function 1 timeuuid_cmp(timeuuid, timeuuid);

create cast (timeuuid as uuid)
    without function as assignment;
create cast (uuid as timeuuid)
    without function as assignment;

create function timeuuid_to_timestamptz(uuid) returns timestamptz
    as 'MODULE_PATHNAME' language c strict immutable;
create function timeuuid_to_timestamptz(timeuuid) returns timestamptz
    as 'MODULE_PATHNAME' language c strict immutable;
create function timestamptz_to_timeuuid(timestamptz) returns timeuuid
    as 'MODULE_PATHNAME' language c strict immutable;

create cast (timeuuid as timestamptz)
    with function timeuuid_to_timestamptz(timeuuid);
create cast (timestamptz as timeuuid)
    with function timestamptz_to_timeuuid(timestamptz);
