6
6
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7
7
* Portions Copyright (c) 1994, Regents of the University of California
8
8
*
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 $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
21
21
#include "catalog/pg_type.h"
22
22
#include "commands/defrem.h"
23
23
#include "commands/prepare.h"
24
+ #include "commands/tablecmds.h"
24
25
#include "miscadmin.h"
25
26
#include "nodes/makefuncs.h"
26
27
#include "optimizer/clauses.h"
@@ -1075,6 +1076,11 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
1075
1076
TupleConstr * constr ;
1076
1077
AclResult aclresult ;
1077
1078
1079
+ bool including_defaults = false;
1080
+ bool including_constraints = false;
1081
+ bool including_indexes = false;
1082
+ ListCell * elem ;
1083
+
1078
1084
relation = heap_openrv (inhRelation -> relation ,AccessShareLock );
1079
1085
1080
1086
if (relation -> rd_rel -> relkind != RELKIND_RELATION )
@@ -1095,6 +1101,37 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
1095
1101
tupleDesc = RelationGetDescr (relation );
1096
1102
constr = tupleDesc -> constr ;
1097
1103
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
+
1098
1135
/*
1099
1136
* Insert the inherited attributes into the cxt for the new table
1100
1137
* definition.
@@ -1123,7 +1160,7 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
1123
1160
def -> typename = makeTypeNameFromOid (attribute -> atttypid ,
1124
1161
attribute -> atttypmod );
1125
1162
def -> inhcount = 0 ;
1126
- def -> is_local = false ;
1163
+ def -> is_local = true ;
1127
1164
def -> is_not_null = attribute -> attnotnull ;
1128
1165
def -> raw_default = NULL ;
1129
1166
def -> cooked_default = NULL ;
@@ -1138,7 +1175,7 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
1138
1175
/*
1139
1176
* Copy default if any, and the default has been requested
1140
1177
*/
1141
- if (attribute -> atthasdef && inhRelation -> including_defaults )
1178
+ if (attribute -> atthasdef && including_defaults )
1142
1179
{
1143
1180
char * this_default = NULL ;
1144
1181
AttrDefault * attrdef ;
@@ -1165,6 +1202,27 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
1165
1202
def -> cooked_default = pstrdup (this_default );
1166
1203
}
1167
1204
}
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
+ }
1168
1226
1169
1227
/*
1170
1228
* Close the parent rel, but keep our AccessShareLock on it until xact