This patch makes pg_get_constraintdef support UNIQUE, PRIMARY KEY and
authorBruce Momjian
Thu, 13 Feb 2003 05:10:39 +0000 (05:10 +0000)
committerBruce Momjian
Thu, 13 Feb 2003 05:10:39 +0000 (05:10 +0000)
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

index 10ee725b30ab5bc46a2d5999001eb67a2ce44d80..205ffd7540b123e27fbe46e7435336bac8f621ec 100644 (file)
@@ -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);