Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Databases (chapter 3/3) ODMG-OQL for querying the database Simple OQL Queries, Retrieving Objects – an example Database Entry Points Retrieving.

Similar presentations


Presentation on theme: "Object-Oriented Databases (chapter 3/3) ODMG-OQL for querying the database Simple OQL Queries, Retrieving Objects – an example Database Entry Points Retrieving."— Presentation transcript:

1 Object-Oriented Databases (chapter 3/3) ODMG-OQL for querying the database Simple OQL Queries, Retrieving Objects – an example Database Entry Points Retrieving data from multiple objects Unnesting and Nesting Collections Objects network sample - OQL vs. SQL ODMG- Binding languages ODMG Types C++ ODL, OMT, OQL ODMG C++ Object Manipulation Language, and functions Persistent Java Systems Exercise Reconsidering the University schema… Object vs. Relational Object Relational Mapping Object Relational Model Object Relational Framework Seminar - ODB Design (Self-Study) Course outlines

2 2 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. ODMG - OQL for querying the database

3 3 Simple OQL Queries Basic syntax: select…from…where… SELECT d.name FROM d in departments WHEREd.college = ‘Engineering’;  An entry point to the database is needed for each query  An extent name (e.g., departments in the above example) may serve as an entry point Iterator Variables  Iterator variables are defined whenever a collection is referenced in an OQL query  Iterator d in the above example serves as an iterator and ranges over each object in the collection  Syntactical options for specifying an iterator:  d in departments  departments d  departments as d ODMG - OQL for querying the database

4 4 Retrieving Objects – an example Select 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. 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. ODMG - OQL for querying the database

5 5 Database Entry Points  Named objects are entry points to the database (same as table or view names in RDBs). Like Departments and Lecturers in the previous examples  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 (collection) member objects in the school of computing and mathematics.  Students  which returns a set of all Student objects in the database. ODMG - OQL for querying the database

6 6 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). class Lecturer extends Person (extent Lecturers ) { attribute short room; relationship set tutees inverse Student :: tutor; relationship Department worksFor inverse Department :: staff; relationship set teaches inverse Unit :: taughtBy; boolean teachUnit( in Unit U); }; class Department (extent Departments ) { attribute string name; relationship set staff inverse Lecturer :: worksFor; relationship set offers inverse Course :: offeredBy; }; ODMG - OQL for querying the database

7 7 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. 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 >. The data type of a query result can be any type defined in the ODMG model ODMG - OQL for querying the database

8 8 Courses Objects network sample - OQL vs. SQL ODMG - OQL ? Relational DB Students s1 s2 … Departments d1 d2 … Units u1 u2 c1 c2 … Lecturers l1 l2 … u3 Navigation links Entry point Reconsider the university object database; following a sample objects-network Consider that (no need to apply a join): Select struct(l.name, s.name) From l in lecturers, u in l.teaches, s in u.takenBy Such a query requires a complex join in the case of relational database/tables; references are materialized via join operations Select struct(l.name, s.name) From Lecturers l, Units u, Students s, StudUnits su Where l.key = u.lecturer and u.key = su.unitKey … Also we can largely using object’s methods (operations) under the clause where, example: Select struct(l.name, s.name) From l in lecturers, u in l.teaches, s in u.takenBy Where l.age() >= s.age()+5 …

9 9 ODMG - 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. import com.poet.odmg.*;... Database db = new Database(); db.open("TrainsDB", Database.OPEN_READ_WRITE); Transaction txn = new Transaction(); txn.begin(); Train theTrain = new Train(...); db.bind(myObject, theTrain.getTno()); txn.commit(); Language bindings map database classes to programming language classes, and provide associated utility functionalities. A simple java code

10 10 ODMG Types  Template class d_Ref used to specify references (persistent pointers)  Template class d_Set used to define sets of objects. Methods include insert_element(e) and delete_element(e)  Other collection classes such as d_Bag (set with duplicates allowed), d_List and d_Varray (variable length array) also provided.  d_ version of many standard types provided, e.g. d_Long and d_string  Interpretation of these types is platform independent  Dynamically allocated data (e.g. for d_string) allocated in the database, not in main memory ODMG - Language Binding

11 11 C++ ODL class Department ( extent Departments) { attribute string name; relationship set staff inverse Lecturer :: worksFor; relationship set offers inverse Course :: offeredBy; }; ODL const char _worksFor[ ] = “worksFor”; const char _offeredBy[ ] = “offeredBy”; class Department : d_Object {public : d_String name; d_Rel_Set staff; d_Rel_Set offers; } ; d_Extent Departments(DB1); // for extent // DB1 is the database name C++ for inverse relationships ODMG - Language Binding

12 12 C++ OML Object Creation d_Ref S = new(DB1, “Student”) Student; // creates a new Student object in DB1 database d_Ref 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”; // 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 Deletion L.delete_object(); // deletes the Lecturer object from the database ODMG - Language Binding

13 13 C++ OQL Define variables for the query result d_Bag > ComMathStaff; d_Bag > 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 ODMG - Language Binding

14 14 ODMG C++ Object Manipulation Language Uses persistent versions of C++ operators such as new(db) d_Ref account = new(bank_db, “Account”) Account;  new allocates the object in the specified database, rather than in memory.  The second argument (“Account”) gives typename used in the database.  Dereference operator -> when applied on a d_Ref reference loads the referenced object in memory (if not already present) before continuing with usual C++ dereference.  Constructor for a class – a special method to initialize objects when they are created; called automatically on new call.  Class extents maintained automatically on object creation and deletion  Only for classes for which this feature has been specified  Automatic maintenance of class extents not supported in earlier versions of ODMG ODMG - Language Binding

15 15 ODMG C++OML: Database and Object Functions (1/2) Class d_Database provides methods to  open a database: open(databasename)  give names to objects: set_object_name(object, name)  look up objects by name: lookup_object(name)  rename objects: rename_object(oldname, newname)  close a database (close()); Class d_Object is inherited by all persistent classes.  provides methods to allocate and delete objects  method mark_modified() must be called before an object is updated. Is automatically called when object is created Example int create_account_owner(String name, String Address) { Database bank_db.obj; Database * bank_db= & bank_db.obj; bank_db =>open(“Bank-DB”); d_Transaction Trans; Trans.begin(); d_Ref account = new(bank_db) Account; d_Ref cust = new(bank_db) Customer; cust->name - name; cust->address = address; cust->accounts.insert_element(account);... Code to initialize other fields Trans.commit(); } ODMG - Language Binding

16 16 ODMG C++OML: Database and Object Functions (2/2) Class extents maintained automatically in the database.  To access a class extent: d_Extent customerExtent(bank_db); Class d_Extent provides method d_Iterator create_iterator() to create an iterator on the class extent  Also provides select( predicate pred) method to return iterator on objects that satisfy selection predicate pred.  Iterators help step through objects in a collection or class extent.  Collections (sets, lists etc.) also provide create_iterator() method. Example int print_customers() { Database bank_db_obj; Database * bank_db = &bank_db_obj; bank_db->open (“Bank-DB”); d_Transaction Trans; Trans.begin (); d_Extent all_customers(bank_db); d_Iterator > iter; iter = all_customers–>create_iterator(); d_Ref p; while{iter.next (p)) print_cust (p); // Function assumed to be defined elsewhere …Can also apply any referred p’s method Trans.commit(); } ODMG - Language Binding

17 17 ODMG C++ Binding: Other Features Declarative query language OQL, looks like SQL Form query as a string, and execute it to get a set of results (actually a bag, since duplicates may be present) d_Set > result; d_OQL_Query Q1("select a from Customer c, c.accounts a where c.name=‘Jones’ and a.find_balance() > 100"); d_oql_execute(Q1, result);  Provides error handling mechanism based on C++ exceptions, through class d_Error  Provides API for accessing the schema of a database. ODMG - Language Binding

18 18 Persistent Java Systems ODMG-3.0 defines extensions to Java for persistence Java does not support templates, so language extensions are required  Model for persistence: persistence by reachability  Matches Java’s garbage collection model  Garbage collection needed on the database also  Only one pointer type for transient and persistent pointers  Class is made persistence capable by running a post-processor on object code generated by the Java compiler  Contrast with pre-processor used in C++  Post-processor adds mark_modified() automatically  Defines collection types DSet, DBag, DList, etc.  Uses Java iterators, no need for new iterator class ODMG Java  Transaction must start accessing database from one of the root object (looked up by name) finds other objects by following pointers from the root objects  Objects referred to from a fetched object are allocated space in memory, but not necessarily fetched  Fetching can be done lazily  An object with space allocated but not yet fetched is called a hollow object  When a hollow object is accessed, its data is fetched from disk.

19 19 Exercise /Reconsider the University schema…

20 Object Relational Mapping Use Vertical mapping when: there is significant overlap between types changing types is common Use Horizontal mapping when: there is little overlap between types changing types is uncommon Use Filtered mapping for: simple or shallow hierarchies with little overlap between types Mapping ER-Diagram into Class Diagram The Object Relational Model Mapping Frameworks Guidelines

21 21 Object Relational Mapping  Most business database applications use relational databases  Need to map the objects in the application to tables in the database  Sometimes be a simple matter of mapping individual classes to separate database tables  However, if the class structure is more complex, then the mapping must be carefully considered to allow data to be represented and accessed as efficiently as possible

22 22 OR Mapping: Inheritance Horizontal mapping Vertical mapping Filtered mapping

23 23 OR Mapping: Many-to-Many

24 24 Mapping ER-Diagram into Class Diagram The ER Diagram The equivalent Class-Diagram Using Power Designer tool

25 25 The Object Relational Model  The object relational model is an extension of the relational model, with the following features:  a field may contain an object with attributes and operations.  complex objects can be stored in relational tables  the object relational model offers some of the advantages of both the relational and object data models  has the commercial advantage of being supported by some of the major RDBMS vendors  An object relational DBMS is sometimes referred to as a hybrid DBMS

26 26 OR Mapping Frameworks  Sun’s Enterprise JavaBeans (EJB), an advanced server-side Java component architecture, has its own persistence mechanism, Container Managed Persistence (CMP)  There are also open-source OR mapping frameworks which work in a similar way to JDO, including Hibernate, ObJectRelationalBridge (OJB) and Castor.  Commercial products such as Toplink make it easier to define mappings.  Some OR frameworks, including Hibernate and OJB, are compliant with the ODMG 3.0 standard for interfacing with a database.

27 Seminar OODB Design (Self-Study) Purpose To understand how an OODB is structured. How a class diagram in UML is mapped onto a generic OO representation. How a relational mapping of a UML class diagram can be compared with an object- oriented representation.

28 28 OODB Design Conceptual vs. Relational schemas Scenario  A UML class diagram of a Company database is given in Figure 1, which shows a conceptual model of the database.  Company database models the data for a company that has several departments, many employees work for departments on different projects.  The database also stores data on employee’s dependents.  A mapping of the conceptual model into relational model is given in Figure 2 and a sample relational database shown in Figure 3 (at the end of handout).  It is assumed that the students understand UML notations and are familiar with relational concepts. Company Relational DB Schema Rebuild, using Power-Designer tool, the following models Conceptual model for Company database OODB Design (Self-Study)

29 29 Learning outcomes Relational Schema  Relationships are implemented using foreign keys. See arrows (  ) from foreign keys to the primary keys.  Many-to-many relationships are implemented as a linking relation using primary keys of participating relations (see WORKS_ON relation).  The real-world concept that an instance of one entity is associated with one or many instances of another entity is not present in relational model. For example, the concept that a department has many employees working for it is not present in the model. The only thing that exist is that an employee has an extra attribute (DNO) that holds the primary key value of his/her department.  Multi-valued attributes are implemented as a separate relation (e.g., DEPT_LOCATIONS).  Composite attributes (e.g. Name) is implemented in terms of its components (e.g., Fname, Minit, Lname). Object Oriented Database Design- Fundamental Principles  All kinds of relationships are implemented directly using either single valued or multi-valued attributes in both participating entities (classes).  Multi-valued attributes are implemented as collection valued attributes (e.g., set, bag, list).  Composite attributes are implemented directly (i.e., struct).  Primary keys are not required but supported for their usefulness in query processing.  Value-based foreign keys are not present ODBs.  The data type of attributes can be primitive (e.g., short, float, string, etc) as well as constructed types and collections (e.g., Department, Employee, set, etc). OODB Design (Self-Study)

30 30  It is assumed that you understand what the UML notations stand for and how they are mapped onto relational model.  The UML conceptual model is provided to give you an idea of how the relational model came into existence in the first place.  Represent the relational schema in Object Oriented representation.  The most important thing to do is thing about the basic concepts of OO databases and try to represent relationships more directly in terms of types, classes, and entities rather than primary or foreign keys. Home work  Translate the given UML class diagram into ODL scripts  Write the scripts leading to map the given relational data (next slide) into the ODL classes  Construct the corresponding object network and validate this one by navigating through the inter-objects links Tasks for the Seminar OODB Design (Self-Study)

31 31 Sample Company Database (relational)


Download ppt "Object-Oriented Databases (chapter 3/3) ODMG-OQL for querying the database Simple OQL Queries, Retrieving Objects – an example Database Entry Points Retrieving."

Similar presentations


Ads by Google