1 /*-------------------------------------------------------------------------
3 * UUID generation functions using the OSSP UUID library
5 * Copyright (c) 2007 PostgreSQL Global Development Group
7 * $PostgreSQL: pgsql/contrib/uuid-ossp/uuid-ossp.c,v 1.1 2007/04/21 17:26:17 petere Exp $
9 *-------------------------------------------------------------------------
14 #include "utils/builtins.h"
15 #include "utils/uuid.h"
17 #include <ossp/uuid.h>
20 /* better both be 16 */
21 #if (UUID_LEN != UUID_LEN_BIN)
22 #error UUID length mismatch
29 Datum uuid_nil(PG_FUNCTION_ARGS);
30 Datum uuid_ns_dns(PG_FUNCTION_ARGS);
31 Datum uuid_ns_url(PG_FUNCTION_ARGS);
32 Datum uuid_ns_oid(PG_FUNCTION_ARGS);
33 Datum uuid_ns_x500(PG_FUNCTION_ARGS);
35 Datum uuid_generate_v1(PG_FUNCTION_ARGS);
36 Datum uuid_generate_v1mc(PG_FUNCTION_ARGS);
37 Datum uuid_generate_v3(PG_FUNCTION_ARGS);
38 Datum uuid_generate_v4(PG_FUNCTION_ARGS);
39 Datum uuid_generate_v5(PG_FUNCTION_ARGS);
42 PG_FUNCTION_INFO_V1(uuid_nil);
43 PG_FUNCTION_INFO_V1(uuid_ns_dns);
44 PG_FUNCTION_INFO_V1(uuid_ns_url);
45 PG_FUNCTION_INFO_V1(uuid_ns_oid);
46 PG_FUNCTION_INFO_V1(uuid_ns_x500);
48 PG_FUNCTION_INFO_V1(uuid_generate_v1);
49 PG_FUNCTION_INFO_V1(uuid_generate_v1mc);
50 PG_FUNCTION_INFO_V1(uuid_generate_v3);
51 PG_FUNCTION_INFO_V1(uuid_generate_v4);
52 PG_FUNCTION_INFO_V1(uuid_generate_v5);
56 uuid_to_string(const uuid_t *uuid)
58 char *buf = palloc(UUID_LEN_STR + 1);
60 size_t len = UUID_LEN_STR + 1;
62 uuid_export(uuid, UUID_FMT_STR, &ptr, &len);
69 string_to_uuid(const char *str, uuid_t *uuid)
71 uuid_import(uuid, UUID_FMT_STR, str, UUID_LEN_STR + 1);
76 special_uuid_value(const char *name)
82 uuid_load(uuid, name);
83 str = uuid_to_string(uuid);
86 return DirectFunctionCall1(uuid_in, CStringGetDatum(str));
91 uuid_nil(PG_FUNCTION_ARGS)
93 return special_uuid_value("nil");
98 uuid_ns_dns(PG_FUNCTION_ARGS)
100 return special_uuid_value("ns:DNS");
105 uuid_ns_url(PG_FUNCTION_ARGS)
107 return special_uuid_value("ns:URL");
112 uuid_ns_oid(PG_FUNCTION_ARGS)
114 return special_uuid_value("ns:OID");
119 uuid_ns_x500(PG_FUNCTION_ARGS)
121 return special_uuid_value("ns:X500");
126 uuid_generate_internal(int mode, const uuid_t *ns, const char *name)
132 uuid_make(uuid, mode, ns, name);
133 str = uuid_to_string(uuid);
136 return DirectFunctionCall1(uuid_in, CStringGetDatum(str));
141 uuid_generate_v1(PG_FUNCTION_ARGS)
143 return uuid_generate_internal(UUID_MAKE_V1, NULL, NULL);
148 uuid_generate_v1mc(PG_FUNCTION_ARGS)
150 return uuid_generate_internal(UUID_MAKE_V1 | UUID_MAKE_MC, NULL, NULL);
155 uuid_generate_v35_internal(int mode, pg_uuid_t *ns, text *name)
160 uuid_create(&ns_uuid);
161 string_to_uuid(DatumGetCString(DirectFunctionCall1(uuid_out, UUIDPGetDatum(ns))),
164 result = uuid_generate_internal(mode,
166 DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(name))));
168 uuid_destroy(ns_uuid);
175 uuid_generate_v3(PG_FUNCTION_ARGS)
177 pg_uuid_t *ns = PG_GETARG_UUID_P(0);
178 text *name = PG_GETARG_TEXT_P(1);
180 return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
185 uuid_generate_v4(PG_FUNCTION_ARGS)
187 return uuid_generate_internal(UUID_MAKE_V4, NULL, NULL);
192 uuid_generate_v5(PG_FUNCTION_ARGS)
194 pg_uuid_t *ns = PG_GETARG_UUID_P(0);
195 text *name = PG_GETARG_TEXT_P(1);
197 return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);