]> pd.if.org Git - uuid/blob - postgres/purepguuid.sql
Initial commit
[uuid] / postgres / purepguuid.sql
1 create or replace function uuid_bytespp(howmany integer) returns bytea as $$
2 declare
3         bytes bytea;
4 begin
5         bytes = rpad('', howmany);
6
7         for i in 0 .. howmany-1 loop
8                 bytes := set_byte(bytes, i, (random()*256)::integer);
9         end loop;
10
11         return bytes;
12 end;
13 $$ language 'plpgsql';
14
15 create or replace function uuid_v1pp() returns uuid as $$
16 declare
17         bytes bytea;
18         byte    integer;
19         str     text;
20         id      uuid;
21         tslow   bytea;
22         tsmid   bytea;
23         tshigh  bytea;
24         tsi     bigint;
25 begin
26         -- 122192928000000000
27
28         select extract(epoch from now() - '1582-10-15 00:00:00 UTC'::timestamp with time zone)::bigint * 10000000 +
29         extract(microseconds from now() - '1582-10-15 00:00:00 UTC'::timestamp with time zone)::integer % 1000000 * 10
30         into tsi
31         ;
32
33         tslow := decode(lpad(to_hex(tsi & x'ffffffff'::bigint), 8, '0'), 'hex');
34         tsmid := decode(lpad(to_hex((tsi>>32) & x'ffff'::bigint), 4, '0'), 'hex');
35         tshigh := decode(lpad(to_hex((tsi>>48) & x'ffff'::bigint), 4, '0'), 'hex');
36
37         -- we don't store any state, so the clock sequence is random,
38         -- and we don't have any way to get the macaddr, so that's
39         -- random too.  though we set the multicast bit (the real one,
40         -- not the one in the rfc)
41         bytes := tslow || tsmid || tshigh || uuid_bytespp(8);
42
43         byte = get_byte(bytes, 6);
44         byte = (byte & 15) | 16;
45         bytes = set_byte(bytes, 6, byte);
46
47         byte = get_byte(bytes, 8);
48         byte = (byte & 63) | 128;
49         bytes = set_byte(bytes, 8, byte);
50
51         byte = get_byte(bytes, 10);
52         byte = (byte & 127) | 128;
53         bytes = set_byte(bytes, 10, byte);
54
55         str = encode(bytes, 'hex');
56         id = str::uuid;
57         return id;
58 end;
59 $$ language 'plpgsql';
60
61 create or replace function uuid_v4pp() returns uuid as $$
62 declare
63         bytes bytea;
64         byte    integer;
65         str     text;
66         id      uuid;
67 begin
68         bytes = uuid_bytespp(16);
69
70         byte = get_byte(bytes, 6);
71         byte = (byte & 15) | 64;
72         bytes = set_byte(bytes, 6, byte);
73
74         byte = get_byte(bytes, 8);
75         byte = (byte & 63) | 128;
76         bytes = set_byte(bytes, 8, byte);
77
78         str = encode(bytes, 'hex');
79         id = str::uuid;
80         return id;
81 end;
82 $$ language 'plpgsql';
83
84 create or replace function uuid_v3pp(ns uuid, content bytea) returns uuid as $$
85 declare
86         bytes bytea;
87         nsbytes bytea;
88         byte    integer;
89         str     text;
90         id      uuid;
91 begin
92         nsbytes = decode(regexp_replace(ns::text, '-', '', 'g'), 'hex');
93         bytes = decode(md5(nsbytes || content), 'hex');
94
95         byte = get_byte(bytes, 6);
96         byte = (byte & 15) | 48;
97         bytes = set_byte(bytes, 6, byte);
98
99         byte = get_byte(bytes, 8);
100         byte = (byte & 63) | 128;
101         bytes = set_byte(bytes, 8, byte);
102
103         str = encode(bytes, 'hex');
104         id = str::uuid;
105         return id;
106 end;
107 $$ language 'plpgsql';
108
109 create or replace function uuid_v5pp(ns uuid, content bytea) returns uuid as $$
110 declare
111         bytes bytea;
112         nsbytes bytea;
113         byte    integer;
114         str     text;
115         id      uuid;
116 begin
117         nsbytes = decode(regexp_replace(ns::text, '-', '', 'g'), 'hex');
118         bytes = substring(decode(sha1(nsbytes || content), 'hex') from 1 for 16);
119
120         byte = get_byte(bytes, 6);
121         byte = (byte & 15) | 80;
122         bytes = set_byte(bytes, 6, byte);
123
124         byte = get_byte(bytes, 8);
125         byte = (byte & 63) | 128;
126         bytes = set_byte(bytes, 8, byte);
127
128         str = encode(bytes, 'hex');
129         id = str::uuid;
130         return id;
131 end;
132 $$ language 'plpgsql';