1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* bool.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Functions for the built-in type "bool".
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2007-01-05 23:20:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2007-06-02 01:40:19 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.39 2007/06/01 23:40:18 neilc Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-11-03 07:54:38 +01:00
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2007-06-02 01:40:19 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2003-05-13 01:08:52 +02:00
|
|
|
#include "libpq/pqformat.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "utils/builtins.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* USER I/O ROUTINES *
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* boolin - converts "t" or "f" to 1 or 0
|
1997-10-09 07:06:12 +02:00
|
|
|
*
|
1997-10-17 07:38:32 +02:00
|
|
|
* Check explicitly for "true/false" and TRUE/FALSE, 1/0, YES/NO.
|
|
|
|
* Reject other values. - thomas 1997-10-05
|
|
|
|
*
|
|
|
|
* In the switch statement, check the most-used possibilities first.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
boolin(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2007-06-02 01:40:19 +02:00
|
|
|
const char *in_str = PG_GETARG_CSTRING(0);
|
|
|
|
const char *str;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip leading and trailing whitespace
|
|
|
|
*/
|
|
|
|
str = in_str;
|
|
|
|
while (isspace((unsigned char) *str))
|
|
|
|
str++;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
while (len > 0 && isspace((unsigned char) str[len - 1]))
|
|
|
|
len--;
|
|
|
|
|
|
|
|
switch (*str)
|
1998-02-26 05:46:47 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
case 't':
|
|
|
|
case 'T':
|
2007-06-02 01:40:19 +02:00
|
|
|
if (pg_strncasecmp(str, "true", len) == 0)
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(true);
|
1997-10-17 07:38:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
2007-06-02 01:40:19 +02:00
|
|
|
if (pg_strncasecmp(str, "false", len) == 0)
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(false);
|
1997-10-17 07:38:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'y':
|
|
|
|
case 'Y':
|
2007-06-02 01:40:19 +02:00
|
|
|
if (pg_strncasecmp(str, "yes", len) == 0)
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(true);
|
1997-10-25 07:09:58 +02:00
|
|
|
break;
|
|
|
|
|
1997-10-17 07:38:32 +02:00
|
|
|
case '1':
|
2007-06-02 01:40:19 +02:00
|
|
|
if (pg_strncasecmp(str, "1", len) == 0)
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(true);
|
1997-10-17 07:38:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
2007-06-02 01:40:19 +02:00
|
|
|
if (pg_strncasecmp(str, "no", len) == 0)
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(false);
|
1997-10-25 07:09:58 +02:00
|
|
|
break;
|
|
|
|
|
1997-10-17 07:38:32 +02:00
|
|
|
case '0':
|
2007-06-02 01:40:19 +02:00
|
|
|
if (pg_strncasecmp(str, "0", len) == 0)
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(false);
|
1997-10-17 07:38:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-07-27 06:53:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
2007-06-02 01:40:19 +02:00
|
|
|
errmsg("invalid input syntax for type boolean: \"%s\"", in_str)));
|
2000-06-05 09:29:25 +02:00
|
|
|
|
1997-11-07 07:30:20 +01:00
|
|
|
/* not reached */
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* boolout - converts 1 or 0 to "t" or "f"
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
boolout(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
bool b = PG_GETARG_BOOL(0);
|
1997-09-08 04:41:22 +02:00
|
|
|
char *result = (char *) palloc(2);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
result[0] = (b) ? 't' : 'f';
|
1997-09-07 07:04:48 +02:00
|
|
|
result[1] = '\0';
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-05-13 01:08:52 +02:00
|
|
|
/*
|
|
|
|
* boolrecv - converts external binary format to bool
|
|
|
|
*
|
|
|
|
* The external representation is one byte. Any nonzero value is taken
|
|
|
|
* as "true".
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
boolrecv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
|
|
|
int ext;
|
|
|
|
|
|
|
|
ext = pq_getmsgbyte(buf);
|
|
|
|
PG_RETURN_BOOL((ext != 0) ? true : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* boolsend - converts bool to binary format
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
boolsend(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
bool arg1 = PG_GETARG_BOOL(0);
|
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
pq_sendbyte(&buf, arg1 ? 1 : 0);
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
|
2007-06-02 01:40:19 +02:00
|
|
|
/*
|
|
|
|
* textbool - cast function for text => bool
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
textbool(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
Datum in_text = PG_GETARG_DATUM(0);
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = DatumGetCString(DirectFunctionCall1(textout, in_text));
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall1(boolin, CStringGetDatum(str)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* booltext - cast function for bool => text
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
booltext(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
bool arg1 = PG_GETARG_BOOL(0);
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
if (arg1)
|
|
|
|
str = "true";
|
|
|
|
else
|
|
|
|
str = "false";
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall1(textin, CStringGetDatum(str)));
|
|
|
|
}
|
|
|
|
|
1997-11-07 07:30:20 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* PUBLIC ROUTINES *
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
booleq(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
bool arg1 = PG_GETARG_BOOL(0);
|
|
|
|
bool arg2 = PG_GETARG_BOOL(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 == arg2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
boolne(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
bool arg1 = PG_GETARG_BOOL(0);
|
|
|
|
bool arg2 = PG_GETARG_BOOL(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 != arg2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
boollt(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
bool arg1 = PG_GETARG_BOOL(0);
|
|
|
|
bool arg2 = PG_GETARG_BOOL(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 < arg2);
|
1997-04-27 21:20:16 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
boolgt(PG_FUNCTION_ARGS)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
bool arg1 = PG_GETARG_BOOL(0);
|
|
|
|
bool arg2 = PG_GETARG_BOOL(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 > arg2);
|
1997-04-27 21:20:16 +02:00
|
|
|
}
|
1997-10-30 17:45:12 +01:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
boolle(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
bool arg1 = PG_GETARG_BOOL(0);
|
|
|
|
bool arg2 = PG_GETARG_BOOL(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 <= arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
boolge(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
bool arg1 = PG_GETARG_BOOL(0);
|
|
|
|
bool arg2 = PG_GETARG_BOOL(1);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(arg1 >= arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Per SQL92, istrue() and isfalse() should return false, not NULL,
|
|
|
|
* when presented a NULL input (since NULL is our implementation of
|
|
|
|
* UNKNOWN). Conversely isnottrue() and isnotfalse() should return true.
|
|
|
|
* Therefore, these routines are all declared not-strict in pg_proc
|
|
|
|
* and must do their own checking for null inputs.
|
|
|
|
*
|
|
|
|
* Note we don't need isunknown() and isnotunknown() functions, since
|
|
|
|
* nullvalue() and nonnullvalue() will serve.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Datum
|
|
|
|
istrue(PG_FUNCTION_ARGS)
|
2000-02-10 20:51:52 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
bool b;
|
|
|
|
|
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
|
|
|
|
b = PG_GETARG_BOOL(0);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(b);
|
2000-02-10 20:51:52 +01:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
isfalse(PG_FUNCTION_ARGS)
|
2000-02-10 20:51:52 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
bool b;
|
|
|
|
|
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
|
|
|
|
b = PG_GETARG_BOOL(0);
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
PG_RETURN_BOOL(!b);
|
2000-02-10 20:51:52 +01:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
isnottrue(PG_FUNCTION_ARGS)
|
1997-10-30 17:45:12 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
bool b;
|
1997-10-30 17:45:12 +01:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
|
|
|
|
b = PG_GETARG_BOOL(0);
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
PG_RETURN_BOOL(!b);
|
2000-06-05 09:29:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
isnotfalse(PG_FUNCTION_ARGS)
|
1997-10-30 17:45:12 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
bool b;
|
|
|
|
|
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
|
|
|
|
b = PG_GETARG_BOOL(0);
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(b);
|
|
|
|
}
|
2004-05-26 17:26:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* boolean-and and boolean-or aggregates.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* function for standard EVERY aggregate implementation conforming to SQL 2003.
|
|
|
|
* must be strict. It is also named bool_and for homogeneity.
|
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
Datum
|
|
|
|
booland_statefunc(PG_FUNCTION_ARGS)
|
2004-05-26 17:26:28 +02:00
|
|
|
{
|
|
|
|
PG_RETURN_BOOL(PG_GETARG_BOOL(0) && PG_GETARG_BOOL(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* function for standard ANY/SOME aggregate conforming to SQL 2003.
|
|
|
|
* must be strict. The name of the aggregate is bool_or. See the doc.
|
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
Datum
|
|
|
|
boolor_statefunc(PG_FUNCTION_ARGS)
|
2004-05-26 17:26:28 +02:00
|
|
|
{
|
|
|
|
PG_RETURN_BOOL(PG_GETARG_BOOL(0) || PG_GETARG_BOOL(1));
|
|
|
|
}
|