1 /*-------------------------------------------------------------------------
4 * Support routines for various kinds of object creation.
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $PostgreSQL: pgsql/src/backend/commands/define.c,v 1.92 2004/12/31 21:59:41 pgsql Exp $
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 "parser/parse_type.h"
41 #include "parser/scansup.h"
42 #include "utils/int8.h"
46 * Translate the input language name to lower case, and truncate if needed.
48 * Returns a palloc'd string
51 case_translate_language_name(const char *input)
53 return downcase_truncate_identifier(input, strlen(input), false);
58 * Extract a string value (otherwise uninterpreted) from a DefElem.
61 defGetString(DefElem *def)
65 (errcode(ERRCODE_SYNTAX_ERROR),
66 errmsg("%s requires a parameter",
68 switch (nodeTag(def->arg))
72 char *str = palloc(32);
74 snprintf(str, 32, "%ld", (long) intVal(def->arg));
80 * T_Float values are kept in string form, so this type cheat
81 * works (and doesn't risk losing precision)
83 return strVal(def->arg);
85 return strVal(def->arg);
87 return TypeNameToString((TypeName *) def->arg);
89 return NameListToString((List *) def->arg);
91 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
93 return NULL; /* keep compiler quiet */
97 * Extract a numeric value (actually double) from a DefElem.
100 defGetNumeric(DefElem *def)
102 if (def->arg == NULL)
104 (errcode(ERRCODE_SYNTAX_ERROR),
105 errmsg("%s requires a numeric value",
107 switch (nodeTag(def->arg))
110 return (double) intVal(def->arg);
112 return floatVal(def->arg);
115 (errcode(ERRCODE_SYNTAX_ERROR),
116 errmsg("%s requires a numeric value",
119 return 0; /* keep compiler quiet */
123 * Extract a boolean value from a DefElem.
126 defGetBoolean(DefElem *def)
129 * Presently, boolean flags must simply be present or absent. Later we
130 * could allow 'flag = t', 'flag = f', etc.
132 if (def->arg == NULL)
135 (errcode(ERRCODE_SYNTAX_ERROR),
136 errmsg("%s does not take a parameter",
138 return false; /* keep compiler quiet */
142 * Extract an int64 value from a DefElem.
145 defGetInt64(DefElem *def)
147 if (def->arg == NULL)
149 (errcode(ERRCODE_SYNTAX_ERROR),
150 errmsg("%s requires a numeric value",
152 switch (nodeTag(def->arg))
155 return (int64) intVal(def->arg);
159 * Values too large for int4 will be represented as Float
160 * constants by the lexer. Accept these if they are valid
163 return DatumGetInt64(DirectFunctionCall1(int8in,
164 CStringGetDatum(strVal(def->arg))));
167 (errcode(ERRCODE_SYNTAX_ERROR),
168 errmsg("%s requires a numeric value",
171 return 0; /* keep compiler quiet */
175 * Extract a possibly-qualified name (as a List of Strings) from a DefElem.
178 defGetQualifiedName(DefElem *def)
180 if (def->arg == NULL)
182 (errcode(ERRCODE_SYNTAX_ERROR),
183 errmsg("%s requires a parameter",
185 switch (nodeTag(def->arg))
188 return ((TypeName *) def->arg)->names;
190 return (List *) def->arg;
192 /* Allow quoted name for backwards compatibility */
193 return list_make1(def->arg);
196 (errcode(ERRCODE_SYNTAX_ERROR),
197 errmsg("argument of %s must be a name",
200 return NIL; /* keep compiler quiet */
204 * Extract a TypeName from a DefElem.
206 * Note: we do not accept a List arg here, because the parser will only
207 * return a bare List when the name looks like an operator name.
210 defGetTypeName(DefElem *def)
212 if (def->arg == NULL)
214 (errcode(ERRCODE_SYNTAX_ERROR),
215 errmsg("%s requires a parameter",
217 switch (nodeTag(def->arg))
220 return (TypeName *) def->arg;
223 /* Allow quoted typename for backwards compatibility */
224 TypeName *n = makeNode(TypeName);
226 n->names = list_make1(def->arg);
232 (errcode(ERRCODE_SYNTAX_ERROR),
233 errmsg("argument of %s must be a type name",
236 return NULL; /* keep compiler quiet */
240 * Extract a type length indicator (either absolute bytes, or
241 * -1 for "variable") from a DefElem.
244 defGetTypeLength(DefElem *def)
246 if (def->arg == NULL)
248 (errcode(ERRCODE_SYNTAX_ERROR),
249 errmsg("%s requires a parameter",
251 switch (nodeTag(def->arg))
254 return intVal(def->arg);
257 (errcode(ERRCODE_SYNTAX_ERROR),
258 errmsg("%s requires an integer value",
262 if (pg_strcasecmp(strVal(def->arg), "variable") == 0)
263 return -1; /* variable length */
266 /* cope if grammar chooses to believe "variable" is a typename */
267 if (pg_strcasecmp(TypeNameToString((TypeName *) def->arg),
269 return -1; /* variable length */
272 /* must be an operator name */
275 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
278 (errcode(ERRCODE_SYNTAX_ERROR),
279 errmsg("invalid argument for %s: \"%s\"",
280 def->defname, defGetString(def))));
281 return 0; /* keep compiler quiet */