1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-31 17:02:12 +03:00

Remove the recently added node types ReloptElem and OptionDefElem in favor

of adding optional namespace and action fields to DefElem.  Having three
node types that do essentially the same thing bloats the code and leads
to errors of confusion, such as in yesterday's bug report from Khee Chin.
This commit is contained in:
Tom Lane
2009-04-04 21:12:31 +00:00
parent c973051ae6
commit 090173a3f9
17 changed files with 208 additions and 333 deletions

View File

@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/define.c,v 1.103 2009/02/02 19:31:38 alvherre Exp $
* $PostgreSQL: pgsql/src/backend/commands/define.c,v 1.104 2009/04/04 21:12:31 tgl Exp $
*
* DESCRIPTION
* The "DefineFoo" routines take the parse tree and pick out the
@@ -55,20 +55,24 @@ case_translate_language_name(const char *input)
}
static char *
nodeGetString(Node *value, char *name)
/*
* Extract a string value (otherwise uninterpreted) from a DefElem.
*/
char *
defGetString(DefElem *def)
{
if (value == NULL)
if (def->arg == NULL)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("%s requires a parameter", name)));
switch (nodeTag(value))
errmsg("%s requires a parameter",
def->defname)));
switch (nodeTag(def->arg))
{
case T_Integer:
{
char *str = palloc(32);
snprintf(str, 32, "%ld", (long) intVal(value));
snprintf(str, 32, "%ld", (long) intVal(def->arg));
return str;
}
case T_Float:
@@ -77,28 +81,19 @@ nodeGetString(Node *value, char *name)
* T_Float values are kept in string form, so this type cheat
* works (and doesn't risk losing precision)
*/
return strVal(value);
return strVal(def->arg);
case T_String:
return strVal(value);
return strVal(def->arg);
case T_TypeName:
return TypeNameToString((TypeName *) value);
return TypeNameToString((TypeName *) def->arg);
case T_List:
return NameListToString((List *) value);
return NameListToString((List *) def->arg);
default:
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(value));
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
}
return NULL; /* keep compiler quiet */
}
/*
* Extract a string value (otherwise uninterpreted) from a DefElem.
*/
char *
defGetString(DefElem *def)
{
return nodeGetString(def->arg, def->defname);
}
/*
* Extract a numeric value (actually double) from a DefElem.
*/
@@ -125,22 +120,25 @@ defGetNumeric(DefElem *def)
return 0; /* keep compiler quiet */
}
static bool
nodeGetBoolean(Node *value, char *name)
/*
* Extract a boolean value from a DefElem.
*/
bool
defGetBoolean(DefElem *def)
{
/*
* If no parameter given, assume "true" is meant.
*/
if (value == NULL)
if (def->arg == NULL)
return true;
/*
* Allow 0, 1, "true", "false"
*/
switch (nodeTag(value))
switch (nodeTag(def->arg))
{
case T_Integer:
switch (intVal(value))
switch (intVal(def->arg))
{
case 0:
return false;
@@ -153,7 +151,7 @@ nodeGetBoolean(Node *value, char *name)
break;
default:
{
char *sval = nodeGetString(value, name);
char *sval = defGetString(def);
if (pg_strcasecmp(sval, "true") == 0)
return true;
@@ -165,19 +163,11 @@ nodeGetBoolean(Node *value, char *name)
}
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("%s requires a Boolean value", name)));
errmsg("%s requires a Boolean value",
def->defname)));
return false; /* keep compiler quiet */
}
/*
* Extract a boolean value from a DefElem.
*/
bool
defGetBoolean(DefElem *def)
{
return nodeGetBoolean(def->arg, def->defname);
}
/*
* Extract an int64 value from a DefElem.
*/
@@ -315,35 +305,11 @@ defGetTypeLength(DefElem *def)
return 0; /* keep compiler quiet */
}
/*
* Extract a string value (otherwise uninterpreted) from a ReloptElem.
* Create a DefElem setting "oids" to the specified value.
*/
char *
reloptGetString(ReloptElem *relopt)
DefElem *
defWithOids(bool value)
{
return nodeGetString(relopt->arg, relopt->optname);
}
/*
* Extract a boolean value from a ReloptElem.
*/
bool
reloptGetBoolean(ReloptElem *relopt)
{
return nodeGetBoolean(relopt->arg, relopt->optname);
}
/*
* Create a ReloptElem setting "oids" to the specified value.
*/
ReloptElem *
reloptWithOids(bool value)
{
ReloptElem *f = makeNode(ReloptElem);
f->optname = "oids";
f->nmspc = NULL;
f->arg = (Node *) makeInteger(value);
return f;
return makeDefElem("oids", (Node *) makeInteger(value));
}

View File

@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/foreigncmds.c,v 1.6 2009/02/24 10:06:32 petere Exp $
* $PostgreSQL: pgsql/src/backend/commands/foreigncmds.c,v 1.7 2009/04/04 21:12:31 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -76,33 +76,31 @@ optionListToArray(List *options)
/*
* Transform the list of OptionDefElem into list of generic options.
* The result is converted to array of text suitable for storing in
* options.
* Transform a list of DefElem into text array format. This is substantially
* the same thing as optionListToArray(), except we recognize SET/ADD/DROP
* actions for modifying an existing list of options, which is passed in
* Datum form as oldOptions. Also, if fdwvalidator isn't InvalidOid
* it specifies a validator function to call on the result.
*
* Returns the array in the form of a Datum, or PointerGetDatum(NULL)
* if the list is empty.
*
* This is used by CREATE/ALTER of FOREIGN DATA WRAPPER/SERVER/USER
* MAPPING. In the ALTER cases, oldOptions is the current text array
* of options.
* This is used by CREATE/ALTER of FOREIGN DATA WRAPPER/SERVER/USER MAPPING.
*/
static Datum
transformGenericOptions(Datum oldOptions,
List *optionDefList,
GenericOptionFlags flags,
ForeignDataWrapper *fdw,
List *options,
Oid fdwvalidator)
{
List *resultOptions = untransformRelOptions(oldOptions);
ListCell *optcell;
Datum result;
foreach(optcell, optionDefList)
foreach(optcell, options)
{
OptionDefElem *od = lfirst(optcell);
ListCell *cell;
ListCell *prev = NULL;
DefElem *od = lfirst(optcell);
ListCell *cell;
ListCell *prev = NULL;
/*
* Find the element in resultOptions. We need this for
@@ -112,7 +110,7 @@ transformGenericOptions(Datum oldOptions,
{
DefElem *def = lfirst(cell);
if (strcmp(def->defname, od->def->defname) == 0)
if (strcmp(def->defname, od->defname) == 0)
break;
else
prev = cell;
@@ -121,41 +119,42 @@ transformGenericOptions(Datum oldOptions,
/*
* It is possible to perform multiple SET/DROP actions on the
* same option. The standard permits this, as long as the
* options to be added are unique.
* options to be added are unique. Note that an unspecified
* action is taken to be ADD.
*/
switch (od->alter_op)
switch (od->defaction)
{
case ALTER_OPT_DROP:
case DEFELEM_DROP:
if (!cell)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("option \"%s\" not found",
od->def->defname)));
od->defname)));
resultOptions = list_delete_cell(resultOptions, cell, prev);
break;
case ALTER_OPT_SET:
case DEFELEM_SET:
if (!cell)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("option \"%s\" not found",
od->def->defname)));
lfirst(cell) = od->def;
od->defname)));
lfirst(cell) = od;
break;
case ALTER_OPT_ADD:
case DEFELEM_ADD:
case DEFELEM_UNSPEC:
if (cell)
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("option \"%s\" provided more than once",
od->def->defname)));
resultOptions = lappend(resultOptions, od->def);
od->defname)));
resultOptions = lappend(resultOptions, od);
break;
default:
elog(ERROR, "unrecognized action %d on option \"%s\"",
od->alter_op, od->def->defname);
(int) od->defaction, od->defname);
break;
}
}
@@ -163,7 +162,7 @@ transformGenericOptions(Datum oldOptions,
result = optionListToArray(resultOptions);
if (fdwvalidator)
OidFunctionCall2(fdwvalidator, result, 0);
OidFunctionCall2(fdwvalidator, result, (Datum) 0);
return result;
}
@@ -386,7 +385,6 @@ CreateForeignDataWrapper(CreateFdwStmt *stmt)
nulls[Anum_pg_foreign_data_wrapper_fdwacl - 1] = true;
fdwoptions = transformGenericOptions(PointerGetDatum(NULL), stmt->options,
FdwOpt, NULL,
fdwvalidator);
if (PointerIsValid(DatumGetPointer(fdwoptions)))
@@ -504,8 +502,7 @@ AlterForeignDataWrapper(AlterFdwStmt *stmt)
datum = PointerGetDatum(NULL);
/* Transform the options */
datum = transformGenericOptions(datum, stmt->options, FdwOpt,
NULL, fdwvalidator);
datum = transformGenericOptions(datum, stmt->options, fdwvalidator);
if (PointerIsValid(DatumGetPointer(datum)))
repl_val[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = datum;
@@ -672,7 +669,6 @@ CreateForeignServer(CreateForeignServerStmt *stmt)
/* Add server options */
srvoptions = transformGenericOptions(PointerGetDatum(NULL), stmt->options,
ServerOpt, fdw,
fdw->fdwvalidator);
if (PointerIsValid(DatumGetPointer(srvoptions)))
@@ -770,8 +766,8 @@ AlterForeignServer(AlterForeignServerStmt *stmt)
datum = PointerGetDatum(NULL);
/* Prepare the options array */
datum = transformGenericOptions(datum, stmt->options, ServerOpt,
fdw, fdw->fdwvalidator);
datum = transformGenericOptions(datum, stmt->options,
fdw->fdwvalidator);
if (PointerIsValid(DatumGetPointer(datum)))
repl_val[Anum_pg_foreign_server_srvoptions - 1] = datum;
@@ -942,8 +938,7 @@ CreateUserMapping(CreateUserMappingStmt *stmt)
/* Add user options */
useoptions = transformGenericOptions(PointerGetDatum(NULL), stmt->options,
UserMappingOpt,
fdw, fdw->fdwvalidator);
fdw->fdwvalidator);
if (PointerIsValid(DatumGetPointer(useoptions)))
values[Anum_pg_user_mapping_umoptions - 1] = useoptions;
@@ -1037,8 +1032,8 @@ AlterUserMapping(AlterUserMappingStmt *stmt)
datum = PointerGetDatum(NULL);
/* Prepare the options array */
datum = transformGenericOptions(datum, stmt->options, UserMappingOpt,
fdw, fdw->fdwvalidator);
datum = transformGenericOptions(datum, stmt->options,
fdw->fdwvalidator);
if (PointerIsValid(DatumGetPointer(datum)))
repl_val[Anum_pg_user_mapping_umoptions - 1] = datum;

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.158 2009/02/02 19:31:38 alvherre Exp $
* $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.159 2009/04/04 21:12:31 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -198,7 +198,7 @@ DefineSequence(CreateSeqStmt *seq)
stmt->relation = seq->sequence;
stmt->inhRelations = NIL;
stmt->constraints = NIL;
stmt->options = list_make1(reloptWithOids(false));
stmt->options = list_make1(defWithOids(false));
stmt->oncommit = ONCOMMIT_NOOP;
stmt->tablespacename = NULL;

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.132 2009/02/24 01:38:09 tgl Exp $
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.133 2009/04/04 21:12:31 tgl Exp $
*
* DESCRIPTION
* The "DefineFoo" routines take the parse tree and pick out the
@@ -1496,7 +1496,7 @@ DefineCompositeType(const RangeVar *typevar, List *coldeflist)
createStmt->tableElts = coldeflist;
createStmt->inhRelations = NIL;
createStmt->constraints = NIL;
createStmt->options = list_make1(reloptWithOids(false));
createStmt->options = list_make1(defWithOids(false));
createStmt->oncommit = ONCOMMIT_NOOP;
createStmt->tablespacename = NULL;

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/view.c,v 1.114 2009/02/02 19:31:39 alvherre Exp $
* $PostgreSQL: pgsql/src/backend/commands/view.c,v 1.115 2009/04/04 21:12:31 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -229,7 +229,7 @@ DefineVirtualRelation(const RangeVar *relation, List *tlist, bool replace)
createStmt->tableElts = attrList;
createStmt->inhRelations = NIL;
createStmt->constraints = NIL;
createStmt->options = list_make1(reloptWithOids(false));
createStmt->options = list_make1(defWithOids(false));
createStmt->oncommit = ONCOMMIT_NOOP;
createStmt->tablespacename = NULL;