1 /*-------------------------------------------------------------------------
4 * Support routines for various kinds of object creation.
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
15 * The "DefineFoo" routines take the parse tree and pick out the
16 * appropriate arguments/flags, passing the results to the
17 * corresponding "FooDefine" routines (in src/catalog) that do
18 * the actual catalog-munging. These routines also verify permission
19 * of the user to execute the command.
22 * These things must be defined and committed in the following order:
24 * input/output, recv/send procedures
31 *-------------------------------------------------------------------------
38 #include "catalog/namespace.h"
39 #include "commands/defrem.h"
40 #include "nodes/makefuncs.h"
41 #include "parser/parse_type.h"
42 #include "parser/scansup.h"
43 #include "utils/int8.h"
47 * Translate the input language name to lower case, and truncate if needed.
49 * Returns a palloc'd string
52 case_translate_language_name(const char *input
)
54 return downcase_truncate_identifier(input
, strlen(input
), false);
59 * Extract a string value (otherwise uninterpreted) from a DefElem.
62 defGetString(DefElem
*def
)
66 (errcode(ERRCODE_SYNTAX_ERROR
),
67 errmsg("%s requires a parameter",
69 switch (nodeTag(def
->arg
))
73 char *str
= palloc(32);
75 snprintf(str
, 32, "%ld", (long) intVal(def
->arg
));
81 * T_Float values are kept in string form, so this type cheat
82 * works (and doesn't risk losing precision)
84 return strVal(def
->arg
);
86 return strVal(def
->arg
);
88 return TypeNameToString((TypeName
*) def
->arg
);
90 return NameListToString((List
*) def
->arg
);
92 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(def
->arg
));
94 return NULL
; /* keep compiler quiet */
98 * Extract a numeric value (actually double) from a DefElem.
101 defGetNumeric(DefElem
*def
)
103 if (def
->arg
== NULL
)
105 (errcode(ERRCODE_SYNTAX_ERROR
),
106 errmsg("%s requires a numeric value",
108 switch (nodeTag(def
->arg
))
111 return (double) intVal(def
->arg
);
113 return floatVal(def
->arg
);
116 (errcode(ERRCODE_SYNTAX_ERROR
),
117 errmsg("%s requires a numeric value",
120 return 0; /* keep compiler quiet */
124 * Extract a boolean value from a DefElem.
127 defGetBoolean(DefElem
*def
)
130 * If no parameter given, assume "true" is meant.
132 if (def
->arg
== NULL
)
136 * Allow 0, 1, "true", "false"
138 switch (nodeTag(def
->arg
))
141 switch (intVal(def
->arg
))
148 /* otherwise, error out below */
154 char *sval
= defGetString(def
);
156 if (pg_strcasecmp(sval
, "true") == 0)
158 if (pg_strcasecmp(sval
, "false") == 0)
165 (errcode(ERRCODE_SYNTAX_ERROR
),
166 errmsg("%s requires a Boolean value",
168 return false; /* keep compiler quiet */
172 * Extract an int64 value from a DefElem.
175 defGetInt64(DefElem
*def
)
177 if (def
->arg
== NULL
)
179 (errcode(ERRCODE_SYNTAX_ERROR
),
180 errmsg("%s requires a numeric value",
182 switch (nodeTag(def
->arg
))
185 return (int64
) intVal(def
->arg
);
189 * Values too large for int4 will be represented as Float
190 * constants by the lexer. Accept these if they are valid int8
193 return DatumGetInt64(DirectFunctionCall1(int8in
,
194 CStringGetDatum(strVal(def
->arg
))));
197 (errcode(ERRCODE_SYNTAX_ERROR
),
198 errmsg("%s requires a numeric value",
201 return 0; /* keep compiler quiet */
205 * Extract a possibly-qualified name (as a List of Strings) from a DefElem.
208 defGetQualifiedName(DefElem
*def
)
210 if (def
->arg
== NULL
)
212 (errcode(ERRCODE_SYNTAX_ERROR
),
213 errmsg("%s requires a parameter",
215 switch (nodeTag(def
->arg
))
218 return ((TypeName
*) def
->arg
)->names
;
220 return (List
*) def
->arg
;
222 /* Allow quoted name for backwards compatibility */
223 return list_make1(def
->arg
);
226 (errcode(ERRCODE_SYNTAX_ERROR
),
227 errmsg("argument of %s must be a name",
230 return NIL
; /* keep compiler quiet */
234 * Extract a TypeName from a DefElem.
236 * Note: we do not accept a List arg here, because the parser will only
237 * return a bare List when the name looks like an operator name.
240 defGetTypeName(DefElem
*def
)
242 if (def
->arg
== NULL
)
244 (errcode(ERRCODE_SYNTAX_ERROR
),
245 errmsg("%s requires a parameter",
247 switch (nodeTag(def
->arg
))
250 return (TypeName
*) def
->arg
;
252 /* Allow quoted typename for backwards compatibility */
253 return makeTypeNameFromNameList(list_make1(def
->arg
));
256 (errcode(ERRCODE_SYNTAX_ERROR
),
257 errmsg("argument of %s must be a type name",
260 return NULL
; /* keep compiler quiet */
264 * Extract a type length indicator (either absolute bytes, or
265 * -1 for "variable") from a DefElem.
268 defGetTypeLength(DefElem
*def
)
270 if (def
->arg
== NULL
)
272 (errcode(ERRCODE_SYNTAX_ERROR
),
273 errmsg("%s requires a parameter",
275 switch (nodeTag(def
->arg
))
278 return intVal(def
->arg
);
281 (errcode(ERRCODE_SYNTAX_ERROR
),
282 errmsg("%s requires an integer value",
286 if (pg_strcasecmp(strVal(def
->arg
), "variable") == 0)
287 return -1; /* variable length */
290 /* cope if grammar chooses to believe "variable" is a typename */
291 if (pg_strcasecmp(TypeNameToString((TypeName
*) def
->arg
),
293 return -1; /* variable length */
296 /* must be an operator name */
299 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(def
->arg
));
302 (errcode(ERRCODE_SYNTAX_ERROR
),
303 errmsg("invalid argument for %s: \"%s\"",
304 def
->defname
, defGetString(def
))));
305 return 0; /* keep compiler quiet */
309 * Create a DefElem setting "oids" to the specified value.
312 defWithOids(bool value
)
314 DefElem
*f
= makeNode(DefElem
);
317 f
->arg
= (Node
*) makeInteger(value
);