PostgreSQL Source Code  git master
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

◆ BoolAggState

typedef struct BoolAggState BoolAggState

Function Documentation

◆ bool_accum()

Datum bool_accum ( PG_FUNCTION_ARGS  )

Definition at line 332 of file bool.c.

333 {
335 
336  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
337 
338  /* Create the state data on first call */
339  if (state == NULL)
340  state = makeBoolAggState(fcinfo);
341 
342  if (!PG_ARGISNULL(1))
343  {
344  state->aggcount++;
345  if (PG_GETARG_BOOL(1))
346  state->aggtrue++;
347  }
348 
350 }
static BoolAggState * makeBoolAggState(FunctionCallInfo fcinfo)
Definition: bool.c:315
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
Definition: regguts.h:323

References makeBoolAggState(), PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ bool_accum_inv()

Datum bool_accum_inv ( PG_FUNCTION_ARGS  )

Definition at line 353 of file bool.c.

354 {
356 
357  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
358 
359  /* bool_accum should have created the state data */
360  if (state == NULL)
361  elog(ERROR, "bool_accum_inv called with NULL state");
362 
363  if (!PG_ARGISNULL(1))
364  {
365  state->aggcount--;
366  if (PG_GETARG_BOOL(1))
367  state->aggtrue--;
368  }
369 
371 }
#define ERROR
Definition: elog.h:39

References elog(), ERROR, PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ bool_alltrue()

Datum bool_alltrue ( PG_FUNCTION_ARGS  )

Definition at line 374 of file bool.c.

375 {
377 
378  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
379 
380  /* if there were no non-null values, return NULL */
381  if (state == NULL || state->aggcount == 0)
382  PG_RETURN_NULL();
383 
384  /* true if all non-null values are true */
385  PG_RETURN_BOOL(state->aggtrue == state->aggcount);
386 }
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_BOOL, and PG_RETURN_NULL.

◆ bool_anytrue()

Datum bool_anytrue ( PG_FUNCTION_ARGS  )

Definition at line 389 of file bool.c.

390 {
392 
393  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
394 
395  /* if there were no non-null values, return NULL */
396  if (state == NULL || state->aggcount == 0)
397  PG_RETURN_NULL();
398 
399  /* true if any non-null value is true */
400  PG_RETURN_BOOL(state->aggtrue > 0);
401 }

References PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_BOOL, and PG_RETURN_NULL.

◆ booland_statefunc()

Datum booland_statefunc ( PG_FUNCTION_ARGS  )

Definition at line 291 of file bool.c.

292 {
294 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ booleq()

Datum booleq ( PG_FUNCTION_ARGS  )

Definition at line 227 of file bool.c.

228 {
229  bool arg1 = PG_GETARG_BOOL(0);
230  bool arg2 = PG_GETARG_BOOL(1);
231 
232  PG_RETURN_BOOL(arg1 == arg2);
233 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolge()

Datum boolge ( PG_FUNCTION_ARGS  )

Definition at line 272 of file bool.c.

273 {
274  bool arg1 = PG_GETARG_BOOL(0);
275  bool arg2 = PG_GETARG_BOOL(1);
276 
277  PG_RETURN_BOOL(arg1 >= arg2);
278 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolgt()

Datum boolgt ( PG_FUNCTION_ARGS  )

Definition at line 254 of file bool.c.

255 {
256  bool arg1 = PG_GETARG_BOOL(0);
257  bool arg2 = PG_GETARG_BOOL(1);
258 
259  PG_RETURN_BOOL(arg1 > arg2);
260 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolin()

Datum boolin ( PG_FUNCTION_ARGS  )

Definition at line 130 of file bool.c.

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  ereturn(fcinfo->context, (Datum) 0,
152  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
153  errmsg("invalid input syntax for type %s: \"%s\"",
154  "boolean", in_str)));
155 }
bool parse_bool_with_len(const char *value, size_t len, bool *result)
Definition: bool.c:36
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
const void size_t len
uintptr_t Datum
Definition: postgres.h:64

References ereturn, errcode(), errmsg(), len, parse_bool_with_len(), PG_GETARG_CSTRING, PG_RETURN_BOOL, and generate_unaccent_rules::str.

◆ boolle()

Datum boolle ( PG_FUNCTION_ARGS  )

Definition at line 263 of file bool.c.

264 {
265  bool arg1 = PG_GETARG_BOOL(0);
266  bool arg2 = PG_GETARG_BOOL(1);
267 
268  PG_RETURN_BOOL(arg1 <= arg2);
269 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boollt()

Datum boollt ( PG_FUNCTION_ARGS  )

Definition at line 245 of file bool.c.

246 {
247  bool arg1 = PG_GETARG_BOOL(0);
248  bool arg2 = PG_GETARG_BOOL(1);
249 
250  PG_RETURN_BOOL(arg1 < arg2);
251 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolne()

Datum boolne ( PG_FUNCTION_ARGS  )

Definition at line 236 of file bool.c.

237 {
238  bool arg1 = PG_GETARG_BOOL(0);
239  bool arg2 = PG_GETARG_BOOL(1);
240 
241  PG_RETURN_BOOL(arg1 != arg2);
242 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolor_statefunc()

Datum boolor_statefunc ( PG_FUNCTION_ARGS  )

Definition at line 303 of file bool.c.

304 {
306 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolout()

Datum boolout ( PG_FUNCTION_ARGS  )

Definition at line 161 of file bool.c.

162 {
163  bool b = PG_GETARG_BOOL(0);
164  char *result = (char *) palloc(2);
165 
166  result[0] = (b) ? 't' : 'f';
167  result[1] = '\0';
168  PG_RETURN_CSTRING(result);
169 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
int b
Definition: isn.c:70
void * palloc(Size size)
Definition: mcxt.c:1226

References b, palloc(), PG_GETARG_BOOL, and PG_RETURN_CSTRING.

◆ boolrecv()

Datum boolrecv ( PG_FUNCTION_ARGS  )

Definition at line 178 of file bool.c.

179 {
181  int ext;
182 
183  ext = pq_getmsgbyte(buf);
184  PG_RETURN_BOOL(ext != 0);
185 }
static char * buf
Definition: pg_test_fsync.c:67
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:402
StringInfoData * StringInfo
Definition: stringinfo.h:44

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

◆ boolsend()

Datum boolsend ( PG_FUNCTION_ARGS  )

Definition at line 191 of file bool.c.

192 {
193  bool arg1 = PG_GETARG_BOOL(0);
195 
197  pq_sendbyte(&buf, arg1 ? 1 : 0);
199 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:349
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161

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

◆ booltext()

Datum booltext ( PG_FUNCTION_ARGS  )

Definition at line 208 of file bool.c.

209 {
210  bool arg1 = PG_GETARG_BOOL(0);
211  const char *str;
212 
213  if (arg1)
214  str = "true";
215  else
216  str = "false";
217 
219 }
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
text * cstring_to_text(const char *s)
Definition: varlena.c:182

References cstring_to_text(), PG_GETARG_BOOL, PG_RETURN_TEXT_P, and generate_unaccent_rules::str.

◆ makeBoolAggState()

static BoolAggState* makeBoolAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 315 of file bool.c.

316 {
318  MemoryContext agg_context;
319 
320  if (!AggCheckCallContext(fcinfo, &agg_context))
321  elog(ERROR, "aggregate function called in non-aggregate context");
322 
323  state = (BoolAggState *) MemoryContextAlloc(agg_context,
324  sizeof(BoolAggState));
325  state->aggcount = 0;
326  state->aggtrue = 0;
327 
328  return state;
329 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4522

References AggCheckCallContext(), elog(), ERROR, and MemoryContextAlloc().

Referenced by bool_accum().

◆ parse_bool()

bool parse_bool ( const char *  value,
bool result 
)

Definition at line 30 of file bool.c.

31 {
32  return parse_bool_with_len(value, strlen(value), result);
33 }
static struct @147 value

References parse_bool_with_len(), and value.

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

◆ parse_bool_with_len()

bool parse_bool_with_len ( const char *  value,
size_t  len,
bool result 
)

Definition at line 36 of file bool.c.

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

References len, pg_strncasecmp(), and value.

Referenced by boolin(), and parse_bool().