Create data using OQL
We show in this section how to create the objects and relationships between these objects as defined in the ODL schema using the Object Query Language (OQL). The EyeDB OQL is a superset of the standard ODMG 3 OQL.
To enter the following OQL statement, we can use the eyedboql
interactive tool included in the distribution.
Creating a few courses
We begin by creating a few courses:
oodbms := new Course(title : "OODBMS", description : "Object database management systems"); rdbms := new Course(title : "RDBMS", description : "Relational database management systems"); uml := new Course(title : "UML", description : "Unified Modeling Language"); cplus := new Course(title : "C++", description : "C++ Language"); java := new Course(title : "Java", description : "Java Language"); php := new Course(title : "PHP", description : "PHP Language");
A few comments:
- The
new
OQL operator allows us to create a persistent object of the given type - To construct an object, we give a list of pairs of attribute name and attributes value as shown previously, for instance:
title: "Java", description : "Java Language"
. If an attribute value is not given during construction, its value is assigned to theNULL
value (not initialised) - The OQL variables
oodbms, rdbms
and so on are not persistent: they are assigned to persistent objects, but there scope is limitated to the current OQL session
Creating a few students
We can now create a few students:
john_harris := new Student(firstname : "John", lastname : "Harris", begin_year : 2002); suzan_mulder := new Student(firstname : "Suzan", lastname : "Mulder", begin_year : 2002); francois_martin := new Student(firstname : "Francois", lastname : "Martin", begin_year : 2001);
Note that as a Student
inherits from a Person
, it includes the firstname
and lastname
attributes.
Creating two teachers
We create now two teachers:
eric_viara := new Teacher(firstname : "Eric", lastname : "Viara"); francois_dechelle := new Teacher(firstname : "Francois", lastname : "Dechelle");
Assigning the courses to the teachers
We deal now with the relationship “in charge” between Course
and Teacher
:
oodbms.teacher := eric_viara; rdbms.teacher := eric_viara; uml.teacher := francois_dechelle; cplus.teacher := eric_viara; java.teacher := francois_dechelle; php.teacher := francois_dechelle;
Important notices:
- When we assigned a teacher to a given course (for instance
oodbms.teacher := eric_viara
), the inverse attributecourses
in theTeacher
class is automatically updated (the course is added to thecourses
collections of the teachereric_viara
) because of the inverse directive - When we change the teacher of a given course (for instance
oodbms.teacher := francois_dechelle
), the inverse attributecourses
oferic_viara
is updated (the course is suppressed from itscourses
collection) and the inverse attributecourses
offrancois_dechelle
is also updated (the course is added to thecourses
collections of the teacherfrancois_dechelle
)
Assigning the courses to the students
We deal now with the relationship “registered to” between Student
and Course
:
add oodbms to john_harris.courses; add rdbms to john_harris.courses; add oodbms to suzan_mulder.courses; add uml to suzan_mulder.courses; add java to suzan_mulder.courses; add oodbms to francois_martin.courses; add rdbms to francois_martin.courses; add uml to francois_martin.courses; add java to francois_martin.courses; add cplus to francois_martin.courses; add php to francois_martin.courses;
Important notices:
- When we add a course to a given student (for instance
add oodbms to suzan_mulder.courses
), the inverse attributestudents
in theCourse
class is automatically updated (the student is added to thestudents
collections of the courseoodbms
) because of the inverse directive - When we suppress a course of a given student (for instance
suppress oodbms from suzan_mulder.courses
), the inverse attributestudents
ofoodbms
is updated (the student is suppressed from itsstudents
collection)