Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Orientation in Query Languages By: Toan Nguyen Class: CS 157A.

Similar presentations


Presentation on theme: "Object-Orientation in Query Languages By: Toan Nguyen Class: CS 157A."— Presentation transcript:

1 Object-Orientation in Query Languages By: Toan Nguyen Class: CS 157A

2 OQL OQL, the Object Query Language, gives us an SQL like notation for expressing queries. OQL, the Object Query Language, gives us an SQL like notation for expressing queries. It is intended that OQL will be used as an extension to some object-oriented host languages, such as C++, Smalltalk, or Java. It is intended that OQL will be used as an extension to some object-oriented host languages, such as C++, Smalltalk, or Java.

3 Path Expressions We can access component of object and structures using a dot notation that is similar to the dot used in C and also related to the dot used in SQL. The general rule is as follows. We can access component of object and structures using a dot notation that is similar to the dot used in C and also related to the dot used in SQL. The general rule is as follows. 1. If p is an attribute, then a.p is the value of that attribute in object a. 2. If p is a relationship, then a.p is the object or collection of object related to a by relationship b. 3. If p is a method, then a.p(…) is the result of applying p to a.

4 Select-From-Where Expressions in OQL OQL permits us to write expressions using a select-from-where syntax similar to SQL’s OQL permits us to write expressions using a select-from-where syntax similar to SQL’s 1) The keyword SELECT followed by a list of expressions. 2) The keyword FROM followed by a list of one or more variable declarations. 3) The keyword WHERE and Boolean-value expressions

5 Modifying the Result To make the result a set, use the keyword DISTINCT after SELECT, as in SQL. To make the result a set, use the keyword DISTINCT after SELECT, as in SQL. To make the result a list, add ORDER BY clause at the end of the query again as in SQL. To make the result a list, add ORDER BY clause at the end of the query again as in SQL.

6 Example SELECT m FROM Movies m WHERE m.ownedBy.name = “Disney” ORDER BY m.length, m.title This query finding the set of Disney movies, and let the result be a list of movies, order by length. If there are ties, let the movies of equal length be order alphabetically.

7 Complex Output Types The elements in the SELECT clause need not be simple variables. They can be any expression, including expressions built using type constructors. For example, we can apply the Struct type constructor to several expressions and get a select-from- where query that produces a set or bag of structures. The elements in the SELECT clause need not be simple variables. They can be any expression, including expressions built using type constructors. For example, we can apply the Struct type constructor to several expressions and get a select-from- where query that produces a set or bag of structures.

8 Example SELECT DISTINCT Struct(start1: s1, start2: s2) FROM Star s1, Star s2 WHERE s1.address = s2.address AND s1.name<s2.name We want the set of pairs of stars living at the same address.

9 Quantifier Expressions Test whether all members of a collection satisfy some condition, and we can test whether at least one member of a collection satisfies a condition. Test whether all members of a collection satisfy some condition, and we can test whether at least one member of a collection satisfies a condition. FOR ALL x IN S: C (x) FOR ALL x IN S: C (x) The result of this expression is TRUE if every x in S satisfies C(x) EXISTS x IN S: C (x) EXISTS x IN S: C (x) The result of the expression is TRUE if there is at least one x in such that C(x) is TRUE and it has value FALSE otherwise. The result of the expression is TRUE if there is at least one x in such that C(x) is TRUE and it has value FALSE otherwise.

10 Aggregation Expressions OQL uses the same five aggregation operators that SQL does: AVG, COUNT, SUM, MIN, and MAX. OQL uses the same five aggregation operators that SQL does: AVG, COUNT, SUM, MIN, and MAX. Example: Example: AVG(SELECT m.length FROM Movies m) AVG(SELECT m.length FROM Movies m)

11 Union, Intersection, and Difference We may apply the union, intersection, and difference operators to two objects of set or bag type. These three operators are represented, as in SQL, by the keywords UNION, INTERSECT, and EXCEPT, respectively. EXCEPT, respectively. (SELECT DISTINCT m (SELECT DISTINCT m FROM Movies m, m.stars s FROM Movies m, m.stars s WHERE s.name = “Harrison Ford”) WHERE s.name = “Harrison Ford”) EXCEPT EXCEPT (SELECT DISTINCT m (SELECT DISTINCT m FROM Movies m FROM Movies m WHERE m.ownedBy.name = “Disney” WHERE m.ownedBy.name = “Disney”

12 Assigning Values to Host-Language Variables The expressions of OQL that we have learned, such as select-from-where, produce object as values. It is possible to assign to any host-language variable of the proper type The expressions of OQL that we have learned, such as select-from-where, produce object as values. It is possible to assign to any host-language variable of the proper type Example: Example: oldMovies = SELECT DISTINCT m oldMovies = SELECT DISTINCT m FROM Movies m FROM Movies m WHERE m.year<1920; WHERE m.year<1920; The following query produces the set of all those movies made before 1920. If oldMovies is a host-language variable of the same type, then the values of oldMovies will become the set of these Movie object.

13 Obtaining Each Member of a Collection To obtaining each member of a set or bag we need to turn our set or bag into a list. We do so with a select-from-where expressions that uses ORDER BY.

14 Example 1) movieList = SELECT m FROM Movies m FROM Movies m ORDER BY m.title, m.year ORDER BY m.title, m.year 2) numberOfMovies = COUNT(Movies); 3) for(i=0; i<numberOfMovies; i++){ 4) movie = movieList[i]; 5) count<<movie.title<<“ “<<movie.year<<“ “ 6) <<movie.length<<“\n”; Line 1 sort the movie class. Line 2 computes the number of movies, using the OQL operator COUNT. Line 3 and 4 use for loop in which integer variable i ranges over each position of the list. After that the i element of the list assigned to variable movie. Line 5 and 6 are for print.

15 Constants in OQL Basic values a) Atomic values: integers, floats, characters, a) Atomic values: integers, floats, characters, strings, and booleans. strings, and booleans. b) Enumerations: The values in an enumeration are b) Enumerations: The values in an enumeration are actually declared in ODL. Any one of these values actually declared in ODL. Any one of these values may be used as a constant. may be used as a constant. Complex values built using the following type constructors: a) Set (…). a) Set (…). b) Bag (…). b) Bag (…). c) List (…). c) List (…). d) Array (…). d) Array (…). e) Struct (…). e) Struct (…).

16 Defining Types in SQL A simple form of UDT definition is: A simple form of UDT definition is: 1. The keywords CREATE TYPE 2. The name for the type 3. The keyword AS 4. A parenthesized, comma-separated list of attributes and their types. 5. A comma-separated list of methods, including their argument type(s), and return type

17 Example CREATE TYPE AddressType AS { street CHAR(50), street CHAR(50), city CHAR(20) city CHAR(20)}; CREATE TYPE StarType AS { name CHAR (30), name CHAR (30), address AddressType address AddressType};

18 Generator and Mutator Functions In order to create data that conforms to a UDT, or to change components of object with UDT, we can use two kinds of methods. In order to create data that conforms to a UDT, or to change components of object with UDT, we can use two kinds of methods. 1. A generator method. 1. A generator method. This method has the name of the type and no argument. It also has the unusual property that it make be invoked without being applied to any object 2. 2. Mutator method. For each attribute x of UDT T, there is a mutator method x(v). When applied to an object of type T, it changes the x attribute of that object to have value v.

19 References A First Course In Database Systems A First Course In Database Systems Jefferey D. Ullman and Jennifer Widom Jefferey D. Ullman and Jennifer Widom


Download ppt "Object-Orientation in Query Languages By: Toan Nguyen Class: CS 157A."

Similar presentations


Ads by Google