1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type "uuid".
6 * Copyright (c) 2007-2012, PostgreSQL Global Development Group
9 * src/backend/utils/adt/uuid.c
11 *-------------------------------------------------------------------------
16 #include "access/hash.h"
17 #include "libpq/pqformat.h"
18 #include "utils/builtins.h"
19 #include "utils/uuid.h"
21 /* uuid size in bytes */
24 /* pg_uuid_t is declared to be struct pg_uuid_t in uuid.h */
27 unsigned char data[UUID_LEN];
30 static void string_to_uuid(const char *source, pg_uuid_t *uuid);
31 static int uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2);
34 uuid_in(PG_FUNCTION_ARGS)
36 char *uuid_str = PG_GETARG_CSTRING(0);
39 uuid = (pg_uuid_t *) palloc(sizeof(*uuid));
40 string_to_uuid(uuid_str, uuid);
41 PG_RETURN_UUID_P(uuid);
45 uuid_out(PG_FUNCTION_ARGS)
47 pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
48 static const char hex_chars[] = "0123456789abcdef";
53 for (i = 0; i < UUID_LEN; i++)
59 * We print uuid values as a string of 8, 4, 4, 4, and then 12
60 * hexadecimal characters, with each group is separated by a hyphen
61 * ("-"). Therefore, add the hyphens at the appropriate places here.
63 if (i == 4 || i == 6 || i == 8 || i == 10)
64 appendStringInfoChar(&buf, '-');
66 hi = uuid->data[i] >> 4;
67 lo = uuid->data[i] & 0x0F;
69 appendStringInfoChar(&buf, hex_chars[hi]);
70 appendStringInfoChar(&buf, hex_chars[lo]);
73 PG_RETURN_CSTRING(buf.data);
77 * We allow UUIDs as a series of 32 hexadecimal digits with an optional dash
78 * after each group of 4 hexadecimal digits, and optionally surrounded by {}.
79 * (The canonical format 8x-4x-4x-4x-12x, where "nx" means n hexadecimal
80 * digits, is the only one used for output.)
83 string_to_uuid(const char *source, pg_uuid_t *uuid)
85 const char *src = source;
95 for (i = 0; i < UUID_LEN; i++)
99 if (src[0] == '\0' || src[1] == '\0')
101 memcpy(str_buf, src, 2);
102 if (!isxdigit((unsigned char) str_buf[0]) ||
103 !isxdigit((unsigned char) str_buf[1]))
107 uuid->data[i] = (unsigned char) strtoul(str_buf, NULL, 16);
109 if (src[0] == '-' && (i % 2) == 1 && i < UUID_LEN - 1)
127 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
128 errmsg("invalid input syntax for uuid: \"%s\"",
133 uuid_recv(PG_FUNCTION_ARGS)
135 StringInfo buffer = (StringInfo) PG_GETARG_POINTER(0);
138 uuid = (pg_uuid_t *) palloc(UUID_LEN);
139 memcpy(uuid->data, pq_getmsgbytes(buffer, UUID_LEN), UUID_LEN);
140 PG_RETURN_POINTER(uuid);
144 uuid_send(PG_FUNCTION_ARGS)
146 pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
147 StringInfoData buffer;
149 pq_begintypsend(&buffer);
150 pq_sendbytes(&buffer, (char *) uuid->data, UUID_LEN);
151 PG_RETURN_BYTEA_P(pq_endtypsend(&buffer));
154 /* internal uuid compare function */
156 uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
158 return memcmp(arg1->data, arg2->data, UUID_LEN);
162 uuid_lt(PG_FUNCTION_ARGS)
164 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
165 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
167 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) < 0);
171 uuid_le(PG_FUNCTION_ARGS)
173 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
174 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
176 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) <= 0);
180 uuid_eq(PG_FUNCTION_ARGS)
182 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
183 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
185 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) == 0);
189 uuid_ge(PG_FUNCTION_ARGS)
191 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
192 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
194 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) >= 0);
198 uuid_gt(PG_FUNCTION_ARGS)
200 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
201 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
203 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) > 0);
207 uuid_ne(PG_FUNCTION_ARGS)
209 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
210 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
212 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) != 0);
215 /* handler for btree index operator */
217 uuid_cmp(PG_FUNCTION_ARGS)
219 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
220 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
222 PG_RETURN_INT32(uuid_internal_cmp(arg1, arg2));
225 /* hash index support */
227 uuid_hash(PG_FUNCTION_ARGS)
229 pg_uuid_t *key = PG_GETARG_UUID_P(0);
231 return hash_any(key->data, UUID_LEN);