Object%20Query%20Language - PowerPoint PPT Presentation

About This Presentation
Title:

Object%20Query%20Language

Description:

An ad hoc query takes a database and returns some data from it: ... as a free standing function - deprecated in the most recent version, but e.g. in ... – PowerPoint PPT presentation

Number of Views:219
Avg rating:3.0/5.0
Slides: 21
Provided by: computin7
Category:

less

Transcript and Presenter's Notes

Title: Object%20Query%20Language


1
Object Query Language
  • The need for ad hoc querying
  • What SQL does
  • What OQL must do
  • OQL syntax and examples

2
Ad hoc Querying
  • An ad hoc query takes a database and returns some
    data from it
  • identifying some parts of the database of
    interest
  • and specifying criteria which the data returned
    must satisfy
  • It doesnt specify how the query should be
    carried out

3
What SQL Queries Do
  • The query takes
  • a set of relations (tables or queries)
  • a boolean expression on the columns
  • a set of columns
  • and returns
  • a relation (always)
  • SQL also provides constructs for data definition
    and data manipulation

4
What OQL Queries Must Do
  • Take data from the object model
  • and a boolean criterion
  • Return data in the object model
  • So this is not as simple since there is more
    variety in the data model

5
Features of OQL
  • queries are a superset of SQL select queries - no
    update or data definition facilities
  • however, a query can invoke an operation on an
    object and this operation can cause an update to
    occur.
  • not computationally complete
  • OQL is declarative and hence optimisable
  • Operators can be freely composed as long as they
    obey the type system

6
More Features of OQL
  • A query can return any value expressable in the
    object model
  • usually this will be a collection
  • but it could be a list, a record, an integer or a
    single object
  • It is designed to support two kinds of usage
  • interactively - keyboard input with results
    displayed immediately
  • embedded in a program - the queries are written
    as part of the program and the results returned
    to the program
  • Implementation vary!

7
Simple Queries
  • Any expression over values in the data model is
    an OQL query
  • an atomic value 5 23 10
  • using path expressions superUser . incoming-gtsize
  • creating a record struct( name superUser .
    name, unread superUser . incoming . size( )
    )
  • creating a collection list( 'A', 'b', 'c' )
  • an object superUser
  • creating an object UserDetails(userNamesuperUser
    .name, userLogin superUser.login )

8
Collections as Query Results
  • Collections can be returned as the result of a
    query in four ways
  • by explicit creation - as shown in the previous
    slide
  • set( 7, 5, 23 )
  • as the result of a select query
  • select distinct U from U in theUsers
  • as the result of accessing a multi-valued
    property of an object
  • superUser . storage
  • as the value of a persistent root - very often
    this will be the extent of a type and hence be a
    collection
  • theUsers

9
Manipulating Collections
  •  Union, intersection, difference of unordered
    collections
  • superUser . storage union set( superUser .
    incoming )
  •  Quantification operations
  • for all U in theUsers U . incoming . size( ) gt
    0
  • exists U in theUsers U . incoming . size( ) gt 0
  •  select queries iterate over collections
  • select U . name from U in theUsers
  •  

10
Manipulating Collections II
  • Individual elements can be extracted from
    singleton collections
  • element( list( 3 ) )
  • Individual elements can be extracted from an
    ordered collection by its position
  • list( 23.1, 44.2, 39.7, 12.4 ) 2
  • "123456789"5
  • Portions of ordered collections can also be
    retrieved.
  • list( 5, 7, 9, 11, 13 ) 23
  • "123456789"57

11
Manipulating Collections III
  • Tests for membership of a collection
  • 6 in list( 5, 7, 9, 11, 13 )
  • '6' in "123456789"
  • Aggregation operations
  • count( superUser . storage )
  • Collections can be turned from one form to
    another.
  • listtoset( superUser . incoming . holds )
  • distinct( select U . name from U in theUsers )
  • flatten( set( set( 1, 2, 3 ), set( 4, 5 ), set(
    6, 7, 8, 9 ) ) )
  • this returns set( 1, 2, 3, 4, 5, 6, 7, 8, 9 )

12
Select Queries
  • These are the same as SQL
  • select U . incoming . size( )
  • from U in theUsers
  • order by U . name
  • This returns a collection of sizes

13
Group By
  • This has a slightly different effect
  • select U . name from U in theUsers
  • group by inBoxSize U . incoming . size
  • which returns a collection of pairs of the size
    and the collection of names of people with that
    size
  • ( inBoxSize 0, partition bag("Jill", "Joe",
    "Pan") ),
  • ( inBoxSize 5, partition bag("John", "Kwame"
    ) ), etc.
  • Add in the following to return only those sizes
    with fewer than four users
  • having count( select N from partition ) lt 4

14
Named Queries
  • A query (view) for how many messages in jbowns
    "TODO box
  • define TODOsize as
  • select M-gtsize from U in theUsers, M in U .
    storage
  • where M . name "TODO" and U . login
    "jbrown"
  • which can then be executed by
  • TODOsize
  • Named queries can be parameterised as in the
    example
  • define IncomingSize( loginName ) as
  • select U . incoming. size from U in theUsers
  • where U . login loginName
  • which could be used as follows
  • IncomingSize( "jbrown" )

15
Type Coercion or Downcasting
  • Sometimes it is necessary to make an object more
    specific
  • e.g. given a collection of vehicles some of which
    are members of the car subclass
  • to get the number of wheels for a particular car
  • select ( (Car) V ). wheels
  • from V in theVehicles
  • where V . registration "KLS 504D
  • V is a vehicle but (Car) V is a car

16
Embedded OQL
  • There are three techniques used
  • as a free standing function - deprecated in the
    most recent version, but e.g. in O2C
  • o2query( aUser, "element( select U from U in
    theUsers where U . login 1 )", userLogin )
  • as a Query class
  • as methods on the most general collection class

17
The Java Query Class
  • This is defined as
  • class OQLQuery
  • public OQLQuery()
  • public OQLQuery(String Query) set query
  • public OQLQuery create(String Query) assign
  • public void bind(Object param) set param
    value
  • public Object execute() execute the query

18
Query Methods on ODMG Collections
  • query retrieves the results of an OQL query into
    a collection object.
  • the collection variable to be populated and the
    selection condition are passed as collection and
    string parameters
  • select_element retrieves the single result of
    the query
  • the condition is passed as a string parameter,
    the result is an object of some kind
  • exists_element tests if there are any results to
    this query
  • the selection condition is passed as a string
    parameter, the result is a boolean
  • select returns an iterator over the collection of
    results.
  • the query is passed as a string parameter, the
    result is a collection of some kind

19
Query Method on Java Collections
  • In Java there is only one method which filters a
    collection.
  • It has the following form
  • Collection query( String Filter )
  • Example
  • Bag Unreadusers theUsers.query(
  • "this.incoming.length() gt 0" )

20
Example
  • User JB theUsers.query( "select U in this
    where login \"jbrown\" ")
  • OQLquery query new OQLQuery(
  • "select subject from M in MB.holds where
    M.sender 1 and MB in select U.incoming
    from U in theUsers" )
  • query.bind( JB )
  • Bag result (Bag) query.execute()
Write a Comment
User Comments (0)
About PowerShow.com