Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

Similar presentations


Presentation on theme: "OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University."— Presentation transcript:

1 OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University

2 OOPSLA LAB. 2 Contents 4.1 Introduction 4.2 Principles 4.3 Query Input and Result 4.4 Object Identity 4.5 Path Expressions 4.6 Null Values 4.7 Method Invoking 4.8 Polymorphism 4.9 Operator Composition 4.10 Language Definition 4.11 Syntactical Abbreviations 4.12 OQL BNF

3 OOPSLA LAB. 3 4.2 Principles Principles and assumptions –rely on ODMG object model –very close to SQL92 –high-level primitives to deal with sets of objects –functional language –not computationally complete –can be invoked within programming language –no explicit update operators –declarative access to objects –formal semantics of OQL can easily be defined

4 OOPSLA LAB. 4 4.3 Query Input and Output(1) OQL as a stand-alone language –query denotable objects starting from their names OQL as an embedded language –query denotable objects which are supported by the native language through expressions yielding atoms, structures, collections, and literals Example Select distinct x.age from Person x where x.name = “Pat” Returns a literal of type set

5 OOPSLA LAB. 5 4.3 Query Input and Output(2) select distinct struct(a: x.age, s: x.sex) from Person x where x.name = “Pat” returns a literal of type set select distinct struct(name: x.name, hps: (select y from x.subordinates as y where y.salary > 100000)) from Employees x returns a literal of type set )> Chairman returns the Chairman object Chairman.subordinates returns the set of subordinates of the Chairman

6 OOPSLA LAB. 6 4.4 Object Identity(1) Denotable object has an OID Literal –identity = their value Create objects –a type name constructor is used Person(name: “Pat”, birthdate: “3/28/56”, salary: 100,000) struct(a:10, b: “Pat”) typedef set vectint interface stat { attributes attribute Short a; attribute Char c; }; typedef bag stats;

7 OOPSLA LAB. 7 4.4 Object Identity(2) Vectint(select distinct age from Persons where name = “Pat”) returns an object of type vectint stats(select stat (a: age, s: sex) from Persons where name = “Pat”) returns an object of type stats Selecting existing objects –the extraction expressions may return : a collection of objects with identity an object with identity a collection of literals, and literal

8 OOPSLA LAB. 8 4.5 Path Expressions(1) 1-1 relationship –find the name of the city where the person p’s spouse lives p.spouse.address.city.name n-p relationships –find the names of the children of the person p select c.name from p.children c

9 OOPSLA LAB. 9 4.5 Path Expressions(2) Predicate select c.address from Person P, p.children c where p.address.street = “Main Street” and count(p.children) >= 2 and c.address.city != p.address.city Join select p from Person p, Flowers f where p.name = f.name

10 OOPSLA LAB. 10 4.6 Null Values The rules for managing UNDEFINED are: –accessing operations applied to an UNDEFINED operand produce UNDEFINED –comparison operations with either or both operands being UNDEFINED produce False –is_undefined(UNDEFINED) returns True: is_defined(UNDEFINED) returns False –Any other operation with any UNDEFINED operands results in a run- time error

11 OOPSLA LAB. 11 4.7 Method Invoking A method with parameters select max(select c.age from p.children c) from Persons p where p.name = “Paul” A method without parameters select p.oldest_child.address.street from Persons p where p.lives_in(“Paris”)

12 OOPSLA LAB. 12 4.8 Polymorphism For instance, the set Persons contains objects of classes Person, Employee, and Student Late binding –example select p.activities from Persons p Class indicator –example select ((Student)p).grade from Person p where “course of study” in p.activities

13 OOPSLA LAB. 13 4.9 Operator Composition(1) All operators can be composed freely as long as the type system is respected. Example –find the name of the street where employees live and have the smallest salary on average, compared to employees living in other streets 1. define Employees() as select (Employee) p from Persons p where “has a job” in p.activities

14 OOPSLA LAB. 14 4.9 Operator Composition(2) 2. define salary_map() as select street, average_salary:avg(select x.e.salary from partition x) from Employees() e group by street: e.address.street 3. define sorted_salary_map() as select s from salary_map() s order by s.average_salary 4. first(sorted_salary_map()).street

15 OOPSLA LAB. 15 4.10 Language Definition(1) OQL is an expression language OQL is a typed language Notation –q : query name, –e : expression, –p : property name –x : variable, –t : type name, –f : operation name

16 OOPSLA LAB. 16 4.10 Language Definition(2) Named query definition –example define smiths() as select p from Persons p where p.name=“Smith” Elementary Expression –atomic literal nil, false, true, 27, ‘z’ –name object Student –iterator variable e as x, e x, x in e –named query smith()

17 OOPSLA LAB. 17 4.10 Language Definition(3) Construction expressions –object : t(p 1 :e 1, …….., p n :e n ) Employee(name: “Peter”, boss: Chairman) –structure : struct(p 1 :e 1, …….., p n :e n ) struct(name: “Peter”, age:25) –set : set(e 1, …., e n ) set(1,2,3) –bag : bag(e 1, …., e n ) bag(1,1,2,3,3) –list : list(e 1, …., e n ) list(1,1,2,3,3) –array : array(e 1, …., e n ) array(1,1,2,3,3)

18 OOPSLA LAB. 18 4.10 Language Description(4) Atomic type expressions –una r y expressions : e not(true) –binary expressions : e 1 e 2 count(Students) - count(TA) –string expressions ‘a nice string’ like ‘%nice%str_ng’ is true Object Expressions –comparison of objects Doe = element(select s from Students s where s.name = “Doe”) –comparison of literals e 1 =e 2, e 1 != e 2, not(e 1 =e 2 )

19 OOPSLA LAB. 19 4.10 Language Description(5) –Applying an operation to an object jones->number_of_students Doe->apply_course(“Math”, Turing)->number Collection expressions –universal quantification : for all x in e 1 :e 2 for all x in Students:x.student_id > 0 –existential quantification : exists x in e 1 :e 2 exists x in Doe.takes:x.taught_by.name=“Turing” –membership testing : e 1 in e 2 Doe in TA –aggregate operators max(select salary from Professors)

20 OOPSLA LAB. 20 4.10 Language Definition(6) Select from where –select [distinct] e from x 1 in e 1, …, x n in e n where e’ –select [distinct] e from e 1 as x 1, …, e n as x n where e’ select couple(student:x.name, professor:z.name) from Student as x, x.takes as y, y.taught_by as z where z.rank = “full professor” Group-by operator –select_query group by partition_attributes [having predicate] select * from Employees e group by low: x.salary < 1000, medium: x.salary >= 1000 and x.salary < 10000, high: x.salary >= 10000

21 OOPSLA LAB. 21 4.10 Language Definition(7) returns set )> Order-by operator –select_query order by e1, …, en select p from Persons p order by p.age, p.name Indexed collection expressions –get i-th element : e1[e2] list(a, b, c, d)[1] element(select x from Course x where x.name = “Math’ and x.number=“101”).requires[2]

22 OOPSLA LAB. 22 4.10 Language Definition(8) –extracting a subcollection : e1[e2:e3] list(a, b, c, d)[1:3] element(select x from Course x where x.name = “Math’ and x.number=“101”).requires[2] –last and first element : first(e), last(e) element(select x from Course x where x.name = “Math’ and x.number=“101”).requires[2] –concatenating : e1 + e2 list(1,2) + list(2, 3)

23 OOPSLA LAB. 23 4.10 Language Definition(9) Binary set expression –union, intersection, difference bag(2,2,3,3,3) union bag(2,3,3,3) bag(2,2,3,3,3) intersection bag(2,3,3,3) bag(2,2,3,3,3) except bag(2,3,3,3) –inclusion e1 < e2 is true if e1 is include in e2 bit not equal to e2 e1 <= e2 is true if e1 is included in e2 set(1,2,3) < set(3,4,2,1) is true

24 OOPSLA LAB. 24 4.10 Language Definition(10) Conversion expressions –extracting the element of a singleton : element(e) element(select x from Professors x in x.name = “Turning”) –list to set : listtoset(e) listtoset(list(1,2,3,2)) –flattening : flatten(e) flatten(list(set(1,2,3), set(3,4,5,6),set(7))) == set(1,2,3,4,5,6,7) flatten(list(list(1,2), list(1,2,3))) == list(1,2,1,2,3) flatten(set(list(1,2), list(1,2,3))) == set(1,2,3) –typing an expression : (t)e select ( (Employee) s).salary from Students s where s in (select sec.assistant from sec in sections)

25 OOPSLA LAB. 25 4.10 Language Definition(11) Scope rules –explicit variable : select ….. from Persons p… p.age –implicit variable : select …… from Persons Persons.age –a name appearing in a (nested) query a variable in the current scope a named query a named object an attribute name or an operation name of a variable in the current scope –Example

26 OOPSLA LAB. 26 4.10 Language Definition(12) scope1 : Persons, c, Cities, all property name of class Person and class City scope2 : child, Persons, c, Cities, the property names of the class City scope3 & scope 4 : age, partition, p, v, the same names from scope1, except “age”, “partition”, “p”, and “v” if they exist scope5 : p and the same name from scope1, except “p” if it exists scope6 : p, v, Persons, c, Cities, the property names of the class City Select scope1 from Persons, Cities c where exists (select scope2 from children as child) or count (select scope3, (select scope4 from partition) from children p, scope5 v group by age:scope6 )

27 OOPSLA LAB. 27 4.11 Syntactical Abbreviations(1) OQL –orthogonal expression language –functional language Structure constructions –an alternate syntax is allowed in two contexts select projection {, projection}.. select … group by projection {, projection} –example select p.name, salary, student_id from Professors p, p.teaches returns bag

28 OOPSLA LAB. 28 4.11 Syntactical Abbreviations(2) Aggregate operators –example select count(*) from …… is equivalent to count(select * from ….) select aggregate(query) from …. Is equivalent to aggregate(select query from …. ) select aggregate(distinct query) from …. Is equivalent to aggregate(distinct( select query from …. )) Composite predicate –example 10 < some(8, 15, 7, 22) is true


Download ppt "OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University."

Similar presentations


Ads by Google