-
Notifications
You must be signed in to change notification settings - Fork 9
/
ulid--0.0.1.sql
150 lines (120 loc) · 3.4 KB
/
ulid--0.0.1.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
CREATE TYPE ulid;
--
-- Generate and cast functions.
--
CREATE FUNCTION gen_ulid () RETURNS ulid AS 'ulid' LANGUAGE C STRICT PARALLEL SAFE;
CREATE FUNCTION ulid_to_timestamp (ulid) RETURNS TIMESTAMP AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION timestamp_to_ulid (TIMESTAMP) RETURNS ulid AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
--
-- Input and output functions.
--
CREATE FUNCTION ulid_in (cstring) RETURNS ulid AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION ulid_out (ulid) RETURNS cstring AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION ulid_recv (internal) RETURNS ulid AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION ulid_send (ulid) RETURNS bytea AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
--
-- The type itself.
--
CREATE TYPE ulid (
INPUT = ulid_in,
OUTPUT = ulid_out,
INTERNALLENGTH = 16,
SEND = ulid_send,
RECEIVE = ulid_recv
);
--
-- Implicit and assignment type casts.
--
CREATE CAST(ulid AS text)
WITH
INOUT AS IMPLICIT;
CREATE CAST(text AS ulid)
WITH
INOUT AS IMPLICIT;
CREATE CAST(ulid AS TIMESTAMP)
WITH
FUNCTION ulid_to_timestamp (ulid) AS IMPLICIT;
CREATE CAST (timestamp AS ulid)
WITH
FUNCTION timestamp_to_ulid(timestamp) AS IMPLICIT;
--
-- Operator Functions.
--
CREATE FUNCTION ulid_eq (ulid, ulid) RETURNS BOOLEAN AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION ulid_neq (ulid, ulid) RETURNS BOOLEAN AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION ulid_leq (ulid, ulid) RETURNS BOOLEAN AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION ulid_lt (ulid, ulid) RETURNS BOOLEAN AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION ulid_geq (ulid, ulid) RETURNS BOOLEAN AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION ulid_gt (ulid, ulid) RETURNS BOOLEAN AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
--
-- Operators.
--
CREATE OPERATOR = (
PROCEDURE = ulid_eq,
LEFTARG = ulid,
RIGHTARG = ulid,
COMMUTATOR = =,
NEGATOR = <>,
RESTRICT = eqsel,
JOIN = eqjoinsel,
MERGES,
HASHES
);
CREATE OPERATOR <> (
PROCEDURE = ulid_neq,
LEFTARG = ulid,
RIGHTARG = ulid,
COMMUTATOR = <>,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
CREATE OPERATOR < (
PROCEDURE = ulid_lt,
LEFTARG = ulid,
RIGHTARG = ulid,
COMMUTATOR = >,
NEGATOR = >=,
RESTRICT = scalarltsel,
JOIN = scalarltjoinsel
);
CREATE OPERATOR > (
PROCEDURE = ulid_gt,
LEFTARG = ulid,
RIGHTARG = ulid,
COMMUTATOR = <,
NEGATOR = <=,
RESTRICT = scalargtsel,
JOIN = scalargtjoinsel
);
CREATE OPERATOR <= (
PROCEDURE = ulid_leq,
LEFTARG = ulid,
RIGHTARG = ulid,
COMMUTATOR = >=,
NEGATOR = >,
RESTRICT = scalarltsel,
JOIN = scalarltjoinsel
);
CREATE OPERATOR >= (
PROCEDURE = ulid_geq,
LEFTARG = ulid,
RIGHTARG = ulid,
COMMUTATOR = <=,
NEGATOR = <,
RESTRICT = scalargtsel,
JOIN = scalargtjoinsel
);
--
-- Support functions for indexing.
--
CREATE FUNCTION ulid_cmp (ulid, ulid) RETURNS INT AS 'ulid' LANGUAGE C IMMUTABLE STRICT;
CREATE OPERATOR CLASS btree_ulid_ops DEFAULT FOR TYPE ulid USING btree AS OPERATOR 1 <,
OPERATOR 2 <=,
OPERATOR 3 =,
OPERATOR 4 >=,
OPERATOR 5 >,
FUNCTION 1 ulid_cmp (ulid, ulid);
CREATE FUNCTION ulid_hash (ulid) RETURNS int4 AS 'ulid' LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
CREATE OPERATOR CLASS ulid_ops DEFAULT FOR TYPE ulid USING hash AS OPERATOR 1 = (ulid, ulid),
FUNCTION 1 ulid_hash (ulid);