home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / lib / H / rules / prs2.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  24.7 KB  |  883 lines

  1. /*
  2.  * FILE:
  3.  *   prs2.h
  4.  *
  5.  * IDENTIFICATION:
  6.  *   $Header: /private/postgres/src/lib/H/rules/RCS/prs2.h,v 1.31 1992/03/11 02:20:18 mer Exp $
  7.  *
  8.  * DESCRIPTION:
  9.  *   All you need to include if you are a PRS2 person!
  10.  */
  11.  
  12. /*
  13.  * Include this file only once...
  14.  */
  15. #ifndef Prs2Included
  16. #define Prs2Included
  17.  
  18. #include "tmp/postgres.h"
  19.  
  20. #include "access/heapam.h"
  21. #include "access/tupdesc.h"
  22. #include "nodes/pg_lisp.h"
  23. #include "storage/buf.h"
  24. #include "utils/rel.h"
  25.  
  26. #include "rules/params.h"
  27.  
  28. /*------------------------------------------------------------------
  29.  * Include PRS2 lock definition
  30.  * These defs were put in a separate file because they are included
  31.  * bit "htup.h" and that created a circular dependency...
  32.  */
  33. #include "rules/prs2locks.h"
  34.  
  35. /*------------------------------------------------------------------
  36.  * Comment out the following line to supress debugging output
  37.  * #define PRS2_DEBUG 1
  38.  */
  39.  
  40. /*==================================================================
  41.  * --Prs2RuleData--
  42.  *
  43.  * This is the structure where we keep all the information
  44.  * needed to define a rule.
  45.  *
  46.  * ruleText:
  47.  *     the text of the rule (i.e. a string)
  48.  * ruleName:
  49.  *     the name of the rule
  50.  * ruleId:
  51.  *     the oid of the rule.
  52.  *     NOTE: we do NOT know it until we add the rule in the system
  53.  *     catalogs!
  54.  * isInstead:
  55.  *     is this an "instead" rule ??
  56.  * eventType:
  57.  *     the event type that triggers the rule
  58.  * actionType:
  59.  *     the type of the action that will be executed if the rule istriggered.
  60.  * eventRelationOid:
  61.  * eventRelationName:
  62.  *     oid & name of the relation referenced in the "event" clause of the rule.
  63.  * eventAttributeNumber:
  64.  * eventAttributeName:
  65.  *     the number & name of the attribute referenced in the "event" clause
  66.  *     of the rule. If no such attribute exists (i.e. "on retrieve to EMP" -
  67.  *     a view rule) then this is an InvalidAttributeNumber & NULL
  68.  *     respectively.
  69.  * updatedAttributeNumber:
  70.  *     The attribute no of the attribute that is updated from the rule.
  71.  *     For instance "on retrieve ... then do replace CURRENT(attr = ...)"
  72.  *     or "on retrieve to ... then do retrieve (attr = ....)"
  73.  * parseTree:
  74.  *     the parse tree for the rule definition.
  75.  * ruleQual:
  76.  *     the qualification of the rule
  77.  * ruleAction:
  78.  *     a list of all the actions.
  79.  * paramParseTree:
  80.  * paramRuleQual:
  81.  * paramRuleAction:
  82.  *     same as the parseTree, ruleQual & ruleAction but with all the
  83.  *     Var nodes that reference the CURRENT and/or NEW relations changed
  84.  *     into the appropriate Param nodes...
  85.  * 
  86.  */
  87. typedef struct Prs2RuleDataData {
  88.     ObjectId        ruleId;
  89.     char        *ruleText;
  90.     Name        ruleName;
  91.     bool        isInstead;
  92.     EventType        eventType;
  93.     ActionType        actionType;
  94.     ObjectId        eventRelationOid;
  95.     Name        eventRelationName;
  96.     AttributeNumber    eventAttributeNumber;
  97.     Name        eventAttributeName;
  98.     AttributeNumber    updatedAttributeNumber;
  99.     List        parseTree;
  100.     List        ruleQual;
  101.     List        ruleAction;
  102.     List        paramParseTree;
  103.     List        paramRuleQual;
  104.     List        paramRuleAction;
  105. } Prs2RuleDataData;
  106.  
  107. typedef Prs2RuleDataData *Prs2RuleData;
  108.  
  109. /*==================================================================
  110.  * PRS2 MAIN ROUTINES
  111.  *
  112.  * These are almost all the routines a non Prs2 person wants to know
  113.  * about!
  114.  *==================================================================
  115.  */
  116.  
  117.  
  118. /*------------------------------------------------------------------
  119.  * return true if the executor must call the rule manager, or
  120.  * false if there is no need to do so (no rules defined).
  121.  */
  122. extern
  123. bool
  124. prs2MustCallRuleManager();/* BAD PROTOTYPE DELETED -- glass */
  125. /*    ARGS((
  126.     RelationInfo    relationInfo,
  127.     HeapTuple           oldTuple,
  128.     Buffer              oldBuffer,
  129.     int            operation
  130. ));*/
  131.  
  132. /*------------------------------------------------------------------
  133.  * prs2Main
  134.  * The rule manager itself! Normally this should only be called
  135.  * from within the executor...
  136.  */
  137.  
  138. typedef int Prs2Status;
  139.  
  140. #define PRS2_STATUS_TUPLE_UNCHANGED    1
  141. #define PRS2_STATUS_TUPLE_CHANGED    2
  142. #define PRS2_STATUS_INSTEAD    3
  143.  
  144. extern
  145. Prs2Status
  146. prs2Main(); /* BAD PROTOTYPE DELETED -- glass */
  147. /*ARGS((
  148.     EState        estate,
  149.     RelationInfo    scanRelInfo,
  150.     int                 operation,
  151.     int                 userId,
  152.     Relation            relation,
  153.     HeapTuple           oldTuple,
  154.     Buffer              oldBuffer,
  155.     HeapTuple           newTuple,
  156.     Buffer              newBuffer,
  157.     HeapTuple           rawTuple,
  158.     Buffer              rawBuffer,
  159.     AttributeNumberPtr  attributeArray,
  160.     int                 numberOfAttributes,
  161.     HeapTuple           *returnedTupleP,
  162.     Buffer              *returnedBufferP
  163. ));
  164. */
  165. /*==================================================================
  166.  * PRS2 LOCKS
  167.  *==================================================================
  168.  */
  169.  
  170. /*------------------------------------------------------------------
  171.  * prs2FreeLocks
  172.  *    free the space occupied by a rule lock
  173.  */
  174. extern
  175. void
  176. prs2FreeLocks ARGS((
  177.     RuleLock lock
  178. ));
  179.  
  180. /*------------------------------------------------------------------
  181.  * prs2MakeLocks
  182.  *    return a pointer to a 'Prs2LocksData' structure containing
  183.  *    no locks, i.e. numberOfLocks = 0;
  184.  */
  185. extern
  186. RuleLock
  187. prs2MakeLocks ARGS((
  188. ));
  189.  
  190. /*------------------------------------------------------------------
  191.  * prs2AddLock
  192.  *    Add a new lock (filled in with the given data) to a 'prs2Locks'
  193.  *    Note that this frees the space occupied by 'prs2Locks' and reallocates
  194.  *    some other. So this routine should be used with caution!
  195.  */
  196. extern
  197. RuleLock
  198. prs2AddLock ARGS((
  199.     RuleLock        oldLocks,
  200.     ObjectId        ruleId,
  201.     Prs2LockType    lockType,
  202.     AttributeNumber    attributeNumber,
  203.     Prs2PlanNumber    planNumber,
  204.     int            partialindx,
  205.     int             npartial
  206. ));
  207.  
  208. /*------------------------------------------------------------------
  209.  * prs2GetOneLockFromLocks
  210.  *    Given a 'RuleLock' return a pointer to its Nth lock..
  211.  *    (the first locks is lock number 0).
  212.  */
  213. extern
  214. Prs2OneLock
  215. prs2GetOneLockFromLocks ARGS((
  216.     RuleLock    locks,
  217.     int        n
  218. ));
  219.  
  220. /*------------------------------------------------------------------
  221.  * prs2OneLocksAreTheSame
  222.  * return true iff the two 'Prs2OneLock' are the same...
  223.  */
  224. extern
  225. bool
  226. prs2OneLocksAreTheSame ARGS((
  227.     Prs2OneLock    oneLock1,
  228.     Prs2OneLock    oneLock2
  229. ));
  230.  
  231. /*------------------------------------------------------------------
  232.  * prs2OneLockIsMemberOfLocks
  233.  * return true iff the given `Prs2OneLock' is one of the locks of
  234.  * `locks'
  235.  */
  236. extern
  237. bool
  238. prs2OneLockIsMemberOfLocks ARGS((
  239.     Prs2OneLock    oneLock,
  240.     RuleLock    locks
  241. ));
  242.  
  243. /*------------------------------------------------------------------
  244.  * prs2GetLocksFromTuple
  245.  *  Extract the locks from a tuple. It returns a 'RuleLock',
  246.  *  (NOTE:it will never return NULL! Even if the tuple has no
  247.  *  locks in it, it will return a 'RuleLock' with 'numberOfLocks'
  248.  *  equal to 0.
  249.  *  The returned rule lock is a copy, and must be pfreed to avoid memory leaks.
  250.  */
  251. extern
  252. RuleLock
  253. prs2GetLocksFromTuple ARGS((
  254.     HeapTuple        tuple,
  255.     Buffer        buffer
  256. ));
  257.  
  258. /*------------------------------------------------------------------
  259.  * prs2PutLocksInTuple
  260.  *    given a tuple, update its rule lock.
  261.  *    NOTE: the old rule lock is pfreed!
  262.  */
  263. extern
  264. void
  265. prs2PutLocksInTuple ARGS((
  266.     HeapTuple    tuple,
  267.     Buffer    buffer,
  268.     Relation    relation,
  269.     RuleLock    newLocks
  270. ));
  271.  
  272. /*------------------------------------------------------------------
  273.  * prs2PrintLocks
  274.  *    print the prs2 locks in stdout. Used for debugging...
  275.  */
  276. extern
  277. void
  278. prs2PrintLocks ARGS((
  279.     RuleLock    locks
  280. ));
  281.  
  282. /*------------------------------------------------------------------
  283.  * prs2RemoveOneLockInPlace
  284.  *
  285.  * remove a rule lock in place (i.e. no copies)
  286.  */
  287. extern
  288. void
  289. prs2RemoveOneLockInPlace ARGS((
  290.     RuleLock    locks,
  291.     int        n
  292. ));
  293.  
  294. /*------------------------------------------------------------------
  295.  * prs2RemoveAllLocksOfRuleInPlace
  296.  *
  297.  * remove all locks of a given rule in place (i.e. no copies)
  298.  * returns true if the lcoks have changed, false otherwise (i.e.
  299.  * if no locks for this rule were found).
  300.  */
  301. extern
  302. bool
  303. prs2RemoveAllLocksOfRuleInPlace ARGS((
  304.     RuleLock    locks,
  305.     ObjectId    ruleId
  306. ));
  307.  
  308. /*------------------------------------------------------------------
  309.  * prs2RemoveAllLocksOfRule
  310.  *    remove all the locks that have a ruleId equal to the given.
  311.  *    the old `RuleLock' is destroyed and should never be
  312.  *    referenced again.
  313.  *    The new lock is returned.
  314.  */
  315. extern
  316. RuleLock
  317. prs2RemoveAllLocksOfRule ARGS((
  318.     RuleLock    oldLocks,
  319.     ObjectId    ruleId
  320. ));
  321.  
  322. /*------------------------------------------------------------------
  323.  * prs2CopyLocks
  324.  *    Make a copy of a prs2 lock..
  325.  */
  326. extern
  327. RuleLock
  328. prs2CopyLocks ARGS((
  329.     RuleLock    locks
  330. ));
  331.  
  332. /*------------------------------------------------------------------
  333.  * prs2GetLocksFromRelation
  334.  *   Get locks from the RelationRelation
  335.  */
  336. extern
  337. RuleLock
  338. prs2GetLocksFromRelation ARGS((
  339.     Name relationName
  340. ));
  341.  
  342. /*------------------------------------------------------------------
  343.  * prs2LockUnion
  344.  *   Create the union of two RuleLock.
  345.  */
  346. extern
  347. RuleLock
  348. prs2LockUnion ARGS((
  349.     RuleLock    lock1,
  350.     RuleLock    lock2
  351. ));
  352.  
  353. /*------------------------------------------------------------------
  354.  * prs2LockDifference
  355.  *   Create the difference of two RuleLock.
  356.  */
  357. extern
  358. RuleLock
  359. prs2LockDifference ARGS((
  360.     RuleLock    lock1,
  361.     RuleLock    lock2
  362. ));
  363.  
  364. /*------------------------------------------------------------------
  365.  * prs2FindLocksOfType
  366.  *    return a copy of all the locks of a given type.
  367.  */
  368. extern
  369. RuleLock
  370. prs2FindLocksOfType ARGS((
  371.     RuleLock        locks,
  372.     Prs2LockType    locktype
  373. ));
  374.  
  375. /*------------------------------------------------------------------
  376.  * prs2RemoveLocksOfTypeInPlace
  377.  * remove all the lcoks of the given type in place (i.e. No copies)
  378.  */
  379. extern
  380. void
  381. prs2RemoveLocksOfTypeInPlace ARGS((
  382.         RuleLock    locks,
  383.     Prs2LockType    lockType
  384. ));
  385.  
  386. /*------------------------------------------------------------------
  387.  * RuleLockToString
  388.  *   greate a string containing a representation of the given
  389.  *   lock, more suitable for the human brain & eyes than a
  390.  *   sequence of bytes.
  391.  */
  392. extern
  393. char *
  394. RuleLockToString ARGS((
  395.     RuleLock    lock
  396. ));
  397.  
  398. /*------------------------------------------------------------------
  399.  * StringToRuleLock
  400.  *   the opposite of 'RuleLockToString()'
  401.  */
  402. extern
  403. RuleLock
  404. StringToRuleLock ARGS((
  405.     char     *string
  406. ));
  407.  
  408. /*==================================================================
  409.  * Routine to extract rule info from the ParseTree
  410.  *==================================================================
  411.  */
  412. #define GetRuleHintFromParse(p)        (CADR(nth(1,p)))
  413. #define GetRuleNameFromParse(p)        (nth(2,p))
  414. #define GetRuleEventTypeFromParse(p)    (nth(0,nth(3,p)))
  415. #define GetRuleEventTargetFromParse(p)    (nth(1,nth(3,p)))
  416. #define GetRuleQualFromParse(p)        (nth(2,nth(3,p)))
  417. #define GetRuleInsteadFromParse(p)    (nth(3,nth(3,p)))
  418. #define GetRuleActionFromParse(p)    (nth(4,nth(3,p)))
  419. #define GetRuleRangeTableFromParse(p)    (nth(4,p))
  420.  
  421. /*------------------------------------------------------------------
  422.  * Various routines...
  423.  *------------------------------------------------------------------
  424.  */
  425.  
  426. /*====================== FILE: prs2define.c ============================*/
  427. /*------------------------------------------------------------------
  428.  * prs2DefineTupleRule
  429.  *    Define a PRS2 rule (tuple level proccessing)
  430.  */
  431. extern
  432. void
  433. prs2DefineTupleRule ARGS((
  434.     LispValue    parseTree,
  435.     char    *ruleText
  436. ));
  437.  
  438. /*------------------------------------------------------------------
  439.  * prs2RemoveTupleRule
  440.  *    Remove a prs2 rule given its name.
  441.  */
  442. extern
  443. void
  444. prs2RemoveTupleRule ARGS((
  445.     Name    ruleName
  446. ));
  447.  
  448. extern Prs2RuleData prs2FindRuleData();
  449. extern ObjectId prs2InsertRuleInfoInCatalog();
  450. extern void prs2DeleteRuleInfoFromCatalog();
  451. extern void prs2InsertRulePlanInCatalog();
  452. extern void prs2DeleteRulePlanFromCatalog();
  453. extern LispValue prs2GenerateActionPlans();
  454. extern EventType prs2FindEventTypeFromParse();
  455. extern ActionType prs2FindActionTypeFromParse();
  456. extern void changeReplaceToRetrieve();
  457. extern bool prs2AttributeIsOfBasicType();
  458.  
  459. /*====================== FILE: prs2putlocks.c ============================*/
  460.  
  461. /*------------------------
  462.  * prs2AddTheNewRule
  463.  *     add a new rule. Decide what kind of lock to use (tuple-level or
  464.  *     relation-level lock). Add locks and/or stubs and the appropriate
  465.  *    system catalog info.
  466.  *------------------------
  467.  */
  468. extern
  469. void
  470. prs2AddTheNewRule ARGS((
  471.     Prs2RuleData    r,
  472.     List        hint
  473. ));
  474.  
  475. /*------------------------
  476.  * prs2DeleteTheOldRule
  477.  *    delete a rule given its rule oid. Find if it was a tuple-level
  478.  *    lock or a relation-level lock rule and do the right thing
  479.  *    (delete locks/stubs & system catalog info).
  480.  *------------------------
  481.  */
  482. extern
  483. void
  484. prs2DeleteTheOldRule ARGS((
  485.     ObjectId    ruleId
  486. ));
  487.  
  488. extern void prs2FindLockTypeAndAttrNo();
  489. extern LispValue prs2FindConstantQual();
  490. extern LispValue prs2FindConstantClause();
  491. extern bool prs2IsATupleLevelLockRule();
  492.  
  493. /*====================== FILE: prs2tup.c ============================*/
  494. extern bool prs2DefTupleLevelLockRule();
  495. extern RuleLock prs2FindLocksThatWeMustPut();
  496. extern bool prs2DoesStubDependsOnAttribute();
  497. extern bool prs2LockWritesAttributes();
  498. extern void prs2FindAttributesOfQual();
  499. extern void prs2UndefTupleLeveLockRule();
  500. extern void prs2RemoveTupleLeveLocksAndStubsOfManyRules();
  501.  
  502. /*====================== FILE: prs2rel.c ============================*/
  503. extern void prs2DefRelationLevelLockRule();
  504. extern void prs2UndefRelationLevelLockRule();
  505. extern void prs2RemoveRelationLevelLocksOfRule();
  506. extern void prs2SetRelationLevelLocks();
  507. extern void prs2AddRelationLevelLock();
  508.  
  509. /*===================================================================
  510.  * RULE PLANS
  511.  *
  512.  * The rule plans are stored in the "pg_prs2plans" relation.
  513.  * There are different types of plans.
  514.  * Each "plan" contains appart from one or more parsetrees/plans
  515.  * some rule information.
  516.  * The first item of all the plans is a string describing the type of the
  517.  * plan. This string can be "action" for the ACTION plans, and "export"
  518.  * for the EXPORT plans.
  519.  * The second item is the plan information itself, and its format depends
  520.  * on the type of plan:
  521.  *
  522.  * a) ACTION PLANS:
  523.  *    They are a list with at least two items:
  524.  *
  525.  *    The first item is a list giving some info for the rule.
  526.  *    Currently this can have the following elements:
  527.  *    The first element is an integer, equal to 1 if the rule is an
  528.  *    instead rule, 0 otherwise.
  529.  *    The second is the "event attribute" number, i.e. the attrno
  530.  *    of the attribute specified in the "on <action> to <rel>.<attr>"
  531.  *    rule clause.
  532.  *    The third is the "updated attribute" number, i.e. the attrno 
  533.  *     of the attribute updated by a backward chaining rule.
  534.  *
  535.  *    The second item is the qualification of the rule.
  536.  *    This should be tested before and only if it succeeds should the
  537.  *    action part of the rule be executed.
  538.  *    This qualifcation can be either "nil" which means that
  539.  *    there is no qualification and the action part of the rule should be
  540.  *    always executed, or a 2 item list, the first item beeing the parse
  541.  *    tree corresponding to the qualification and the second one the plan.
  542.  *
  543.  *    Finally the (optional) rest items are the actions of the rule.
  544.  *    Each one of them can be either a 2 item list (parse tree + plan)
  545.  *    or the string "abort" which means that the current Xaction should be
  546.  *    aborted.
  547.  *
  548.  * b) EXPORT PLANS:
  549.  *    this is the kind of plan to be executed when an export
  550.  *    lock is broken.
  551.  *    The first item of the plan is a list with information
  552.  *    about this export lock. This information consists of the
  553.  *    following items:
  554.  *        1) a string containing the lock (use `lockin' to recreate
  555.  *        the lock).
  556.  *    Then there is the plan to be run in order to add/delete
  557.  *    rule locks when this export lock is broken. This plan is actually
  558.  *     a two item list conatining the parsetree and the actual plan.
  559.  *    
  560.  *===================================================================
  561.  */
  562.  
  563. #define Prs2RulePlanType_EXPORT            ("export")
  564. #define Prs2RulePlanType_ACTION            ("action")
  565.  
  566. #define prs2GetTypeOfRulePlan(x)        (CAR(x))
  567. #define prs2GetPlanInfoFromRulePlan(x)        (CDR(x))
  568.  
  569. #define prs2GetRuleInfoFromActionPlan(x)    (CAR(CDR(x)))
  570. #define prs2GetQualFromActionPlan(x)        (CADR(CDR(x)))
  571. #define prs2GetActionsFromActionPlan(x)        (CDR(CDR(CDR(x))))
  572.  
  573. #define prs2GetLockInfoFromExportPlan(x)    (CAR(CDR(x)))
  574. #define prs2GetActionPlanFromExportPlan(x)    (CADR(CDR(x)))
  575.  
  576. extern
  577. Boolean
  578. prs2IsRuleInsteadFromRuleInfo ARGS((
  579.     LispValue ruleInfo
  580. ));
  581.  
  582. extern
  583. AttributeNumber
  584. prs2GetEventAttributeNumberFromRuleInfo ARGS((
  585.     LispValue ruleInfo
  586. ));
  587.  
  588. extern
  589. AttributeNumber
  590. prs2GetUpdatedAttributeNumberFromRuleInfo ARGS((
  591.     LispValue ruleInfo
  592. ));
  593.  
  594. #define prs2GetParseTreeFromOneActionPlan(x)    (CAR(x))
  595. #define prs2GetPlanFromOneActionPlan(x)        (CADR(x))
  596.  
  597. /*
  598.  * prs2GetRulePlanFromCatalog
  599.  * Given a rule id and a plan number get the appropriate plan
  600.  * from the system catalogs. At the same time construct a list
  601.  * with all the Param nodes contained in this plan.
  602.  */
  603. extern
  604. LispValue
  605. prs2GetRulePlanFromCatalog ARGS((
  606.     ObjectId        ruleId,
  607.     Prs2PlanNumber    planNumber,
  608.     ParamListInfo    *paramListP
  609. ));
  610.  
  611. /*==================================================================
  612.  * AttributeValues stuff...
  613.  *==================================================================
  614.  *
  615.  * These definitions & routines make possible to use an array of
  616.  * attribute values instead of a tuple, thus making easier to change
  617.  * these values without having to make multiple copies of the tuple.
  618.  * It also makes easier to keep some extra information about how the value
  619.  * of attributes were calculated.
  620.  * 
  621.  * Explanation of the fileds of a 'AttributesValuesData':
  622.  * For each attribute of the tuple there is a corresponding
  623.  * 'AttributeValuesData' structure, with the following fields:
  624.  *    value:    the value of this attribute. This is initially
  625.  *        initialized to the value stroed in the tuple.
  626.  *    isNull: true if the coresponding attribute in the tuple is
  627.  *        null.
  628.  *    isCalculated: Initially false. It becomes true if we have
  629.  *        checked this attribute for possibly applicable
  630.  *        rules, and we have finally calculated its correct value.
  631.  *    isChanged: If an attribute has been checked for rules (i.e.
  632.  *        has its 'isCalculated' field equal to true), then two
  633.  *        things can happen. Either thre was no applicable rule
  634.  *        found, in which case we have used the value stored
  635.  *        in the tuple and 'isChanged' is equal to false,
  636.  *        or there was a rule that calculated a value for
  637.  *        this attribute, in which case 'isChanged' is true, and
  638.  *        'value' is this new value.
  639.  */
  640.  
  641. typedef struct AttributeValuesData {
  642.     Datum    value;
  643.     Boolean    isNull;
  644.     Boolean    isCalculated;
  645.     Boolean    isChanged;
  646. } AttributeValuesData;
  647.  
  648. typedef AttributeValuesData *AttributeValues;
  649. #define InvalidAttributeValues ((AttributeValues)NULL)
  650.  
  651. /*---------------------------------------------------------------------
  652.  * attributeValuesCreate
  653.  *    Given a tuple create the corresponding 'AttributeValues' array.
  654.  *    Note that in the beginning all the 'value' entries are copied
  655.  *    from the tuple and that 'isCalculated' are all false.
  656.  */
  657. extern
  658. AttributeValues
  659. attributeValuesCreate ARGS((
  660.     HeapTuple tuple,
  661.     Buffer buffer,
  662.     Relation relation
  663. ));
  664.  
  665. /*---------------------------------------------------------------------
  666.  * attributeValuesFree
  667.  *    Free a previously allocated 'AttributeValues' array.
  668.  */
  669. extern
  670. void
  671. attributeValuesFree ARGS((
  672.     AttributeValues a,
  673.     Relation relation
  674. ));
  675.  
  676. /*--------------------------------------------------------------------
  677.  * attributeValuesMakeNewTuple
  678.  *    Given the old tuple and the values that the new tuple should have
  679.  *    create a new tuple.
  680.  *    Returns 1 if a new tuple has been created (stored in *newTupleP)
  681.  *    and 0 if the old tuple is the same as the new tuple, in which
  682.  *    case we can use 'tuple' instead, in order to avoid a redudant
  683.  *    copy operation.
  684.  */
  685. extern
  686. int
  687. attributeValuesMakeNewTuple ARGS((
  688.     HeapTuple tuple,
  689.     Buffer buffer,
  690.     AttributeValues attrValues,
  691.     RuleLock locks,
  692.     Prs2LockType lockType,
  693.     Relation relation,
  694.     HeapTuple *newTupleP
  695. ));
  696.  
  697. /*--------------------------------------------------------------------
  698.  * attributeValuesCombineNewAndOldTuple
  699.  *    In the case of a replace command, given the 'raw' tuple, i.e.
  700.  *    the tuple as retrieved by the AM (*not* changed by any backward
  701.  *    chaining rules) and the 'new' tuple (the one with all backward
  702.  *    chaning rules activated + with all user updates)
  703.  *    form the tuple that will finally replace the old one.
  704.  */
  705. extern
  706. HeapTuple
  707. attributeValuesCombineNewAndOldTuple ARGS((
  708.     AttributeValues rawAttrValues,
  709.     AttributeValues newAttrValues,
  710.     Relation relation,
  711.     AttributeNumberPtr attributeArray,
  712.     AttributeNumber numberOfAttributes
  713. ));
  714.  
  715. /*========================================================================
  716.  * EState Rule Info + Rule Stack
  717.  *
  718.  * This structure (which is kept inside the executor state node EState)
  719.  * contains some information used by the rule manager.
  720.  *
  721.  * RULE DETECTION MECHANISM:
  722.  * prs2Stack: A stack where enough information is ketp in order to detect
  723.  *    rule loops.
  724.  * prs2StackPointer: the next (free) stack entry
  725.  * prs2MaxStackSize: number of stack entries allocated. If
  726.  *      prs2StackPointer is >= prs2MaxStackSize the stack is full
  727.  *    and we have to reallocate some more memory...
  728.  *
  729.  * MISC INFO:
  730.  * 
  731.  *========================================================================
  732.  */
  733. typedef struct Prs2StackData {
  734.     ObjectId        ruleId;        /* OID of the rule */
  735.     ObjectId        tupleOid;    /* the tuple that activated the rule*/
  736.     AttributeNumber    attrNo;        /* the locked attribute */
  737. } Prs2StackData;
  738.  
  739. typedef Prs2StackData *Prs2Stack;
  740.  
  741. typedef struct Prs2EStateInfoData {
  742.     Prs2Stack    prs2Stack;    /* the stack used for loop detection */
  743.     int        prs2StackPointer;    /* the next free stack entry */
  744.     int        prs2MaxStackSize;    /* the max number of entries */
  745. } Prs2EStateInfoData;
  746.  
  747. typedef  Prs2EStateInfoData *Prs2EStateInfo;
  748.  
  749. /*
  750.  * prs2RuleStackPush
  751.  *    Add a new entry to the rule stack. First check if there is enough
  752.  *    stack space. otherwise reallocate some more memory...
  753.  */
  754. extern
  755. void
  756. prs2RuleStackPush ARGS((
  757.     Prs2EStateInfo p,
  758.     ObjectId ruleId,
  759.     ObjectId tupleOid,
  760.     AttributeNumber attributeNumber
  761. ));
  762.  
  763. /*
  764.  * prs2RuleStackPop
  765.  *    Discard the top entry of the stack
  766.  */
  767. extern
  768. void
  769. prsRuleStackPop ARGS((
  770.     Prs2EStateInfo p
  771. ));
  772.  
  773. /*
  774.  * prs2RuleStackSearch
  775.  *   Search for a stack entry matching the given arguments.
  776.  *   Return true if found, false otherwise...
  777.  */
  778. extern
  779. bool
  780. prs2RuleStackSearch ARGS((
  781.     Prs2EStateInfo p,
  782.     ObjectId ruleId,
  783.     ObjectId tupleOid,
  784.     AttributeNumber attributeNumber
  785. ));
  786.  
  787. /*
  788.  * prs2RuleStackInitialize
  789.  *   Intialize the stack.
  790.  */
  791. extern
  792. Prs2EStateInfo
  793. prs2RuleStackInitialize ARGS((
  794. ));
  795.  
  796. /*
  797.  * prs2RuleStackFree
  798.  *   Free the memory occupied by the stack.
  799.  */
  800. extern
  801. void
  802. prs2RuleStackFree ARGS((
  803.     Prs2EStateInfo p
  804. ));
  805.  
  806. /*========================================================================
  807.  * VARIOUS ROUTINES....
  808.  *========================================================================
  809.  */
  810. /*
  811.  * PlanToString
  812.  *
  813.  * Given a plan (or an arbritary (?) lisp structure, transform it into a
  814.  * a strign which has the following two properties
  815.  *  a) it is readable by (some) humans
  816.  *  b) it can be used to recreate the original plan/lisp structure
  817.  *     (see routine "StringToPlan").
  818.  *
  819.  *
  820.  * XXX Maybe this should be placed in another header file...
  821.  */
  822. extern
  823. char *
  824. PlanToString ARGS((
  825.     LispValue lispStructure
  826. ));
  827.  
  828. /*========================================================================
  829.  * These routines are some 'internal' routines. 
  830.  *========================================================================
  831.  */
  832.  
  833. extern LispValue StringToPlan();
  834. extern LispValue StringToPlanWithParams();
  835.  
  836. /* XXX this causes circular dependency!
  837. extern EState CreateExecutorState();
  838. */
  839.  
  840. extern Prs2Status prs2Retrieve();
  841. extern Prs2Status prs2Append();
  842. extern Prs2Status prs2Delete();
  843. extern Prs2Status prs2Replace();
  844. extern RuleLock prs2FindLocksForNewTupleFromStubs();
  845.  
  846. /*
  847.  * These are functions in prs2plans.c
  848.  */
  849. /*extern LispValue prs2GetRulePlanFromCatalog(); -glass */
  850. extern int prs2CheckQual();
  851. extern void prs2RunActionPlans();
  852. extern int prs2RunOnePlanAndGetValue();
  853. extern void prs2RunOnePlan();
  854. extern LispValue prs2MakeQueryDescriptorFromPlan();
  855.  
  856. /*
  857.  * functions in prs2bkwd.c
  858.  */
  859. extern void prs2CalculateAttributesOfParamNodes();
  860. extern void prs2ActivateBackwardChainingRules();
  861. extern void prs2ActivateForwardChainingRules();
  862.  
  863. /*
  864.  * functions in prs2impexp.c
  865.  */
  866. extern RuleLock prs2FindNewExportLocksFromLocks();
  867. extern RuleLock prs2GetExportLockFromCatalog();
  868. extern void prs2ActivateExportLockRulePlan();
  869.  
  870. /*============== RULE STATISTICS ============================*/
  871. /*
  872.  * the following variables/routines are used to print stats about
  873.  * the tuple level system usage.
  874.  * (all this stuff is defined in "rules/prs2/prs2main.c")
  875.  */
  876. extern int Prs2Stats_rulesActivated;
  877. extern int Prs2Stats_rulesTested;
  878. extern void ResetPrs2Stats();
  879. extern void ShowPrs2Stats();
  880.  
  881. #endif Prs2Included
  882.  
  883.