PostgreSQL Source Code  git master
conditional.c File Reference
#include "postgres_fe.h"
#include "fe_utils/conditional.h"
Include dependency graph for conditional.c:

Go to the source code of this file.

Functions

ConditionalStack conditional_stack_create (void)
 
void conditional_stack_destroy (ConditionalStack cstack)
 
void conditional_stack_push (ConditionalStack cstack, ifState new_state)
 
bool conditional_stack_pop (ConditionalStack cstack)
 
int conditional_stack_depth (ConditionalStack cstack)
 
ifState conditional_stack_peek (ConditionalStack cstack)
 
bool conditional_stack_poke (ConditionalStack cstack, ifState new_state)
 
bool conditional_stack_empty (ConditionalStack cstack)
 
bool conditional_active (ConditionalStack cstack)
 
void conditional_stack_set_query_len (ConditionalStack cstack, int len)
 
int conditional_stack_get_query_len (ConditionalStack cstack)
 
void conditional_stack_set_paren_depth (ConditionalStack cstack, int depth)
 
int conditional_stack_get_paren_depth (ConditionalStack cstack)
 

Function Documentation

◆ conditional_active()

bool conditional_active ( ConditionalStack  cstack)

Definition at line 128 of file conditional.c.

References conditional_stack_peek(), IFSTATE_ELSE_TRUE, IFSTATE_NONE, and IFSTATE_TRUE.

Referenced by advanceConnectionState(), exec_command(), exec_command_if(), get_prompt(), HandleSlashCmds(), MainLoop(), and psql_get_variable().

129 {
130  ifState s = conditional_stack_peek(cstack);
131 
132  return s == IFSTATE_NONE || s == IFSTATE_TRUE || s == IFSTATE_ELSE_TRUE;
133 }
ifState
Definition: conditional.h:29
ifState conditional_stack_peek(ConditionalStack cstack)
Definition: conditional.c:94

◆ conditional_stack_create()

ConditionalStack conditional_stack_create ( void  )

Definition at line 18 of file conditional.c.

References ConditionalStackData::head, and pg_malloc().

Referenced by CheckConditional(), main(), and MainLoop().

19 {
21 
22  cstack->head = NULL;
23  return cstack;
24 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
IfStackElem * head
Definition: conditional.h:68

◆ conditional_stack_depth()

int conditional_stack_depth ( ConditionalStack  cstack)

Definition at line 72 of file conditional.c.

References ConditionalStackData::head, and IfStackElem::next.

73 {
74  if (cstack == NULL)
75  return -1;
76  else
77  {
78  IfStackElem *p = cstack->head;
79  int depth = 0;
80 
81  while (p != NULL)
82  {
83  depth++;
84  p = p->next;
85  }
86  return depth;
87  }
88 }
IfStackElem * head
Definition: conditional.h:68
struct IfStackElem * next
Definition: conditional.h:63

◆ conditional_stack_destroy()

void conditional_stack_destroy ( ConditionalStack  cstack)

Definition at line 30 of file conditional.c.

References conditional_stack_pop(), and free.

Referenced by CheckConditional(), main(), and MainLoop().

31 {
32  while (conditional_stack_pop(cstack))
33  continue;
34  free(cstack);
35 }
bool conditional_stack_pop(ConditionalStack cstack)
Definition: conditional.c:57
#define free(a)
Definition: header.h:65

◆ conditional_stack_empty()

◆ conditional_stack_get_paren_depth()

int conditional_stack_get_paren_depth ( ConditionalStack  cstack)

Definition at line 172 of file conditional.c.

References conditional_stack_empty(), ConditionalStackData::head, and IfStackElem::paren_depth.

Referenced by discard_query_text().

173 {
174  if (conditional_stack_empty(cstack))
175  return -1;
176  return cstack->head->paren_depth;
177 }
IfStackElem * head
Definition: conditional.h:68
int paren_depth
Definition: conditional.h:62
bool conditional_stack_empty(ConditionalStack cstack)
Definition: conditional.c:118

◆ conditional_stack_get_query_len()

int conditional_stack_get_query_len ( ConditionalStack  cstack)

Definition at line 150 of file conditional.c.

References conditional_stack_empty(), ConditionalStackData::head, and IfStackElem::query_len.

Referenced by discard_query_text().

151 {
152  if (conditional_stack_empty(cstack))
153  return -1;
154  return cstack->head->query_len;
155 }
IfStackElem * head
Definition: conditional.h:68
bool conditional_stack_empty(ConditionalStack cstack)
Definition: conditional.c:118

◆ conditional_stack_peek()

ifState conditional_stack_peek ( ConditionalStack  cstack)

Definition at line 94 of file conditional.c.

References conditional_stack_empty(), ConditionalStackData::head, IfStackElem::if_state, and IFSTATE_NONE.

Referenced by advanceConnectionState(), CheckConditional(), conditional_active(), exec_command_elif(), exec_command_else(), exec_command_endif(), and executeMetaCommand().

95 {
96  if (conditional_stack_empty(cstack))
97  return IFSTATE_NONE;
98  return cstack->head->if_state;
99 }
IfStackElem * head
Definition: conditional.h:68
ifState if_state
Definition: conditional.h:60
bool conditional_stack_empty(ConditionalStack cstack)
Definition: conditional.c:118

◆ conditional_stack_poke()

bool conditional_stack_poke ( ConditionalStack  cstack,
ifState  new_state 
)

Definition at line 106 of file conditional.c.

References conditional_stack_empty(), ConditionalStackData::head, and IfStackElem::if_state.

Referenced by advanceConnectionState(), CheckConditional(), exec_command_elif(), exec_command_else(), exec_command_if(), and executeMetaCommand().

107 {
108  if (conditional_stack_empty(cstack))
109  return false;
110  cstack->head->if_state = new_state;
111  return true;
112 }
IfStackElem * head
Definition: conditional.h:68
ifState if_state
Definition: conditional.h:60
bool conditional_stack_empty(ConditionalStack cstack)
Definition: conditional.c:118

◆ conditional_stack_pop()

bool conditional_stack_pop ( ConditionalStack  cstack)

Definition at line 57 of file conditional.c.

References free, ConditionalStackData::head, and IfStackElem::next.

Referenced by advanceConnectionState(), CheckConditional(), conditional_stack_destroy(), exec_command_endif(), executeMetaCommand(), HandleSlashCmds(), and MainLoop().

58 {
59  IfStackElem *p = cstack->head;
60 
61  if (!p)
62  return false;
63  cstack->head = cstack->head->next;
64  free(p);
65  return true;
66 }
IfStackElem * head
Definition: conditional.h:68
#define free(a)
Definition: header.h:65
struct IfStackElem * next
Definition: conditional.h:63

◆ conditional_stack_push()

void conditional_stack_push ( ConditionalStack  cstack,
ifState  new_state 
)

Definition at line 41 of file conditional.c.

References ConditionalStackData::head, IfStackElem::if_state, IfStackElem::next, IfStackElem::paren_depth, pg_malloc(), and IfStackElem::query_len.

Referenced by advanceConnectionState(), CheckConditional(), exec_command_if(), executeMetaCommand(), and HandleSlashCmds().

42 {
43  IfStackElem *p = (IfStackElem *) pg_malloc(sizeof(IfStackElem));
44 
45  p->if_state = new_state;
46  p->query_len = -1;
47  p->paren_depth = -1;
48  p->next = cstack->head;
49  cstack->head = p;
50 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
IfStackElem * head
Definition: conditional.h:68
int paren_depth
Definition: conditional.h:62
ifState if_state
Definition: conditional.h:60
struct IfStackElem * next
Definition: conditional.h:63

◆ conditional_stack_set_paren_depth()

void conditional_stack_set_paren_depth ( ConditionalStack  cstack,
int  depth 
)

Definition at line 161 of file conditional.c.

References Assert, conditional_stack_empty(), ConditionalStackData::head, and IfStackElem::paren_depth.

Referenced by save_query_text_state().

162 {
164  cstack->head->paren_depth = depth;
165 }
IfStackElem * head
Definition: conditional.h:68
#define Assert(condition)
Definition: c.h:733
int paren_depth
Definition: conditional.h:62
bool conditional_stack_empty(ConditionalStack cstack)
Definition: conditional.c:118

◆ conditional_stack_set_query_len()

void conditional_stack_set_query_len ( ConditionalStack  cstack,
int  len 
)

Definition at line 139 of file conditional.c.

References Assert, conditional_stack_empty(), ConditionalStackData::head, and IfStackElem::query_len.

Referenced by save_query_text_state().

140 {
142  cstack->head->query_len = len;
143 }
IfStackElem * head
Definition: conditional.h:68
#define Assert(condition)
Definition: c.h:733
bool conditional_stack_empty(ConditionalStack cstack)
Definition: conditional.c:118