66 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
77 * Portions Copyright (c) 1994, Regents of the University of California
88 *
9- *$PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.335 2006/06/21 18:30:11 tgl Exp $
9+ *$PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.336 2006/06/27 03:43:20 momjian Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
2121#include "catalog/pg_type.h"
2222#include "commands/defrem.h"
2323#include "commands/prepare.h"
24+ #include "commands/tablecmds.h"
2425#include "miscadmin.h"
2526#include "nodes/makefuncs.h"
2627#include "optimizer/clauses.h"
@@ -1075,6 +1076,11 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
10751076TupleConstr * constr ;
10761077AclResult aclresult ;
10771078
1079+ bool including_defaults = false;
1080+ bool including_constraints = false;
1081+ bool including_indexes = false;
1082+ ListCell * elem ;
1083+
10781084relation = heap_openrv (inhRelation -> relation ,AccessShareLock );
10791085
10801086if (relation -> rd_rel -> relkind != RELKIND_RELATION )
@@ -1095,6 +1101,37 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
10951101tupleDesc = RelationGetDescr (relation );
10961102constr = tupleDesc -> constr ;
10971103
1104+ foreach (elem ,inhRelation -> options )
1105+ {
1106+ int option = lfirst_int (elem );
1107+ switch (option )
1108+ {
1109+ case CREATE_TABLE_LIKE_INCLUDING_DEFAULTS :
1110+ including_defaults = true;
1111+ break ;
1112+ case CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS :
1113+ including_defaults = false;
1114+ break ;
1115+ case CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS :
1116+ including_constraints = true;
1117+ break ;
1118+ case CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS :
1119+ including_constraints = false;
1120+ break ;
1121+ case CREATE_TABLE_LIKE_INCLUDING_INDEXES :
1122+ including_indexes = true;
1123+ break ;
1124+ case CREATE_TABLE_LIKE_EXCLUDING_INDEXES :
1125+ including_indexes = false;
1126+ break ;
1127+ default :
1128+ elog (ERROR ,"unrecognized CREATE TABLE LIKE option: %d" ,option );
1129+ }
1130+ }
1131+
1132+ if (including_indexes )
1133+ elog (ERROR ,"TODO" );
1134+
10981135/*
10991136 * Insert the inherited attributes into the cxt for the new table
11001137 * definition.
@@ -1123,7 +1160,7 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
11231160def -> typename = makeTypeNameFromOid (attribute -> atttypid ,
11241161attribute -> atttypmod );
11251162def -> inhcount = 0 ;
1126- def -> is_local = false ;
1163+ def -> is_local = true ;
11271164def -> is_not_null = attribute -> attnotnull ;
11281165def -> raw_default = NULL ;
11291166def -> cooked_default = NULL ;
@@ -1138,7 +1175,7 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
11381175/*
11391176 * Copy default if any, and the default has been requested
11401177 */
1141- if (attribute -> atthasdef && inhRelation -> including_defaults )
1178+ if (attribute -> atthasdef && including_defaults )
11421179{
11431180char * this_default = NULL ;
11441181AttrDefault * attrdef ;
@@ -1165,6 +1202,27 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
11651202def -> cooked_default = pstrdup (this_default );
11661203}
11671204}
1205+
1206+ if (including_constraints && tupleDesc -> constr ) {
1207+ int ccnum ;
1208+ AttrNumber * attmap = varattnos_map_schema (tupleDesc ,cxt -> columns );
1209+
1210+ for (ccnum = 0 ;ccnum < tupleDesc -> constr -> num_check ;ccnum ++ ) {
1211+ char * ccname = tupleDesc -> constr -> check [ccnum ].ccname ;
1212+ char * ccbin = tupleDesc -> constr -> check [ccnum ].ccbin ;
1213+ Node * ccbin_node = stringToNode (ccbin );
1214+ Constraint * n = makeNode (Constraint );
1215+
1216+ change_varattnos_of_a_node (ccbin_node ,attmap );
1217+
1218+ n -> contype = CONSTR_CHECK ;
1219+ n -> name = pstrdup (ccname );
1220+ n -> raw_expr = ccbin_node ;
1221+ n -> cooked_expr = NULL ;
1222+ n -> indexspace = NULL ;
1223+ cxt -> ckconstraints = lappend (cxt -> ckconstraints , (Node * )n );
1224+ }
1225+ }
11681226
11691227/*
11701228 * Close the parent rel, but keep our AccessShareLock on it until xact