PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 "utils/fmgrprotos.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)
 
Oid defGetObjectId (DefElem *def)
 
ListdefGetQualifiedName (DefElem *def)
 
TypeNamedefGetTypeName (DefElem *def)
 
int defGetTypeLength (DefElem *def)
 
ListdefGetStringList (DefElem *def)
 
void errorConflictingDefElem (DefElem *defel, ParseState *pstate)
 

Function Documentation

◆ defGetBoolean()

bool defGetBoolean ( DefElem def)

Definition at line 107 of file define.c.

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

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

Referenced by AlterDatabase(), AlterOperator(), AlterReplicationSlot(), apply_server_options(), apply_table_options(), cluster(), createdb(), CreateExtension(), DefineAggregate(), DefineCollation(), DefineOperator(), DefineType(), dintdict_init(), dsimple_init(), dsynonym_init(), dxsyn_init(), ExecReindex(), ExecVacuum(), ExplainQuery(), file_fdw_validator(), get_file_fdw_attribute_options(), get_vacoptval_from_boolean(), GetCommandLogLevel(), make_new_connection(), parse_basebackup_options(), parse_output_parameters(), parse_publication_options(), parse_subscription_options(), parseCreateReplSlotOptions(), postgres_fdw_validator(), postgresExecForeignTruncate(), postgresImportForeignSchema(), postgresIsForeignRelUpdatable(), ProcessCopyOptions(), transformExplainStmt(), transformRelOptions(), and UserMappingPasswordRequired().

◆ defGetInt32()

int32 defGetInt32 ( DefElem def)

Definition at line 162 of file define.c.

163 {
164  if (def->arg == NULL)
165  ereport(ERROR,
166  (errcode(ERRCODE_SYNTAX_ERROR),
167  errmsg("%s requires an integer value",
168  def->defname)));
169  switch (nodeTag(def->arg))
170  {
171  case T_Integer:
172  return (int32) intVal(def->arg);
173  default:
174  ereport(ERROR,
175  (errcode(ERRCODE_SYNTAX_ERROR),
176  errmsg("%s requires an integer value",
177  def->defname)));
178  }
179  return 0; /* keep compiler quiet */
180 }
signed int int32
Definition: c.h:508

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

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

◆ defGetInt64()

int64 defGetInt64 ( DefElem def)

Definition at line 186 of file define.c.

187 {
188  if (def->arg == NULL)
189  ereport(ERROR,
190  (errcode(ERRCODE_SYNTAX_ERROR),
191  errmsg("%s requires a numeric value",
192  def->defname)));
193  switch (nodeTag(def->arg))
194  {
195  case T_Integer:
196  return (int64) intVal(def->arg);
197  case T_Float:
198 
199  /*
200  * Values too large for int4 will be represented as Float
201  * constants by the lexer. Accept these if they are valid int8
202  * strings.
203  */
205  CStringGetDatum(castNode(Float, def->arg)->fval)));
206  default:
207  ereport(ERROR,
208  (errcode(ERRCODE_SYNTAX_ERROR),
209  errmsg("%s requires a numeric value",
210  def->defname)));
211  }
212  return 0; /* keep compiler quiet */
213 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
Datum int8in(PG_FUNCTION_ARGS)
Definition: int8.c:50
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:385
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
Definition: value.h:48

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

Referenced by defGetCopyRejectLimitOption(), init_params(), and parse_basebackup_options().

◆ defGetNumeric()

double defGetNumeric ( DefElem def)

Definition at line 81 of file define.c.

82 {
83  if (def->arg == NULL)
84  ereport(ERROR,
85  (errcode(ERRCODE_SYNTAX_ERROR),
86  errmsg("%s requires a numeric value",
87  def->defname)));
88  switch (nodeTag(def->arg))
89  {
90  case T_Integer:
91  return (double) intVal(def->arg);
92  case T_Float:
93  return floatVal(def->arg);
94  default:
95  ereport(ERROR,
96  (errcode(ERRCODE_SYNTAX_ERROR),
97  errmsg("%s requires a numeric value",
98  def->defname)));
99  }
100  return 0; /* keep compiler quiet */
101 }
#define floatVal(v)
Definition: value.h:80

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

Referenced by AlterFunction(), and compute_function_attributes().

◆ defGetObjectId()

Oid defGetObjectId ( DefElem def)

Definition at line 219 of file define.c.

220 {
221  if (def->arg == NULL)
222  ereport(ERROR,
223  (errcode(ERRCODE_SYNTAX_ERROR),
224  errmsg("%s requires a numeric value",
225  def->defname)));
226  switch (nodeTag(def->arg))
227  {
228  case T_Integer:
229  return (Oid) intVal(def->arg);
230  case T_Float:
231 
232  /*
233  * Values too large for int4 will be represented as Float
234  * constants by the lexer. Accept these if they are valid OID
235  * strings.
236  */
238  CStringGetDatum(castNode(Float, def->arg)->fval)));
239  default:
240  ereport(ERROR,
241  (errcode(ERRCODE_SYNTAX_ERROR),
242  errmsg("%s requires a numeric value",
243  def->defname)));
244  }
245  return 0; /* keep compiler quiet */
246 }
Datum oidin(PG_FUNCTION_ARGS)
Definition: oid.c:37
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:242
unsigned int Oid
Definition: postgres_ext.h:31

References DefElem::arg, castNode, CStringGetDatum(), DatumGetObjectId(), DefElem::defname, DirectFunctionCall1, ereport, errcode(), errmsg(), ERROR, intVal, nodeTag, and oidin().

Referenced by createdb().

◆ defGetQualifiedName()

List* defGetQualifiedName ( DefElem def)

Definition at line 252 of file define.c.

253 {
254  if (def->arg == NULL)
255  ereport(ERROR,
256  (errcode(ERRCODE_SYNTAX_ERROR),
257  errmsg("%s requires a parameter",
258  def->defname)));
259  switch (nodeTag(def->arg))
260  {
261  case T_TypeName:
262  return ((TypeName *) def->arg)->names;
263  case T_List:
264  return (List *) def->arg;
265  case T_String:
266  /* Allow quoted name for backwards compatibility */
267  return list_make1(def->arg);
268  default:
269  ereport(ERROR,
270  (errcode(ERRCODE_SYNTAX_ERROR),
271  errmsg("argument of %s must be a name",
272  def->defname)));
273  }
274  return NIL; /* keep compiler quiet */
275 }
#define NIL
Definition: pg_list.h:68
#define list_make1(x1)
Definition: pg_list.h:212
Definition: pg_list.h:54

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

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

◆ defGetString()

char* defGetString ( DefElem def)

Definition at line 48 of file define.c.

49 {
50  if (def->arg == NULL)
51  ereport(ERROR,
52  (errcode(ERRCODE_SYNTAX_ERROR),
53  errmsg("%s requires a parameter",
54  def->defname)));
55  switch (nodeTag(def->arg))
56  {
57  case T_Integer:
58  return psprintf("%ld", (long) intVal(def->arg));
59  case T_Float:
60  return castNode(Float, def->arg)->fval;
61  case T_Boolean:
62  return boolVal(def->arg) ? "true" : "false";
63  case T_String:
64  return strVal(def->arg);
65  case T_TypeName:
66  return TypeNameToString((TypeName *) def->arg);
67  case T_List:
68  return NameListToString((List *) def->arg);
69  case T_A_Star:
70  return pstrdup("*");
71  default:
72  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
73  }
74  return NULL; /* keep compiler quiet */
75 }
#define elog(elevel,...)
Definition: elog.h:225
char * pstrdup(const char *in)
Definition: mcxt.c:1696
char * NameListToString(const List *names)
Definition: namespace.c:3594
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:478
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
#define boolVal(v)
Definition: value.h:81
#define strVal(v)
Definition: value.h:82

References DefElem::arg, boolVal, castNode, DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, intVal, NameListToString(), nodeTag, psprintf(), pstrdup(), strVal, and TypeNameToString().

Referenced by AlterDatabase(), AlterType(), apply_server_options(), apply_table_options(), check_selective_binary_conversion(), createdb(), CreateExtension(), defGetBoolean(), defGetCopyHeaderChoice(), defGetCopyLogVerbosityChoice(), defGetCopyOnErrorChoice(), defGetStreamingMode(), defGetTypeLength(), DefineAggregate(), DefineCollation(), DefineType(), deparseAnalyzeSql(), deparseColumnRef(), deparseRelation(), dintdict_init(), dispell_init(), dsimple_init(), dsnowball_init(), dsynonym_init(), dxsyn_init(), ExecReindex(), ExecVacuum(), ExplainQuery(), ExplainResultDesc(), ExtractConnectionOptions(), extractModify(), file_fdw_validator(), fileGetOptions(), get_batch_size_option(), GrantRole(), optionListToArray(), parse_basebackup_options(), parse_output_parameters(), parse_publication_options(), parse_subscription_options(), parseCreateReplSlotOptions(), postgres_fdw_validator(), postgresAcquireSampleRowsFunc(), ProcessCopyOptions(), prsd_headline(), serialize_deflist(), thesaurus_init(), transformRelOptions(), and unaccent_init().

◆ defGetStringList()

List* defGetStringList ( DefElem def)

Definition at line 356 of file define.c.

357 {
358  ListCell *cell;
359 
360  if (def->arg == NULL)
361  ereport(ERROR,
362  (errcode(ERRCODE_SYNTAX_ERROR),
363  errmsg("%s requires a parameter",
364  def->defname)));
365  if (nodeTag(def->arg) != T_List)
366  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
367 
368  foreach(cell, (List *) def->arg)
369  {
370  Node *str = (Node *) lfirst(cell);
371 
372  if (!IsA(str, String))
373  elog(ERROR, "unexpected node type in name list: %d",
374  (int) nodeTag(str));
375  }
376 
377  return (List *) def->arg;
378 }
const char * str
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define lfirst(lc)
Definition: pg_list.h:172
Definition: nodes.h:129
Definition: value.h:64

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

◆ defGetTypeLength()

int defGetTypeLength ( DefElem def)

Definition at line 312 of file define.c.

313 {
314  if (def->arg == NULL)
315  ereport(ERROR,
316  (errcode(ERRCODE_SYNTAX_ERROR),
317  errmsg("%s requires a parameter",
318  def->defname)));
319  switch (nodeTag(def->arg))
320  {
321  case T_Integer:
322  return intVal(def->arg);
323  case T_Float:
324  ereport(ERROR,
325  (errcode(ERRCODE_SYNTAX_ERROR),
326  errmsg("%s requires an integer value",
327  def->defname)));
328  break;
329  case T_String:
330  if (pg_strcasecmp(strVal(def->arg), "variable") == 0)
331  return -1; /* variable length */
332  break;
333  case T_TypeName:
334  /* cope if grammar chooses to believe "variable" is a typename */
336  "variable") == 0)
337  return -1; /* variable length */
338  break;
339  case T_List:
340  /* must be an operator name */
341  break;
342  default:
343  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
344  }
345  ereport(ERROR,
346  (errcode(ERRCODE_SYNTAX_ERROR),
347  errmsg("invalid argument for %s: \"%s\"",
348  def->defname, defGetString(def))));
349  return 0; /* keep compiler quiet */
350 }

References DefElem::arg, defGetString(), DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, intVal, nodeTag, pg_strcasecmp(), strVal, and TypeNameToString().

Referenced by DefineType().

◆ defGetTypeName()

TypeName* defGetTypeName ( DefElem def)

Definition at line 284 of file define.c.

285 {
286  if (def->arg == NULL)
287  ereport(ERROR,
288  (errcode(ERRCODE_SYNTAX_ERROR),
289  errmsg("%s requires a parameter",
290  def->defname)));
291  switch (nodeTag(def->arg))
292  {
293  case T_TypeName:
294  return (TypeName *) def->arg;
295  case T_String:
296  /* Allow quoted typename for backwards compatibility */
298  default:
299  ereport(ERROR,
300  (errcode(ERRCODE_SYNTAX_ERROR),
301  errmsg("argument of %s must be a type name",
302  def->defname)));
303  }
304  return NULL; /* keep compiler quiet */
305 }
TypeName * makeTypeNameFromNameList(List *names)
Definition: makefuncs.c:481

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

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

◆ errorConflictingDefElem()

void errorConflictingDefElem ( DefElem defel,
ParseState pstate 
)