7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.13 1997/08/21 04:05:22 vadim Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.14 1997/08/22 03:03:56 vadim Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
35
35
36
36
static int checkAttrExists (char * attributeName ,
37
37
char * attributeType ,List * schema );
38
- static List * MergeAttributes (List * schema ,List * supers );
38
+ static List * MergeAttributes (List * schema ,List * supers , List * * supconstr );
39
39
static void StoreCatalogInheritance (Oid relationId ,List * supers );
40
40
41
41
/* ----------------------------------------------------------------
@@ -46,15 +46,16 @@ static void StoreCatalogInheritance(Oid relationId, List *supers);
46
46
void
47
47
DefineRelation (CreateStmt * stmt )
48
48
{
49
- char * relname = palloc (NAMEDATALEN );
50
- List * schema = stmt -> tableElts ;
51
- int numberOfAttributes ;
52
- Oid relationId ;
53
- char archChar ;
54
- List * inheritList = NULL ;
55
- char * archiveName = NULL ;
49
+ char * relname = palloc (NAMEDATALEN );
50
+ List * schema = stmt -> tableElts ;
51
+ int numberOfAttributes ;
52
+ Oid relationId ;
53
+ char archChar ;
54
+ List * inheritList = NULL ;
55
+ char * archiveName = NULL ;
56
56
TupleDesc descriptor ;
57
- int heaploc ,archloc ;
57
+ List * constraints ;
58
+ int heaploc ,archloc ;
58
59
59
60
char * typename = NULL ;/* the typename of this relation. not useod for now */
60
61
@@ -116,7 +117,8 @@ DefineRelation(CreateStmt *stmt)
116
117
*generate relation schema, including inherited attributes.
117
118
* ----------------
118
119
*/
119
- schema = MergeAttributes (schema ,inheritList );
120
+ schema = MergeAttributes (schema ,inheritList ,& constraints );
121
+ constraints = nconc (constraints ,stmt -> constraints );
120
122
121
123
numberOfAttributes = length (schema );
122
124
if (numberOfAttributes <=0 ) {
@@ -130,6 +132,55 @@ DefineRelation(CreateStmt *stmt)
130
132
* ----------------
131
133
*/
132
134
descriptor = BuildDescForRelation (schema ,relname );
135
+
136
+ if (constraints != NIL )
137
+ {
138
+ List * entry ;
139
+ int nconstr = length (constraints );
140
+ ConstrCheck * check = (ConstrCheck * )palloc (nconstr * sizeof (ConstrCheck ));
141
+ int ncheck = 0 ;
142
+ int i ;
143
+
144
+ foreach (entry ,constraints )
145
+ {
146
+ ConstraintDef * cdef = (ConstraintDef * )lfirst (entry );
147
+
148
+ if (cdef -> type == CONSTR_CHECK )
149
+ {
150
+ if (cdef -> name != NULL )
151
+ {
152
+ for (i = 0 ;i < ncheck ;i ++ )
153
+ {
154
+ if (strcmp (check [i ].ccname ,cdef -> name )== 0 )
155
+ elog (WARN ,"DefineRelation: name (%s) of CHECK constraint duplicated" ,cdef -> name );
156
+ }
157
+ check [ncheck ].ccname = cdef -> name ;
158
+ }
159
+ else
160
+ {
161
+ check [ncheck ].ccname = (char * )palloc (NAMEDATALEN );
162
+ sprintf (check [ncheck ].ccname ,"$%d" ,ncheck + 1 );
163
+ }
164
+ check [ncheck ].ccbin = NULL ;
165
+ check [ncheck ].ccsrc = (char * )cdef -> def ;
166
+ ncheck ++ ;
167
+ }
168
+ }
169
+ if (ncheck > 0 )
170
+ {
171
+ if (ncheck < nconstr )
172
+ check = (ConstrCheck * )repalloc (check ,ncheck * sizeof (ConstrCheck ));
173
+ if (descriptor -> constr == NULL )
174
+ {
175
+ descriptor -> constr = (TupleConstr * )palloc (sizeof (TupleConstr ));
176
+ descriptor -> constr -> num_defval = 0 ;
177
+ descriptor -> constr -> has_not_null = false;
178
+ }
179
+ descriptor -> constr -> num_check = ncheck ;
180
+ descriptor -> constr -> check = check ;
181
+ }
182
+ }
183
+
133
184
relationId = heap_create (relname ,
134
185
typename ,
135
186
archChar ,
@@ -138,11 +189,12 @@ DefineRelation(CreateStmt *stmt)
138
189
139
190
StoreCatalogInheritance (relationId ,inheritList );
140
191
141
- /*----------------
192
+ /*
142
193
*create an archive relation if necessary
143
- * ----------------
144
194
*/
145
- if (archChar != 'n' ) {
195
+ if (archChar != 'n' )
196
+ {
197
+ TupleDesc tupdesc ;
146
198
/*
147
199
* Need to create an archive relation for this heap relation.
148
200
* We cobble up the command by hand, and increment the command
@@ -152,12 +204,15 @@ DefineRelation(CreateStmt *stmt)
152
204
CommandCounterIncrement ();
153
205
archiveName = MakeArchiveName (relationId );
154
206
155
- relationId = heap_create (archiveName ,
156
- typename ,
157
- 'n' ,/* archive isn't archived */
158
- archloc ,
159
- descriptor );
207
+ tupdesc = CreateTupleDescCopy (descriptor );/* get rid of constraints */
208
+ (void )heap_create (archiveName ,
209
+ typename ,
210
+ 'n' ,/* archive isn't archived */
211
+ archloc ,
212
+ tupdesc );
160
213
214
+ FreeTupleDesc (tupdesc );
215
+ FreeTupleDesc (descriptor );
161
216
pfree (archiveName );
162
217
}
163
218
}
@@ -213,10 +268,11 @@ RemoveRelation(char *name)
213
268
* stud_emp {7:percent}
214
269
*/
215
270
static List *
216
- MergeAttributes (List * schema ,List * supers )
271
+ MergeAttributes (List * schema ,List * supers , List * * supconstr )
217
272
{
218
273
List * entry ;
219
274
List * inhSchema = NIL ;
275
+ List * constraints = NIL ;
220
276
221
277
/*
222
278
* Validates that there are no duplications.
@@ -258,6 +314,7 @@ MergeAttributes(List *schema, List *supers)
258
314
List * partialResult = NIL ;
259
315
AttrNumber attrno ;
260
316
TupleDesc tupleDesc ;
317
+ TupleConstr * constr ;
261
318
262
319
relation = heap_openr (name );
263
320
if (relation == NULL ) {
@@ -271,12 +328,12 @@ MergeAttributes(List *schema, List *supers)
271
328
name );
272
329
}
273
330
tupleDesc = RelationGetTupleDescriptor (relation );
331
+ constr = tupleDesc -> constr ;
274
332
275
333
for (attrno = relation -> rd_rel -> relnatts - 1 ;attrno >=0 ;attrno -- ) {
276
334
AttributeTupleForm attribute = tupleDesc -> attrs [attrno ];
277
335
char * attributeName ;
278
336
char * attributeType ;
279
- TupleConstr constraints ;
280
337
HeapTuple tuple ;
281
338
ColumnDef * def ;
282
339
TypeName * typename ;
@@ -285,7 +342,6 @@ MergeAttributes(List *schema, List *supers)
285
342
* form name, type and constraints
286
343
*/
287
344
attributeName = (attribute -> attname ).data ;
288
- constraints .has_not_null = attribute -> attnotnull ;
289
345
tuple =
290
346
SearchSysCacheTuple (TYPOID ,
291
347
ObjectIdGetDatum (attribute -> atttypid ),
@@ -313,10 +369,46 @@ MergeAttributes(List *schema, List *supers)
313
369
def -> colname = pstrdup (attributeName );
314
370
typename -> name = pstrdup (attributeType );
315
371
def -> typename = typename ;
316
- def -> is_not_null = constraints .has_not_null ;
372
+ def -> is_not_null = attribute -> attnotnull ;
373
+ def -> defval = NULL ;
374
+ if (attribute -> atthasdef )
375
+ {
376
+ AttrDefault * attrdef = constr -> defval ;
377
+ int i ;
378
+
379
+ Assert (constr != NULL && constr -> num_defval > 0 );
380
+
381
+ for (i = 0 ;i < constr -> num_defval ;i ++ )
382
+ {
383
+ if (attrdef [i ].adnum != attrno + 1 )
384
+ continue ;
385
+ def -> defval = pstrdup (attrdef [i ].adsrc );
386
+ break ;
387
+ }
388
+ Assert (def -> defval != NULL );
389
+ }
317
390
partialResult = lcons (def ,partialResult );
318
391
}
319
392
393
+ if (constr && constr -> num_check > 0 )
394
+ {
395
+ ConstrCheck * check = constr -> check ;
396
+ int i ;
397
+
398
+ for (i = 0 ;i < constr -> num_check ;i ++ )
399
+ {
400
+ ConstraintDef * cdef = (ConstraintDef * )palloc (sizeof (ConstraintDef ));
401
+
402
+ cdef -> type = CONSTR_CHECK ;
403
+ if (check [i ].ccname [0 ]== '$' )
404
+ cdef -> name = NULL ;
405
+ else
406
+ cdef -> name = pstrdup (check [i ].ccname );
407
+ cdef -> def = (void * )pstrdup (check [i ].ccsrc );
408
+ constraints = lappend (constraints ,cdef );
409
+ }
410
+ }
411
+
320
412
/*
321
413
* iteration cleanup and result collection
322
414
*/
@@ -333,7 +425,7 @@ MergeAttributes(List *schema, List *supers)
333
425
* put the inherited schema before our the schema for this table
334
426
*/
335
427
schema = nconc (inhSchema ,schema );
336
-
428
+ * supconstr = constraints ;
337
429
return (schema );
338
430
}
339
431
@@ -557,4 +649,3 @@ MakeArchiveName(Oid relationId)
557
649
558
650
return arch ;
559
651
}
560
-