Summary

The use of metadata is critical to the premise of Pbooks. This page will attempt to describe why this is so, and how to develop within these confines.

Why?

Because metadata allows PBooks to be more flexible, while maintaining a standard and efficient data model. PBooks must be flexible because accounting and bookkeeping is done differently from one company to the next, yet the core of this exercise is the same.

When?

Strictly speaking, use metadata whenever you are dealing with anything beyond the basic accounting equation: credits, debits, accounts, the journal, and the ledger.

How?

Metadata is achieved through the use of XML files which are stored in . These files can be sources from the sitemap, and then used to provide structure and context for simple key-value pairs.

In the database, several tables have been setup for storing and retrieving metadata. These can be identified by a "_meta" suffix on the table name. They are pretty much the same structure, setup for key-value pairs.

Here is an example metadata definition file which is used by PBooks for handling invoices:

<entry_meta>
    <meta>
        <meta_key>client_id</meta_key>
        <meta_type>integer</meta_type>
    </meta>
    <meta>
        <meta_key>invoice_number</meta_key>
        <meta_type>integer</meta_type>
    </meta>
    <meta>
        <meta_key>due_date</meta_key>
        <meta_type>text</meta_type>
    </meta>
    <meta>
        <meta_key>paid</meta_key>
        <meta_type>boolean</meta_type>
    </meta>
</entry_meta>

Entry Metadata

Here's a good example of how metadata is created for an entry:

<query name="entry_create_meta" loop="//entry_meta/meta" default="1">
    <connection>pbooks</connection>
    <params>
        <param name="_post/entry_id" type="text"/>
        <param array="//entry_meta/meta/meta_key" type="text"/>
        <!-- This xpath deserves an explanation. An XML doc supplies the key 
        to locate the corresponding value! See ../xml/invoice_meta.xml -->
        <param array="//*[name()=//entry_meta/meta/meta_key]/." type="text"/>
    </params>
    <sql>
        INSERT INTO &pb_entry_metadata;
        (entry_id,meta_key,meta_value) VALUES (?,?,?)
    </sql>
</query>

The xpath is actually pretty cool, it uses the XML document as a wrapper of sorts to build the SQL query dyamically, in a simple and standard manner. I'm sure its pretty slow, but man is it flexible! :-)

Business Process Models

SQL Optimization

I've recently (as of Feb 2008) started to migrate the cross referencing of metadata from XSL to SQL as the business models solidify. So far so good. I like how flexible the models can be, but the XML trees which are created for output are actually very simple and straightforward. Its good stuff. The SQL can be complex, but that's OK, as its such a straightforward language, its always possible to dissect it.

Caveats

  • Strange, but true. When submitting a form that has metadata in it, you must preserve the order of the data elements! (Apr 2009 - is this still true?)
  • '''The meta_type is not currently used but may be used in the future. '''
  • http://www.pbooks.org/trac/ticket/84