-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeid.sql
112 lines (103 loc) · 4.41 KB
/
typeid.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
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE OR REPLACE FUNCTION gen_ulid()
RETURNS text
AS
$$
DECLARE
-- Crockford's Base32
encoding bytea = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
timestamp bytea = E'\\000\\000\\000\\000\\000\\000';
output text = '';
unix_time bigint;
ulid bytea;
BEGIN
unix_time = (extract(epoch from clock_timestamp()) * 1000)::bigint;
timestamp = set_byte(timestamp, 0, (unix_time >> 40)::bit(8)::integer);
timestamp = set_byte(timestamp, 1, (unix_time >> 32)::bit(8)::integer);
timestamp = set_byte(timestamp, 2, (unix_time >> 24)::bit(8)::integer);
timestamp = set_byte(timestamp, 3, (unix_time >> 16)::bit(8)::integer);
timestamp = set_byte(timestamp, 4, (unix_time >> 8)::bit(8)::integer);
timestamp = set_byte(timestamp, 5, unix_time::bit(8)::integer);
-- 10 entropy bytes
ulid = timestamp || gen_random_bytes(10);
-- Encode the timestamp
output = output || chr(get_byte(encoding, (get_byte(ulid, 0) & 224) >> 5));
output = output || chr(get_byte(encoding, (get_byte(ulid, 0) & 31)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 1) & 248) >> 3));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 1) & 7) << 2) | ((get_byte(ulid, 2) & 192) >> 6)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 2) & 62) >> 1));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 2) & 1) << 4) | ((get_byte(ulid, 3) & 240) >> 4)));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 3) & 15) << 1) | ((get_byte(ulid, 4) & 128) >> 7)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 4) & 124) >> 2));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 4) & 3) << 3) | ((get_byte(ulid, 5) & 224) >> 5)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 5) & 31)));
-- Encode the entropy
output = output || chr(get_byte(encoding, (get_byte(ulid, 6) & 248) >> 3));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 6) & 7) << 2) | ((get_byte(ulid, 7) & 192) >> 6)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 7) & 62) >> 1));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 7) & 1) << 4) | ((get_byte(ulid, 8) & 240) >> 4)));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 8) & 15) << 1) | ((get_byte(ulid, 9) & 128) >> 7)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 9) & 124) >> 2));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 9) & 3) << 3) | ((get_byte(ulid, 10) & 224) >> 5)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 10) & 31)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 11) & 248) >> 3));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 11) & 7) << 2) | ((get_byte(ulid, 12) & 192) >> 6)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 12) & 62) >> 1));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 12) & 1) << 4) | ((get_byte(ulid, 13) & 240) >> 4)));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 13) & 15) << 1) | ((get_byte(ulid, 14) & 128) >> 7)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 14) & 124) >> 2));
output = output || chr(get_byte(encoding, ((get_byte(ulid, 14) & 3) << 3) | ((get_byte(ulid, 15) & 224) >> 5)));
output = output || chr(get_byte(encoding, (get_byte(ulid, 15) & 31)));
RETURN output;
END
$$
LANGUAGE plpgsql
VOLATILE;
CREATE OR REPLACE FUNCTION check_ulid(
id text
)
RETURNS boolean
AS
$$
BEGIN
IF id ~ '^[0-9a-zA-Z]{26}$' THEN
RETURN true;
ELSE
RETURN false;
END IF;
END
$$
LANGUAGE plpgsql
IMMUTABLE;
CREATE OR REPLACE FUNCTION gen_typeid(
prefix text
)
RETURNS text
AS
$$
BEGIN
IF (prefix IS NULL) OR (length(prefix) = 0) OR NOT (prefix ~ '^[a-z]+(_[a-z]+)*$') THEN
RAISE EXCEPTION 'typeid prefix must not be null or empty, must start with a lowercase letter, consist of lowercase letters and underscores, and cannot start with an underscore or end with an underscore';
END IF;
RETURN (prefix || '_' || gen_ulid());
END
$$
LANGUAGE plpgsql
IMMUTABLE;
CREATE OR REPLACE FUNCTION check_typeid(
id text,
prefix text
)
RETURNS boolean
AS
$$
BEGIN
IF id ~ ('^' || prefix || '_[0-9a-zA-Z]{26}$') THEN
RETURN true;
ELSE
RETURN false;
END IF;
END
$$
LANGUAGE plpgsql
IMMUTABLE;