Force a checkpoint in CREATE DATABASE before starting to copy the files,
[PostgreSQL.git] / src / backend / commands / define.c
blob0aec124991e70707d2f622a15d22c9f716e49c7f
1 /*-------------------------------------------------------------------------
3 * define.c
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
11 * IDENTIFICATION
12 * $PostgreSQL$
14 * DESCRIPTION
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.
21 * NOTES
22 * These things must be defined and committed in the following order:
23 * "create function":
24 * input/output, recv/send procedures
25 * "create type":
26 * type
27 * "create operator":
28 * operators
31 *-------------------------------------------------------------------------
33 #include "postgres.h"
35 #include <ctype.h>
36 #include <math.h>
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
51 char *
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.
61 char *
62 defGetString(DefElem *def)
64 if (def->arg == NULL)
65 ereport(ERROR,
66 (errcode(ERRCODE_SYNTAX_ERROR),
67 errmsg("%s requires a parameter",
68 def->defname)));
69 switch (nodeTag(def->arg))
71 case T_Integer:
73 char *str = palloc(32);
75 snprintf(str, 32, "%ld", (long) intVal(def->arg));
76 return str;
78 case T_Float:
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);
85 case T_String:
86 return strVal(def->arg);
87 case T_TypeName:
88 return TypeNameToString((TypeName *) def->arg);
89 case T_List:
90 return NameListToString((List *) def->arg);
91 default:
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.
100 double
101 defGetNumeric(DefElem *def)
103 if (def->arg == NULL)
104 ereport(ERROR,
105 (errcode(ERRCODE_SYNTAX_ERROR),
106 errmsg("%s requires a numeric value",
107 def->defname)));
108 switch (nodeTag(def->arg))
110 case T_Integer:
111 return (double) intVal(def->arg);
112 case T_Float:
113 return floatVal(def->arg);
114 default:
115 ereport(ERROR,
116 (errcode(ERRCODE_SYNTAX_ERROR),
117 errmsg("%s requires a numeric value",
118 def->defname)));
120 return 0; /* keep compiler quiet */
124 * Extract a boolean value from a DefElem.
126 bool
127 defGetBoolean(DefElem *def)
130 * If no parameter given, assume "true" is meant.
132 if (def->arg == NULL)
133 return true;
136 * Allow 0, 1, "true", "false"
138 switch (nodeTag(def->arg))
140 case T_Integer:
141 switch (intVal(def->arg))
143 case 0:
144 return false;
145 case 1:
146 return true;
147 default:
148 /* otherwise, error out below */
149 break;
151 break;
152 default:
154 char *sval = defGetString(def);
156 if (pg_strcasecmp(sval, "true") == 0)
157 return true;
158 if (pg_strcasecmp(sval, "false") == 0)
159 return false;
162 break;
164 ereport(ERROR,
165 (errcode(ERRCODE_SYNTAX_ERROR),
166 errmsg("%s requires a Boolean value",
167 def->defname)));
168 return false; /* keep compiler quiet */
172 * Extract an int64 value from a DefElem.
174 int64
175 defGetInt64(DefElem *def)
177 if (def->arg == NULL)
178 ereport(ERROR,
179 (errcode(ERRCODE_SYNTAX_ERROR),
180 errmsg("%s requires a numeric value",
181 def->defname)));
182 switch (nodeTag(def->arg))
184 case T_Integer:
185 return (int64) intVal(def->arg);
186 case T_Float:
189 * Values too large for int4 will be represented as Float
190 * constants by the lexer. Accept these if they are valid int8
191 * strings.
193 return DatumGetInt64(DirectFunctionCall1(int8in,
194 CStringGetDatum(strVal(def->arg))));
195 default:
196 ereport(ERROR,
197 (errcode(ERRCODE_SYNTAX_ERROR),
198 errmsg("%s requires a numeric value",
199 def->defname)));
201 return 0; /* keep compiler quiet */
205 * Extract a possibly-qualified name (as a List of Strings) from a DefElem.
207 List *
208 defGetQualifiedName(DefElem *def)
210 if (def->arg == NULL)
211 ereport(ERROR,
212 (errcode(ERRCODE_SYNTAX_ERROR),
213 errmsg("%s requires a parameter",
214 def->defname)));
215 switch (nodeTag(def->arg))
217 case T_TypeName:
218 return ((TypeName *) def->arg)->names;
219 case T_List:
220 return (List *) def->arg;
221 case T_String:
222 /* Allow quoted name for backwards compatibility */
223 return list_make1(def->arg);
224 default:
225 ereport(ERROR,
226 (errcode(ERRCODE_SYNTAX_ERROR),
227 errmsg("argument of %s must be a name",
228 def->defname)));
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.
239 TypeName *
240 defGetTypeName(DefElem *def)
242 if (def->arg == NULL)
243 ereport(ERROR,
244 (errcode(ERRCODE_SYNTAX_ERROR),
245 errmsg("%s requires a parameter",
246 def->defname)));
247 switch (nodeTag(def->arg))
249 case T_TypeName:
250 return (TypeName *) def->arg;
251 case T_String:
252 /* Allow quoted typename for backwards compatibility */
253 return makeTypeNameFromNameList(list_make1(def->arg));
254 default:
255 ereport(ERROR,
256 (errcode(ERRCODE_SYNTAX_ERROR),
257 errmsg("argument of %s must be a type name",
258 def->defname)));
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)
271 ereport(ERROR,
272 (errcode(ERRCODE_SYNTAX_ERROR),
273 errmsg("%s requires a parameter",
274 def->defname)));
275 switch (nodeTag(def->arg))
277 case T_Integer:
278 return intVal(def->arg);
279 case T_Float:
280 ereport(ERROR,
281 (errcode(ERRCODE_SYNTAX_ERROR),
282 errmsg("%s requires an integer value",
283 def->defname)));
284 break;
285 case T_String:
286 if (pg_strcasecmp(strVal(def->arg), "variable") == 0)
287 return -1; /* variable length */
288 break;
289 case T_TypeName:
290 /* cope if grammar chooses to believe "variable" is a typename */
291 if (pg_strcasecmp(TypeNameToString((TypeName *) def->arg),
292 "variable") == 0)
293 return -1; /* variable length */
294 break;
295 case T_List:
296 /* must be an operator name */
297 break;
298 default:
299 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
301 ereport(ERROR,
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.
311 DefElem *
312 defWithOids(bool value)
314 DefElem *f = makeNode(DefElem);
316 f->defname = "oids";
317 f->arg = (Node *) makeInteger(value);
318 return f;