180 likes | 339 Views
Object Query Language ( OQL ). Mari án Kamenišťák MFF – UK Praha 26.11.2003. Úvod. podobný SQL jazyku založený na ODMG štandarte Architektúra: Nástroje: C, C++, Java, … DB engine: napr. O2 engine & store Interfaces: O2ODBC, O2Cobra,. Z ákladné dotazy (C++).
E N D
Object Query Language (OQL) Marián Kamenišťák MFF – UK Praha 26.11.2003
Úvod • podobný SQL jazyku • založený na ODMG štandarte • Architektúra: • Nástroje: C, C++, Java, … • DB engine: napr. O2 engine & store • Interfaces: O2ODBC, O2Cobra, ..
Základné dotazy (C++) • class item {String what; double price;}; • class client {String name; Array <item> order; int amount(); };
Základné dotazy • Select distinct e.salary from e in Schema.employees where e.family != nil and e.position = “reporter”; • … where count(e.children) >=1; • … group by … having … • … order by … (vracia pole) • existujú agregačné fcie: min, max, sum, avg
Tvorenie výsledkov • Define MyEmployees as select struct(employee: struct(e.name, position: e.post), salary: e.salary) from e in Company.employees; • element (select e from e in MyEmployees where e.salary>150000);
Množinové operátory • Zjednotenie(+): MyEmployees + YourEmployees • Prienik (*) • Rozdiel (-)
Konverzie • listtoset(Companies[2].clients) • Naopak: select e from e in MyEmployees order by e.name; • z kolekcie kolekcií na plochú kolekciu: flatten(select e.clients from c in Companies); • flatten(list(list(1,2), list(1,2,3))) -> list(1,2,1,2,3) • ‘string’ like ‘%nice_%’
Preddefinované typy kolekcií • Set<T> - mna • set(1,2,5) • Bag<T> - multimna • bag(1,2,2,3,3) • Varray<T> - dynamické pole • List<T> - Varray s metódami pre manipuláciu (insert, delete,..) • list(1,2,2,3); list(3..5) • Ref - perzistentný pointer
Pokročilé OQL • class Person { String name; Date birth; set<ref<Person>> childs inverse parents; set<ref<Person>> parents inverse childs; Person(); //constructor void birth(ref<Person> child); //narodenie d. virtual set<String> activities(); }
OQL • class Employee:Person{ //podtrieda float salary; virtual set<String> activities();//predefinit. }; • Join: • Select p from p in Persons, b in (select a.building from a in Apartments) Where p.name = b.address.street;
OQL – Data manipulation • select struct (me:p.name, address:p.lives_in.building.address; childs: (select struct(name: c.name, address: c.lives_in.building.address) from c in p.children) ) from p in Persons; //adresy detí cez všetky osoby.
OQL Polymorfizmus • napr. mna Persons obsahuje prvky tried Preson, Employee, Student. • Potom sa virtuálna metóda activities() vykoná pre konkrétnu triedu. • Select ((Student)p).grade from p in Persons where “lecture” in p.activities;
OQL • define salary_map as select street, average_salary: avg(select p.e.salary from partition p) from e in Employees group by e.lives_in.building.address.street; //returns Bug<struct {String street; float avg_salary;>};
OQL • define sorted_salary_map as select s from s in salary_map order by s.average_salary List<struct {String street; float avg_salary;} > • sorted_salary_map[2].street;
OQL • define Jakes as select x from Students x where x.name = “Jake”; • define Jake as first(element( select x from Students x where x.name = “Jake”)); • //first, last operators in list
OQL • Prechádzanie štruktúrami: • person.name == person->name //rovnaké • Jake->woman!=nil and Jake->woman->name=Carol • Dereferencie • p1 = p2 je pravda, ak ukazujú na rovnaký objekt • *p1 = *p2 je pravda, ak majú rovnaké hodnoty
OQL - group by • select * from Employees e group by low: e.salary<1000, medium: e.salary>=1000 and e.salary<2000, high: e.salary>=2000; Vytvorí pomenované partition bugy
Literatúra • ODMG OQL user manual 5.0 • feb. 1998