diff options
author | Bruce Momjian <bruce@momjian.us> | 2003-02-13 05:10:39 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 2003-02-13 05:10:39 +0000 |
commit | 8add2e1bcafd92a06fada1098b110638a3d4b7f6 (patch) | |
tree | 75cb8c616cdc613de14b82319bb577a2b26d4fe5 /src/backend/utils/adt/ruleutils.c | |
parent | d21de3b12119e9b35c9f30fae0f053410a80b906 (diff) | |
download | postgresql-8add2e1bcafd92a06fada1098b110638a3d4b7f6.tar.gz postgresql-8add2e1bcafd92a06fada1098b110638a3d4b7f6.zip |
This patch makes pg_get_constraintdef support UNIQUE, PRIMARY KEY and
CHECK constraints.
There are apparently no other types of constraint in pg_constraint, so
now all bases are covered. Also, this patch assumes that consrc for a
CHECK constraint is always bracketed so that it's not necessary to add
extra brackets.
Christopher Kings-Lynne
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r-- | src/backend/utils/adt/ruleutils.c | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 10ee725b30a..205ffd7540b 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -3,7 +3,7 @@ * back to source text * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.134 2003/02/03 21:15:44 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.135 2003/02/13 05:10:39 momjian Exp $ * * This software is copyrighted by Jan Wieck - Hamburg. * @@ -546,9 +546,6 @@ pg_get_indexdef(PG_FUNCTION_ARGS) * * Returns the definition for the constraint, ie, everything that needs to * appear after "ALTER TABLE ... ADD CONSTRAINT <constraintname>". - * - * XXX The present implementation only works for foreign-key constraints, but - * it could and should handle anything pg_constraint stores. */ Datum pg_get_constraintdef(PG_FUNCTION_ARGS) @@ -698,10 +695,53 @@ pg_get_constraintdef(PG_FUNCTION_ARGS) break; } + case CONSTRAINT_PRIMARY: + case CONSTRAINT_UNIQUE: + { + Datum val; + bool isnull; - /* - * XXX Add more code here for other contypes - */ + /* Start off the constraint definition */ + if (conForm->contype == CONSTRAINT_PRIMARY) + appendStringInfo(&buf, "PRIMARY KEY ("); + else + appendStringInfo(&buf, "UNIQUE ("); + + /* Fetch and build target column list */ + val = heap_getattr(tup, Anum_pg_constraint_conkey, + RelationGetDescr(conDesc), &isnull); + if (isnull) + elog(ERROR, "pg_get_constraintdef: Null conkey for constraint %u", + constraintId); + + decompile_column_index_array(val, conForm->conrelid, &buf); + + appendStringInfo(&buf, ")"); + + break; + } + case CONSTRAINT_CHECK: + { + Datum val; + bool isnull; + + /* Start off the constraint definition */ + /* The consrc for CHECK constraints always seems to be + bracketed, so we don't add extra brackets here. */ + appendStringInfo(&buf, "CHECK "); + + /* Fetch constraint source */ + val = heap_getattr(tup, Anum_pg_constraint_consrc, + RelationGetDescr(conDesc), &isnull); + if (isnull) + elog(ERROR, "pg_get_constraintdef: Null consrc for constraint %u", + constraintId); + + /* Append the constraint source */ + appendStringInfo(&buf, DatumGetCString(DirectFunctionCall1(textout, val))); + + break; + } default: elog(ERROR, "pg_get_constraintdef: unsupported constraint type '%c'", conForm->contype); |