PostgreSQL Source Code  git master
bool.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "common/hashfn.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 hashbool (PG_FUNCTION_ARGS)
 
Datum hashboolextended (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 341 of file bool.c.

342 {
344 
345  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
346 
347  /* Create the state data on first call */
348  if (state == NULL)
349  state = makeBoolAggState(fcinfo);
350 
351  if (!PG_ARGISNULL(1))
352  {
353  state->aggcount++;
354  if (PG_GETARG_BOOL(1))
355  state->aggtrue++;
356  }
357 
359 }
static BoolAggState * makeBoolAggState(FunctionCallInfo fcinfo)
Definition: bool.c:324
#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 362 of file bool.c.

363 {
365 
366  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
367 
368  /* bool_accum should have created the state data */
369  if (state == NULL)
370  elog(ERROR, "bool_accum_inv called with NULL state");
371 
372  if (!PG_ARGISNULL(1))
373  {
374  state->aggcount--;
375  if (PG_GETARG_BOOL(1))
376  state->aggtrue--;
377  }
378 
380 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225

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 383 of file bool.c.

384 {
386 
387  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
388 
389  /* if there were no non-null values, return NULL */
390  if (state == NULL || state->aggcount == 0)
391  PG_RETURN_NULL();
392 
393  /* true if all non-null values are true */
394  PG_RETURN_BOOL(state->aggtrue == state->aggcount);
395 }
#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 398 of file bool.c.

399 {
401 
402  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
403 
404  /* if there were no non-null values, return NULL */
405  if (state == NULL || state->aggcount == 0)
406  PG_RETURN_NULL();
407 
408  /* true if any non-null value is true */
409  PG_RETURN_BOOL(state->aggtrue > 0);
410 }

References PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_BOOL, and PG_RETURN_NULL.

◆ booland_statefunc()

Datum booland_statefunc ( PG_FUNCTION_ARGS  )

Definition at line 300 of file bool.c.

301 {
303 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ booleq()

Datum booleq ( PG_FUNCTION_ARGS  )

Definition at line 224 of file bool.c.

225 {
226  bool arg1 = PG_GETARG_BOOL(0);
227  bool arg2 = PG_GETARG_BOOL(1);
228 
229  PG_RETURN_BOOL(arg1 == arg2);
230 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolge()

Datum boolge ( PG_FUNCTION_ARGS  )

Definition at line 269 of file bool.c.

270 {
271  bool arg1 = PG_GETARG_BOOL(0);
272  bool arg2 = PG_GETARG_BOOL(1);
273 
274  PG_RETURN_BOOL(arg1 >= arg2);
275 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolgt()

Datum boolgt ( PG_FUNCTION_ARGS  )

Definition at line 251 of file bool.c.

252 {
253  bool arg1 = PG_GETARG_BOOL(0);
254  bool arg2 = PG_GETARG_BOOL(1);
255 
256  PG_RETURN_BOOL(arg1 > arg2);
257 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolin()

Datum boolin ( PG_FUNCTION_ARGS  )

Definition at line 127 of file bool.c.

128 {
129  const char *in_str = PG_GETARG_CSTRING(0);
130  const char *str;
131  size_t len;
132  bool result;
133 
134  /*
135  * Skip leading and trailing whitespace
136  */
137  str = in_str;
138  while (isspace((unsigned char) *str))
139  str++;
140 
141  len = strlen(str);
142  while (len > 0 && isspace((unsigned char) str[len - 1]))
143  len--;
144 
145  if (parse_bool_with_len(str, len, &result))
146  PG_RETURN_BOOL(result);
147 
148  ereturn(fcinfo->context, (Datum) 0,
149  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
150  errmsg("invalid input syntax for type %s: \"%s\"",
151  "boolean", in_str)));
152 }
bool parse_bool_with_len(const char *value, size_t len, bool *result)
Definition: bool.c:37
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereturn(context, dummy_value,...)
Definition: elog.h:277
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
const char * str
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 str.

◆ boolle()

Datum boolle ( PG_FUNCTION_ARGS  )

Definition at line 260 of file bool.c.

261 {
262  bool arg1 = PG_GETARG_BOOL(0);
263  bool arg2 = PG_GETARG_BOOL(1);
264 
265  PG_RETURN_BOOL(arg1 <= arg2);
266 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boollt()

Datum boollt ( PG_FUNCTION_ARGS  )

Definition at line 242 of file bool.c.

243 {
244  bool arg1 = PG_GETARG_BOOL(0);
245  bool arg2 = PG_GETARG_BOOL(1);
246 
247  PG_RETURN_BOOL(arg1 < arg2);
248 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolne()

Datum boolne ( PG_FUNCTION_ARGS  )

Definition at line 233 of file bool.c.

234 {
235  bool arg1 = PG_GETARG_BOOL(0);
236  bool arg2 = PG_GETARG_BOOL(1);
237 
238  PG_RETURN_BOOL(arg1 != arg2);
239 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolor_statefunc()

Datum boolor_statefunc ( PG_FUNCTION_ARGS  )

Definition at line 312 of file bool.c.

313 {
315 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolout()

Datum boolout ( PG_FUNCTION_ARGS  )

Definition at line 158 of file bool.c.

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

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

Referenced by ExecGetJsonValueItemString().

◆ boolrecv()

Datum boolrecv ( PG_FUNCTION_ARGS  )

Definition at line 175 of file bool.c.

176 {
178  int ext;
179 
180  ext = pq_getmsgbyte(buf);
181  PG_RETURN_BOOL(ext != 0);
182 }
static char * buf
Definition: pg_test_fsync.c:72
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:399
StringInfoData * StringInfo
Definition: stringinfo.h:54

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

◆ boolsend()

Datum boolsend ( PG_FUNCTION_ARGS  )

Definition at line 188 of file bool.c.

189 {
190  bool arg1 = PG_GETARG_BOOL(0);
192 
194  pq_sendbyte(&buf, arg1 ? 1 : 0);
196 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:160

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 205 of file bool.c.

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

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

◆ hashbool()

Datum hashbool ( PG_FUNCTION_ARGS  )

Definition at line 278 of file bool.c.

279 {
280  return hash_uint32((int32) PG_GETARG_BOOL(0));
281 }
signed int int32
Definition: c.h:482
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43

References hash_uint32(), and PG_GETARG_BOOL.

◆ hashboolextended()

Datum hashboolextended ( PG_FUNCTION_ARGS  )

Definition at line 284 of file bool.c.

285 {
287 }
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
static Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.h:49

References hash_uint32_extended(), PG_GETARG_BOOL, and PG_GETARG_INT64.

◆ makeBoolAggState()

static BoolAggState* makeBoolAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 324 of file bool.c.

325 {
327  MemoryContext agg_context;
328 
329  if (!AggCheckCallContext(fcinfo, &agg_context))
330  elog(ERROR, "aggregate function called in non-aggregate context");
331 
332  state = (BoolAggState *) MemoryContextAlloc(agg_context,
333  sizeof(BoolAggState));
334  state->aggcount = 0;
335  state->aggtrue = 0;
336 
337  return state;
338 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4510

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

Referenced by bool_accum().

◆ parse_bool()

bool parse_bool ( const char *  value,
bool *  result 
)

◆ parse_bool_with_len()

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

Definition at line 37 of file bool.c.

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