Web Object Language Files - DataBase Layer

The Database Abstraction Layer is the servers lower bound towards the database. It is a simple translation of the database structure into usable PHP objects. The woc compiler generates one class per table.

One of the first statements is the manifest for the database schema:

<DataBase instance="dbInst" schema="dbSchema" version="00.01" defaultUpdating="no"/>
The attribute "instance" tells woc what variable name it should use for the database connection instance, likewise "schema" tells woc which variable represents the database schema (usually an instance of WobSchema, which is generated by woc). The "version" attribute tells woc which database schema version is described in this wolf file. The "defaultUpdating" attribute tells woc whether transactions are normally assumed to write to the database.

Tables

For each database table woc needs a description of that table:
<Table name="ticket" backup="yes" base="BarcodeTable">
  <Column name="ticketid" type="string:32" primarykey="yes"/>
  <Column name="eventid" type="int32" foreignkey="event:eventid"/>
  <Column name="price" type="int32" notnull="yes"/>
  <Column name="status" type="enum32" notnull="yes">
    <Value name="Reserved" value="0x301"/> <!--dec: 769-->
    <Value name="Cancelled" value="0x4"/> <!--dec: 4-->
    <Value name="MaskUsable" value="0x300"/> <!--dec: 768-->
  </Column>
  <Column name="orderid" type="int32" foreignkey="order:orderid" notnull="yes"/>
  <AuditColumn name="auditrabbit" type="string:32" null="yes">
    <Call lang="php" method="MyRabbit::getRandomRabbitString()"/>
  </AuditColumn>
  <Preset>
    <V col="ticketid" val="1"/>
    <V col="eventid" val="10"/>
    <V col="price" code="rand()"/>
    <V col="status" val="0x4"/>
    <V col="orderid" val="1"/>
  </Preset>
  <Foreign method="getRabbit" via="rabbit:rabbitid=eventid"/>
  <Unique>price,status</Unique>
</Table>

The XML Tags are:
TagDescription
TableEncompasses the entire table description.
ColumnDescribes a single column.
  Column/ValueFor enum columns: describes the values that the column can take.
AuditColumnDescribes a column that exists in the audit-sub-table only.
ForeignDefines a method that returns rows from a sub-ordinate table via the foreign key relation given.
PresetDefines a row that will be inserted into the table at creation time.
  Preset/VDefines a column value for a preset row.
UniqueDefines a Unique constraint that spans several columns.

Table attributes:
AttributeDescription
namethe name of the table, the generated class will be Wttablename
backupbool, contains whether the table is in the backup routine (default: false)
baseoptional, contains the class that this tables class is derived from, the base class must be derived from WobTable (default is to derive from WobTable directly)
auditoptional, contains boolean value whether this table has an audit shadow table, default is false

The Table tag can have Doc subtags to embed documentation.

Column and AuditColumn attributes:
AttributeDescription
namethe name of the column in the database
typethe type of the column, this must be a woc type which is then translated into SQL by woc
notnull, nullbool, marks the column to (not) allow NULL values, only one of the two attributes is allowed, currently the default is to allow NULL, but this may change in the future
foreignkeydeclares a reference to a foreign key column, the syntax is "foreigntablename:column" - the table that is referenced must be declared before the current one
uniquebool, declares the column UNIQUE
primarykeybool, tells woc that this column is part of the primary key (multiple columns may be part of the primary key)
indexbool, tells woc that this column should have an index to speed up read operations, the database driver should select an appropriate one
defaultdeclares a default value for the column (currently defaults for enums must be given as integer)

Column Types:
AttributeDescription
string:int, stringa string column (SQL: VARCHAR), if the :int is given the column will have that maximum length assigned, if it is not given the database maximum is used; most databases support VARCHARs up to 255 characters
int32, int64a 32-bit/64-bit integer type
enum, enum32, enum64a 32bit/64bit (enum=enum32) integer interpreted as enumeration, the column must contain "Value" tags to declare the valid values
boola boolean column (the SQL representation may vary per database system)
seq32, seq64a 32bit/64bit integer that counts up automatically (ie. that has a sequence attached to it) - this is usually only used for primary keys, some database types require that only one such column exists and that it is the primary key or part of it
texta large text field
blobbinary large object

All names in table descriptions must follow a very strict syntax in order to be compatible with as many database systems as possible. Woc allows names that start with letters and contain only letters, underscores and digits.

The Value tags for enum types function similar to enums in C++. If a value attribute is given that value is assigned to the enum symbol, if no value is given the previous one is increased by one (the first value is 0).

Columns can be documented by adding the description directly to the Column tag or by embedding it with a Doc tag inside the Column tag. Enum values can be documented by embedding the description between <Value> and </Value>.

AuditColumns define columns that are only present in the corresponding audit shadow table. See below for details.

Call defines a kind of default - whenever a new row is created the column is preset with the result of the call.

Call attributes:
AttributeDescription
langthe language in which the call is formulated (eg. "php" or "php/server")
methodthe expression that is assigned to the column

Presets are rows created when the database is populated. Each row has a "Preset" elements, each column has a "V" element. You can chose between giving the value directly with the "val" attribute or as a call with the "code" attribute - the code must not assume that any tables exist yet or that presets are created in a specific order.

Foreign defines getters that are not automatic (foreign keys in the same table are automatic, foreign keys in a different table are not) - the "method" attribute defines tha name of the method for that getter, while the "via" attribute defines the target table, and the comparison to make. The format of "via" is always "targetTable:targetColumn=hereColumn".

Auditing Shadow Tables

The PACK toolkit allows for automatic auditing of tables. A table that is marked for auditing will automatically generate and keep copies of all data that is written to the table. On each insert, delete and update a copy of the new data is inserted into a new row of the auditing shadow table. The shadow table will have the same name as the base table plus the suffix _audit and it will also have a corresponding table class in the generated bindings, so that it can be queried.

Usually you will want to define additional columns that describe the auditing copies. These are defined inside the main DataBase tag of the WOLF file:

<DataBase instance="dbInst" schema="dbSchema" version="00.01" defaultUpdating="no"/>
  <AuditTables>
    <Column name="audittime" type="int64">Time at which the change was made.
      <Call lang="php" method="time()"/>
    </Column>
  </AuditTables>
</DataBase>
The Column tags use the same syntax as those for normal tables except that for each server target there must be a corresponding Call tag that tells the Woc how to generate this data.

It is then enough to add the audit="yes" tag to each table that will have an audit shadow table:

<Table name="ticket" backup="yes" base="BarcodeTable" audit="yes">
...

Previous: Overall Format
Next: Communication Layer