CS6362 1998 - Northumbria University
Download
Report
Transcript CS6362 1998 - Northumbria University
The ODMG Standard for Object Databases
Object Query Language (OQL)
and Language Binding
Advanced Database Technologies
School of Computing, Engineering and
Information Sciences
Dr. Akhtar Ali
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
1
OQL – for querying the database
• OQL is the the query language in ODMG standard.
• OQL is a superset of the SQL (in select-from-where
clauses).
• It can be interactive as well as embedded in other
programming languages.
• It is a declarative language.
• OQL can invoke operations written in programming
languages (C++, Java, Smalltalk).
• The operators in the language can be freely
composed.
• OQL includes operators for creating collections,
arithmetic and logical operators, aggregation, sorting
and grouping.
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
2
Retrieving Objects
select l
from l in Lecturers
where l.address = “Newcastle”
or
select l
from Lecturers as l
where l.address = “Newcastle”
•
The query returns all Lecturer objects
who live in Newcastle. The type of
the query result is bag<Person>.
•
Whenever the scope of an OQL query
is a collection (e.g. Lecturers, a set of
Lecturer objects), we define an
iterator variable that ranges over the
collection (e.g. l in Lecturer). In the
query l denotes an object of type
Lecturer.
or
select l
from Lecturers l
where l.address = “Newcastle”
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
3
Database Entry Points
• Named objects are entry points to the database (same
as table or view names in RDBs).
• Class extents are usually used to refer to in OQL
queries.
• Some frequently used objects can be assigned unique
names e.g. CEIS a named Department object or AdvDB
as named Unit object.
• To use named objects, you don’t have to write a query
in select-from-where form. You can simply write:
CEIS.staff
• which returns all staff member objects in the school of
computing and mathematics.
Students
• which returns a set of all Student objects in the database.
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
4
Retrieving data from multiple objects
• Without using Joins
select struct(LName:l.name, DName:l.worksFor.name)
from l in Lecturers
– The query contains an implicit join based on the relationship
that exists between Lecturer and Department object types
and due to the path expression l.worksFor.name. However,
the user does not need to tell the OQL compiler.
• Using Joins
select struct(LName:l.name, DName:d.name)
from l in Lecturers, d in Departments
where l.worksFor = d
– Here the user explicitly tells that a join should be performed
on the basis of matching OIDs (l.worksFor and d denote an
OID of a Department object).
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
5
Unnesting and Nesting Collections
• Unnesting a collection
select struct(LName:l.name, SName:s.name)
from l in Lecturers, s in l.tutees
– The query retrieves names of lecturers and their tutees in pairs.
– The query iterates over the collection Lecturers via the variable l; then
iterates over l.tutees (a set of Student objects) via the variable s; then
projects out name from l and name from s. The type of the query result is
bag<LName:string, SName:string>.
• Nesting a collection
select struct(LName:l.name,
SNames:(select s.name from s in l.tutees)
from l in Lecturers
– The query retrieves for every lecturer, his/her name and all his/her tutees
names.
– Here instead of unnesting l.tutees, the query iterates over l.tutees via s
and projects out name attribute and then constructs a collection out of it.
The type of the query is
bag<LName:string, SNames:bag<string>>.
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
6
Aggregate Functions
• A query to list module name, the number of lecturers teaching
on the module and the number of students taking the module:
select struct(Uname:u.name,
Tno: count(u.taughtBy),
Sno: count(u.takenBy))
from u in Units
– The query calls the count() aggregate function over the collections
taughtBy and takenBy to return the required numbers.
– There is no need to do group by as the collections are integral parts of
module objects.
– On a similar relational database, you would require to join several tables
and use group by to answer a similar question in SQL, which may not be
that efficient and easily expressed as the above OQL query.
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
7
Language Binding
• Extends an OOPL (C++, Java, Smalltalk) with ODL
and OML (Object Manipulation Language) for building
OODB applications.
• ODL binding defines how ODL constructs are mapped
onto an OOPL.
• OML binding defines how objects are created, deleted,
and modified in an OOPL.
• An OOPL may embed OQL where results returned by
OQL queries can be manipulated by OOPL.
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
8
C++ ODL
class Department (extent Departments)
{ attribute string name;
relationship set<Lecturer> staff inverse Lecturer::worksFor;
relationship set<Course)> offers inverse Course::offeredBy;
};
const char _worksFor[ ] = “worksFor”;
const char _offeredBy[ ] = “offeredBy”;
for inverse relationships
class Department : d_Object
{ public:
d_String name;
d_Rel_Set <Lecturer, _worksFor> staff;
d_Rel_Set <Course, _offeredBy> offers;
};
d_Extent<Department> Departments(DB1); // for extent
// DB1 is the database name
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
O
D
L
C
+
+
9
C++ OML
• Object Creation
d_Ref<Student> S = new(DB1, “Student”) Student;
// creates a new Student object in DB1 database
d_Ref<Lecturer> L = new(DB1, “Lecturer”) Lecturer;
// creates a new Lecturer object in DB1 database
• Object Modification
S->name = “Richard Spring”;
S->address = “Durham”;
L->room = 103;
L->tutees.insert_element(&S);
// modifies name attribute of Student S
// similarly
// modifies room attribute of Lecturer L
// establishes a relationship
// between Lecturer L and Student S
• Object Deletion
L.delete_object();
// deletes the Lecturer object from the database
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
10
C++ OQL
• Define variables for the query result
d_Bag<d_Ref<Lecturer>> CEIS_Staff;
d_Bag<d_Ref<Student>> ToonArmy;
• Create an instance of type d_OQL_Query
d_OQL_Query q (“select l from l in Lecturers where l.worksFor.name = $1);
d_OQL_Query q1 (“select s from s in Students where s.address = “Newcastle”);
• Modify the query with Parameters
q << “CEIS”;
// Imposes a restriction on the query
• Execute the query
d_oql_execute(q, CEIS_Staff);
// executes the query q and stores the result into CEIS_Staff variable
d_oql_execute(q1, ToonArmy);
// executes the query q1 and stores the result into ToonArmy variable
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
11
Summary
• An overview of ODMG standard
• An introduction to the Object Data Model
• A little bit of ODL
• Introduction to OQL
• Introduction to language binding
Advanced Databases (CM036) – Lecture # 11: The ODMG Standard for Object Databases
12