0% found this document useful (0 votes)
13 views23 pages

Object Query Language

Uploaded by

harts.toys
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
13 views23 pages

Object Query Language

Uploaded by

harts.toys
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 23

OBJECT QUERY

LANGUAGE
OQL
• Stand Alone:
– Select struct (n: p.name, s: p.sex) from people
as p where p.age < 18

• Embedded in C++:
– D_oql_execute(teenagers, “Select p from
people as p where p.age < 18”
Operand
• Constans:
– 1, true, “string”, Nill
• Named Object
– People; President
• Variable
– P; p.age or p->age; I[i]
Operator
• Numerical: +, - , *, /, mod, abs
• Comarison : =, !=, >, <, >=
• Boolean : not, and, or
• Constructor:
– Object Employee(name:”John”, Salary:15000)
– Collection :Vector(1, 2, 3)
– Structure : struct(name: P.name, age: p.age)
– Literal Collection : set(1,2,3)
Operator on Collections
• Set
– Intersection, union, except
• Selection
– Select result(x, y, …) from col1 as x, col2 as y,…
where predicate(x,y,…)
• Quantificators
– For All x in collection
– Exist x in collection
• Conversion
– Element(singleton)
– Flatten(collection_of_collection)
OQL Select – From - Where
Select <list of values> from <list of collection and
typical member> where <condition>
• Collection in From can be any expression that
evaluate to a collection
• Following a collection is a name form a typical
member alias), optionally preceded by AS
• Typical usage of attributes:
– If x is an object, the path expression can be extended
to access an atribut’s value
– If x is a cllection, it is used in the FROM list in order to
access atribut of x
OQL Select – From – Where…
• Default type of the result : bag of structs, field
name taken from the ends of path names in
SELECT clause
• The fields can be renamed by giving prefix to the
path with the desired name and a colon
• Changing collection type:
– Use Select Distinct to get a set of structs
– Use Order By clause to get List of structs
– We can extract from a list as if it were an array
SELECT– FROM—WHERE
Example
• Print all females in database
– Select P from p in Persons where not(p.male)
– Result is of type : set(Person)
• Print the values of all female in database:
– Select *p from p in Person where not(p.male)
– Relust is of type:
• Set(tupe(name: string; male: boolean;
spose:Person; children:list(Person); DoB:Date)
SELECT– FROM—WHERE
Example…
• Print the names of all females in the database
– Select distinct p.name from p in Persons where
not(p.male)
– Rusult is of type: Set (String)
• Print the name and birth-date of all female in the
DB who were born in July
– Select struct(name:p.name, dob: p.dob) from p in
Persons where not(p.male) and p.dob.month=7
– Ruslt is of type: Set(tuple(name:string; dob:date))
Pat Expressions Example
• Print the names of all spouses
– Select p.spouse.name from p in Persons
– Result is of type: Set(String)
• Print the names and dob of all spouses who
were born in June
– Select struct(name:p.spouse.name,
dob:p.spouse.dob) from p in Persons where
p.spouse.dob.month=6
– Result is of type: set tuple(name:string; dob:Date))
Method Invocation
• Method are function which implicity have
as a parameter “self” and optional
additional parameters. None of the
example method have any additional
parameter
• Example: print the names and age of all
employee
– Select struct(name:e.name; age:e.age()) from
e in Employees
Aggregation
• Aggregate operator cpunt operates on collection
of elemens of any type, min and max operate on
collection of ordered type, avg and sum operates
on collection of number
• Example
– Select * from Employee as e group by e.dept as dpt
order by avg(select e.salary from partition)
– Result is of type:
• List <struct(dept:int; Partition: Bag<Struct(e:Employe)>>
Aggregation….
• Print the number of Eployee
– Count(Employee)
– Result is of type : integer
• Print the maximum numer of children of any
person
– Max(select count(p.children) from p in Persons)
• Print the name ane names of children of the
persn with the maximum number of chiledren
– Select struct(name: p.name, children: select c.Name
from c in p.Children) from p in Persons where
count(p.chiledren)=max(select count(p1.children)
from p1 in Person)
Unnesting
• Print all pairs(manager, employee) of
manager in employees
– Select struct(manager: e.name, employee:
s.name) from e in Employees, s in
e.subordinates where
count(e.dubordinates)>0
• Print the names of all subordinates of Joe
Brown
– Select s.Name from e in Employees, s in
e.Subordinater where e.name=“Joe Brown”
Flatten
• Flatten get rid of one level of set nesting, e.g. it
takes something of type set(set(eleent)) and
produces something of type set(element) by
taking the union of all sets of elemen
– Flatten({{1,2},{3,4},{1,5}})={1,2,3,4,5,1}
• Print the names of employees who are
supervised by someone
– Select s.name from s in flatten (select e.subordinates
from e in Employees)
Group by
• Group by introduces a level of set nesting,
and produces something of type
set(tuple(label…, partition: set(…)))
• For each salary, print the employees
earning that salary
– Select * from e employees group by e.salary
– Result is of type: set(tuple(salary: integer,
partition: set(struct(e.Employee))))
Group by … having
• For each salary earned by more than 2
employees, print the set of names of
employees earning that salary”
– Select struct(salary: e.salary, names: (select
e1.name from e1 in partition)) from e n
employee group by e.salary having
count(partion)>2
– Result is of type
• Set(tuple(salary:integer, name:set(string)))
Indexing into a list
• List are ordered sets, and are treated as array
indexed starting from 0
• “Print all first-born chiledren”
– Select p.children[0] from p in person where
p.children<>list()
– Result is of type: set(Person)
• Print the names of all second-born children
– Select p.children[1].name from p in Person where
count(p.children)>1
– Result is of type: set(String)
Sort… in … by
• “Select .. From.. Where” creates a bag of
element from an inpt set, “select distinct..
From… where” creates a set. To create a list of
element as output from an input set, we use
“sort..in..by”
• Print the names and ages of employee by
increasing age
– Sort x in(select struct(name:e.name, age:e.age())
from e in employees) by x.age()
– Result type is list(tuple(name:string, age:integer))
Creating sets from list
• A list can be converted to a set using
listtoset. For ex. Suppose we want to print
each person with children represented as
a set rather than list.
– Select struct(name:p.name,
children:listtoset(p.children)) from p in Person
– Resul type is set(tuple(name: string,
children:set(person))
Set Operations: Difference
• “Print the employee who are supervised by
none”
– Select e from employees where not(e in
flatten(select e1.subordinates from
e1.employes))
OR
(Select e in Employees)-flatten(select e.subordinates
from e in employees)

Result type is Set(Employee)


Casting
• Casting is sometimes necessary to suport
type inferencing
• For ex, suppose we know that all people in
our database are either employee or
consultant: “print the name of all
consultant”
– Select c.name from c in (Person-
(select(Person) e from e in Employee))
– Result type is : set(String)
Quantifiers
• Boolean valued expression for use in where clause
– For all x in <collection>:<condition>
– Existx in <collecton> : <condition>
• The expression has value True if the condition is ru for
all elements of collection
• “Print then names of people who have had at least one
boy
– Select struct(name:p.name) from p in Person where exist c in
p.children: c.male
• “Print the name of people who had all boys”
– Select struct(name:p.name) from p in Person where forall c in
p.choildren:c.male

You might also like