316
321
if (numParents == 0)
317
322
continue; /* nothing to see here, move along */
319
/*----------------------------------------------------------------
320
* For each attr, check the parent info: if no parent has an attr
321
* with the same name, then it's not inherited. If there *is* an
322
* attr with the same name, then only dump it if:
324
* - it is NOT NULL and zero parents are NOT NULL
326
* - it has a default value AND the default value does not match
327
* all parent default values, or no parents specify a default.
329
* See discussion on -hackers around 2-Apr-2001.
330
*----------------------------------------------------------------
324
/* For each column, search for matching column names in parent(s) */
332
325
for (j = 0; j < tbinfo->numatts; j++)
334
bool foundAttr; /* Attr was found in a parent */
335
327
bool foundNotNull; /* Attr was NOT NULL in a parent */
336
bool defaultsMatch; /* All non-empty defaults match */
337
bool defaultsFound; /* Found a default in a parent */
338
AttrDefInfo *attrDef;
328
bool foundDefault; /* Found a default in a parent */
330
/* no point in examining dropped columns */
331
if (tbinfo->attisdropped[j])
341
334
foundNotNull = false;
342
defaultsMatch = true;
343
defaultsFound = false;
345
attrDef = tbinfo->attrdefs[j];
335
foundDefault = false;
347
336
for (k = 0; k < numParents; k++)
338
TableInfo *parent = parents[k];
352
341
inhAttrInd = strInArray(tbinfo->attnames[j],
353
342
parent->attnames,
354
343
parent->numatts);
356
if (inhAttrInd != -1)
358
AttrDefInfo *inhDef = parent->attrdefs[inhAttrInd];
361
346
foundNotNull |= parent->notnull[inhAttrInd];
364
defaultsFound = true;
367
* If any parent has a default and the child doesn't,
368
* we have to emit an explicit DEFAULT NULL clause for
369
* the child, else the parent's default will win.
373
attrDef = (AttrDefInfo *) malloc(sizeof(AttrDefInfo));
374
attrDef->dobj.objType = DO_ATTRDEF;
375
attrDef->dobj.catId.tableoid = 0;
376
attrDef->dobj.catId.oid = 0;
377
AssignDumpId(&attrDef->dobj);
378
attrDef->adtable = tbinfo;
379
attrDef->adnum = j + 1;
380
attrDef->adef_expr = strdup("NULL");
382
attrDef->dobj.name = strdup(tbinfo->dobj.name);
383
attrDef->dobj.namespace = tbinfo->dobj.namespace;
385
attrDef->dobj.dump = tbinfo->dobj.dump;
387
attrDef->separate = false;
388
addObjectDependency(&tbinfo->dobj,
389
attrDef->dobj.dumpId);
391
tbinfo->attrdefs[j] = attrDef;
393
if (strcmp(attrDef->adef_expr, inhDef->adef_expr) != 0)
395
defaultsMatch = false;
398
* Whenever there is a non-matching parent
399
* default, add a dependency to force the parent
400
* default to be dumped first, in case the
401
* defaults end up being dumped as separate
402
* commands. Otherwise the parent default will
403
* override the child's when it is applied.
405
addObjectDependency(&attrDef->dobj,
406
inhDef->dobj.dumpId);
347
foundDefault |= (parent->attrdefs[inhAttrInd] != NULL);
413
* Based on the scan of the parents, decide if we can rely on the
416
if (foundAttr) /* Attr was inherited */
351
/* Remember if we found inherited NOT NULL */
352
tbinfo->inhNotNull[j] = foundNotNull;
354
/* Manufacture a DEFAULT NULL clause if necessary */
355
if (foundDefault && tbinfo->attrdefs[j] == NULL)
418
/* Set inherited flag by default */
419
tbinfo->inhAttrs[j] = true;
420
tbinfo->inhAttrDef[j] = true;
421
tbinfo->inhNotNull[j] = true;
424
* Clear it if attr had a default, but parents did not, or
427
if ((attrDef != NULL) && (!defaultsFound || !defaultsMatch))
429
tbinfo->inhAttrs[j] = false;
430
tbinfo->inhAttrDef[j] = false;
434
* Clear it if NOT NULL and none of the parents were NOT NULL
436
if (tbinfo->notnull[j] && !foundNotNull)
438
tbinfo->inhAttrs[j] = false;
439
tbinfo->inhNotNull[j] = false;
442
/* Clear it if attr has local definition */
443
if (tbinfo->attislocal[j])
444
tbinfo->inhAttrs[j] = false;
357
AttrDefInfo *attrDef;
359
attrDef = (AttrDefInfo *) malloc(sizeof(AttrDefInfo));
360
attrDef->dobj.objType = DO_ATTRDEF;
361
attrDef->dobj.catId.tableoid = 0;
362
attrDef->dobj.catId.oid = 0;
363
AssignDumpId(&attrDef->dobj);
364
attrDef->dobj.name = strdup(tbinfo->dobj.name);
365
attrDef->dobj.namespace = tbinfo->dobj.namespace;
366
attrDef->dobj.dump = tbinfo->dobj.dump;
368
attrDef->adtable = tbinfo;
369
attrDef->adnum = j + 1;
370
attrDef->adef_expr = strdup("NULL");
372
/* Will column be dumped explicitly? */
373
if (shouldPrintColumn(tbinfo, j))
375
attrDef->separate = false;
376
/* No dependency needed: NULL cannot have dependencies */
380
/* column will be suppressed, print default separately */
381
attrDef->separate = true;
382
/* ensure it comes out after the table */
383
addObjectDependency(&attrDef->dobj,
384
tbinfo->dobj.dumpId);
387
tbinfo->attrdefs[j] = attrDef;