|
8 | 8 | * |
9 | 9 | * |
10 | 10 | * IDENTIFICATION |
11 | | - * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.157 2005/05/10 13:16:26 momjian Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.158 2005/05/30 06:52:38 neilc Exp $ |
12 | 12 | * |
13 | 13 | *------------------------------------------------------------------------- |
14 | 14 | */ |
@@ -4380,52 +4380,33 @@ validateForeignKeyConstraint(FkConstraint *fkconstraint, |
4380 | 4380 | pfree(trig.tgargs); |
4381 | 4381 | } |
4382 | 4382 |
|
4383 | | -/* |
4384 | | - * Create the triggers that implement an FK constraint. |
4385 | | - */ |
4386 | 4383 | staticvoid |
4387 | | -createForeignKeyTriggers(Relationrel,FkConstraint*fkconstraint, |
4388 | | -OidconstrOid) |
| 4384 | +CreateFKCheckTrigger(RangeVar*myRel,FkConstraint*fkconstraint, |
| 4385 | +ObjectAddress*constrobj,ObjectAddress*trigobj, |
| 4386 | +boolon_insert) |
4389 | 4387 | { |
4390 | | -RangeVar*myRel; |
4391 | 4388 | CreateTrigStmt*fk_trigger; |
4392 | 4389 | ListCell*fk_attr; |
4393 | 4390 | ListCell*pk_attr; |
4394 | | -ObjectAddresstrigobj, |
4395 | | -constrobj; |
4396 | | - |
4397 | | -/* |
4398 | | - * Reconstruct a RangeVar for my relation (not passed in, |
4399 | | - * unfortunately). |
4400 | | - */ |
4401 | | -myRel=makeRangeVar(get_namespace_name(RelationGetNamespace(rel)), |
4402 | | -pstrdup(RelationGetRelationName(rel))); |
4403 | 4391 |
|
4404 | | -/* |
4405 | | - * Preset objectAddress fields |
4406 | | - */ |
4407 | | -constrobj.classId=ConstraintRelationId; |
4408 | | -constrobj.objectId=constrOid; |
4409 | | -constrobj.objectSubId=0; |
4410 | | -trigobj.classId=TriggerRelationId; |
4411 | | -trigobj.objectSubId=0; |
4412 | | - |
4413 | | -/* Make changes-so-far visible */ |
4414 | | -CommandCounterIncrement(); |
4415 | | - |
4416 | | -/* |
4417 | | - * Build and execute a CREATE CONSTRAINT TRIGGER statement for the |
4418 | | - * CHECK action. |
4419 | | - */ |
4420 | 4392 | fk_trigger=makeNode(CreateTrigStmt); |
4421 | 4393 | fk_trigger->trigname=fkconstraint->constr_name; |
4422 | 4394 | fk_trigger->relation=myRel; |
4423 | | -fk_trigger->funcname=SystemFuncName("RI_FKey_check_ins"); |
4424 | 4395 | fk_trigger->before= false; |
4425 | 4396 | fk_trigger->row= true; |
4426 | | -fk_trigger->actions[0]='i'; |
4427 | | -fk_trigger->actions[1]='u'; |
4428 | | -fk_trigger->actions[2]='\0'; |
| 4397 | + |
| 4398 | +/* Either ON INSERT or ON UPDATE */ |
| 4399 | +if (on_insert) |
| 4400 | +{ |
| 4401 | +fk_trigger->funcname=SystemFuncName("RI_FKey_check_ins"); |
| 4402 | +fk_trigger->actions[0]='i'; |
| 4403 | +} |
| 4404 | +else |
| 4405 | +{ |
| 4406 | +fk_trigger->funcname=SystemFuncName("RI_FKey_check_upd"); |
| 4407 | +fk_trigger->actions[0]='u'; |
| 4408 | +} |
| 4409 | +fk_trigger->actions[1]='\0'; |
4429 | 4410 |
|
4430 | 4411 | fk_trigger->isconstraint= true; |
4431 | 4412 | fk_trigger->deferrable=fkconstraint->deferrable; |
@@ -4453,13 +4434,54 @@ createForeignKeyTriggers(Relation rel, FkConstraint *fkconstraint, |
4453 | 4434 | fk_trigger->args=lappend(fk_trigger->args,lfirst(pk_attr)); |
4454 | 4435 | } |
4455 | 4436 |
|
4456 | | -trigobj.objectId=CreateTrigger(fk_trigger, true); |
| 4437 | +trigobj->objectId=CreateTrigger(fk_trigger, true); |
4457 | 4438 |
|
4458 | 4439 | /* Register dependency from trigger to constraint */ |
4459 | | -recordDependencyOn(&trigobj,&constrobj,DEPENDENCY_INTERNAL); |
| 4440 | +recordDependencyOn(trigobj,constrobj,DEPENDENCY_INTERNAL); |
4460 | 4441 |
|
4461 | 4442 | /* Make changes-so-far visible */ |
4462 | 4443 | CommandCounterIncrement(); |
| 4444 | +} |
| 4445 | + |
| 4446 | +/* |
| 4447 | + * Create the triggers that implement an FK constraint. |
| 4448 | + */ |
| 4449 | +staticvoid |
| 4450 | +createForeignKeyTriggers(Relationrel,FkConstraint*fkconstraint, |
| 4451 | +OidconstrOid) |
| 4452 | +{ |
| 4453 | +RangeVar*myRel; |
| 4454 | +CreateTrigStmt*fk_trigger; |
| 4455 | +ListCell*fk_attr; |
| 4456 | +ListCell*pk_attr; |
| 4457 | +ObjectAddresstrigobj, |
| 4458 | +constrobj; |
| 4459 | + |
| 4460 | +/* |
| 4461 | + * Reconstruct a RangeVar for my relation (not passed in, |
| 4462 | + * unfortunately). |
| 4463 | + */ |
| 4464 | +myRel=makeRangeVar(get_namespace_name(RelationGetNamespace(rel)), |
| 4465 | +pstrdup(RelationGetRelationName(rel))); |
| 4466 | + |
| 4467 | +/* |
| 4468 | + * Preset objectAddress fields |
| 4469 | + */ |
| 4470 | +constrobj.classId=ConstraintRelationId; |
| 4471 | +constrobj.objectId=constrOid; |
| 4472 | +constrobj.objectSubId=0; |
| 4473 | +trigobj.classId=TriggerRelationId; |
| 4474 | +trigobj.objectSubId=0; |
| 4475 | + |
| 4476 | +/* Make changes-so-far visible */ |
| 4477 | +CommandCounterIncrement(); |
| 4478 | + |
| 4479 | +/* |
| 4480 | + * Build and execute a CREATE CONSTRAINT TRIGGER statement for the |
| 4481 | + * CHECK action for both INSERTs and UPDATEs on the referencing table. |
| 4482 | + */ |
| 4483 | +CreateFKCheckTrigger(myRel,fkconstraint,&constrobj,&trigobj, true); |
| 4484 | +CreateFKCheckTrigger(myRel,fkconstraint,&constrobj,&trigobj, false); |
4463 | 4485 |
|
4464 | 4486 | /* |
4465 | 4487 | * Build and execute a CREATE CONSTRAINT TRIGGER statement for the ON |
|