|
3 | 3 | * back to source text
|
4 | 4 | *
|
5 | 5 | * IDENTIFICATION
|
6 |
| - * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.115 2002/08/16 20:55:09 tgl Exp $ |
| 6 | + * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.116 2002/08/16 23:01:19 tgl Exp $ |
7 | 7 | *
|
8 | 8 | * This software is copyrighted by Jan Wieck - Hamburg.
|
9 | 9 | *
|
|
40 | 40 | #include <unistd.h>
|
41 | 41 | #include <fcntl.h>
|
42 | 42 |
|
| 43 | +#include "access/genam.h" |
| 44 | +#include "catalog/catname.h" |
43 | 45 | #include "catalog/heap.h"
|
44 | 46 | #include "catalog/index.h"
|
| 47 | +#include "catalog/indexing.h" |
45 | 48 | #include "catalog/namespace.h"
|
46 | 49 | #include "catalog/pg_cast.h"
|
| 50 | +#include "catalog/pg_constraint.h" |
47 | 51 | #include "catalog/pg_index.h"
|
48 | 52 | #include "catalog/pg_opclass.h"
|
49 | 53 | #include "catalog/pg_operator.h"
|
|
60 | 64 | #include "parser/parsetree.h"
|
61 | 65 | #include "rewrite/rewriteManip.h"
|
62 | 66 | #include "rewrite/rewriteSupport.h"
|
| 67 | +#include "utils/array.h" |
| 68 | +#include "utils/fmgroids.h" |
63 | 69 | #include "utils/lsyscache.h"
|
64 | 70 |
|
65 | 71 |
|
@@ -116,6 +122,8 @@ static char *query_getviewrule = "SELECT * FROM pg_catalog.pg_rewrite WHERE ev_c
|
116 | 122 | * ----------
|
117 | 123 | */
|
118 | 124 | static text *pg_do_getviewdef(Oid viewoid);
|
| 125 | +static void decompile_column_index_array(Datum column_index_array, Oid relId, |
| 126 | + StringInfo buf); |
119 | 127 | static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc);
|
120 | 128 | static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc);
|
121 | 129 | static void get_query_def(Query *query, StringInfo buf, List *parentnamespace,
|
@@ -528,6 +536,214 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
|
528 | 536 | }
|
529 | 537 |
|
530 | 538 |
|
| 539 | +/* |
| 540 | + * pg_get_constraintdef |
| 541 | + * |
| 542 | + * Returns the definition for the constraint, ie, everything that needs to |
| 543 | + * appear after "ALTER TABLE ... ADD CONSTRAINT <constraintname>". |
| 544 | + * |
| 545 | + * XXX The present implementation only works for foreign-key constraints, but |
| 546 | + * it could and should handle anything pg_constraint stores. |
| 547 | + */ |
| 548 | +Datum |
| 549 | +pg_get_constraintdef(PG_FUNCTION_ARGS) |
| 550 | +{ |
| 551 | + Oid constraintId = PG_GETARG_OID(0); |
| 552 | + text *result; |
| 553 | + StringInfoData buf; |
| 554 | + int len; |
| 555 | + Relation conDesc; |
| 556 | + SysScanDesc conscan; |
| 557 | + ScanKeyData skey[1]; |
| 558 | + HeapTuple tup; |
| 559 | + Form_pg_constraint conForm; |
| 560 | + |
| 561 | + /* |
| 562 | + * Fetch the pg_constraint row. There's no syscache for pg_constraint |
| 563 | + * so we must do it the hard way. |
| 564 | + */ |
| 565 | + conDesc = heap_openr(ConstraintRelationName, AccessShareLock); |
| 566 | + |
| 567 | + ScanKeyEntryInitialize(&skey[0], 0x0, |
| 568 | + ObjectIdAttributeNumber, F_OIDEQ, |
| 569 | + ObjectIdGetDatum(constraintId)); |
| 570 | + |
| 571 | + conscan = systable_beginscan(conDesc, ConstraintOidIndex, true, |
| 572 | + SnapshotNow, 1, skey); |
| 573 | + |
| 574 | + tup = systable_getnext(conscan); |
| 575 | + if (!HeapTupleIsValid(tup)) |
| 576 | + elog(ERROR, "Failed to find constraint with OID %u", constraintId); |
| 577 | + conForm = (Form_pg_constraint) GETSTRUCT(tup); |
| 578 | + |
| 579 | + initStringInfo(&buf); |
| 580 | + |
| 581 | + switch (conForm->contype) |
| 582 | + { |
| 583 | + case CONSTRAINT_FOREIGN: |
| 584 | + { |
| 585 | + Datum val; |
| 586 | + bool isnull; |
| 587 | + const char *string; |
| 588 | + |
| 589 | + /* Start off the constraint definition */ |
| 590 | + appendStringInfo(&buf, "FOREIGN KEY ("); |
| 591 | + |
| 592 | + /* Fetch and build referencing-column list */ |
| 593 | + val = heap_getattr(tup, Anum_pg_constraint_conkey, |
| 594 | + RelationGetDescr(conDesc), &isnull); |
| 595 | + if (isnull) |
| 596 | + elog(ERROR, "pg_get_constraintdef: Null conkey for constraint %u", |
| 597 | + constraintId); |
| 598 | + |
| 599 | + decompile_column_index_array(val, conForm->conrelid, &buf); |
| 600 | + |
| 601 | + /* add foreign relation name */ |
| 602 | + appendStringInfo(&buf, ") REFERENCES %s(", |
| 603 | + generate_relation_name(conForm->confrelid)); |
| 604 | + |
| 605 | + /* Fetch and build referenced-column list */ |
| 606 | + val = heap_getattr(tup, Anum_pg_constraint_confkey, |
| 607 | + RelationGetDescr(conDesc), &isnull); |
| 608 | + if (isnull) |
| 609 | + elog(ERROR, "pg_get_constraintdef: Null confkey for constraint %u", |
| 610 | + constraintId); |
| 611 | + |
| 612 | + decompile_column_index_array(val, conForm->confrelid, &buf); |
| 613 | + |
| 614 | + appendStringInfo(&buf, ")"); |
| 615 | + |
| 616 | + /* Add match type */ |
| 617 | + switch (conForm->confmatchtype) |
| 618 | + { |
| 619 | + case FKCONSTR_MATCH_FULL: |
| 620 | + string = " MATCH FULL"; |
| 621 | + break; |
| 622 | + case FKCONSTR_MATCH_PARTIAL: |
| 623 | + string = " MATCH PARTIAL"; |
| 624 | + break; |
| 625 | + case FKCONSTR_MATCH_UNSPECIFIED: |
| 626 | + string = ""; |
| 627 | + break; |
| 628 | + default: |
| 629 | + elog(ERROR, "pg_get_constraintdef: Unknown confmatchtype '%c' for constraint %u", |
| 630 | + conForm->confmatchtype, constraintId); |
| 631 | + string = ""; /* keep compiler quiet */ |
| 632 | + break; |
| 633 | + } |
| 634 | + appendStringInfo(&buf, "%s", string); |
| 635 | + |
| 636 | + /* Add ON UPDATE and ON DELETE clauses */ |
| 637 | + switch (conForm->confupdtype) |
| 638 | + { |
| 639 | + case FKCONSTR_ACTION_NOACTION: |
| 640 | + string = "NO ACTION"; |
| 641 | + break; |
| 642 | + case FKCONSTR_ACTION_RESTRICT: |
| 643 | + string = "RESTRICT"; |
| 644 | + break; |
| 645 | + case FKCONSTR_ACTION_CASCADE: |
| 646 | + string = "CASCADE"; |
| 647 | + break; |
| 648 | + case FKCONSTR_ACTION_SETNULL: |
| 649 | + string = "SET NULL"; |
| 650 | + break; |
| 651 | + case FKCONSTR_ACTION_SETDEFAULT: |
| 652 | + string = "SET DEFAULT"; |
| 653 | + break; |
| 654 | + default: |
| 655 | + elog(ERROR, "pg_get_constraintdef: Unknown confupdtype '%c' for constraint %u", |
| 656 | + conForm->confupdtype, constraintId); |
| 657 | + string = ""; /* keep compiler quiet */ |
| 658 | + break; |
| 659 | + } |
| 660 | + appendStringInfo(&buf, " ON UPDATE %s", string); |
| 661 | + |
| 662 | + switch (conForm->confdeltype) |
| 663 | + { |
| 664 | + case FKCONSTR_ACTION_NOACTION: |
| 665 | + string = "NO ACTION"; |
| 666 | + break; |
| 667 | + case FKCONSTR_ACTION_RESTRICT: |
| 668 | + string = "RESTRICT"; |
| 669 | + break; |
| 670 | + case FKCONSTR_ACTION_CASCADE: |
| 671 | + string = "CASCADE"; |
| 672 | + break; |
| 673 | + case FKCONSTR_ACTION_SETNULL: |
| 674 | + string = "SET NULL"; |
| 675 | + break; |
| 676 | + case FKCONSTR_ACTION_SETDEFAULT: |
| 677 | + string = "SET DEFAULT"; |
| 678 | + break; |
| 679 | + default: |
| 680 | + elog(ERROR, "pg_get_constraintdef: Unknown confdeltype '%c' for constraint %u", |
| 681 | + conForm->confdeltype, constraintId); |
| 682 | + string = ""; /* keep compiler quiet */ |
| 683 | + break; |
| 684 | + } |
| 685 | + appendStringInfo(&buf, " ON DELETE %s", string); |
| 686 | + |
| 687 | + break; |
| 688 | + } |
| 689 | + |
| 690 | + /* |
| 691 | + * XXX Add more code here for other contypes |
| 692 | + */ |
| 693 | + default: |
| 694 | + elog(ERROR, "pg_get_constraintdef: unsupported constraint type '%c'", |
| 695 | + conForm->contype); |
| 696 | + break; |
| 697 | + } |
| 698 | + |
| 699 | + /* Record the results */ |
| 700 | + len = buf.len + VARHDRSZ; |
| 701 | + result = (text *) palloc(len); |
| 702 | + VARATT_SIZEP(result) = len; |
| 703 | + memcpy(VARDATA(result), buf.data, buf.len); |
| 704 | + |
| 705 | + /* Cleanup */ |
| 706 | + pfree(buf.data); |
| 707 | + systable_endscan(conscan); |
| 708 | + heap_close(conDesc, AccessShareLock); |
| 709 | + |
| 710 | + PG_RETURN_TEXT_P(result); |
| 711 | +} |
| 712 | + |
| 713 | + |
| 714 | +/* |
| 715 | + * Convert an int16[] Datum into a comma-separated list of column names |
| 716 | + * for the indicated relation; append the list to buf. |
| 717 | + */ |
| 718 | +static void |
| 719 | +decompile_column_index_array(Datum column_index_array, Oid relId, |
| 720 | + StringInfo buf) |
| 721 | +{ |
| 722 | + Datum *keys; |
| 723 | + int nKeys; |
| 724 | + int j; |
| 725 | + |
| 726 | + /* Extract data from array of int16 */ |
| 727 | + deconstruct_array(DatumGetArrayTypeP(column_index_array), |
| 728 | + true, 2, 's', |
| 729 | + &keys, &nKeys); |
| 730 | + |
| 731 | + for (j = 0; j < nKeys; j++) |
| 732 | + { |
| 733 | + char *colName; |
| 734 | + |
| 735 | + colName = get_attname(relId, DatumGetInt16(keys[j])); |
| 736 | + |
| 737 | + if (j == 0) |
| 738 | + appendStringInfo(buf, "%s", |
| 739 | + quote_identifier(colName)); |
| 740 | + else |
| 741 | + appendStringInfo(buf, ", %s", |
| 742 | + quote_identifier(colName)); |
| 743 | + } |
| 744 | +} |
| 745 | + |
| 746 | + |
531 | 747 | /* ----------
|
532 | 748 | * get_expr - Decompile an expression tree
|
533 | 749 | *
|
|
0 commit comments