]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/bool.c
Pgindent run for 8.0.
[postgresql] / src / backend / utils / adt / bool.c
1 /*-------------------------------------------------------------------------
2  *
3  * bool.c
4  *        Functions for the built-in type "bool".
5  *
6  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.35 2004/08/29 05:06:49 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include "libpq/pqformat.h"
19 #include "utils/builtins.h"
20
21 /*****************************************************************************
22  *       USER I/O ROUTINES                                                                                                               *
23  *****************************************************************************/
24
25 /*
26  *              boolin                  - converts "t" or "f" to 1 or 0
27  *
28  * Check explicitly for "true/false" and TRUE/FALSE, 1/0, YES/NO.
29  * Reject other values. - thomas 1997-10-05
30  *
31  * In the switch statement, check the most-used possibilities first.
32  */
33 Datum
34 boolin(PG_FUNCTION_ARGS)
35 {
36         char       *b = PG_GETARG_CSTRING(0);
37
38         switch (*b)
39         {
40                 case 't':
41                 case 'T':
42                         if (pg_strncasecmp(b, "true", strlen(b)) == 0)
43                                 PG_RETURN_BOOL(true);
44                         break;
45
46                 case 'f':
47                 case 'F':
48                         if (pg_strncasecmp(b, "false", strlen(b)) == 0)
49                                 PG_RETURN_BOOL(false);
50                         break;
51
52                 case 'y':
53                 case 'Y':
54                         if (pg_strncasecmp(b, "yes", strlen(b)) == 0)
55                                 PG_RETURN_BOOL(true);
56                         break;
57
58                 case '1':
59                         if (pg_strncasecmp(b, "1", strlen(b)) == 0)
60                                 PG_RETURN_BOOL(true);
61                         break;
62
63                 case 'n':
64                 case 'N':
65                         if (pg_strncasecmp(b, "no", strlen(b)) == 0)
66                                 PG_RETURN_BOOL(false);
67                         break;
68
69                 case '0':
70                         if (pg_strncasecmp(b, "0", strlen(b)) == 0)
71                                 PG_RETURN_BOOL(false);
72                         break;
73
74                 default:
75                         break;
76         }
77
78         ereport(ERROR,
79                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
80                          errmsg("invalid input syntax for type boolean: \"%s\"", b)));
81
82         /* not reached */
83         PG_RETURN_BOOL(false);
84 }
85
86 /*
87  *              boolout                 - converts 1 or 0 to "t" or "f"
88  */
89 Datum
90 boolout(PG_FUNCTION_ARGS)
91 {
92         bool            b = PG_GETARG_BOOL(0);
93         char       *result = (char *) palloc(2);
94
95         result[0] = (b) ? 't' : 'f';
96         result[1] = '\0';
97         PG_RETURN_CSTRING(result);
98 }
99
100 /*
101  *              boolrecv                        - converts external binary format to bool
102  *
103  * The external representation is one byte.  Any nonzero value is taken
104  * as "true".
105  */
106 Datum
107 boolrecv(PG_FUNCTION_ARGS)
108 {
109         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
110         int                     ext;
111
112         ext = pq_getmsgbyte(buf);
113         PG_RETURN_BOOL((ext != 0) ? true : false);
114 }
115
116 /*
117  *              boolsend                        - converts bool to binary format
118  */
119 Datum
120 boolsend(PG_FUNCTION_ARGS)
121 {
122         bool            arg1 = PG_GETARG_BOOL(0);
123         StringInfoData buf;
124
125         pq_begintypsend(&buf);
126         pq_sendbyte(&buf, arg1 ? 1 : 0);
127         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
128 }
129
130
131 /*****************************************************************************
132  *       PUBLIC ROUTINES                                                                                                                 *
133  *****************************************************************************/
134
135 Datum
136 booleq(PG_FUNCTION_ARGS)
137 {
138         bool            arg1 = PG_GETARG_BOOL(0);
139         bool            arg2 = PG_GETARG_BOOL(1);
140
141         PG_RETURN_BOOL(arg1 == arg2);
142 }
143
144 Datum
145 boolne(PG_FUNCTION_ARGS)
146 {
147         bool            arg1 = PG_GETARG_BOOL(0);
148         bool            arg2 = PG_GETARG_BOOL(1);
149
150         PG_RETURN_BOOL(arg1 != arg2);
151 }
152
153 Datum
154 boollt(PG_FUNCTION_ARGS)
155 {
156         bool            arg1 = PG_GETARG_BOOL(0);
157         bool            arg2 = PG_GETARG_BOOL(1);
158
159         PG_RETURN_BOOL(arg1 < arg2);
160 }
161
162 Datum
163 boolgt(PG_FUNCTION_ARGS)
164 {
165         bool            arg1 = PG_GETARG_BOOL(0);
166         bool            arg2 = PG_GETARG_BOOL(1);
167
168         PG_RETURN_BOOL(arg1 > arg2);
169 }
170
171 Datum
172 boolle(PG_FUNCTION_ARGS)
173 {
174         bool            arg1 = PG_GETARG_BOOL(0);
175         bool            arg2 = PG_GETARG_BOOL(1);
176
177         PG_RETURN_BOOL(arg1 <= arg2);
178 }
179
180 Datum
181 boolge(PG_FUNCTION_ARGS)
182 {
183         bool            arg1 = PG_GETARG_BOOL(0);
184         bool            arg2 = PG_GETARG_BOOL(1);
185
186         PG_RETURN_BOOL(arg1 >= arg2);
187 }
188
189 /*
190  * Per SQL92, istrue() and isfalse() should return false, not NULL,
191  * when presented a NULL input (since NULL is our implementation of
192  * UNKNOWN).  Conversely isnottrue() and isnotfalse() should return true.
193  * Therefore, these routines are all declared not-strict in pg_proc
194  * and must do their own checking for null inputs.
195  *
196  * Note we don't need isunknown() and isnotunknown() functions, since
197  * nullvalue() and nonnullvalue() will serve.
198  */
199
200 Datum
201 istrue(PG_FUNCTION_ARGS)
202 {
203         bool            b;
204
205         if (PG_ARGISNULL(0))
206                 PG_RETURN_BOOL(false);
207
208         b = PG_GETARG_BOOL(0);
209
210         PG_RETURN_BOOL(b);
211 }
212
213 Datum
214 isfalse(PG_FUNCTION_ARGS)
215 {
216         bool            b;
217
218         if (PG_ARGISNULL(0))
219                 PG_RETURN_BOOL(false);
220
221         b = PG_GETARG_BOOL(0);
222
223         PG_RETURN_BOOL(!b);
224 }
225
226 Datum
227 isnottrue(PG_FUNCTION_ARGS)
228 {
229         bool            b;
230
231         if (PG_ARGISNULL(0))
232                 PG_RETURN_BOOL(true);
233
234         b = PG_GETARG_BOOL(0);
235
236         PG_RETURN_BOOL(!b);
237 }
238
239 Datum
240 isnotfalse(PG_FUNCTION_ARGS)
241 {
242         bool            b;
243
244         if (PG_ARGISNULL(0))
245                 PG_RETURN_BOOL(true);
246
247         b = PG_GETARG_BOOL(0);
248
249         PG_RETURN_BOOL(b);
250 }
251
252 /*
253  * boolean-and and boolean-or aggregates.
254  */
255
256 /* function for standard EVERY aggregate implementation conforming to SQL 2003.
257  * must be strict. It is also named bool_and for homogeneity.
258  */
259 Datum
260 booland_statefunc(PG_FUNCTION_ARGS)
261 {
262         PG_RETURN_BOOL(PG_GETARG_BOOL(0) && PG_GETARG_BOOL(1));
263 }
264
265 /* function for standard ANY/SOME aggregate conforming to SQL 2003.
266  * must be strict. The name of the aggregate is bool_or. See the doc.
267  */
268 Datum
269 boolor_statefunc(PG_FUNCTION_ARGS)
270 {
271         PG_RETURN_BOOL(PG_GETARG_BOOL(0) || PG_GETARG_BOOL(1));
272 }