mirror of
https://github.com/postgres/postgres.git
synced 2025-11-22 12:22:45 +03:00
Add TupleDescGetDefault()
This unifies some repetitive code. Note: I didn't push the "not found" error message into the new function, even though all existing callers would be able to make use of it. Using the existing error handling as-is would probably require exposing the Relation type via tupdesc.h, which doesn't seem desirable. (Or even if we changed it to just report the OID, it would inject the concept of a relation containing the tuple descriptor into tupdesc.h, which might be a layering violation. Perhaps some further improvements could be considered here separately.) Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da%40eisentraut.org
This commit is contained in:
@@ -927,3 +927,28 @@ BuildDescFromLists(const List *names, const List *types, const List *typmods, co
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get default expression (or NULL if none) for the given attribute number.
|
||||
*/
|
||||
Node *
|
||||
TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
|
||||
{
|
||||
Node *result = NULL;
|
||||
|
||||
if (tupdesc->constr)
|
||||
{
|
||||
AttrDefault *attrdef = tupdesc->constr->defval;
|
||||
|
||||
for (int i = 0; i < tupdesc->constr->num_defval; i++)
|
||||
{
|
||||
if (attrdef[i].adnum == attnum)
|
||||
{
|
||||
result = stringToNode(attrdef[i].adbin);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user