Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "postgres.h"
00013 #include "fmgr.h"
00014 #include "utils/builtins.h"
00015 #include "utils/uuid.h"
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_OSSP_UUID_H
00024 #include <ossp/uuid.h>
00025 #else
00026 #ifdef HAVE_UUID_H
00027 #include <uuid.h>
00028 #else
00029 #error OSSP uuid.h not found
00030 #endif
00031 #endif
00032
00033
00034 #if (UUID_LEN != UUID_LEN_BIN)
00035 #error UUID length mismatch
00036 #endif
00037
00038
00039 PG_MODULE_MAGIC;
00040
00041
00042 Datum uuid_nil(PG_FUNCTION_ARGS);
00043 Datum uuid_ns_dns(PG_FUNCTION_ARGS);
00044 Datum uuid_ns_url(PG_FUNCTION_ARGS);
00045 Datum uuid_ns_oid(PG_FUNCTION_ARGS);
00046 Datum uuid_ns_x500(PG_FUNCTION_ARGS);
00047
00048 Datum uuid_generate_v1(PG_FUNCTION_ARGS);
00049 Datum uuid_generate_v1mc(PG_FUNCTION_ARGS);
00050 Datum uuid_generate_v3(PG_FUNCTION_ARGS);
00051 Datum uuid_generate_v4(PG_FUNCTION_ARGS);
00052 Datum uuid_generate_v5(PG_FUNCTION_ARGS);
00053
00054
00055 PG_FUNCTION_INFO_V1(uuid_nil);
00056 PG_FUNCTION_INFO_V1(uuid_ns_dns);
00057 PG_FUNCTION_INFO_V1(uuid_ns_url);
00058 PG_FUNCTION_INFO_V1(uuid_ns_oid);
00059 PG_FUNCTION_INFO_V1(uuid_ns_x500);
00060
00061 PG_FUNCTION_INFO_V1(uuid_generate_v1);
00062 PG_FUNCTION_INFO_V1(uuid_generate_v1mc);
00063 PG_FUNCTION_INFO_V1(uuid_generate_v3);
00064 PG_FUNCTION_INFO_V1(uuid_generate_v4);
00065 PG_FUNCTION_INFO_V1(uuid_generate_v5);
00066
00067 static void
00068 pguuid_complain(uuid_rc_t rc)
00069 {
00070 char *err = uuid_error(rc);
00071
00072 if (err != NULL)
00073 ereport(ERROR,
00074 (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
00075 errmsg("OSSP uuid library failure: %s", err)));
00076 else
00077 ereport(ERROR,
00078 (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
00079 errmsg("OSSP uuid library failure: error code %d", rc)));
00080 }
00081
00082 static char *
00083 uuid_to_string(const uuid_t *uuid)
00084 {
00085 char *buf = palloc(UUID_LEN_STR + 1);
00086 void *ptr = buf;
00087 size_t len = UUID_LEN_STR + 1;
00088 uuid_rc_t rc;
00089
00090 rc = uuid_export(uuid, UUID_FMT_STR, &ptr, &len);
00091 if (rc != UUID_RC_OK)
00092 pguuid_complain(rc);
00093
00094 return buf;
00095 }
00096
00097
00098 static void
00099 string_to_uuid(const char *str, uuid_t *uuid)
00100 {
00101 uuid_rc_t rc;
00102
00103 rc = uuid_import(uuid, UUID_FMT_STR, str, UUID_LEN_STR + 1);
00104 if (rc != UUID_RC_OK)
00105 pguuid_complain(rc);
00106 }
00107
00108
00109 static Datum
00110 special_uuid_value(const char *name)
00111 {
00112 uuid_t *uuid;
00113 char *str;
00114 uuid_rc_t rc;
00115
00116 rc = uuid_create(&uuid);
00117 if (rc != UUID_RC_OK)
00118 pguuid_complain(rc);
00119 rc = uuid_load(uuid, name);
00120 if (rc != UUID_RC_OK)
00121 pguuid_complain(rc);
00122 str = uuid_to_string(uuid);
00123 rc = uuid_destroy(uuid);
00124 if (rc != UUID_RC_OK)
00125 pguuid_complain(rc);
00126
00127 return DirectFunctionCall1(uuid_in, CStringGetDatum(str));
00128 }
00129
00130
00131 Datum
00132 uuid_nil(PG_FUNCTION_ARGS)
00133 {
00134 return special_uuid_value("nil");
00135 }
00136
00137
00138 Datum
00139 uuid_ns_dns(PG_FUNCTION_ARGS)
00140 {
00141 return special_uuid_value("ns:DNS");
00142 }
00143
00144
00145 Datum
00146 uuid_ns_url(PG_FUNCTION_ARGS)
00147 {
00148 return special_uuid_value("ns:URL");
00149 }
00150
00151
00152 Datum
00153 uuid_ns_oid(PG_FUNCTION_ARGS)
00154 {
00155 return special_uuid_value("ns:OID");
00156 }
00157
00158
00159 Datum
00160 uuid_ns_x500(PG_FUNCTION_ARGS)
00161 {
00162 return special_uuid_value("ns:X500");
00163 }
00164
00165
00166 static Datum
00167 uuid_generate_internal(int mode, const uuid_t *ns, const char *name)
00168 {
00169 uuid_t *uuid;
00170 char *str;
00171 uuid_rc_t rc;
00172
00173 rc = uuid_create(&uuid);
00174 if (rc != UUID_RC_OK)
00175 pguuid_complain(rc);
00176 rc = uuid_make(uuid, mode, ns, name);
00177 if (rc != UUID_RC_OK)
00178 pguuid_complain(rc);
00179 str = uuid_to_string(uuid);
00180 rc = uuid_destroy(uuid);
00181 if (rc != UUID_RC_OK)
00182 pguuid_complain(rc);
00183
00184 return DirectFunctionCall1(uuid_in, CStringGetDatum(str));
00185 }
00186
00187
00188 Datum
00189 uuid_generate_v1(PG_FUNCTION_ARGS)
00190 {
00191 return uuid_generate_internal(UUID_MAKE_V1, NULL, NULL);
00192 }
00193
00194
00195 Datum
00196 uuid_generate_v1mc(PG_FUNCTION_ARGS)
00197 {
00198 return uuid_generate_internal(UUID_MAKE_V1 | UUID_MAKE_MC, NULL, NULL);
00199 }
00200
00201
00202 static Datum
00203 uuid_generate_v35_internal(int mode, pg_uuid_t *ns, text *name)
00204 {
00205 uuid_t *ns_uuid;
00206 Datum result;
00207 uuid_rc_t rc;
00208
00209 rc = uuid_create(&ns_uuid);
00210 if (rc != UUID_RC_OK)
00211 pguuid_complain(rc);
00212 string_to_uuid(DatumGetCString(DirectFunctionCall1(uuid_out, UUIDPGetDatum(ns))),
00213 ns_uuid);
00214
00215 result = uuid_generate_internal(mode,
00216 ns_uuid,
00217 text_to_cstring(name));
00218
00219 rc = uuid_destroy(ns_uuid);
00220 if (rc != UUID_RC_OK)
00221 pguuid_complain(rc);
00222
00223 return result;
00224 }
00225
00226
00227 Datum
00228 uuid_generate_v3(PG_FUNCTION_ARGS)
00229 {
00230 pg_uuid_t *ns = PG_GETARG_UUID_P(0);
00231 text *name = PG_GETARG_TEXT_P(1);
00232
00233 return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
00234 }
00235
00236
00237 Datum
00238 uuid_generate_v4(PG_FUNCTION_ARGS)
00239 {
00240 return uuid_generate_internal(UUID_MAKE_V4, NULL, NULL);
00241 }
00242
00243
00244 Datum
00245 uuid_generate_v5(PG_FUNCTION_ARGS)
00246 {
00247 pg_uuid_t *ns = PG_GETARG_UUID_P(0);
00248 text *name = PG_GETARG_TEXT_P(1);
00249
00250 return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);
00251 }