Download presentation
Presentation is loading. Please wait.
Published byBlaise Cobb Modified over 9 years ago
1
8 October, 2001DBIS4 - RLC108 Object Query Language The need for ad hoc querying What SQL does What OQL must do OQL syntax and examples
2
8 October, 2001DBIS4 - RLC109 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 doesn’t specify how the query should be carried out
3
8 October, 2001DBIS4 - RLC110 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
8 October, 2001DBIS4 - RLC111 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
8 October, 2001DBIS4 - RLC112 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
8 October, 2001DBIS4 - RLC113 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
8 October, 2001DBIS4 - RLC114 Simple Queries Any expression over values in the data model is an OQL query: an atomic value:5 +23 * 10 using path expressionssuperUser. incoming->size creating a recordstruct( name: superUser. name, unread: superUser. incoming. size( ) ) creating a collectionlist( 'A', 'b', 'c' ) an objectsuperUser creating an objectUserDetails(userName:superUser.name, userLogin: superUser.login )
8
8 October, 2001DBIS4 - RLC115 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
8 October, 2001DBIS4 - RLC116 Manipulating Collections Union, intersection, difference of unordered collections superUser. storage union set( superUser. incoming ) Quantification operations for all U in theUsers : U. incoming. size( ) > 0 exists U in theUsers : U. incoming. size( ) > 0 select queries iterate over collections select U. name from U in theUsers
10
8 October, 2001DBIS4 - RLC117 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 ) [2:3] "123456789"[5:7]
11
8 October, 2001DBIS4 - RLC118 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
8 October, 2001DBIS4 - RLC119 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
8 October, 2001DBIS4 - RLC120 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 ) < 4
14
8 October, 2001DBIS4 - RLC121 Named Queries A query (view) for how many messages in jbown’s "TODO” box: define TODOsize as select M->size 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
8 October, 2001DBIS4 - RLC122 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
8 October, 2001DBIS4 - RLC123 Embedded OQL There are three techniques used: –as a free standing function - deprecated in the most recent version, but e.g. in O 2 C 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
8 October, 2001DBIS4 - RLC124 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
8 October, 2001DBIS4 - RLC125 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
8 October, 2001DBIS4 - RLC126 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() > 0" )
20
8 October, 2001DBIS4 - RLC127 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();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.