|
15 | 15 | *
|
16 | 16 | *
|
17 | 17 | * IDENTIFICATION
|
18 |
| - * $Header: /cvsroot/pgsql/src/backend/executor/execTuples.c,v 1.51 2002/03/21 06:21:04 tgl Exp $ |
| 18 | + * $Header: /cvsroot/pgsql/src/backend/executor/execTuples.c,v 1.52 2002/06/20 17:19:08 momjian Exp $ |
19 | 19 | *
|
20 | 20 | *-------------------------------------------------------------------------
|
21 | 21 | */
|
|
107 | 107 | */
|
108 | 108 | #include "postgres.h"
|
109 | 109 |
|
| 110 | +#include "funcapi.h" |
110 | 111 | #include "access/heapam.h"
|
111 | 112 | #include "catalog/pg_type.h"
|
112 | 113 | #include "executor/executor.h"
|
113 | 114 |
|
114 |
| - |
115 | 115 | /* ----------------------------------------------------------------
|
116 | 116 | * tuple table create/delete functions
|
117 | 117 | * ----------------------------------------------------------------
|
@@ -673,3 +673,123 @@ ExecTypeFromTL(List *targetList)
|
673 | 673 |
|
674 | 674 | return typeInfo;
|
675 | 675 | }
|
| 676 | + |
| 677 | +/* |
| 678 | + * TupleDescGetSlot - Initialize a slot based on the supplied |
| 679 | + * tupledesc |
| 680 | + */ |
| 681 | +TupleTableSlot * |
| 682 | +TupleDescGetSlot(TupleDesc tupdesc) |
| 683 | +{ |
| 684 | + TupleTableSlot *slot; |
| 685 | + |
| 686 | + /* Make a standalone slot */ |
| 687 | + slot = MakeTupleTableSlot(); |
| 688 | + |
| 689 | + /* Bind the tuple description to the slot */ |
| 690 | + ExecSetSlotDescriptor(slot, tupdesc, true); |
| 691 | + |
| 692 | + /* Return the slot */ |
| 693 | + return slot; |
| 694 | +} |
| 695 | + |
| 696 | +/* |
| 697 | + * TupleDescGetAttInMetadata - Get a pointer to AttInMetadata based on the |
| 698 | + * supplied TupleDesc. AttInMetadata can be used in conjunction with C strings |
| 699 | + * to produce a properly formed tuple. |
| 700 | + */ |
| 701 | +AttInMetadata * |
| 702 | +TupleDescGetAttInMetadata(TupleDesc tupdesc) |
| 703 | +{ |
| 704 | + int natts; |
| 705 | + int i; |
| 706 | + Oid atttypeid; |
| 707 | + Oid attinfuncid; |
| 708 | + Oid attelem; |
| 709 | + FmgrInfo *attinfuncinfo; |
| 710 | + Oid *attelems; |
| 711 | + int4 *atttypmods; |
| 712 | + AttInMetadata *attinmeta; |
| 713 | + |
| 714 | + attinmeta = (AttInMetadata *) palloc(sizeof(AttInMetadata)); |
| 715 | + natts = tupdesc->natts; |
| 716 | + |
| 717 | + /* |
| 718 | + * Gather info needed later to call the "in" function for each attribute |
| 719 | + */ |
| 720 | + attinfuncinfo = (FmgrInfo *) palloc(natts * sizeof(FmgrInfo)); |
| 721 | + attelems = (Oid *) palloc(natts * sizeof(Oid)); |
| 722 | + atttypmods = (int4 *) palloc(natts * sizeof(int4)); |
| 723 | + |
| 724 | + for (i = 0; i < natts; i++) |
| 725 | + { |
| 726 | + atttypeid = tupdesc->attrs[i]->atttypid; |
| 727 | + get_type_metadata(atttypeid, &attinfuncid, &attelem); |
| 728 | + |
| 729 | + fmgr_info(attinfuncid, &attinfuncinfo[i]); |
| 730 | + attelems[i] = attelem; |
| 731 | + atttypmods[i] = tupdesc->attrs[i]->atttypmod; |
| 732 | + } |
| 733 | + attinmeta->tupdesc = tupdesc; |
| 734 | + attinmeta->attinfuncs = attinfuncinfo; |
| 735 | + attinmeta->attelems = attelems; |
| 736 | + attinmeta->atttypmods = atttypmods; |
| 737 | + |
| 738 | + return attinmeta; |
| 739 | +} |
| 740 | + |
| 741 | +/* |
| 742 | + * BuildTupleFromCStrings - build a HeapTuple given user data in C string form. |
| 743 | + * values is an array of C strings, one for each attribute of the return tuple. |
| 744 | + */ |
| 745 | +HeapTuple |
| 746 | +BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values) |
| 747 | +{ |
| 748 | + TupleDesc tupdesc; |
| 749 | + int natts; |
| 750 | + HeapTuple tuple; |
| 751 | + char *nulls; |
| 752 | + int i; |
| 753 | + Datum *dvalues; |
| 754 | + FmgrInfo attinfuncinfo; |
| 755 | + Oid attelem; |
| 756 | + int4 atttypmod; |
| 757 | + |
| 758 | + tupdesc = attinmeta->tupdesc; |
| 759 | + natts = tupdesc->natts; |
| 760 | + |
| 761 | + dvalues = (Datum *) palloc(natts * sizeof(Datum)); |
| 762 | + |
| 763 | + /* Call the "in" function for each attribute */ |
| 764 | + for (i = 0; i < natts; i++) |
| 765 | + { |
| 766 | + if (values[i] != NULL) |
| 767 | + { |
| 768 | + attinfuncinfo = attinmeta->attinfuncs[i]; |
| 769 | + attelem = attinmeta->attelems[i]; |
| 770 | + atttypmod = attinmeta->atttypmods[i]; |
| 771 | + |
| 772 | + dvalues[i] = FunctionCall3(&attinfuncinfo, CStringGetDatum(values[i]), |
| 773 | + ObjectIdGetDatum(attelem), |
| 774 | + Int32GetDatum(atttypmod)); |
| 775 | + } |
| 776 | + else |
| 777 | + dvalues[i] = PointerGetDatum(NULL); |
| 778 | + } |
| 779 | + |
| 780 | + /* |
| 781 | + * Form a tuple |
| 782 | + */ |
| 783 | + nulls = (char *) palloc(natts * sizeof(char)); |
| 784 | + for (i = 0; i < natts; i++) |
| 785 | + { |
| 786 | + if (DatumGetPointer(dvalues[i]) != NULL) |
| 787 | + nulls[i] = ' '; |
| 788 | + else |
| 789 | + nulls[i] = 'n'; |
| 790 | + } |
| 791 | + tuple = heap_formtuple(tupdesc, dvalues, nulls); |
| 792 | + |
| 793 | + return tuple; |
| 794 | +} |
| 795 | + |
0 commit comments