PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
bool.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "libpq/pqformat.h"
#include "utils/builtins.h"
Include dependency graph for bool.c:

Go to the source code of this file.

Data Structures

struct  BoolAggState
 

Typedefs

typedef struct BoolAggState BoolAggState
 

Functions

bool parse_bool (const char *value, bool *result)
 
bool parse_bool_with_len (const char *value, size_t len, bool *result)
 
Datum boolin (PG_FUNCTION_ARGS)
 
Datum boolout (PG_FUNCTION_ARGS)
 
Datum boolrecv (PG_FUNCTION_ARGS)
 
Datum boolsend (PG_FUNCTION_ARGS)
 
Datum booltext (PG_FUNCTION_ARGS)
 
Datum booleq (PG_FUNCTION_ARGS)
 
Datum boolne (PG_FUNCTION_ARGS)
 
Datum boollt (PG_FUNCTION_ARGS)
 
Datum boolgt (PG_FUNCTION_ARGS)
 
Datum boolle (PG_FUNCTION_ARGS)
 
Datum boolge (PG_FUNCTION_ARGS)
 
Datum booland_statefunc (PG_FUNCTION_ARGS)
 
Datum boolor_statefunc (PG_FUNCTION_ARGS)
 
static BoolAggStatemakeBoolAggState (FunctionCallInfo fcinfo)
 
Datum bool_accum (PG_FUNCTION_ARGS)
 
Datum bool_accum_inv (PG_FUNCTION_ARGS)
 
Datum bool_alltrue (PG_FUNCTION_ARGS)
 
Datum bool_anytrue (PG_FUNCTION_ARGS)
 

Typedef Documentation

Function Documentation

Datum bool_accum ( PG_FUNCTION_ARGS  )

Definition at line 335 of file bool.c.

References BoolAggState::aggcount, BoolAggState::aggtrue, makeBoolAggState(), PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_POINTER, and PG_RETURN_POINTER.

336 {
338 
339  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
340 
341  /* Create the state data on first call */
342  if (state == NULL)
343  state = makeBoolAggState(fcinfo);
344 
345  if (!PG_ARGISNULL(1))
346  {
347  state->aggcount++;
348  if (PG_GETARG_BOOL(1))
349  state->aggtrue++;
350  }
351 
352  PG_RETURN_POINTER(state);
353 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
static BoolAggState * makeBoolAggState(FunctionCallInfo fcinfo)
Definition: bool.c:318
int64 aggcount
Definition: bool.c:313
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
Definition: regguts.h:298
int64 aggtrue
Definition: bool.c:314
Datum bool_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 356 of file bool.c.

References BoolAggState::aggcount, BoolAggState::aggtrue, elog, ERROR, PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_POINTER, and PG_RETURN_POINTER.

357 {
359 
360  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
361 
362  /* bool_accum should have created the state data */
363  if (state == NULL)
364  elog(ERROR, "bool_accum_inv called with NULL state");
365 
366  if (!PG_ARGISNULL(1))
367  {
368  state->aggcount--;
369  if (PG_GETARG_BOOL(1))
370  state->aggtrue--;
371  }
372 
373  PG_RETURN_POINTER(state);
374 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
int64 aggcount
Definition: bool.c:313
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define ERROR
Definition: elog.h:43
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
Definition: regguts.h:298
#define elog
Definition: elog.h:219
int64 aggtrue
Definition: bool.c:314
Datum bool_alltrue ( PG_FUNCTION_ARGS  )

Definition at line 377 of file bool.c.

References BoolAggState::aggcount, BoolAggState::aggtrue, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_BOOL, and PG_RETURN_NULL.

378 {
380 
381  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
382 
383  /* if there were no non-null values, return NULL */
384  if (state == NULL || state->aggcount == 0)
385  PG_RETURN_NULL();
386 
387  /* true if all non-null values are true */
388  PG_RETURN_BOOL(state->aggtrue == state->aggcount);
389 }
int64 aggcount
Definition: bool.c:313
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
Definition: regguts.h:298
int64 aggtrue
Definition: bool.c:314
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum bool_anytrue ( PG_FUNCTION_ARGS  )

Definition at line 392 of file bool.c.

References BoolAggState::aggcount, BoolAggState::aggtrue, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_BOOL, and PG_RETURN_NULL.

393 {
395 
396  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
397 
398  /* if there were no non-null values, return NULL */
399  if (state == NULL || state->aggcount == 0)
400  PG_RETURN_NULL();
401 
402  /* true if any non-null value is true */
403  PG_RETURN_BOOL(state->aggtrue > 0);
404 }
int64 aggcount
Definition: bool.c:313
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
Definition: regguts.h:298
int64 aggtrue
Definition: bool.c:314
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum booland_statefunc ( PG_FUNCTION_ARGS  )

Definition at line 294 of file bool.c.

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

295 {
297 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum booleq ( PG_FUNCTION_ARGS  )

Definition at line 230 of file bool.c.

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

231 {
232  bool arg1 = PG_GETARG_BOOL(0);
233  bool arg2 = PG_GETARG_BOOL(1);
234 
235  PG_RETURN_BOOL(arg1 == arg2);
236 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum boolge ( PG_FUNCTION_ARGS  )

Definition at line 275 of file bool.c.

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

276 {
277  bool arg1 = PG_GETARG_BOOL(0);
278  bool arg2 = PG_GETARG_BOOL(1);
279 
280  PG_RETURN_BOOL(arg1 >= arg2);
281 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum boolgt ( PG_FUNCTION_ARGS  )

Definition at line 257 of file bool.c.

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

258 {
259  bool arg1 = PG_GETARG_BOOL(0);
260  bool arg2 = PG_GETARG_BOOL(1);
261 
262  PG_RETURN_BOOL(arg1 > arg2);
263 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum boolin ( PG_FUNCTION_ARGS  )

Definition at line 130 of file bool.c.

References ereport, errcode(), errmsg(), ERROR, parse_bool_with_len(), PG_GETARG_CSTRING, and PG_RETURN_BOOL.

131 {
132  const char *in_str = PG_GETARG_CSTRING(0);
133  const char *str;
134  size_t len;
135  bool result;
136 
137  /*
138  * Skip leading and trailing whitespace
139  */
140  str = in_str;
141  while (isspace((unsigned char) *str))
142  str++;
143 
144  len = strlen(str);
145  while (len > 0 && isspace((unsigned char) str[len - 1]))
146  len--;
147 
148  if (parse_bool_with_len(str, len, &result))
149  PG_RETURN_BOOL(result);
150 
151  ereport(ERROR,
152  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
153  errmsg("invalid input syntax for type %s: \"%s\"",
154  "boolean", in_str)));
155 
156  /* not reached */
157  PG_RETURN_BOOL(false);
158 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
bool parse_bool_with_len(const char *value, size_t len, bool *result)
Definition: bool.c:36
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
Datum boolle ( PG_FUNCTION_ARGS  )

Definition at line 266 of file bool.c.

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

267 {
268  bool arg1 = PG_GETARG_BOOL(0);
269  bool arg2 = PG_GETARG_BOOL(1);
270 
271  PG_RETURN_BOOL(arg1 <= arg2);
272 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum boollt ( PG_FUNCTION_ARGS  )

Definition at line 248 of file bool.c.

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

249 {
250  bool arg1 = PG_GETARG_BOOL(0);
251  bool arg2 = PG_GETARG_BOOL(1);
252 
253  PG_RETURN_BOOL(arg1 < arg2);
254 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum boolne ( PG_FUNCTION_ARGS  )

Definition at line 239 of file bool.c.

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

240 {
241  bool arg1 = PG_GETARG_BOOL(0);
242  bool arg2 = PG_GETARG_BOOL(1);
243 
244  PG_RETURN_BOOL(arg1 != arg2);
245 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum boolor_statefunc ( PG_FUNCTION_ARGS  )

Definition at line 306 of file bool.c.

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

307 {
309 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum boolout ( PG_FUNCTION_ARGS  )

Definition at line 164 of file bool.c.

References palloc(), PG_GETARG_BOOL, and PG_RETURN_CSTRING.

165 {
166  bool b = PG_GETARG_BOOL(0);
167  char *result = (char *) palloc(2);
168 
169  result[0] = (b) ? 't' : 'f';
170  result[1] = '\0';
171  PG_RETURN_CSTRING(result);
172 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
void * palloc(Size size)
Definition: mcxt.c:848
Datum boolrecv ( PG_FUNCTION_ARGS  )

Definition at line 181 of file bool.c.

References buf, PG_GETARG_POINTER, PG_RETURN_BOOL, and pq_getmsgbyte().

182 {
184  int ext;
185 
186  ext = pq_getmsgbyte(buf);
187  PG_RETURN_BOOL((ext != 0) ? true : false);
188 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static char * buf
Definition: pg_test_fsync.c:67
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
Datum boolsend ( PG_FUNCTION_ARGS  )

Definition at line 194 of file bool.c.

References buf, PG_GETARG_BOOL, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendbyte().

195 {
196  bool arg1 = PG_GETARG_BOOL(0);
198 
199  pq_begintypsend(&buf);
200  pq_sendbyte(&buf, arg1 ? 1 : 0);
202 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
static void pq_sendbyte(StringInfo buf, int8 byt)
Definition: pqformat.h:164
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static char * buf
Definition: pg_test_fsync.c:67
Datum booltext ( PG_FUNCTION_ARGS  )

Definition at line 211 of file bool.c.

References cstring_to_text(), PG_GETARG_BOOL, and PG_RETURN_TEXT_P.

212 {
213  bool arg1 = PG_GETARG_BOOL(0);
214  const char *str;
215 
216  if (arg1)
217  str = "true";
218  else
219  str = "false";
220 
222 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
static BoolAggState* makeBoolAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 318 of file bool.c.

References AggCheckCallContext(), BoolAggState::aggcount, BoolAggState::aggtrue, elog, ERROR, and MemoryContextAlloc().

Referenced by bool_accum().

319 {
321  MemoryContext agg_context;
322 
323  if (!AggCheckCallContext(fcinfo, &agg_context))
324  elog(ERROR, "aggregate function called in non-aggregate context");
325 
326  state = (BoolAggState *) MemoryContextAlloc(agg_context,
327  sizeof(BoolAggState));
328  state->aggcount = 0;
329  state->aggtrue = 0;
330 
331  return state;
332 }
int64 aggcount
Definition: bool.c:313
#define ERROR
Definition: elog.h:43
Definition: regguts.h:298
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4116
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
#define elog
Definition: elog.h:219
int64 aggtrue
Definition: bool.c:314
bool parse_bool ( const char *  value,
bool result 
)

Definition at line 30 of file bool.c.

References parse_bool_with_len().

Referenced by parse_and_validate_value(), parse_extension_control_file(), parse_one_reloption(), pg_decode_startup(), ProcessStartupPacket(), and readRecoveryCommandFile().

31 {
32  return parse_bool_with_len(value, strlen(value), result);
33 }
static struct @121 value
bool parse_bool_with_len(const char *value, size_t len, bool *result)
Definition: bool.c:36
bool parse_bool_with_len ( const char *  value,
size_t  len,
bool result 
)

Definition at line 36 of file bool.c.

References pg_strncasecmp().

Referenced by boolin(), and parse_bool().

37 {
38  switch (*value)
39  {
40  case 't':
41  case 'T':
42  if (pg_strncasecmp(value, "true", len) == 0)
43  {
44  if (result)
45  *result = true;
46  return true;
47  }
48  break;
49  case 'f':
50  case 'F':
51  if (pg_strncasecmp(value, "false", len) == 0)
52  {
53  if (result)
54  *result = false;
55  return true;
56  }
57  break;
58  case 'y':
59  case 'Y':
60  if (pg_strncasecmp(value, "yes", len) == 0)
61  {
62  if (result)
63  *result = true;
64  return true;
65  }
66  break;
67  case 'n':
68  case 'N':
69  if (pg_strncasecmp(value, "no", len) == 0)
70  {
71  if (result)
72  *result = false;
73  return true;
74  }
75  break;
76  case 'o':
77  case 'O':
78  /* 'o' is not unique enough */
79  if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
80  {
81  if (result)
82  *result = true;
83  return true;
84  }
85  else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
86  {
87  if (result)
88  *result = false;
89  return true;
90  }
91  break;
92  case '1':
93  if (len == 1)
94  {
95  if (result)
96  *result = true;
97  return true;
98  }
99  break;
100  case '0':
101  if (len == 1)
102  {
103  if (result)
104  *result = false;
105  return true;
106  }
107  break;
108  default:
109  break;
110  }
111 
112  if (result)
113  *result = false; /* suppress compiler warning */
114  return false;
115 }
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
static struct @121 value