From 8add2e1bcafd92a06fada1098b110638a3d4b7f6 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Thu, 13 Feb 2003 05:10:39 +0000 Subject: [PATCH] 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 --- src/backend/utils/adt/ruleutils.c | 54 +++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 10ee725b30..205ffd7540 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 ". - * - * 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);