<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<document>

 <properties>
  <title>Schema Reference</title>
  <author email="warner@warneronstine.com">Warner Onstine</author>
  <author email="jvanzyl@apache.org">Jason van Zyl</author>
  <author email="ville.skytta@iki.fi">Ville Skyttä</author>
  <author email="tv@apache.org">Thomas Vandahl</author>
 </properties>

<body>
  <section name="Torque Schema Reference">
    <p>
      The Torque Database Schema Reference attempts to explain what the
      different elements and attributes are when defining your own database
      schema. In addition I will attempt to explain which attributes mean what
      in the different databases that are currently supported.
    </p>
   </section>

   <section name="Elements and their attributes">
     <p>
       <img src="images/schema.png"/>
     </p>
     <p>
       Some of the following examples are taken from the project-schema.xml
       document in the src/conf/torque/schema.
     </p>
   </section>

   <section name="Element: database">
       <p>
         The database element and its relevant attributes.
       </p>
       <source><![CDATA[
         <database
           name="MY_DATABASE"
           defaultIdMethod="idBroker"
           package="com.myapp.om"
           baseClass="com.myapp.om.BaseClass"
           basePeer="com.myapp.om.BasePeer">

           <table name="SIMPLE">
             <!-- table info goes here -->
           </table>
         </database>]]>
       </source>
       <p>
         The database element has 8 attributes associated with it, they are:
       </p>
       <p>
         <table>
           <tr>
             <td>name</td>
             <td>The name of the database being referenced</td>
           </tr>
           <tr>
             <td>defaultIdMethod</td>
             <td>How will the primary keys be created, defaults to "none"</td>
           </tr>
           <tr>
             <td>defaultJavaType</td>
             <td>Default type of columns in the database
                 (object or primitive, defaults to primitive)</td>
           </tr>
           <tr>
             <td>package</td>
             <td>Used for OM Peer generation</td>
           </tr>
           <tr>
             <td>baseClass</td>
             <td>Used for OM generation</td>
           </tr>
           <tr>
             <td>basePeer</td>
             <td>Used for OM Peer generation</td>
           </tr>
           <tr>
             <td>defaultJavaNamingMethod</td>
             <td>Indicates how a schema table or column name is mapped to a
                 Java class or method name respectively</td>
           </tr>
           <tr>
             <td>heavyIndexing</td>
             <td>Adds extra indices for multi-part primary key
                 columns.  true or false, defaults to false.

                 For databases like MySQL, values in a where clause
                 must match key part order from the left to right.
                 So, in the key definition <code>PRIMARY KEY (FOO_ID,
                 BAR_ID)</code>, <code>FOO_ID</code> <i>must</i> be
                 the first element used in the <code>where</code>
                 clause of the SQL query used against this table for
                 the primary key index to be used.  This feature could
                 cause problems under MySQL with heavily indexed
                 tables, by causing too many indices to be created,
                 overrunning MySQL's table limit.</td>
           </tr>
         </table>
       </p>

       <p>
         The database element can contain the following elements:
       </p>
       <p>
        <table>
          <tr>
            <td>external-schema</td>
            <td>0 or more</td>
          </tr>
          <tr>
            <td>domain</td>
            <td>0 or more</td>
          </tr>
          <tr>
            <td>table</td>
            <td>1 or more</td>
          </tr>
        </table>
       </p>

     <subsection name="Attribute: defaultIdMethod">
       <p>
         By defining this attribute at the database level it applies the
         defaultIdMethod to those tables which do not have an idMethod
         attribute defined. The attribute defaultIdMethod has 5 possible
         values, they are:
       </p>
       <p>
         <table>
           <tr>
             <td>idbroker</td>
             <td>This allows Torque to generate the IDs through its IDBroker
                 Service</td>
           </tr>
           <tr>
             <td>native</td>
             <td>Torque will determine how the database will auto-generate
                 IDs</td>
           </tr>
           <tr>
             <td>none</td>
             <td>Typically used if you do not want IDs generated</td>
           </tr>
         </table>
       </p>
     </subsection>
     <subsection name="Attribute: defaultJavaNamingMethod">
       <p>
         This attribute determines how table or column names, from the name
         attribute of the table or column element, are converted to a Java
         class or method name respectively when creating the OM Java objects.
         defaultJavaNamingMethod can contain 3 different values:
       </p>
       <p>
         <table>
           <tr>
             <td>nochange</td>
             <td>Indicates no change is performed.</td>
           </tr>
           <tr>
             <td>underscore</td>
             <td>Underscores are removed, First letter is capitalized, first
               letter after an underscore is capitalized, the rest of the
               letters are converted to lowercase.
             </td>
           </tr>
           <tr>
             <td>javaname</td>
             <td>Same as underscore, but no letters are converted to lowercase.
             </td>
           </tr>
         </table>
       </p>
     </subsection>
     <subsection name="Attribute: package">
       <p>
         The base package in which this database will generate the Object
         Models associated with it. This overrides the targetPackage property
         in the Torque build.properties file.
       </p>
     </subsection>
     <subsection name="Attribute: baseClass">
       <p>
         The base class to use when generating the Object Model.
         This class does not have to extend org.apache.torque.om.BaseObject.
       </p>
     </subsection>
     <subsection name="Attribute: basePeer">
       <p>
         The base peer to use when generating the Object Model Peers.
         Unlike baseClass, basePeer should extend BasePeer at some point in the
         chain, i.e. it needs to be the superclass.
       </p>
     </subsection>
   </section>

   <section name="Element: external-schema">
       <p>
         Includes another schema file.
       </p>
       <source><![CDATA[
         <external-schema
           filename="extext-schema.xml" />
       ]]>
       </source>
   </section>

   <section name="Element: domain">
       <p>
         Domains are used to define attributes for columns.
       </p>
       <source><![CDATA[
         <domain
           name="amount"
           type="NUMERIC"
           size="10"
           scale="2"
           default="0"
           description="amount domain" />
       ]]>
       </source>
   </section>

   <section name="Element: table">
       <p>
         The table element and its relevant attributes
       </p>
       <source><![CDATA[
         <table
           name="MY_TABLE"
           javaName="table"
           idMethod="idbroker"
           skipSql="false"
           baseClass="com.myapp.om.table.BaseClass"
           basePeer="com.myapp.om.table.BasePeer"
           javaNamingMethod="underscore"
           description="Table for Torque tests">

           <!-- column information here -->

         </table>]]>
       </source>
       <p>
         The table element has 13 attributes associated with it, they are:
       </p>
       <p>
         <table>
           <tr>
             <td>name</td>
             <td>The name of the table being referenced</td>
           </tr>
           <tr>
             <td>javaName</td>
             <td>How this table will be referenced in Java</td>
           </tr>
           <tr>
             <td>idMethod</td>
             <td>How will the primary keys be created, defaults to "null"</td>
           </tr>
           <tr>
             <td>skipSql</td>
             <td>Whether or not to skip SQL generation for this reference</td>
           </tr>
           <tr>
             <td>abstract</td>
             <td>Whether or not to generate the class as Abstract or not</td>
           </tr>
           <tr>
             <td>baseClass</td>
             <td>Used for OM Peer generation</td>
           </tr>
           <tr>
             <td>basePeer</td>
             <td>Used for OM Peer generation</td>
           </tr>
           <tr>
             <td>alias</td>
             <td>The table alias</td>
           </tr>
           <tr>
             <td>interface</td>
             <td>The interface attribute specifies an interface that should
                 be referenced in the <code>implements</code> section of the 
                 generated extension class. If this is a fully qualified 
                 class name (i. e. the string contains dots), the interface 
                 will simply be implemented by the extension object. If the 
                 interface is a simple class name (without dots), an empty 
                 interface file  will be generated in the extension object 
                 package.
                 When this attribute is used, all methods that normally
                 would return the extension object type will now return the
                 interface type. This feature allows to use Torque generated 
                 classes in the context of other applications or APIs.</td>
           </tr>
           <tr>
             <td>javaNamingMethod</td>
             <td>Specifies how the name attribute is converted to the Java
                 class name of the corresponding OM object.  This attribute
                 overrides the defaultJavaNamingMethod attribute of the
                 database element</td>
           </tr>
           <tr>
             <td>heavyIndexing</td>
             <td>? (true or false)</td>
           </tr>
           <tr>
             <td>description</td>
             <td>Used for doc generation</td>
           </tr>
         </table>
       </p>

       <p>
         The table element can contain the following elements:
       </p>
       <p>
         <table>
           <tr>
             <td>column</td>
             <td>1 or more</td>
           </tr>
           <tr>
             <td>foreign-key</td>
             <td>0 or more</td>
           </tr>
           <tr>
             <td>index</td>
             <td>0 or more</td>
           </tr>
           <tr>
             <td>unique</td>
             <td>0 or more</td>
           </tr>
           <tr>
             <td>id-method-parameter</td>
             <td>0 or more</td>
           </tr>
         </table>
       </p>
     <subsection name="Attribute: javaName">
       <p>
         This is the Java class name to use when generating the Table or
         column. If this is missing the Java name is generated in the
         following manner:
       </p>
       <p>
         Underscores are removed, first letter and first letter after each
         underscore is uppercased, all other letters are lowercased.
         So YOUR_TABLE_NAME would become YourTableName.
       </p>
     </subsection>
   </section>

   <section name="Element: column">
       <p>
         The column element and its relevant attributes
       </p>
       <source><![CDATA[
         <column
           name="MY_COLUMN"
           javaName="Column"
           primaryKey="true"
           required="true"
           size="4"
           type="VARCHAR"
           javaNamingMethod="underscore">

           <!-- inheritance info if necessary -->
         </column>]]>
       </source>
       <p>
         The column element has 13 attributes associated with it, they are:
       </p>
       <p>
         <table>
           <tr>
             <td>name</td>
             <td>The name of the column being referenced</td>
           </tr>
           <tr>
             <td>javaName</td>
             <td>How this column will be referred to in Java</td>
           </tr>
           <tr>
             <td>primaryKey</td>
             <td>Is this a primary key or not (true or false, defaults to
                 false)</td>
           </tr>
           <tr>
             <td>required</td>
             <td>Whether a value is required in this field (true or false,
                 defaults to false)</td>
           </tr>
           <tr>
             <td>type</td>
             <td>What type of column is it? Covered below, defaults to
                 VARCHAR</td>
           </tr>
           <tr>
             <td>javaType</td>
             <td>The type of the column in Java (object or primitive)</td>
           </tr>
           <tr>
             <td>size</td>
             <td>How many characters or digits can be stored?</td>
           </tr>
           <tr>
             <td>default</td>
             <td>Default value to insert into field if it is missing.</td>
           </tr>
           <tr>
             <td>autoIncrement</td>
             <td>Whether or not to auto-increment this field (true or false,
                 defaults to false)</td>
           </tr>
           <tr>
             <td>inheritance</td>
             <td>? (single or false, defaults to false)</td>
           </tr>
           <tr>
             <td>inputValidator</td>
             <td>?</td>
           </tr>
           <tr>
             <td>javaNamingMethod</td>
             <td>Specifies how the name attribute is converted to the Java
                 class name of the corresponding OM object.  This attribute
                 overrides the defaultJavaNamingMethod attribute of the
                 database element</td>
           </tr>
           <tr>
             <td>description</td>
             <td>Used for doc generation</td>
           </tr>
         </table>
       </p>
       <p>
         The column element can contain the following elements:
       </p>
       <p>
        <table>
          <tr>
            <td>inheritance</td>
            <td>0 or more</td>
          </tr>
        </table>
       </p>
    </section>

    <section name="Element: inheritance">
        <p>
          The inheritance element and its relevant attributes
        </p>
        <source><![CDATA[
          <inheritance key="key"
            class="classname"
            extends="mybase"/>]]>
        </source>
      <p>
        The inheritance element has 3 attributes associated with it, they are:
      </p>
      <p>
        <table>
          <tr>
            <td>key</td>
            <td>?</td>
          </tr>
          <tr>
            <td>class</td>
            <td>?</td>
          </tr>
          <tr>
            <td>extends</td>
            <td>?</td>
          </tr>
        </table>
      </p>
      <p>
        The inheritance element can not contain other elements.
      </p>
    </section>

    <section name="Element: foreign-key">
      <p>
        The foreign-key element and its relevant attributes
      </p>
      <source><![CDATA[
        <foreign-key foreignTable="MY_TABLE"
          name="MY_TABLE_FK"
          onUpdate="none"
          onDelete="none">
          <!-- reference info -->
        </foreign-key>]]>
      </source>
      <p>
        The foreign-key element has 4 attributes associated with it, they are:
      </p>
      <p>
        <table>
          <tr>
            <td>foreignTable</td>
            <td>The name of the table being referenced</td>
          </tr>
          <tr>
            <td>name</td>
            <td>The name of the foreign key</td>
          </tr>
          <tr>
            <td>onUpdate</td>
            <td>The action to take when the referenced value in foreignTable
                is updated</td>
          </tr>
          <tr>
            <td>onDelete</td>
            <td>The action to take when the referenced value in foreignTable
                is deleted</td>
          </tr>
        </table>
      </p>
      <p>
        The foreign-key element can contain the following elements:
      </p>
      <p>
        <table>
          <tr>
            <td>reference</td>
            <td>1 or more</td>
          </tr>
        </table>
      </p>
    </section>

    <section name="Element: reference">
      <p>
        The reference element and its relevant attributes
      </p>
      <source><![CDATA[
        <reference local="FK_TABLE_ID" foreign="PK_COLUMN_ID"/>]]>
      </source>
      <p>
        The reference element has 2 attributes associated with it, they are:
      </p>
      <p>
        <table>
          <tr>
            <td>local</td>
            <td>The local reference</td>
          </tr>
          <tr>
            <td>foreign</td>
            <td>The foreign key reference</td>
          </tr>
        </table>
      </p>
      <p>
        The reference element can not contain other elements.
      </p>
    </section>

    <section name="Element: index">
      <p>
        The index element and its relevant attributes
      </p>
      <source><![CDATA[
        <index name="MY_INDEX">
          <!-- index-column info -->
        </index>]]>
      </source>
      <p>
        The index element has 1 attribute associated with it, it is:
      </p>
      <p>
        <table>
          <tr>
            <td>name</td>
            <td>The name of the index</td>
          </tr>
        </table>
      </p>
      <p>
        The index element can contain the following elements:
      </p>
      <p>
        <table>
          <tr>
            <td>index-column</td>
            <td>1 or more</td>
          </tr>
        </table>
      </p>
    </section>

    <section name="Element: index-column">
      <p>
        The index-column element and its relevant attributes
      </p>
      <source><![CDATA[
        <index-column name="INDEX_COLUMN"/>]]>
      </source>
      <p>
        The index-column element has 2 attributes associated with it, they are:
      </p>
      <p>
        <table>
          <tr>
            <td>name</td>
            <td>The name of the indexed column</td>
          </tr>
          <tr>
            <td>size</td>
            <td>
              This attribute is not used and will be removed in a future
              version of the dtd.
            </td>
          </tr>
        </table>
      </p>
      <p>
        The index-column element can not contain other elements.
      </p>
    </section>

    <section name="Element: unique">
      <p>
        The unique element and its relevant attributes
      </p>
      <source><![CDATA[
        <unique name="MY_UNIQUE">
          <!-- unique-column info -->
        </unique>]]>
      </source>
      <p>
        The unique element has 1 attribute associated with it, it is:
      </p>
      <p>
        <table>
          <tr>
            <td>name</td>
            <td>The name of the unique constraint</td>
          </tr>
        </table>
      </p>
      <p>
        The unique element can contain the following elements:
      </p>
      <p>
        <table>
          <tr>
            <td>unique-column</td>
            <td>1 or more</td>
          </tr>
        </table>
      </p>
    </section>

    <section name="Element: unique-column">
      <p>
        The unique-column element and its relevant attributes
      </p>
      <source><![CDATA[
        <unique-column name="UNIQUE_COLUMN"/>]]>
      </source>
      <p>
        The unique-column element has 1 attribute associated with it, it is:
      </p>
      <p>
        <table>
          <tr>
            <td>name</td>
            <td>The name of the unique column</td>
          </tr>
        </table>
      </p>
      <p>
        The unique-column element can not contain other elements.
      </p>
  </section>
</body>
</document>
