home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / database-actions.xml < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  16.3 KB  |  445 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3.   Copyright 1999-2004 The Apache Software Foundation
  4.  
  5.   Licensed under the Apache License, Version 2.0 (the "License");
  6.   you may not use this file except in compliance with the License.
  7.   You may obtain a copy of the License at
  8.  
  9.       http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11.   Unless required by applicable law or agreed to in writing, software
  12.   distributed under the License is distributed on an "AS IS" BASIS,
  13.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.   See the License for the specific language governing permissions and
  15.   limitations under the License.
  16. -->
  17. <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.0//EN" "document-v10.dtd">
  18.  
  19. <document>
  20.  
  21.   <header>
  22.     <title>Database Actions</title>
  23.     <authors>
  24.       <person name="Christian Haul" email="haul@apache.org"/>
  25.     </authors>
  26.   </header>
  27.  
  28.   <body>
  29.     <s1 title="Introduction">
  30.       <p>
  31.         Two different sets of actions exist, that deal with (object) relational
  32.         database access through JDBC. The original database actions provide a
  33.         relatively simple interface to store, modify, delete and retrieve data.
  34.         They are oriented towards usage of request parameters for input and
  35.         request attributes together with sitemap variables for output and do
  36.         not support auto increment column types. In addition, the description of
  37.         the database structure is split over several files since these actions
  38.         attempt to use all tables in a provided description.
  39.       </p>
  40.       <p>
  41.         The modular database actions provide similar functionality. In contrast
  42.         to the original actions they allow to store the database meta data in a
  43.         single file and to switch input and output flexible through the use of
  44.         modules. Even for auto increment columns specific modules exist that
  45.         cover a wide range of database management systems.
  46.       </p>
  47.       <p>
  48.         For an overview of column types supported by the modular database
  49.         actions, see javadocs for JDBCTypeConversions. The types supported by
  50.         the original actions are documented in AbstractDatabaseAction.
  51.       </p>
  52.     </s1>
  53.  
  54.     <s1 title="Original Database Actions">
  55.       <p>
  56.         The original database actions have evolved quite a bit and at different
  57.         speeds. The add action is certainly the most complete one, providing
  58.         support for multiple tables and rows. However, the interface has become
  59.         a bit inconsistent.
  60.       </p>
  61.       <p>
  62.         If an error occurs, the original database actions will throw an
  63.         exception.
  64.       </p>
  65.       <s2 title="Describing the Structure of your DB - descriptor.xml">
  66.         <p>
  67.           The key to database actions is a file that describes database meta
  68.           data in XML. The original actions will ignore all but the first table
  69.           and act only on one row. Only the add action will try to access all
  70.           tables that are contained in this description. As a consequence, each
  71.           HTML form needs to have a corresponding descriptor file if different
  72.           tables are affected.
  73.         </p>
  74.         <p>
  75.           The file name has no meaning and does not need to be
  76.           <code>descriptor.xml</code> - it can even be a Cocoon pipeline. The
  77.           name of the root element in a descriptor file is ignored. Only
  78.           <code>table</code> elements nested on first level inside the root
  79.           element are parsed by the actions. All unknown elements or attributes
  80.           are ignored.
  81.         </p>
  82.         <p>
  83.           For each table a <code>table</code> element needs to be present. 
  84.         </p>
  85.         <source>
  86. <![CDATA[
  87. <?xml version="1.0"?>
  88.  
  89. <employee>
  90.   <connection>personnel</connection>
  91.   <table name="employee">
  92.     <keys>
  93.       <key param="employee" dbcol="id" type="int" mode="manual"/>
  94.     </keys>
  95.     <values>
  96.       <value param="name" dbcol="name" type="string"/>
  97.       <value param="department" dbcol="department_id" type="int"/>
  98.     </values>
  99.   </table>
  100. </employee>
  101. ]]>    
  102.         </source>
  103.         <p>
  104.           Describes a single table named "employee". In addition a database
  105.           connection is specified. See <link
  106.             href="../../developing/datasources.html">here</link> for more
  107.           information on database connections. 
  108.         </p>
  109.  
  110.         <s3 title="Key Columns">
  111.           <p>
  112.             Tables may or may not have key columns. A key column is a column
  113.             that is part of the primary key. Actually, candidate keys should do
  114.             as well.
  115.           </p>
  116.           <p>
  117.             All key columns are contained in a <code>keys</code> child element
  118.             of the <code>table</code> element. Each column has a
  119.             <code>key</code> element to define its properties. The
  120.             <code>dbcol</code> attribute holds the column name,
  121.             <code>type</code> is the JDBC type name for this column (have a
  122.             look at AbstactDatabaseAction source for valid type names),
  123.             <code>param</code> specifies the name of the request parameter to
  124.             use, and <code>mode</code> sets how the value for this column is
  125.             obtained on adding a row.
  126.           </p>
  127.           <p>
  128.             Through the mode attribute the behaviour of the add action can be
  129.             changed.
  130.           </p> 
  131.           <p>
  132.             Default mode is "automatic" and to let the database create the key
  133.             value by setting this value to <code>null</code>. The created value
  134.             can not be read back from the database and will not be available as
  135.             request attribute or sitemap variable.
  136.           </p>
  137.           <p>
  138.             A mode of "manual" will query the database for the maximum current
  139.             value, add 1 to it and use that for a value.
  140.           </p>
  141.           <p>
  142.             A mode of "form" will use the corresponding request parameter.
  143.           </p>
  144.           <p>
  145.             A mode of "request-attribute" will use the corresponding request
  146.             attribute. The name specified in the <code>param</code> attribute
  147.             will be automatically prefixed with the class name.
  148.           </p>
  149.           <p>
  150.             Key values will be propagated to sitemap variables and - prefixed
  151.             with the class name - request attributes.
  152.           </p>
  153.         </s3>
  154.         <s3 title="Other Columns">
  155.           <p>
  156.             All other columns are contained in a <code>values</code> child
  157.             element of the <code>table</code> element. Each column has a
  158.             <code>value</code> element to define its properties. Properties are
  159.             similar to those for key columns. A <code>mode</code> attribute
  160.             does not exist for value columns. Instead, request parameters and
  161.             request attributes are tried in this order for the specified
  162.             parameter. 
  163.           </p>
  164.           <p>
  165.             Request attribute names are <em>not</em> prefixed with the class
  166.             name. Thus, to insert the value of a key column of the previous row
  167.             or previous table into a value column, it needs to be named
  168.             <code>org.apache.cocoon.acting.AbstractDatabaseAction:key:table:dbcol</code>. 
  169.           </p>
  170.           <p>
  171.             Value columns are propagated to request attributes with class name
  172.             prefix. They are not available for the sitemap.
  173.           </p>
  174.         </s3>
  175.       </s2>
  176.     </s1>
  177.     <s1 title="Modular Database Actions">
  178.       <p>
  179.         The modular database actions were mainly created to make auto increment
  180.         columns available, handle input and output flexibly, and have a
  181.         consistent interface. A successful action will return the number of
  182.         rows affected in a sitemap parameter named <code>row-count</code>. The
  183.         added features required to change the descriptor file format in
  184.         incompatible ways.
  185.       </p>
  186.       <p>
  187.         It can be configured if an exception will be thrown when an error
  188.         occurs.
  189.       </p>
  190.       <s2 title="Describing the Structure of your DB - descriptor.xml">
  191.         <p>
  192.           Like the original actions, the modular actions need meta data in an
  193.           XML file. However, that file may contain any number of tables, not
  194.           just the ones needed for a single request. The tables actually used
  195.           are referenced through a <code>table-set</code>. Unknown elements and
  196.           attributes are ignored. This way a descriptor file can be shared with
  197.           other actions like the form validator.
  198.         </p>
  199.         <p>
  200.           For the flexible input and output handling, the modular database
  201.           actions rely on <link href="../concepts/modules.html">modules</link>.
  202.           Have a look at those before proceeding.
  203.         </p>
  204.         <p>
  205.           The following is a snippet from a descriptor file. 
  206.         </p>
  207.         <source>
  208. <![CDATA[
  209. <root>
  210.    <connection>personnel</connection>
  211.    <table name="user" alias="user">
  212.       <keys>
  213.          <key name="uid" type="int" autoincrement="true">
  214.             <mode name="auto"  type="autoincr"/>
  215.          </key>
  216.       </keys>
  217.       <values>
  218.          <value name="name"      type="string"></value>
  219.          <value name="firstname" type="string"></value>
  220.          <value name="uname"     type="string"></value>
  221.       </values>   
  222.    </table>
  223. ]]>
  224.         </source>
  225.         <p>
  226.           The <code>table</code> element has an additional attribute
  227.           <code>alias</code> which is an alternative name to reference
  228.           the table from a table set. The descriptor file is searched
  229.           top down for tables whose <code>name</code> or
  230.           <code>alias</code> match. The <code>alias</code>n attribute
  231.           is useful if a complex join expression is used as table
  232.           name. In such a case modifications like update, insert,
  233.           delete will likely fail.
  234.         </p>
  235.         <p>
  236.           Another application of aliases if different numbers of columns should
  237.           be affected by an operation. or if a table contains several candidate
  238.           keys that are used alternatively. This way, different views to a
  239.           table can be created.
  240.         </p>
  241.         <s3 title="Key Columns">
  242.           <p>
  243.             The descriptor file resembles the one for the original actions. One
  244.             major difference is the absence of <code>dbcol</code> and
  245.             <code>param</code> attributes. Instead there is a <code>name</code>
  246.             attribute which corresponds to the <code>dbcol</code> attribute and
  247.             specifies the database column name.
  248.           </p>
  249.           <p>
  250.             If a column is an auto increment column, the similar named attribute
  251.             indicates this. Auto increment columns will be handled differently
  252.             on insert operations.
  253.           </p>
  254.           <p>
  255.             Instead of specifying a parameter name, the actions support to use
  256.             different input modules for each operation through the nested
  257.             <code>mode</code> elements. This is described in more detail below.
  258.           </p>
  259.           <p>
  260.             Note here though, that not every column needs a <code>mode</code>
  261.             element: The actions default to the module defined as
  262.             <code>request</code> which is in a default installation to obtain
  263.             the values from request parameters. The name of the parameter
  264.             defaults to table name dot column name.
  265.           </p>
  266.         </s3>
  267.         <s3 title="Other Columns">
  268.           <p>
  269.             Apart from the fact that the auto increment columns are only
  270.             supported for key columns, everything said above applies to value
  271.             columns as well.
  272.           </p>
  273.         </s3>
  274.         <s3 title="Operation Mode Types">
  275.           <p>
  276.             Basically, two different mode types exist:
  277.             <code>autoincrement</code> which is used whenever data shall be
  278.             inserted into a table and this particular key column has the
  279.             auto increment attribute set and <code>others</code> for all other
  280.             requirements.
  281.           </p>
  282.           <p>
  283.             In addition, a table-set can specify different mode types to use
  284.             instead of the predefined type names. Through this, and the fact
  285.             that every mode can specify a different input module, it is easy to
  286.             use different input modules for different tasks and forms.
  287.           </p>
  288.           <p>
  289.             One special mode type name exists that matches all requested ones:
  290.             <code>all</code> This makes it easier to configure only some
  291.             columns differently for each table-set.
  292.           </p>
  293.         </s3>
  294.         <s3 title="How to obtain Values">
  295.           <p>
  296.             As said above, these actions default to reading from request
  297.             parameters with a default parameter name. By specifying
  298.             <code>mode</code> elements, this can be overridden. Any component
  299.             that implements the <code>InputModule</code> interface can be used
  300.             to obtain values. How to make such modules known to Apache Cocoon
  301.             is described  <link href="../concepts/modules.html">elsewhere</link>. 
  302.           </p>
  303.           <p>
  304.             Beside using different input modules, their parameters can be set
  305.             in place, for example to override parameter names, configure a
  306.             random generator or a message digest algorithm.
  307.           </p>
  308.  
  309.           <source>
  310. <![CDATA[
  311.    <table name="user_groups">
  312.       <keys>
  313.          <key name="uid" type="int">
  314.             <mode name="request" type="request">
  315.                <parameter>user_groups.uid</parameter>
  316.             </mode>
  317.             <mode name="attribute" type="attrib">
  318.                <parameter>org.apache.cocoon.components.modules.output.OutputModule:user.uid[0]</parameter>
  319.             </mode>
  320.          </key>
  321.          <key name="gid" type="int" set="master">
  322.             <mode name="request" type="all">
  323.                <parameter>user_groups.gid</parameter>
  324.             </mode>
  325.          </key>
  326.       </keys>
  327.    </table>
  328. ]]>
  329.           </source>
  330.           <p>
  331.             The above example shows just that: the <code>parameter</code>
  332.             element is not read by the database action itself but the
  333.             complete <code>mode</code> configuration object is passed to the
  334.             input module. Both the request attribute and the request parameter
  335.             input modules understand this parameter attribute which takes
  336.             precedence over the default one.
  337.           </p>
  338.           <p>
  339.             Another feature when obtaining values is tied to the
  340.             <code>type</code> attribute: Different modes can be used in
  341.             different situations. The basic setup uses two different mode
  342.             types: <code>autoincrement</code> when inserting in key columns
  343.             that have an indicator that they are indeed auto increment columns
  344.             and <code>others</code> for insert operations on all other columns
  345.             and all other operations on all columns.
  346.           </p>
  347.           <p>
  348.             Table-sets can override the default names for these two mode type
  349.             name categories with arbitrary names except the special name
  350.             <code>all</code>. A mode that carries the type name "all" is used
  351.             with all requested type names. Lookup obeys first match principle
  352.             so that all modes are tested from top to bottom and the first that
  353.             matches is used.
  354.           </p>
  355.         </s3>
  356.         <s3 title="How to store Values e.g. in your Session">
  357.           <p>
  358.             All modular database action can be configured to use any component
  359.             that implements the <code>OutputModule</code> interface to store
  360.             values. The output module is chosen on declaring the action in the
  361.             sitemap or dynamically with a sitemap parameter. If no output
  362.             module is specified, the default it to use the request attribute
  363.             module.
  364.           </p>
  365.           <p>
  366.             The interface does not allow to pass configuration information to
  367.             the output module. This has to be done when the module is declared
  368.             e.g. in cocoon.xconf.
  369.           </p>
  370.         </s3>
  371.         <s3 title="Inserting Multiple Rows - Sets">
  372.           <p>
  373.             Once common task is to work on more than one row. If the rows are
  374.             in different tables, this is catered for by table-sets. Operating
  375.             on multiple rows of one table requires to mark columns that should
  376.             vary and among those one, that determines the number of rows to
  377.             work on.
  378.           </p>
  379.           <p>
  380.             This is done with sets. All columns that cary a <code>set</code>
  381.             attribute can vary, those, that don't, are kept fixed during the
  382.             operation. The column that is used to determine the number of rows
  383.             is required to have a value of <code>master</code> while all others
  384.             need to have a value of <code>slave</code> for the set
  385.             attribute. There may be only one master in a set.
  386.           </p>
  387.           <p>
  388.             Sets can be tagged either on column or on mode level but not both
  389.             for a single column.
  390.           </p>
  391.         </s3>
  392.         <s3 title="Select Your Tables - Table-Sets">
  393.           <p>
  394.             Tables that should be used during an operation can be grouped
  395.             together with a table-set. A table-set references tables by their
  396.             name or their alias.
  397.           </p>
  398.           <p>
  399.             In addition, a table-set can override the mode type names for the
  400.             two categories "autoincrement" and "others".
  401.           </p>
  402.           <p>
  403.             Operations spanning multiple tables in a table-set are done in a
  404.             single transaction. Thus, if one fails, the other is rolled back.
  405.           </p>
  406.           <source>
  407. <![CDATA[
  408.  
  409.    <table name="groups">
  410.       <keys>
  411.          <key name="gid" type="int" autoincrement="true">
  412.             <mode name="auto" type="autoincr"/>
  413.          </key>
  414.       </keys>
  415.       <values>
  416.          <value name="gname" type="string"/>
  417.       </values>   
  418.    </table>
  419.    
  420.    <table-set name="user">
  421.       <table name="user"/>
  422.    </table-set>
  423.  
  424.    <table-set name="groups">
  425.       <table name="groups"/>
  426.    </table-set>
  427.  
  428.    <table-set name="user+groups">
  429.       <table name="user"/>
  430.       <table name="user_groups" others-mode="attrib"/>
  431.    </table-set>
  432.  
  433.    <table-set name="user_groups">
  434.       <table name="user_groups" others-mode="request"/>
  435.    </table-set>
  436.  
  437. </root>
  438. ]]>
  439.           </source>
  440.         </s3>
  441.       </s2>
  442.     </s1>
  443.   </body>
  444. </document>
  445.