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

329 {
331 
332  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
333 
334  /* Create the state data on first call */
335  if (state == NULL)
336  state = makeBoolAggState(fcinfo);
337 
338  if (!PG_ARGISNULL(1))
339  {
340  state->aggcount++;
341  if (PG_GETARG_BOOL(1))
342  state->aggtrue++;
343  }
344 
346 }
static BoolAggState * makeBoolAggState(FunctionCallInfo fcinfo)
Definition: bool.c:311
#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 349 of file bool.c.

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

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

371 {
373 
374  state = PG_ARGISNULL(0) ? NULL : (BoolAggState *) PG_GETARG_POINTER(0);
375 
376  /* if there were no non-null values, return NULL */
377  if (state == NULL || state->aggcount == 0)
378  PG_RETURN_NULL();
379 
380  /* true if all non-null values are true */
381  PG_RETURN_BOOL(state->aggtrue == state->aggcount);
382 }
#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 385 of file bool.c.

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

References PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_BOOL, and PG_RETURN_NULL.

◆ booland_statefunc()

Datum booland_statefunc ( PG_FUNCTION_ARGS  )

Definition at line 287 of file bool.c.

288 {
290 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ booleq()

Datum booleq ( PG_FUNCTION_ARGS  )

Definition at line 223 of file bool.c.

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

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolge()

Datum boolge ( PG_FUNCTION_ARGS  )

Definition at line 268 of file bool.c.

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

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolgt()

Datum boolgt ( PG_FUNCTION_ARGS  )

Definition at line 250 of file bool.c.

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

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolin()

Datum boolin ( PG_FUNCTION_ARGS  )

Definition at line 126 of file bool.c.

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

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

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boollt()

Datum boollt ( PG_FUNCTION_ARGS  )

Definition at line 241 of file bool.c.

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

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolne()

Datum boolne ( PG_FUNCTION_ARGS  )

Definition at line 232 of file bool.c.

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

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolor_statefunc()

Datum boolor_statefunc ( PG_FUNCTION_ARGS  )

Definition at line 299 of file bool.c.

300 {
302 }

References PG_GETARG_BOOL, and PG_RETURN_BOOL.

◆ boolout()

Datum boolout ( PG_FUNCTION_ARGS  )

Definition at line 157 of file bool.c.

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

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

Referenced by ExecGetJsonValueItemString().

◆ boolrecv()

Datum boolrecv ( PG_FUNCTION_ARGS  )

Definition at line 174 of file bool.c.

175 {
177  int ext;
178 
179  ext = pq_getmsgbyte(buf);
180  PG_RETURN_BOOL(ext != 0);
181 }
static char * buf
Definition: pg_test_fsync.c:73
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 187 of file bool.c.

188 {
189  bool arg1 = PG_GETARG_BOOL(0);
191 
193  pq_sendbyte(&buf, arg1 ? 1 : 0);
195 }
#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 204 of file bool.c.

205 {
206  bool arg1 = PG_GETARG_BOOL(0);
207  const char *str;
208 
209  if (arg1)
210  str = "true";
211  else
212  str = "false";
213 
215 }
#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.

◆ makeBoolAggState()

static BoolAggState* makeBoolAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 311 of file bool.c.

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

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

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