66 * Copyright (c) 2007, PostgreSQL Global Development Group
77 *
88 * IDENTIFICATION
9- * $PostgreSQL: pgsql/src/backend/utils/adt/uuid.c,v 1.1 2007/01/2816:16:52 neilc Exp $
9+ * $PostgreSQL: pgsql/src/backend/utils/adt/uuid.c,v 1.2 2007/01/2820:25:38 neilc Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
3535/* uuid size in bytes */
3636#define UUID_LEN 16
3737
38- /*The uuid_t type is declaredas structuuid_t in uuid.h */
39- struct uuid_t
38+ /*pg_uuid_t is declaredto be structpg_uuid_t in uuid.h */
39+ struct pg_uuid_t
4040{
4141char data [UUID_LEN ];
4242};
4343
44- static void uuid_data_from_string (const char * source ,unsignedchar * data );
45- static void string_from_uuid_data (const char * fmt ,const char * data ,char * uuid_str );
44+ static void string_to_uuid (const char * source ,pg_uuid_t * uuid );
45+ static void uuid_to_string (const char * fmt ,const pg_uuid_t * uuid ,
46+ char * uuid_str );
4647static bool parse_uuid_string (const char * fmt ,const char * chk_fmt ,
47- const char * source ,unsigned char * data );
48+ const char * source ,char * data );
4849static bool is_valid_format (const char * source ,const char * fmt );
49- static int32 uuid_internal_cmp (uuid_t * arg1 ,uuid_t * arg2 );
50+ static int uuid_internal_cmp (const pg_uuid_t * arg1 ,const pg_uuid_t * arg2 );
5051
5152Datum
5253uuid_in (PG_FUNCTION_ARGS )
5354{
5455char * uuid_str = PG_GETARG_CSTRING (0 );
55- uuid_t * uuid ;
56- uint8 data [UUID_LEN ];
56+ pg_uuid_t * uuid ;
5757
58- uuid_data_from_string (uuid_str ,data );
59- uuid = (uuid_t * )palloc (sizeof (uuid_t ));
60- memcpy (uuid -> data ,data ,UUID_LEN );
58+ uuid = (pg_uuid_t * )palloc (sizeof (* uuid ));
59+ string_to_uuid (uuid_str ,uuid );
6160PG_RETURN_UUID_P (uuid );
6261}
6362
6463Datum
6564uuid_out (PG_FUNCTION_ARGS )
6665{
67- uuid_t * uuid = ( uuid_t * ) PG_GETARG_POINTER (0 );
66+ pg_uuid_t * uuid = PG_GETARG_UUID_P (0 );
6867char * uuid_str ;
6968
7069uuid_str = (char * )palloc (PRINT_SIZE );
71- string_from_uuid_data (UUID_FMT1 ,uuid -> data ,uuid_str );
70+ uuid_to_string (UUID_FMT1 ,uuid ,uuid_str );
7271PG_RETURN_CSTRING (uuid_str );
7372}
7473
7574/* string to uuid convertor by various format types */
7675static void
77- uuid_data_from_string (const char * source ,unsigned char * data )
76+ string_to_uuid (const char * source ,pg_uuid_t * uuid )
7877{
79- if (!parse_uuid_string (UUID_FMT1 ,UUID_CHK_FMT1 ,source ,data )&&
80- !parse_uuid_string (UUID_FMT2 ,UUID_CHK_FMT2 ,source ,data )&&
81- !parse_uuid_string (UUID_FMT3 ,UUID_CHK_FMT3 ,source ,data ))
78+ if (!parse_uuid_string (UUID_FMT1 ,UUID_CHK_FMT1 ,source ,uuid -> data )&&
79+ !parse_uuid_string (UUID_FMT2 ,UUID_CHK_FMT2 ,source ,uuid -> data )&&
80+ !parse_uuid_string (UUID_FMT3 ,UUID_CHK_FMT3 ,source ,uuid -> data ))
8281{
8382ereport (ERROR ,
8483(errcode (ERRCODE_INVALID_TEXT_REPRESENTATION ),
@@ -126,7 +125,7 @@ is_valid_format(const char *source, const char *fmt)
126125/* parse the uuid string to a format and return true if okay */
127126static bool
128127parse_uuid_string (const char * fmt ,const char * chk_fmt ,
129- const char * source ,unsigned char * data )
128+ const char * source ,char * data )
130129{
131130int result = sscanf (source ,fmt ,
132131& data [0 ],& data [1 ],& data [2 ],& data [3 ],& data [4 ],
@@ -139,8 +138,9 @@ parse_uuid_string(const char *fmt, const char *chk_fmt,
139138
140139/* create a string representation of the uuid */
141140static void
142- string_from_uuid_data (const char * fmt ,const char * data ,char * uuid_str )
141+ uuid_to_string (const char * fmt ,const pg_uuid_t * uuid ,char * uuid_str )
143142{
143+ const char * data = uuid -> data ;
144144snprintf (uuid_str ,PRINT_SIZE ,fmt ,
145145data [0 ],data [1 ],data [2 ],data [3 ],data [4 ],
146146data [5 ],data [6 ],data [7 ],data [8 ],data [9 ],
@@ -152,17 +152,17 @@ Datum
152152uuid_recv (PG_FUNCTION_ARGS )
153153{
154154StringInfo buffer = (StringInfo )PG_GETARG_POINTER (0 );
155- uuid_t * uuid ;
155+ pg_uuid_t * uuid ;
156156
157- uuid = (uuid_t * )palloc (UUID_LEN );
157+ uuid = (pg_uuid_t * )palloc (UUID_LEN );
158158memcpy (uuid -> data ,pq_getmsgbytes (buffer ,UUID_LEN ),UUID_LEN );
159159PG_RETURN_POINTER (uuid );
160160}
161161
162162Datum
163163uuid_send (PG_FUNCTION_ARGS )
164164{
165- uuid_t * uuid = PG_GETARG_UUID_P (0 );
165+ pg_uuid_t * uuid = PG_GETARG_UUID_P (0 );
166166StringInfoData buffer ;
167167
168168pq_begintypsend (& buffer );
@@ -171,62 +171,62 @@ uuid_send(PG_FUNCTION_ARGS)
171171}
172172
173173/* internal uuid compare function */
174- static int32
175- uuid_internal_cmp (uuid_t * arg1 ,uuid_t * arg2 )
174+ static int
175+ uuid_internal_cmp (const pg_uuid_t * arg1 ,const pg_uuid_t * arg2 )
176176{
177177return memcmp (arg1 -> data ,arg2 -> data ,UUID_LEN );
178178}
179179
180180Datum
181181uuid_lt (PG_FUNCTION_ARGS )
182182{
183- uuid_t * arg1 = PG_GETARG_UUID_P (0 );
184- uuid_t * arg2 = PG_GETARG_UUID_P (1 );
183+ pg_uuid_t * arg1 = PG_GETARG_UUID_P (0 );
184+ pg_uuid_t * arg2 = PG_GETARG_UUID_P (1 );
185185
186186PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 )< 0 );
187187}
188188
189189Datum
190190uuid_le (PG_FUNCTION_ARGS )
191191{
192- uuid_t * arg1 = PG_GETARG_UUID_P (0 );
193- uuid_t * arg2 = PG_GETARG_UUID_P (1 );
192+ pg_uuid_t * arg1 = PG_GETARG_UUID_P (0 );
193+ pg_uuid_t * arg2 = PG_GETARG_UUID_P (1 );
194194
195195PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 ) <=0 );
196196}
197197
198198Datum
199199uuid_eq (PG_FUNCTION_ARGS )
200200{
201- uuid_t * arg1 = PG_GETARG_UUID_P (0 );
202- uuid_t * arg2 = PG_GETARG_UUID_P (1 );
201+ pg_uuid_t * arg1 = PG_GETARG_UUID_P (0 );
202+ pg_uuid_t * arg2 = PG_GETARG_UUID_P (1 );
203203
204204PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 )== 0 );
205205}
206206
207207Datum
208208uuid_ge (PG_FUNCTION_ARGS )
209209{
210- uuid_t * arg1 = PG_GETARG_UUID_P (0 );
211- uuid_t * arg2 = PG_GETARG_UUID_P (1 );
210+ pg_uuid_t * arg1 = PG_GETARG_UUID_P (0 );
211+ pg_uuid_t * arg2 = PG_GETARG_UUID_P (1 );
212212
213213PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 ) >=0 );
214214}
215215
216216Datum
217217uuid_gt (PG_FUNCTION_ARGS )
218218{
219- uuid_t * arg1 = PG_GETARG_UUID_P (0 );
220- uuid_t * arg2 = PG_GETARG_UUID_P (1 );
219+ pg_uuid_t * arg1 = PG_GETARG_UUID_P (0 );
220+ pg_uuid_t * arg2 = PG_GETARG_UUID_P (1 );
221221
222222PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 )> 0 );
223223}
224224
225225Datum
226226uuid_ne (PG_FUNCTION_ARGS )
227227{
228- uuid_t * arg1 = PG_GETARG_UUID_P (0 );
229- uuid_t * arg2 = PG_GETARG_UUID_P (1 );
228+ pg_uuid_t * arg1 = PG_GETARG_UUID_P (0 );
229+ pg_uuid_t * arg2 = PG_GETARG_UUID_P (1 );
230230
231231PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 )!= 0 );
232232}
@@ -235,8 +235,8 @@ uuid_ne(PG_FUNCTION_ARGS)
235235Datum
236236uuid_cmp (PG_FUNCTION_ARGS )
237237{
238- uuid_t * arg1 = PG_GETARG_UUID_P (0 );
239- uuid_t * arg2 = PG_GETARG_UUID_P (1 );
238+ pg_uuid_t * arg1 = PG_GETARG_UUID_P (0 );
239+ pg_uuid_t * arg2 = PG_GETARG_UUID_P (1 );
240240
241241PG_RETURN_INT32 (uuid_internal_cmp (arg1 ,arg2 ));
242242}
@@ -245,8 +245,8 @@ uuid_cmp(PG_FUNCTION_ARGS)
245245Datum
246246uuid_hash (PG_FUNCTION_ARGS )
247247{
248- uuid_t * key = PG_GETARG_UUID_P (0 );
249- return hash_any ((unsignedchar * )key ,sizeof (uuid_t ));
248+ pg_uuid_t * key = PG_GETARG_UUID_P (0 );
249+ return hash_any ((unsignedchar * )key ,sizeof (pg_uuid_t ));
250250}
251251
252252/* cast text to uuid */
@@ -272,8 +272,8 @@ text_uuid(PG_FUNCTION_ARGS)
272272Datum
273273uuid_text (PG_FUNCTION_ARGS )
274274{
275- uuid_t * uuid = PG_GETARG_UUID_P (0 );
276- Datum uuid_str = DirectFunctionCall1 (uuid_out ,UUIDPGetDatum (uuid ));
275+ pg_uuid_t * uuid = PG_GETARG_UUID_P (0 );
276+ Datum uuid_str = DirectFunctionCall1 (uuid_out ,UUIDPGetDatum (uuid ));
277277
278278PG_RETURN_DATUM (DirectFunctionCall1 (textin ,uuid_str ));
279279}