1 advanced databases (cm036) – lecture # 12: the odmg standard for object databases object query...

11
1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies School of Informatics Dr. Akhtar Ali The ODMG Standard for Object Databases

Upload: matilda-hensley

Post on 13-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

1Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

Object Query Language (OQL) & Language Binding

Advanced Database Technologies

School of Informatics

Dr. Akhtar Ali

The ODMG Standard for Object Databases

Page 2: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

2Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

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.

Page 3: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

3Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

Retrieving Objectsselect l

from l in Lecturers

where l.address = “Newcastle”

or

select l

from Lecturers as l

where l.address = “Newcastle”

or

select l

from Lecturers 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.

Page 4: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

4Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

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.

Page 5: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

5Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

Retrieving data from multiple objects

• Without using Joinsselect 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 Joinsselect 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).

Page 6: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

6Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

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 collectionselect 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>>.

Page 7: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

7Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

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.

Page 8: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

8Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

C++ ODLclass Department (extent Departments){ attribute string name; relationship set<Lecturer> staff inverse Lecturer::worksFor; relationship set<Course)> offers inverse Course::offeredBy; };

ODL

const char _worksFor[ ] = “worksFor”;

const char _offeredBy[ ] = “offeredBy”;

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

C++

for inverse relationships

Page 9: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

9Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

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 ModificationS->name = “Richard Spring”; // modifies name attribute of

Student S

S->address = “Durham”; // similarly

L->room = 103; // modifies room attribute of Lecturer L

L->tutees.insert_element(&S); // establishes a relationship

// between Lecturer L and Student S

• Object DeletionL.delete_object(); // deletes the Lecturer object from the database

Page 10: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

10Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

C++ OQL

• Define variables for the query resultd_Bag<d_Ref<Lecturer>> ComMathStaff;

d_Bag<d_Ref<Student>> ToonArmy;

• Create an instance of type d_OQL_Queryd_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 Parametersq << “Computing and Maths”; // Imposes a restriction on the query

• Execute the queryd_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

Page 11: 1 Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases Object Query Language (OQL) & Language Binding Advanced Database Technologies

11Advanced Databases (CM036) – Lecture # 12: The ODMG Standard for Object Databases

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