OODB_III Lecture 12 - Computing at Northumbria
Download
Report
Transcript OODB_III Lecture 12 - Computing at Northumbria
The ODMG Standard for Object Databases
Object Query Language (OQL)
Advanced Database Technologies
School of Informatics
Dr. Akhtar Ali
Advanced Database Technologies (CG096) – Lecture # 12: 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 Database Technologies (CG096) – Lecture # 12: 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 Database Technologies (CG096) – Lecture # 12: 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. ComMath 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:
ComMath.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 Database Technologies (CG096) – Lecture # 12: 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 Database Technologies (CG096) – Lecture # 12: 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 Database Technologies (CG096) – Lecture # 12: The ODMG Standard for Object Databases
6
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 Database Technologies (CG096) – Lecture # 12: The ODMG Standard for Object Databases
7
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 Database Technologies (CG096) – Lecture # 12: The ODMG Standard for Object Databases
O
D
L
C
+
+
8
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”;
// modifies name attribute of
Student S
S->address = “Durham”;
L->room = 103;
L->tutees.insert_element(&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 Database Technologies (CG096) – Lecture # 12: The ODMG Standard for Object Databases
9
C++ OQL
• Define variables for the query result
d_Bag<d_Ref<Lecturer>> ComMathStaff;
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 << “Computing and Maths”;
// Imposes a restriction on the query
• Execute the query
d_oql_execute(q, ComMathStaff);
// executes the query q and stores the result into ComMathsStaff variable
d_oql_execute(q1, ToonArmy);
// executes the query q1 and stores the result into ToonArmy variable
Advanced Database Technologies (CG096) – Lecture # 12: The ODMG Standard for Object Databases
10
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 Database Technologies (CG096) – Lecture # 12: The ODMG Standard for Object Databases
11