Defining a simple schema with ODL

Now that a database has been created, we are going to populate it with objects.

The first step is to define the database schema.

A standard example in databases is the well known Person class (or table in relational system) which contains a few attributes such as a firstname, a lastname, an age, an address, a spouse and a set of children.

We will show the inheritance feature through the simple class Employee which inherits from the Person class and will contains a simple attribute: salary.

Example 3.1, “The ODL schema” shows the ODL schema for the classes Address, Person and Employee:

Example 3.1. The ODL schema

//
// person.odl
//

class Address {
  int num;
  string street;
  string town;
  string country;
};

class Person {
  string firstname;
  string lastname;
  int age;
  Address addr;
  Person * spouse inverse Person::spouse;
  set<Person *> children;
};

class Employee extends Person {
  long salary;
};

	

A few comments about this schema:

To add the previous schema in the foo database, you need to use the eyedbodl tool as follows:

% eyedbodl -d foo -u person.odl
Updating 'person' schema in database foo...
Adding class Address
Adding class Person
Adding class Employee

Done
	  

Note that you must pass the following command line options to the eyedbodl command: -d foo to specify to which database you are applying the schema and -u to update the database schema.

To verify that the update has correctly worked, you can generate the ODL schema from the database, as in Example 3.2, “Generating the ODL schema from a database”.

Example 3.2. Generating the ODL schema from a database

% eyedbodl -d foo --gencode=ODL
//
// EyeDB Version 2.8.4 Copyright (c) 1995-2007 SYSRA
//
// UNTITLED Schema
//
// Generated by eyedbodl at Mon Dec 10 13:44:03 CET 2007
//

class Address (implementation <hash, hints = "key_count = 2048;">) {
        attribute int32 num;
        attribute string street;
        attribute string town;
        attribute string country;
};

class Person (implementation <hash, hints = "key_count = 2048;">) {
        attribute string firstname;
        attribute string lastname;
        attribute int32 age;
        attribute Address addr;
        relationship Person* spouse inverse Person::spouse;
        attribute set<Person*> children;
};

class Employee (implementation <hash, hints = "key_count = 2048;">) extends Person {
        attribute int64 salary;
};

	

Note that the exact output may differ a bit from what is displayed above, depending on EyeDB's version.

By default, eyedbodl generates the ODL on the standard output. You see here that the displayed ODL is very similar to the original ODL except that the keywords attribute and relationship have been added before each attribute declaration. The relationship keyword means that the attribute has an inverse directive.

Note that these two keywords are optional: it is why we have not used them in our example.

Another way to check that the schema has been created within the database, is to use the eyedboql tool, as in Example 3.3, “Checking the database schema using eyedboql

Example 3.3. Checking the database schema using eyedboql

% eyedboql -d foo -c "select schema" --print

= bag(2546.2.120579:oid, 2553.2.112046:oid, 2568.2.515951:oid)
struct Address {2546.2.120579:oid} : struct : agregat : instance : object { 
        attribute int32 num;
        attribute string street;
        attribute string town;
        attribute string country;
};
struct Person {2553.2.112046:oid} : struct : agregat : instance : object { 
        attribute string firstname;
        attribute string lastname;
        attribute int32 age;
        attribute Address addr;
        relationship Person* spouse inverse Person::spouse;
        attribute set<Person*> children;
};
struct Employee {2568.2.515951:oid} : Person : struct : agregat : instance : object { 
        attribute string Person::firstname;
        attribute string Person::lastname;
        attribute int32 Person::age;
        attribute Address Person::addr;
        relationship Person* Person::spouse inverse Person::spouse;
        attribute set<Person*> Person::children;
        attribute int64 salary;
};

	

Again, note that the exact output may differ a bit from what is displayed above, depending on the EyeDB version.

Note that the object identifiers (oid) of the classes are displayed.