1 /*-------------------------------------------------------------------------
4 * Functions for the built-in type "bool".
6 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.35 2004/08/29 05:06:49 momjian Exp $
13 *-------------------------------------------------------------------------
18 #include "libpq/pqformat.h"
19 #include "utils/builtins.h"
21 /*****************************************************************************
23 *****************************************************************************/
26 * boolin - converts "t" or "f" to 1 or 0
28 * Check explicitly for "true/false" and TRUE/FALSE, 1/0, YES/NO.
29 * Reject other values. - thomas 1997-10-05
31 * In the switch statement, check the most-used possibilities first.
34 boolin(PG_FUNCTION_ARGS)
36 char *b = PG_GETARG_CSTRING(0);
42 if (pg_strncasecmp(b, "true", strlen(b)) == 0)
48 if (pg_strncasecmp(b, "false", strlen(b)) == 0)
49 PG_RETURN_BOOL(false);
54 if (pg_strncasecmp(b, "yes", strlen(b)) == 0)
59 if (pg_strncasecmp(b, "1", strlen(b)) == 0)
65 if (pg_strncasecmp(b, "no", strlen(b)) == 0)
66 PG_RETURN_BOOL(false);
70 if (pg_strncasecmp(b, "0", strlen(b)) == 0)
71 PG_RETURN_BOOL(false);
79 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
80 errmsg("invalid input syntax for type boolean: \"%s\"", b)));
83 PG_RETURN_BOOL(false);
87 * boolout - converts 1 or 0 to "t" or "f"
90 boolout(PG_FUNCTION_ARGS)
92 bool b = PG_GETARG_BOOL(0);
93 char *result = (char *) palloc(2);
95 result[0] = (b) ? 't' : 'f';
97 PG_RETURN_CSTRING(result);
101 * boolrecv - converts external binary format to bool
103 * The external representation is one byte. Any nonzero value is taken
107 boolrecv(PG_FUNCTION_ARGS)
109 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
112 ext = pq_getmsgbyte(buf);
113 PG_RETURN_BOOL((ext != 0) ? true : false);
117 * boolsend - converts bool to binary format
120 boolsend(PG_FUNCTION_ARGS)
122 bool arg1 = PG_GETARG_BOOL(0);
125 pq_begintypsend(&buf);
126 pq_sendbyte(&buf, arg1 ? 1 : 0);
127 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
131 /*****************************************************************************
133 *****************************************************************************/
136 booleq(PG_FUNCTION_ARGS)
138 bool arg1 = PG_GETARG_BOOL(0);
139 bool arg2 = PG_GETARG_BOOL(1);
141 PG_RETURN_BOOL(arg1 == arg2);
145 boolne(PG_FUNCTION_ARGS)
147 bool arg1 = PG_GETARG_BOOL(0);
148 bool arg2 = PG_GETARG_BOOL(1);
150 PG_RETURN_BOOL(arg1 != arg2);
154 boollt(PG_FUNCTION_ARGS)
156 bool arg1 = PG_GETARG_BOOL(0);
157 bool arg2 = PG_GETARG_BOOL(1);
159 PG_RETURN_BOOL(arg1 < arg2);
163 boolgt(PG_FUNCTION_ARGS)
165 bool arg1 = PG_GETARG_BOOL(0);
166 bool arg2 = PG_GETARG_BOOL(1);
168 PG_RETURN_BOOL(arg1 > arg2);
172 boolle(PG_FUNCTION_ARGS)
174 bool arg1 = PG_GETARG_BOOL(0);
175 bool arg2 = PG_GETARG_BOOL(1);
177 PG_RETURN_BOOL(arg1 <= arg2);
181 boolge(PG_FUNCTION_ARGS)
183 bool arg1 = PG_GETARG_BOOL(0);
184 bool arg2 = PG_GETARG_BOOL(1);
186 PG_RETURN_BOOL(arg1 >= arg2);
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.
196 * Note we don't need isunknown() and isnotunknown() functions, since
197 * nullvalue() and nonnullvalue() will serve.
201 istrue(PG_FUNCTION_ARGS)
206 PG_RETURN_BOOL(false);
208 b = PG_GETARG_BOOL(0);
214 isfalse(PG_FUNCTION_ARGS)
219 PG_RETURN_BOOL(false);
221 b = PG_GETARG_BOOL(0);
227 isnottrue(PG_FUNCTION_ARGS)
232 PG_RETURN_BOOL(true);
234 b = PG_GETARG_BOOL(0);
240 isnotfalse(PG_FUNCTION_ARGS)
245 PG_RETURN_BOOL(true);
247 b = PG_GETARG_BOOL(0);
253 * boolean-and and boolean-or aggregates.
256 /* function for standard EVERY aggregate implementation conforming to SQL 2003.
257 * must be strict. It is also named bool_and for homogeneity.
260 booland_statefunc(PG_FUNCTION_ARGS)
262 PG_RETURN_BOOL(PG_GETARG_BOOL(0) && PG_GETARG_BOOL(1));
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.
269 boolor_statefunc(PG_FUNCTION_ARGS)
271 PG_RETURN_BOOL(PG_GETARG_BOOL(0) || PG_GETARG_BOOL(1));