1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type "uuid".
6 * Copyright (c) 2007-2008, PostgreSQL Global Development Group
9 * $PostgreSQL: pgsql/src/backend/utils/adt/uuid.c,v 1.8 2008/11/03 22:14:40 petere Exp $
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;
94 for (i = 0; i < UUID_LEN; i++)
98 if (src[0] == '\0' || src[1] == '\0')
100 memcpy(str_buf, src, 2);
101 if (!isxdigit((unsigned char) str_buf[0]) ||
102 !isxdigit((unsigned char) str_buf[1]))
106 uuid->data[i] = (unsigned char) strtoul(str_buf, NULL, 16);
108 if (src[0] == '-' && (i % 2) == 1 && i < UUID_LEN - 1)
126 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
127 errmsg("invalid input syntax for uuid: \"%s\"",
132 uuid_recv(PG_FUNCTION_ARGS)
134 StringInfo buffer = (StringInfo) PG_GETARG_POINTER(0);
137 uuid = (pg_uuid_t *) palloc(UUID_LEN);
138 memcpy(uuid->data, pq_getmsgbytes(buffer, UUID_LEN), UUID_LEN);
139 PG_RETURN_POINTER(uuid);
143 uuid_send(PG_FUNCTION_ARGS)
145 pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
146 StringInfoData buffer;
148 pq_begintypsend(&buffer);
149 pq_sendbytes(&buffer, (char *) uuid->data, UUID_LEN);
150 PG_RETURN_BYTEA_P(pq_endtypsend(&buffer));
153 /* internal uuid compare function */
155 uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
157 return memcmp(arg1->data, arg2->data, UUID_LEN);
161 uuid_lt(PG_FUNCTION_ARGS)
163 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
164 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
166 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) < 0);
170 uuid_le(PG_FUNCTION_ARGS)
172 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
173 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
175 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) <= 0);
179 uuid_eq(PG_FUNCTION_ARGS)
181 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
182 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
184 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) == 0);
188 uuid_ge(PG_FUNCTION_ARGS)
190 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
191 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
193 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) >= 0);
197 uuid_gt(PG_FUNCTION_ARGS)
199 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
200 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
202 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) > 0);
206 uuid_ne(PG_FUNCTION_ARGS)
208 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
209 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
211 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) != 0);
214 /* handler for btree index operator */
216 uuid_cmp(PG_FUNCTION_ARGS)
218 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
219 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
221 PG_RETURN_INT32(uuid_internal_cmp(arg1, arg2));
224 /* hash index support */
226 uuid_hash(PG_FUNCTION_ARGS)
228 pg_uuid_t *key = PG_GETARG_UUID_P(0);
230 return hash_any(key->data, UUID_LEN);