home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre2.z / postgre2 / ref / postquel / definerule < prev    next >
Encoding:
Text File  |  1992-08-27  |  5.8 KB  |  216 lines

  1. .\" XXX standard disclaimer belongs here....
  2. .\" $Header: /private/postgres/ref/postquel/RCS/definerule,v 1.15 1992/07/14 05:54:17 ptong Exp $
  3. .SP "DEFINE RULE" COMMANDS 6/14/90
  4. .XA 2 "Define Rule"
  5. .uh NAME
  6. .lp
  7. define rule \(em Define a new rule
  8. .uh SYNOPSIS
  9. .lp
  10. .(l
  11. \fBdefine\fR [\fBinstance\fR | \fBrewrite\fR] \fBrule\fR rule_name
  12.     [\fBas exception to\fR rule_name_2]
  13.     \fBis\fR \fBon\fR event
  14.       \fBto\fR object [[\fBfrom\fR clause] \fBwhere\fR clause]
  15.     \fBdo\fR [\fBinstead\fR]
  16.     [action | nothing | '[' action ... ']']
  17. .)l
  18. .uh DESCRIPTION
  19. .lp
  20. .b Define
  21. .b rule
  22. is used to define a new rule.
  23. There are two implementations of the rules system, one based on 
  24. .b query
  25. .b rewrite
  26. and the other based on 
  27. .b instance-level
  28. processing.  In general, the instance-level system is more efficient
  29. if there are many rules on a single class, each covering a small
  30. subset of the instances.  The rewrite system is more
  31. efficient if large scope rules are being defined. The user
  32. can optionally choose which rule system to use by specifying 
  33. .i rewrite
  34. or
  35. .i instance
  36. in the command.  If the user does not specify which system to use, \*(PP
  37. defaults to using the instance-level system.  In the long run
  38. \*(PP will automatically decide which rules system to use and the possibility
  39. of user selection will be removed.
  40. .lp
  41. Here, event is one of:
  42. .(l
  43. .ft C
  44. retrieve 
  45. replace 
  46. delete
  47. append
  48. .ft
  49. .)l
  50. Moreover, object is either:
  51. .(l
  52. a class name
  53. \fIor\fR
  54. class.column
  55. .)l
  56. The FROM clause, the WHERE clause, and the
  57. action are respectively normal \*(PQ
  58. FROM clauses, WHERE clauses 
  59. and collections of \*(PQ commands
  60. with the following change:
  61. .in +6
  62. .ll -6
  63.  
  64. .b new
  65. or
  66. .b current
  67. can appear instead of 
  68. an instance variable whenever an instance 
  69. variable is permissible in \*(PQ.
  70. .in -6
  71. .ll +6
  72. .lp
  73. The semantics of a rule is that at the time an individual instance is
  74. accessed, updated, inserted or deleted, there is a current instance (for
  75. retrieves, replaces and deletes) and a new instance (for replaces and appends).
  76. If the event specified in the ON clause and the condition specified
  77. in the WHERE clause are true 
  78. for the current instance,
  79. then the action part of the rule is executed.  First, however, values
  80. from fields in the current instance and/or the new instance are substituted 
  81. for:
  82. .(l
  83. .ft C
  84. current.attribute-name
  85. new.attribute-name
  86. .ft
  87. .)l
  88. The action part of the rule executes with same command and
  89. transaction identifier as the user command that caused activation.  
  90. .lp
  91. A note of caution about \*(PQ rules is in order.  If the same class
  92. name or instance variable appears in the event, where clause 
  93. and the action parts of a rule, they are 
  94. all considered different tuple variables.  
  95. More accurately, new and current are the only
  96. tuple variables that are shared between these clauses.
  97. For example the following two rules have the
  98. same semantics: 
  99. .(l
  100. .ft C
  101. on replace to EMP.salary where EMP.name = "Joe"
  102.     do replace EMP ( ... ) where ...
  103.  
  104. on replace to EMP-1.salary where EMP-2.name = "Joe"
  105.     do replace EMP-3 ( ... ) where ...
  106. .ft
  107. .)l
  108. .lp
  109. Each rule can have the optional tag "instead".  Without this tag the
  110. action will be performed in addition to the user command 
  111. when the event 
  112. in the condition part of the rule occurs. 
  113. Alternately, the action part will be done instead of the user command.
  114. In this later case, the action can be the keyword
  115. .b nothing.
  116. .lp
  117. When choosing between the rewrite and instance rule systems for a particular
  118. rule application, remember that in the rewrite system  'current' refers to a
  119. relation and some qualifiers whereas in the instance system it refers to an
  120. instance (read tuple).
  121.  
  122. .uh EXAMPLES
  123. .lp
  124. .ft C
  125. /* Make Sam get the same salary adjustment as Joe */
  126. .ft
  127. .(l
  128. .ft C
  129. define rule example_1 is
  130.     on replace to EMP.salary where current.name = "Joe"
  131.     do replace EMP (salary = new.salary)
  132.     where EMP.name = "Sam"
  133. .ft
  134. .)l
  135. At the time Joe receives a salary adjustment, the event will become
  136. true and Joe's current instance and proposed new instance are available
  137. to the execution routines.  Hence, his new salary is substituted into the 
  138. action part of the rule which is subsequently executed.  This 
  139. propagates Joe's salary on to Sam.
  140. .lp
  141. .ft C
  142. /* Make Bill get Joe's salary when it is accessed */
  143. .ft
  144. .(l
  145. .ft C
  146. define rule example_2 is
  147.     on retrieve to EMP.salary
  148.         where current.name = "Bill"
  149.     do instead
  150.     retrieve (EMP.salary) where EMP.name = "Joe"
  151. .ft
  152. .)l
  153. .lp
  154. .ft C
  155. /* Deny Joe access to the salary of employees in */
  156. /* the shoe department.  Note: pg_username()     */
  157. /* returns the name of the current user          */
  158. .ft
  159. .(l
  160. .ft C
  161. define rule example_3 is
  162.     on retrieve to EMP.salary
  163.     where current.dept = "shoe"
  164.               and pg_username() = "Joe"
  165.     do instead nothing
  166. .ft
  167. .)l
  168. .lp
  169. .ft C
  170. /* Create a view of the employees working in */
  171. /* the toy department.                       */
  172. .ft
  173. .(l
  174. .ft C
  175. create TOYEMP(name = char16, salary = int4)
  176.  
  177. define rule example_4 is
  178.     on retrieve to TOYEMP
  179.     do instead retrieve (EMP.name, EMP.salary)
  180.     where EMP.dept = "toy"
  181. .ft
  182. .)l
  183. .lp
  184. .ft C
  185. /* All new employees must make 5,000 or less */
  186. .ft
  187. .(l
  188. .ft C
  189. define rule example_5 is
  190.     on append to EMP where new.salary > 5000
  191.     do replace new(salary = 5000)
  192. .ft
  193. .)l
  194. .uh "SEE ALSO"
  195. .lp
  196. postquel(commands).
  197. .uh BUGS
  198. .lp
  199. Exceptions are not implemented in Version \*(PV.
  200. .lp
  201. The object in a \*(PQ rule cannot be an array reference and cannot have
  202. parameters.
  203. .lp
  204. The WHERE clause can not have a FROM clause.
  205. .lp
  206. Only one \*(PQ command can be specified in the action part of a tuple rule
  207. and it can only be a
  208. replace, append, retrieve or delete command.
  209. .lp
  210. The rewrite rule system does support multiple action rules surrouas long as the
  211. event is not retrieve.
  212. .lp
  213. The query rewrite rule system now supports most rule semantics, and closely
  214. parallels the tuple system.  It also attempts to avoid odd semantics by
  215. running instead rules before non-instead rules.
  216.