|
3 | 3 | * back to source text
|
4 | 4 | *
|
5 | 5 | * 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 $ |
7 | 7 | *
|
8 | 8 | * This software is copyrighted by Jan Wieck - Hamburg.
|
9 | 9 | *
|
@@ -546,9 +546,6 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
|
546 | 546 | *
|
547 | 547 | * Returns the definition for the constraint, ie, everything that needs to
|
548 | 548 | * 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. |
552 | 549 | */
|
553 | 550 | Datum
|
554 | 551 | pg_get_constraintdef(PG_FUNCTION_ARGS)
|
@@ -698,10 +695,53 @@ pg_get_constraintdef(PG_FUNCTION_ARGS)
|
698 | 695 |
|
699 | 696 | break;
|
700 | 697 | }
|
| 698 | + case CONSTRAINT_PRIMARY: |
| 699 | + case CONSTRAINT_UNIQUE: |
| 700 | + { |
| 701 | + Datum val; |
| 702 | + bool isnull; |
701 | 703 |
|
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 | + } |
705 | 745 | default:
|
706 | 746 | elog(ERROR, "pg_get_constraintdef: unsupported constraint type '%c'",
|
707 | 747 | conForm->contype);
|
|
0 commit comments