PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
define.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <math.h>
#include "catalog/namespace.h"
#include "commands/defrem.h"
#include "nodes/makefuncs.h"
#include "parser/parse_type.h"
#include "parser/scansup.h"
#include "utils/builtins.h"
Include dependency graph for define.c:

Go to the source code of this file.

Functions

char * defGetString (DefElem *def)
 
double defGetNumeric (DefElem *def)
 
bool defGetBoolean (DefElem *def)
 
int32 defGetInt32 (DefElem *def)
 
int64 defGetInt64 (DefElem *def)
 
ListdefGetQualifiedName (DefElem *def)
 
TypeNamedefGetTypeName (DefElem *def)
 
int defGetTypeLength (DefElem *def)
 
ListdefGetStringList (DefElem *def)
 

Function Documentation

bool defGetBoolean ( DefElem def)

Definition at line 111 of file define.c.

References DefElem::arg, defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, intVal, nodeTag, NULL, pg_strcasecmp(), and T_Integer.

Referenced by AlterDatabase(), apply_server_options(), apply_table_options(), compute_attributes_with_style(), createdb(), CreateExtension(), DefineAggregate(), DefineOperator(), DefineType(), dintdict_init(), dsimple_init(), dsynonym_init(), dxsyn_init(), ExplainQuery(), file_fdw_validator(), get_file_fdw_attribute_options(), GetCommandLogLevel(), interpretOidsOption(), parse_subscription_options(), parseCreateReplSlotOptions(), postgres_fdw_validator(), postgresImportForeignSchema(), postgresIsForeignRelUpdatable(), and ProcessCopyOptions().

112 {
113  /*
114  * If no parameter given, assume "true" is meant.
115  */
116  if (def->arg == NULL)
117  return true;
118 
119  /*
120  * Allow 0, 1, "true", "false", "on", "off"
121  */
122  switch (nodeTag(def->arg))
123  {
124  case T_Integer:
125  switch (intVal(def->arg))
126  {
127  case 0:
128  return false;
129  case 1:
130  return true;
131  default:
132  /* otherwise, error out below */
133  break;
134  }
135  break;
136  default:
137  {
138  char *sval = defGetString(def);
139 
140  /*
141  * The set of strings accepted here should match up with the
142  * grammar's opt_boolean production.
143  */
144  if (pg_strcasecmp(sval, "true") == 0)
145  return true;
146  if (pg_strcasecmp(sval, "false") == 0)
147  return false;
148  if (pg_strcasecmp(sval, "on") == 0)
149  return true;
150  if (pg_strcasecmp(sval, "off") == 0)
151  return false;
152  }
153  break;
154  }
155  ereport(ERROR,
156  (errcode(ERRCODE_SYNTAX_ERROR),
157  errmsg("%s requires a Boolean value",
158  def->defname)));
159  return false; /* keep compiler quiet */
160 }
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define ERROR
Definition: elog.h:43
char * defGetString(DefElem *def)
Definition: define.c:49
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:721
#define NULL
Definition: c.h:229
#define nodeTag(nodeptr)
Definition: nodes.h:514
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:720
int32 defGetInt32 ( DefElem def)

Definition at line 166 of file define.c.

References DefElem::arg, DefElem::defname, ereport, errcode(), errmsg(), ERROR, intVal, nodeTag, NULL, and T_Integer.

Referenced by AlterDatabase(), ATExecSetIdentity(), createdb(), and DefineAggregate().

167 {
168  if (def->arg == NULL)
169  ereport(ERROR,
170  (errcode(ERRCODE_SYNTAX_ERROR),
171  errmsg("%s requires an integer value",
172  def->defname)));
173  switch (nodeTag(def->arg))
174  {
175  case T_Integer:
176  return (int32) intVal(def->arg);
177  default:
178  ereport(ERROR,
179  (errcode(ERRCODE_SYNTAX_ERROR),
180  errmsg("%s requires an integer value",
181  def->defname)));
182  }
183  return 0; /* keep compiler quiet */
184 }
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:721
#define NULL
Definition: c.h:229
#define nodeTag(nodeptr)
Definition: nodes.h:514
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:720
int64 defGetInt64 ( DefElem def)

Definition at line 190 of file define.c.

References DefElem::arg, CStringGetDatum, DatumGetInt64, DefElem::defname, DirectFunctionCall1, ereport, errcode(), errmsg(), ERROR, int8in(), intVal, nodeTag, NULL, strVal, T_Float, and T_Integer.

Referenced by init_params().

191 {
192  if (def->arg == NULL)
193  ereport(ERROR,
194  (errcode(ERRCODE_SYNTAX_ERROR),
195  errmsg("%s requires a numeric value",
196  def->defname)));
197  switch (nodeTag(def->arg))
198  {
199  case T_Integer:
200  return (int64) intVal(def->arg);
201  case T_Float:
202 
203  /*
204  * Values too large for int4 will be represented as Float
205  * constants by the lexer. Accept these if they are valid int8
206  * strings.
207  */
209  CStringGetDatum(strVal(def->arg))));
210  default:
211  ereport(ERROR,
212  (errcode(ERRCODE_SYNTAX_ERROR),
213  errmsg("%s requires a numeric value",
214  def->defname)));
215  }
216  return 0; /* keep compiler quiet */
217 }
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define ERROR
Definition: elog.h:43
#define DatumGetInt64(X)
Definition: postgres.h:613
#define CStringGetDatum(X)
Definition: postgres.h:584
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:721
#define NULL
Definition: c.h:229
Datum int8in(PG_FUNCTION_ARGS)
Definition: int8.c:145
#define nodeTag(nodeptr)
Definition: nodes.h:514
Definition: nodes.h:283
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:720
double defGetNumeric ( DefElem def)

Definition at line 85 of file define.c.

References DefElem::arg, DefElem::defname, ereport, errcode(), errmsg(), ERROR, floatVal, intVal, nodeTag, NULL, T_Float, and T_Integer.

Referenced by AlterFunction().

86 {
87  if (def->arg == NULL)
88  ereport(ERROR,
89  (errcode(ERRCODE_SYNTAX_ERROR),
90  errmsg("%s requires a numeric value",
91  def->defname)));
92  switch (nodeTag(def->arg))
93  {
94  case T_Integer:
95  return (double) intVal(def->arg);
96  case T_Float:
97  return floatVal(def->arg);
98  default:
99  ereport(ERROR,
100  (errcode(ERRCODE_SYNTAX_ERROR),
101  errmsg("%s requires a numeric value",
102  def->defname)));
103  }
104  return 0; /* keep compiler quiet */
105 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define floatVal(v)
Definition: value.h:53
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:721
#define NULL
Definition: c.h:229
#define nodeTag(nodeptr)
Definition: nodes.h:514
Definition: nodes.h:283
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:720
List* defGetQualifiedName ( DefElem def)

Definition at line 223 of file define.c.

References DefElem::arg, DefElem::defname, ereport, errcode(), errmsg(), ERROR, list_make1, NIL, nodeTag, NULL, T_List, T_String, and T_TypeName.

Referenced by AlterOperator(), DefineAggregate(), DefineCollation(), DefineOperator(), DefineRange(), DefineTSConfiguration(), DefineTSDictionary(), DefineType(), get_ts_parser_func(), get_ts_template_func(), and init_params().

224 {
225  if (def->arg == NULL)
226  ereport(ERROR,
227  (errcode(ERRCODE_SYNTAX_ERROR),
228  errmsg("%s requires a parameter",
229  def->defname)));
230  switch (nodeTag(def->arg))
231  {
232  case T_TypeName:
233  return ((TypeName *) def->arg)->names;
234  case T_List:
235  return (List *) def->arg;
236  case T_String:
237  /* Allow quoted name for backwards compatibility */
238  return list_make1(def->arg);
239  default:
240  ereport(ERROR,
241  (errcode(ERRCODE_SYNTAX_ERROR),
242  errmsg("argument of %s must be a name",
243  def->defname)));
244  }
245  return NIL; /* keep compiler quiet */
246 }
#define NIL
Definition: pg_list.h:69
int errcode(int sqlerrcode)
Definition: elog.c:575
#define list_make1(x1)
Definition: pg_list.h:139
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: nodes.h:291
Node * arg
Definition: parsenodes.h:721
#define NULL
Definition: c.h:229
#define nodeTag(nodeptr)
Definition: nodes.h:514
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:720
Definition: pg_list.h:45
char* defGetString ( DefElem def)

Definition at line 49 of file define.c.

References DefElem::arg, DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, intVal, NameListToString(), nodeTag, NULL, psprintf(), pstrdup(), strVal, T_A_Star, T_Float, T_Integer, T_List, T_String, T_TypeName, and TypeNameToString().

Referenced by AlterDatabase(), apply_server_options(), apply_table_options(), check_selective_binary_conversion(), createdb(), CreateExtension(), defGetBoolean(), defGetTypeLength(), DefineAggregate(), DefineCollation(), DefineType(), deparseAnalyzeSql(), deparseColumnRef(), deparseRelation(), dintdict_init(), dispell_init(), dsimple_init(), dsnowball_init(), dsynonym_init(), dxsyn_init(), ExplainQuery(), ExplainResultDesc(), ExtractConnectionOptions(), file_fdw_validator(), fileGetOptions(), optionListToArray(), parse_publication_options(), parse_subscription_options(), postgres_fdw_validator(), postgresAcquireSampleRowsFunc(), ProcessCopyOptions(), prsd_headline(), serialize_deflist(), thesaurus_init(), transformRelOptions(), and unaccent_init().

50 {
51  if (def->arg == NULL)
52  ereport(ERROR,
53  (errcode(ERRCODE_SYNTAX_ERROR),
54  errmsg("%s requires a parameter",
55  def->defname)));
56  switch (nodeTag(def->arg))
57  {
58  case T_Integer:
59  return psprintf("%ld", (long) intVal(def->arg));
60  case T_Float:
61 
62  /*
63  * T_Float values are kept in string form, so this type cheat
64  * works (and doesn't risk losing precision)
65  */
66  return strVal(def->arg);
67  case T_String:
68  return strVal(def->arg);
69  case T_TypeName:
70  return TypeNameToString((TypeName *) def->arg);
71  case T_List:
72  return NameListToString((List *) def->arg);
73  case T_A_Star:
74  return pstrdup("*");
75  default:
76  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
77  }
78  return NULL; /* keep compiler quiet */
79 }
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:459
char * pstrdup(const char *in)
Definition: mcxt.c:1077
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: nodes.h:291
Node * arg
Definition: parsenodes.h:721
char * NameListToString(List *names)
Definition: namespace.c:3020
#define NULL
Definition: c.h:229
#define nodeTag(nodeptr)
Definition: nodes.h:514
Definition: nodes.h:283
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:720
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
List* defGetStringList ( DefElem def)

Definition at line 327 of file define.c.

References DefElem::arg, DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, IsA, lfirst, nodeTag, NULL, and T_List.

328 {
329  ListCell *cell;
330 
331  if (def->arg == NULL)
332  ereport(ERROR,
333  (errcode(ERRCODE_SYNTAX_ERROR),
334  errmsg("%s requires a parameter",
335  def->defname)));
336  if (nodeTag(def->arg) != T_List)
337  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
338 
339  foreach(cell, (List *) def->arg)
340  {
341  Node *str = (Node *) lfirst(cell);
342 
343  if (!IsA(str, String))
344  elog(ERROR, "unexpected node type in name list: %d",
345  (int) nodeTag(str));
346  }
347 
348  return (List *) def->arg;
349 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: nodes.h:291
Node * arg
Definition: parsenodes.h:721
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:514
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:720
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
int defGetTypeLength ( DefElem def)

Definition at line 283 of file define.c.

References DefElem::arg, defGetString(), DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, intVal, nodeTag, NULL, pg_strcasecmp(), strVal, T_Float, T_Integer, T_List, T_String, T_TypeName, and TypeNameToString().

Referenced by DefineType().

284 {
285  if (def->arg == NULL)
286  ereport(ERROR,
287  (errcode(ERRCODE_SYNTAX_ERROR),
288  errmsg("%s requires a parameter",
289  def->defname)));
290  switch (nodeTag(def->arg))
291  {
292  case T_Integer:
293  return intVal(def->arg);
294  case T_Float:
295  ereport(ERROR,
296  (errcode(ERRCODE_SYNTAX_ERROR),
297  errmsg("%s requires an integer value",
298  def->defname)));
299  break;
300  case T_String:
301  if (pg_strcasecmp(strVal(def->arg), "variable") == 0)
302  return -1; /* variable length */
303  break;
304  case T_TypeName:
305  /* cope if grammar chooses to believe "variable" is a typename */
307  "variable") == 0)
308  return -1; /* variable length */
309  break;
310  case T_List:
311  /* must be an operator name */
312  break;
313  default:
314  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
315  }
316  ereport(ERROR,
317  (errcode(ERRCODE_SYNTAX_ERROR),
318  errmsg("invalid argument for %s: \"%s\"",
319  def->defname, defGetString(def))));
320  return 0; /* keep compiler quiet */
321 }
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:459
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define ERROR
Definition: elog.h:43
char * defGetString(DefElem *def)
Definition: define.c:49
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: nodes.h:291
Node * arg
Definition: parsenodes.h:721
#define NULL
Definition: c.h:229
#define nodeTag(nodeptr)
Definition: nodes.h:514
Definition: nodes.h:283
#define intVal(v)
Definition: value.h:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:720
#define elog
Definition: elog.h:219
TypeName* defGetTypeName ( DefElem def)

Definition at line 255 of file define.c.

References DefElem::arg, DefElem::defname, ereport, errcode(), errmsg(), ERROR, list_make1, makeTypeNameFromNameList(), nodeTag, NULL, T_String, and T_TypeName.

Referenced by DefineAggregate(), DefineOperator(), DefineRange(), DefineType(), and init_params().

256 {
257  if (def->arg == NULL)
258  ereport(ERROR,
259  (errcode(ERRCODE_SYNTAX_ERROR),
260  errmsg("%s requires a parameter",
261  def->defname)));
262  switch (nodeTag(def->arg))
263  {
264  case T_TypeName:
265  return (TypeName *) def->arg;
266  case T_String:
267  /* Allow quoted typename for backwards compatibility */
269  default:
270  ereport(ERROR,
271  (errcode(ERRCODE_SYNTAX_ERROR),
272  errmsg("argument of %s must be a type name",
273  def->defname)));
274  }
275  return NULL; /* keep compiler quiet */
276 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define list_make1(x1)
Definition: pg_list.h:139
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:721
#define NULL
Definition: c.h:229
#define nodeTag(nodeptr)
Definition: nodes.h:514
int errmsg(const char *fmt,...)
Definition: elog.c:797
TypeName * makeTypeNameFromNameList(List *names)
Definition: makefuncs.c:453
char * defname
Definition: parsenodes.h:720