6
6
* Copyright (c) 2007, PostgreSQL Global Development Group
7
7
*
8
8
* 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 $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
35
35
/* uuid size in bytes */
36
36
#define UUID_LEN 16
37
37
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
40
40
{
41
41
char data [UUID_LEN ];
42
42
};
43
43
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 );
46
47
static bool parse_uuid_string (const char * fmt ,const char * chk_fmt ,
47
- const char * source ,unsigned char * data );
48
+ const char * source ,char * data );
48
49
static 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 );
50
51
51
52
Datum
52
53
uuid_in (PG_FUNCTION_ARGS )
53
54
{
54
55
char * uuid_str = PG_GETARG_CSTRING (0 );
55
- uuid_t * uuid ;
56
- uint8 data [UUID_LEN ];
56
+ pg_uuid_t * uuid ;
57
57
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 );
61
60
PG_RETURN_UUID_P (uuid );
62
61
}
63
62
64
63
Datum
65
64
uuid_out (PG_FUNCTION_ARGS )
66
65
{
67
- uuid_t * uuid = ( uuid_t * ) PG_GETARG_POINTER (0 );
66
+ pg_uuid_t * uuid = PG_GETARG_UUID_P (0 );
68
67
char * uuid_str ;
69
68
70
69
uuid_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 );
72
71
PG_RETURN_CSTRING (uuid_str );
73
72
}
74
73
75
74
/* string to uuid convertor by various format types */
76
75
static void
77
- uuid_data_from_string (const char * source ,unsigned char * data )
76
+ string_to_uuid (const char * source ,pg_uuid_t * uuid )
78
77
{
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 ))
82
81
{
83
82
ereport (ERROR ,
84
83
(errcode (ERRCODE_INVALID_TEXT_REPRESENTATION ),
@@ -126,7 +125,7 @@ is_valid_format(const char *source, const char *fmt)
126
125
/* parse the uuid string to a format and return true if okay */
127
126
static bool
128
127
parse_uuid_string (const char * fmt ,const char * chk_fmt ,
129
- const char * source ,unsigned char * data )
128
+ const char * source ,char * data )
130
129
{
131
130
int result = sscanf (source ,fmt ,
132
131
& data [0 ],& data [1 ],& data [2 ],& data [3 ],& data [4 ],
@@ -139,8 +138,9 @@ parse_uuid_string(const char *fmt, const char *chk_fmt,
139
138
140
139
/* create a string representation of the uuid */
141
140
static 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 )
143
142
{
143
+ const char * data = uuid -> data ;
144
144
snprintf (uuid_str ,PRINT_SIZE ,fmt ,
145
145
data [0 ],data [1 ],data [2 ],data [3 ],data [4 ],
146
146
data [5 ],data [6 ],data [7 ],data [8 ],data [9 ],
@@ -152,17 +152,17 @@ Datum
152
152
uuid_recv (PG_FUNCTION_ARGS )
153
153
{
154
154
StringInfo buffer = (StringInfo )PG_GETARG_POINTER (0 );
155
- uuid_t * uuid ;
155
+ pg_uuid_t * uuid ;
156
156
157
- uuid = (uuid_t * )palloc (UUID_LEN );
157
+ uuid = (pg_uuid_t * )palloc (UUID_LEN );
158
158
memcpy (uuid -> data ,pq_getmsgbytes (buffer ,UUID_LEN ),UUID_LEN );
159
159
PG_RETURN_POINTER (uuid );
160
160
}
161
161
162
162
Datum
163
163
uuid_send (PG_FUNCTION_ARGS )
164
164
{
165
- uuid_t * uuid = PG_GETARG_UUID_P (0 );
165
+ pg_uuid_t * uuid = PG_GETARG_UUID_P (0 );
166
166
StringInfoData buffer ;
167
167
168
168
pq_begintypsend (& buffer );
@@ -171,62 +171,62 @@ uuid_send(PG_FUNCTION_ARGS)
171
171
}
172
172
173
173
/* 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 )
176
176
{
177
177
return memcmp (arg1 -> data ,arg2 -> data ,UUID_LEN );
178
178
}
179
179
180
180
Datum
181
181
uuid_lt (PG_FUNCTION_ARGS )
182
182
{
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 );
185
185
186
186
PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 )< 0 );
187
187
}
188
188
189
189
Datum
190
190
uuid_le (PG_FUNCTION_ARGS )
191
191
{
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 );
194
194
195
195
PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 ) <=0 );
196
196
}
197
197
198
198
Datum
199
199
uuid_eq (PG_FUNCTION_ARGS )
200
200
{
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 );
203
203
204
204
PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 )== 0 );
205
205
}
206
206
207
207
Datum
208
208
uuid_ge (PG_FUNCTION_ARGS )
209
209
{
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 );
212
212
213
213
PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 ) >=0 );
214
214
}
215
215
216
216
Datum
217
217
uuid_gt (PG_FUNCTION_ARGS )
218
218
{
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 );
221
221
222
222
PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 )> 0 );
223
223
}
224
224
225
225
Datum
226
226
uuid_ne (PG_FUNCTION_ARGS )
227
227
{
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 );
230
230
231
231
PG_RETURN_BOOL (uuid_internal_cmp (arg1 ,arg2 )!= 0 );
232
232
}
@@ -235,8 +235,8 @@ uuid_ne(PG_FUNCTION_ARGS)
235
235
Datum
236
236
uuid_cmp (PG_FUNCTION_ARGS )
237
237
{
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 );
240
240
241
241
PG_RETURN_INT32 (uuid_internal_cmp (arg1 ,arg2 ));
242
242
}
@@ -245,8 +245,8 @@ uuid_cmp(PG_FUNCTION_ARGS)
245
245
Datum
246
246
uuid_hash (PG_FUNCTION_ARGS )
247
247
{
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 ));
250
250
}
251
251
252
252
/* cast text to uuid */
@@ -272,8 +272,8 @@ text_uuid(PG_FUNCTION_ARGS)
272
272
Datum
273
273
uuid_text (PG_FUNCTION_ARGS )
274
274
{
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 ));
277
277
278
278
PG_RETURN_DATUM (DirectFunctionCall1 (textin ,uuid_str ));
279
279
}