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:
the Address
class contains four attributes, one integer and three strings
integer
: there are three types of ODL integers: 16-bits integer, named int16
or short
, 32-bits integer, named int32
or int
and 64-bits integer, named int64
or long
, so the num
attribute is a 32-bits integer
string
: an ODL string is under the form: string
or string<N>
. The first form means that the string is not bounded, the second form means that the string contains at most N characters.
the Person
class contains six attributes: two strings, one 32-bits integer, one Person
object and one set of Person
objects:
the third attribute addr
is of Address
type and is a literal because there is no *
before the attribute name. A literal is an object without identifier: the addr
attribute is tied to a Person
instance, it has no proper existence.
the spouse
attribute is an object, not a literal, because it is preceded by a *
. An object has an identifier and has its proper existence. The *
means a reference or pointer to an object. The directive after the attribute name inverse Person::spouse
is a relationship directive.
the children
attribute is a collection set of Person
objects
the Employee
contains seven attributes: the six Person
attributes because Employee
inherits from Person
and 64-bits integer attribute: salary
.
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.