Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9e9584c

Browse files
committed
Store attr DEFAULT info in heap_create
1 parent55f7d4e commit9e9584c

File tree

1 file changed

+117
-2
lines changed

1 file changed

+117
-2
lines changed

‎src/backend/catalog/heap.c

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.20 1997/08/21 03:01:21 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.21 1997/08/22 02:58:51 vadim Exp $
1111
*
1212
* INTERFACE ROUTINES
1313
*heap_creatr()- Create an uncataloged heap relation
@@ -39,10 +39,13 @@
3939
#include<catalog/pg_proc.h>
4040
#include<catalog/pg_index.h>
4141
#include<catalog/pg_type.h>
42+
#include<catalog/pg_attrdef.h>
43+
#include<catalog/pg_relcheck.h>
4244
#include<storage/bufmgr.h>
4345
#include<storage/lmgr.h>
4446
#include<storage/smgr.h>
4547
#include<parser/catalog_utils.h>
48+
#include<parser/parse_query.h>
4649
#include<rewrite/rewriteRemove.h>
4750
#include<utils/builtins.h>
4851
#include<utils/mcxt.h>
@@ -64,6 +67,9 @@ static void RelationRemoveIndexes(Relation relation);
6467
staticvoidRelationRemoveInheritance(Relationrelation);
6568
staticvoidRemoveFromTempRelList(Relationr);
6669
staticvoidaddNewRelationType(char*typeName,Oidnew_rel_oid);
70+
staticvoidStoreConstraints (Relationrel);
71+
staticvoidStoreAttrDefault (Relationrel,AttrDefault*attrdef);
72+
staticvoidStoreRelCheck (Relationrel,ConstrCheck*check);
6773

6874

6975
/* ----------------------------------------------------------------
@@ -272,7 +278,7 @@ heap_creatr(char *name,
272278
/* ----------
273279
create a new tuple descriptor from the one passed in
274280
*/
275-
rdesc->rd_att=CreateTupleDescCopy(tupDesc);
281+
rdesc->rd_att=CreateTupleDescCopyConstr(tupDesc);
276282

277283
/* ----------------
278284
*initialize the fields of our new relation descriptor
@@ -298,6 +304,8 @@ heap_creatr(char *name,
298304
rdesc->rd_rel->relkind=RELKIND_UNCATALOGED;
299305
rdesc->rd_rel->relnatts=natts;
300306
rdesc->rd_rel->relsmgr=smgr;
307+
if (tupDesc->constr )
308+
rdesc->rd_rel->relchecks=tupDesc->constr->num_check;
301309

302310
for (i=0;i<natts;i++) {
303311
rdesc->rd_att->attrs[i]->attrelid=relid;
@@ -814,6 +822,8 @@ heap_create(char relname[],
814822
arch,
815823
natts);
816824

825+
StoreConstraints (new_rel_desc);
826+
817827
/* ----------------
818828
*ok, the relation has been cataloged, so close our relations
819829
* and return the oid of the newly created relation.
@@ -1458,3 +1468,108 @@ DestroyTempRels(void)
14581468
tempRels=NULL;
14591469
}
14601470

1471+
staticvoid
1472+
StoreConstraints (Relationrel)
1473+
{
1474+
TupleConstr*constr=rel->rd_att->constr;
1475+
inti;
1476+
1477+
if ( !constr )
1478+
return;
1479+
1480+
if (constr->num_defval>0 )
1481+
{
1482+
for (i=0;i<constr->num_defval;i++)
1483+
StoreAttrDefault (rel,&(constr->defval[i]));
1484+
}
1485+
1486+
if (constr->num_check>0 )
1487+
{
1488+
for (i=0;i<constr->num_check;i++)
1489+
StoreRelCheck (rel,&(constr->check[i]));
1490+
}
1491+
1492+
return;
1493+
}
1494+
1495+
externList*flatten_tlist(List*tlist);
1496+
externList*pg_plan(char*query_string,Oid*typev,intnargs,
1497+
QueryTreeList**queryListP,CommandDestdest);
1498+
1499+
staticvoid
1500+
StoreAttrDefault (Relationrel,AttrDefault*attrdef)
1501+
{
1502+
charstr[MAX_PARSE_BUFFER];
1503+
charcast[2*NAMEDATALEN]= {0};
1504+
AttributeTupleFormatp=rel->rd_att->attrs[attrdef->adnum-1];
1505+
QueryTreeList*queryTree_list;
1506+
Query*query;
1507+
List*planTree_list;
1508+
TargetEntry*te;
1509+
Resdom*resdom;
1510+
Node*expr;
1511+
char*adbin;
1512+
MemoryContextoldcxt;
1513+
Relationadrel;
1514+
Relationidescs[Num_pg_attrdef_indices];
1515+
HeapTupletuple;
1516+
Datumvalues[4];
1517+
charnulls[4]= {' ',' ',' ',' '};
1518+
externGlobalMemoryCacheCxt;
1519+
1520+
start:;
1521+
sprintf (str,"select %s%s from %.*s",attrdef->adsrc,cast,
1522+
NAMEDATALEN,rel->rd_rel->relname.data);
1523+
planTree_list= (List*)pg_plan (str,NULL,0,&queryTree_list,None);
1524+
query= (Query*) (queryTree_list->qtrees[0]);
1525+
1526+
if (length (query->rtable)>1||
1527+
flatten_tlist (query->targetList)!=NIL )
1528+
elog (WARN,"AttributeDefault: cannot use attribute(s)");
1529+
te= (TargetEntry*)lfirst (query->targetList);
1530+
resdom=te->resdom;
1531+
expr=te->expr;
1532+
1533+
if (IsA (expr,Const) )
1534+
{
1535+
if ( ((Const*)expr)->consttype!=atp->atttypid )
1536+
{
1537+
if (*cast!=0 )
1538+
elog (WARN,"AttributeDefault: casting failed - const type mismatched");
1539+
sprintf (cast,":: %s",get_id_typname (atp->atttypid));
1540+
gotostart;
1541+
}
1542+
}
1543+
elseif (exprType (expr)!=atp->atttypid )
1544+
elog (WARN,"AttributeDefault: type mismatched");
1545+
1546+
adbin=nodeToString (expr);
1547+
oldcxt=MemoryContextSwitchTo ((MemoryContext)CacheCxt);
1548+
attrdef->adbin= (char*)palloc (strlen (adbin)+1);
1549+
strcpy (attrdef->adbin,adbin);
1550+
(void)MemoryContextSwitchTo (oldcxt);
1551+
pfree (adbin);
1552+
1553+
values[Anum_pg_attrdef_adrelid-1]=rel->rd_id;
1554+
values[Anum_pg_attrdef_adnum-1]=attrdef->adnum;
1555+
values[Anum_pg_attrdef_adbin-1]=PointerGetDatum (textin (attrdef->adbin));
1556+
values[Anum_pg_attrdef_adsrc-1]=PointerGetDatum (textin (attrdef->adsrc));
1557+
adrel=heap_openr (AttrDefaultRelationName);
1558+
tuple=heap_formtuple (adrel->rd_att,values,nulls);
1559+
CatalogOpenIndices (Num_pg_attrdef_indices,Name_pg_attrdef_indices,idescs);
1560+
heap_insert (adrel,tuple);
1561+
CatalogIndexInsert (idescs,Num_pg_attrdef_indices,adrel,tuple);
1562+
CatalogCloseIndices (Num_pg_attrdef_indices,idescs);
1563+
1564+
pfree (DatumGetPointer(values[Anum_pg_attrdef_adbin-1]));
1565+
pfree (DatumGetPointer(values[Anum_pg_attrdef_adsrc-1]));
1566+
pfree (tuple);
1567+
1568+
}
1569+
1570+
staticvoid
1571+
StoreRelCheck (Relationrel,ConstrCheck*check)
1572+
{
1573+
1574+
return;
1575+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp