Skip to content

Commit 8add2e1

Browse files
committed
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
1 parent d21de3b commit 8add2e1

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* back to source text
44
*
55
* IDENTIFICATION
6-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.134 2003/02/03 21:15:44 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.135 2003/02/13 05:10:39 momjian Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -546,9 +546,6 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
546546
*
547547
* Returns the definition for the constraint, ie, everything that needs to
548548
* appear after "ALTER TABLE ... ADD CONSTRAINT <constraintname>".
549-
*
550-
* XXX The present implementation only works for foreign-key constraints, but
551-
* it could and should handle anything pg_constraint stores.
552549
*/
553550
Datum
554551
pg_get_constraintdef(PG_FUNCTION_ARGS)
@@ -698,10 +695,53 @@ pg_get_constraintdef(PG_FUNCTION_ARGS)
698695

699696
break;
700697
}
698+
case CONSTRAINT_PRIMARY:
699+
case CONSTRAINT_UNIQUE:
700+
{
701+
Datum val;
702+
bool isnull;
701703

702-
/*
703-
* XXX Add more code here for other contypes
704-
*/
704+
/* Start off the constraint definition */
705+
if (conForm->contype == CONSTRAINT_PRIMARY)
706+
appendStringInfo(&buf, "PRIMARY KEY (");
707+
else
708+
appendStringInfo(&buf, "UNIQUE (");
709+
710+
/* Fetch and build target column list */
711+
val = heap_getattr(tup, Anum_pg_constraint_conkey,
712+
RelationGetDescr(conDesc), &isnull);
713+
if (isnull)
714+
elog(ERROR, "pg_get_constraintdef: Null conkey for constraint %u",
715+
constraintId);
716+
717+
decompile_column_index_array(val, conForm->conrelid, &buf);
718+
719+
appendStringInfo(&buf, ")");
720+
721+
break;
722+
}
723+
case CONSTRAINT_CHECK:
724+
{
725+
Datum val;
726+
bool isnull;
727+
728+
/* Start off the constraint definition */
729+
/* The consrc for CHECK constraints always seems to be
730+
bracketed, so we don't add extra brackets here. */
731+
appendStringInfo(&buf, "CHECK ");
732+
733+
/* Fetch constraint source */
734+
val = heap_getattr(tup, Anum_pg_constraint_consrc,
735+
RelationGetDescr(conDesc), &isnull);
736+
if (isnull)
737+
elog(ERROR, "pg_get_constraintdef: Null consrc for constraint %u",
738+
constraintId);
739+
740+
/* Append the constraint source */
741+
appendStringInfo(&buf, DatumGetCString(DirectFunctionCall1(textout, val)));
742+
743+
break;
744+
}
705745
default:
706746
elog(ERROR, "pg_get_constraintdef: unsupported constraint type '%c'",
707747
conForm->contype);

0 commit comments

Comments
 (0)