OSDN Git Service

cc3c8a791f792e5c7b65814b6b334b5032d027d6
[pg-rex/syncrep.git] / src / backend / utils / adt / name.c
1 /*-------------------------------------------------------------------------
2  *
3  * name.c
4  *        Functions for the built-in type "name".
5  * name replaces char16 and is carefully implemented so that it
6  * is a string of length NAMEDATALEN.  DO NOT use hard-coded constants anywhere
7  * always use NAMEDATALEN as the symbolic constant!   - jolly 8/21/95
8  *
9  *
10  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  *        $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.32 2001/10/25 05:49:45 momjian Exp $
16  *
17  *-------------------------------------------------------------------------
18  */
19 #include "postgres.h"
20
21 #include "miscadmin.h"
22 #include "utils/builtins.h"
23
24 /*****************************************************************************
25  *       USER I/O ROUTINES (none)                                                                                                *
26  *****************************************************************************/
27
28
29 /*
30  *              namein  - converts "..." to internal representation
31  *
32  *              Note:
33  *                              [Old] Currently if strlen(s) < NAMEDATALEN, the extra chars are nulls
34  *                              Now, always NULL terminated
35  */
36 Datum
37 namein(PG_FUNCTION_ARGS)
38 {
39         char       *s = PG_GETARG_CSTRING(0);
40         NameData   *result;
41         int                     len;
42
43         result = (NameData *) palloc(NAMEDATALEN);
44         /* always keep it null-padded */
45         StrNCpy(NameStr(*result), s, NAMEDATALEN);
46         len = strlen(NameStr(*result));
47         while (len < NAMEDATALEN)
48         {
49                 *(NameStr(*result) + len) = '\0';
50                 len++;
51         }
52         PG_RETURN_NAME(result);
53 }
54
55 /*
56  *              nameout - converts internal representation to "..."
57  */
58 Datum
59 nameout(PG_FUNCTION_ARGS)
60 {
61         Name            s = PG_GETARG_NAME(0);
62
63         PG_RETURN_CSTRING(pstrdup(NameStr(*s)));
64 }
65
66
67 /*****************************************************************************
68  *       PUBLIC ROUTINES                                                                                                                 *
69  *****************************************************************************/
70
71 /*
72  *              nameeq  - returns 1 iff arguments are equal
73  *              namene  - returns 1 iff arguments are not equal
74  *
75  *              BUGS:
76  *                              Assumes that "xy\0\0a" should be equal to "xy\0b".
77  *                              If not, can do the comparison backwards for efficiency.
78  *
79  *              namelt  - returns 1 iff a < b
80  *              namele  - returns 1 iff a <= b
81  *              namegt  - returns 1 iff a < b
82  *              namege  - returns 1 iff a <= b
83  *
84  */
85 Datum
86 nameeq(PG_FUNCTION_ARGS)
87 {
88         Name            arg1 = PG_GETARG_NAME(0);
89         Name            arg2 = PG_GETARG_NAME(1);
90
91         PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) == 0);
92 }
93
94 Datum
95 namene(PG_FUNCTION_ARGS)
96 {
97         Name            arg1 = PG_GETARG_NAME(0);
98         Name            arg2 = PG_GETARG_NAME(1);
99
100         PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) != 0);
101 }
102
103 Datum
104 namelt(PG_FUNCTION_ARGS)
105 {
106         Name            arg1 = PG_GETARG_NAME(0);
107         Name            arg2 = PG_GETARG_NAME(1);
108
109         PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) < 0);
110 }
111
112 Datum
113 namele(PG_FUNCTION_ARGS)
114 {
115         Name            arg1 = PG_GETARG_NAME(0);
116         Name            arg2 = PG_GETARG_NAME(1);
117
118         PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) <= 0);
119 }
120
121 Datum
122 namegt(PG_FUNCTION_ARGS)
123 {
124         Name            arg1 = PG_GETARG_NAME(0);
125         Name            arg2 = PG_GETARG_NAME(1);
126
127         PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) > 0);
128 }
129
130 Datum
131 namege(PG_FUNCTION_ARGS)
132 {
133         Name            arg1 = PG_GETARG_NAME(0);
134         Name            arg2 = PG_GETARG_NAME(1);
135
136         PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) >= 0);
137 }
138
139
140 /* (see char.c for comparison/operation routines) */
141
142 int
143 namecpy(Name n1, Name n2)
144 {
145         if (!n1 || !n2)
146                 return -1;
147         strncpy(NameStr(*n1), NameStr(*n2), NAMEDATALEN);
148         return 0;
149 }
150
151 #ifdef NOT_USED
152 int
153 namecat(Name n1, Name n2)
154 {
155         return namestrcat(n1, NameStr(*n2));            /* n2 can't be any longer
156                                                                                                  * than n1 */
157 }
158 #endif
159
160 #ifdef NOT_USED
161 int
162 namecmp(Name n1, Name n2)
163 {
164         return strncmp(NameStr(*n1), NameStr(*n2), NAMEDATALEN);
165 }
166 #endif
167
168 int
169 namestrcpy(Name name, const char *str)
170 {
171         if (!name || !str)
172                 return -1;
173         StrNCpy(NameStr(*name), str, NAMEDATALEN);
174         return 0;
175 }
176
177 #ifdef NOT_USED
178 int
179 namestrcat(Name name, const char *str)
180 {
181         int                     i;
182         char       *p,
183                            *q;
184
185         if (!name || !str)
186                 return -1;
187         for (i = 0, p = NameStr(*name); i < NAMEDATALEN && *p; ++i, ++p)
188                 ;
189         for (q = str; i < NAMEDATALEN; ++i, ++p, ++q)
190         {
191                 *p = *q;
192                 if (!*q)
193                         break;
194         }
195         return 0;
196 }
197 #endif
198
199 int
200 namestrcmp(Name name, const char *str)
201 {
202         if (!name && !str)
203                 return 0;
204         if (!name)
205                 return -1;                              /* NULL < anything */
206         if (!str)
207                 return 1;                               /* NULL < anything */
208         return strncmp(NameStr(*name), str, NAMEDATALEN);
209 }
210
211
212 /* SQL-functions CURRENT_USER and SESSION_USER */
213 Datum
214 current_user(PG_FUNCTION_ARGS)
215 {
216         PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserName(GetUserId()))));
217 }
218
219 Datum
220 session_user(PG_FUNCTION_ARGS)
221 {
222         PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserName(GetSessionUserId()))));
223 }
224
225
226 /*****************************************************************************
227  *       PRIVATE ROUTINES                                                                                                                *
228  *****************************************************************************/
229
230 #ifdef NOT_USED
231 uint32
232 NameComputeLength(Name name)
233 {
234         char       *charP;
235         int                     length;
236
237         for (length = 0, charP = NameStr(*name);
238                  length < NAMEDATALEN && *charP != '\0';
239                  length++, charP++)
240                 ;
241         return (uint32) length;
242 }
243 #endif