PostgreSQL Source Code  git master
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)
 
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 108 of file define.c.

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

◆ defGetInt32()

int32 defGetInt32 ( DefElem def)

Definition at line 163 of file define.c.

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

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 187 of file define.c.

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

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

Referenced by init_params(), and parse_basebackup_options().

◆ defGetNumeric()

double defGetNumeric ( DefElem def)

Definition at line 82 of file define.c.

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

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

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

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 49 of file define.c.

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  return castNode(Float, def->arg)->fval;
62  case T_Boolean:
63  return boolVal(def->arg) ? "true" : "false";
64  case T_String:
65  return strVal(def->arg);
66  case T_TypeName:
67  return TypeNameToString((TypeName *) def->arg);
68  case T_List:
69  return NameListToString((List *) def->arg);
70  case T_A_Star:
71  return pstrdup("*");
72  default:
73  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
74  }
75  return NULL; /* keep compiler quiet */
76 }
char * pstrdup(const char *in)
Definition: mcxt.c:1483
char * NameListToString(List *names)
Definition: namespace.c:3145
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:478
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#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(), 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 357 of file define.c.

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

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

◆ defGetTypeLength()

int defGetTypeLength ( DefElem def)

Definition at line 313 of file define.c.

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

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 285 of file define.c.

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

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 
)