Sunday, October 12, 2008
Using Oracle Sequence

Oracle database server provides support for sequences of unique values that are typically used to generate unique IDs for primary key columns or new records. Data Aquarium Framework and Aquarium Express are supporting automatic execution of static SQL statements just before a table record insertion takes place.

Consider the application created from HR demo schema that comes with Oracle database servers. The DEPARTMENTS table has a matching DEPARTMENTS_SEQ sequence that can be used to supply unique values for the primary key DEPARTMENT_ID.

Form createForm1 is displayed below. You can see that the primary key field is visible on the form and can be changed by user.

image

Let's modify the data controller descriptor ~/Controllers/HR_DEPARMENTS.xml to use the sequence for primary key column values.

The first step is a definition of command that executes just before a new record is inserted into database.

<command id="GetSequence" type="Text" event="Inserting">
  <text><![CDATA[select departments_seq.nextval from dual]]></text>
  <output>
    <fieldOutput fieldName="DEPARTMENT_ID"/>
  </output>
</command>

Attribute event tells the framework about the timing of execution, element text defines the actual SQL statement, and element fieldOutput directs the framework to copy the first column in the first row of command result set to the DEPARTMENT_ID field.

The second and last step of modification is to hide the DEPARMENT_ID field from user interface.

<view id="createForm1" type="Form" commandId="command1" 
  label="New Hr Departments">
  <headerText>Please fill this form and click OK button to create 
    a new hr departments record. Click Cancel to return to the 
    previous screen.</headerText>
  <categories>
    <category headerText="New Hr Departments">
      <description>Complete the form. Make sure to enter all 
        required fields.</description>
      <dataFields>
        <dataField fieldName="DEPARTMENT_ID" columns="15" hidden="true"/>
        <dataField fieldName="DEPARTMENT_NAME" columns="30" />
        <dataField fieldName="MANAGER_ID" aliasFieldName="MANAGER_LAST_NAME" />
        <dataField fieldName="LOCATION_ID" aliasFieldName="LOCATION_CITY" />
      </dataFields>
    </category>
  </categories>
</view>

Attribute hidden on the field DEPARMENT_ID does the job.

Here is how the form is changed.

image

The new department is displayed in the list below.

image