Download ODMG OQL User Manual
Transcript
ODMG OQL User Manual Release 5.0 - April 1998 In for m ation in t h is docu m en t is su bject t o ch an ge with ou t n ot ice an d sh ou ld n ot be con str u ed as a com m it m en t by O2 Tech n ology. Th e soft war e descr ibed in th is docu m en t is deliver ed u n der a licen se or n on disclosu r e agr eem en t. Th e soft war e can on ly be u sed or copied in accor dan ce wit h t h e ter m s of th e agr eem en t . It is again st t h e law t o copy t h is soft war e t o m agn et ic t ape, disk , or an y ot h er m ediu m for an y pu r pose ot h er t h an t h e pu r ch aser ’s own u se. Copyr igh t 1992-1998 O2 Tech n ology. All r igh t s r eser ved. No par t of t h is pu blicat ion can be r epr odu ced, st or ed in a r et r ieval syst em or t r an sm it t ed in an y for m or by an y m ean s, elect r on ic, m ech an ical, ph ot ocopy with ou t pr ior wr itt en per m ission of O2 Tech n ology. O2 , O 2 En gin e API, O2 C, O2 DBAccess, O2 En gin e, O2 Gr aph , O2 Kit , O2 Look , O2 St or e, O2 Tools, an d O2 Web ar e r egist er ed tr adem ar k s of O2 Tech n ology. SQL an d AIX ar e r egist er ed t r adem ar k s of In t er n at ion al Bu sin ess Mach in es Cor por at ion . Su n , Su n OS, an d SOLARIS ar e r egist er ed t r adem ar k s of Su n Micr osystem s, In c. X Win dow System is a r egist er ed tr adem ar k of t h e Massach u set t s In st itu t e of Tech n ology. Un ix is a r egist er ed t r adem ar k of Un ix Syst em Labor at or ies, In c. HPUX is a r egist er ed t r adem ar k of H ewlet t -Pack ar d Com pan y. BOSX is a r egist er ed t r adem ar k of Bu ll S.A. IRIX is a r egist er ed t r adem ar k of Siem en s Nixdor f, A.G. NeXTSt ep is a r egister ed t r adem ar k of t h e NeXT Com pu t er , In c. Pu r ify, Qu an t ify ar e r egist er ed tr adem ar k s of Pu r e Soft war e In c. Win dows is a r egist er ed t r adem ar k of Micr osoft Cor por ation . All oth er com pan y or pr odu ct n am es qu ot ed ar e t r adem ar k s or r egist er ed t r adem ar k s of t h eir r espect ive t r adem ar k h older s. Who should read this manual OQL is an object -or ien t ed SQL-lik e qu er y lan gu age, t h e ODM G st an dar d. Th is m an u al descr ibes h ow t o u se OQL as an em bedded fu n ct ion in a pr ogr am m in g lan gu age (e.g. O2 C, C, C++, or J ava) or in t er act ively as a qu er y lan gu age. It assu m es pr eviou s k n owledge of t h e O 2 syst em . Oth er docu m en t s available ar e ou t lin ed, click below. See O2 Documentation set. TA BLE O F CONTENT S Th is m an u al is divided in t o t h e followin g ch apt er s: 1 - In tr odu ct ion 2 - Get t in g St ar t ed 3 - OQL Rat ion ale 4 - OQL Refer en ce ODM G OQL User Man u al 5 TABLE OF CONTENTS 1 Introduction 9 1.1 System Overview......................................................................10 OQL .............................................................................................12 Browser Interface .........................................................................12 1.2 Interactive and embedded query language ...........................14 Interactive OQL ...........................................................................15 Embedded OQL ...........................................................................15 1.3 Manual overview.......................................................................16 2 Getting Started 17 2.1 Basic queries ............................................................................18 Database entry points ...................................................................20 Simple queries..............................................................................20 2.2 Select ... from ... where .........................................................22 Set.................................................................................................22 Join ...............................................................................................24 Path expressions ...........................................................................24 Testing on nil ...............................................................................25 List or array ..................................................................................25 2.3 Constructing results ................................................................27 Creating an object ........................................................................29 2.4 Operators ..................................................................................30 Count ............................................................................................30 Define...........................................................................................30 Element ........................................................................................31 Exists ............................................................................................31 Group by ......................................................................................32 Like ..............................................................................................35 Order by .......................................................................................35 2.5 Set operators ............................................................................36 2.6 Conversions..............................................................................37 List to set ......................................................................................37 Set to list.......................................................................................37 6 ODM G OQL User Man u al TABLE OF CONTENTS Flatten .......................................................................................... 38 2.7 Combining operators .............................................................. 38 2.8 Indexes ..................................................................................... 39 Display index............................................................................... 40 2.9 Chapter Summary.................................................................... 41 3 OQL Rationale 43 3.1 The ODMG standard ................................................................ 44 3.2 The ODMG model..................................................................... 44 3.3 OQL by example ...................................................................... 49 Path expressions .......................................................................... 49 Data manipulation ....................................................................... 51 Method invoking ......................................................................... 52 Polymorphism.............................................................................. 53 Operator composition .................................................................. 54 4 OQL Reference 57 4.1 Introduction.............................................................................. 58 4.2 Principles.................................................................................. 58 4.3 Language Definition ................................................................ 59 4.4 Syntactical Abbreviations ...................................................... 82 4.5 OQL BNF.................................................................................. 85 INDEX ODM G OQL User Man u al 91 7 TABLE OF CONTENTS 8 ODM G OQL User Man u al 1 1 I ntr oduction Con gr at u lat ion s! You ar e n ow a u ser of t h e object-or ien t ed qu er y lan gu age OQL. O2 is a r evolu t ion ar y syst em t h at is par t icu lar ly well adapt ed for developin g lar ge-scale clien t/ ser ver application s in bot h fields of bu sin ess an d t ech n ical soft war e developm en t . Th is ch apt er in t r odu ces t h e O2 syst em an d t h e OQL qu er y lan gu age. Th e ch apt er is divided in t o t h e followin g sect ion s : • Syst em Over view • In t er active an d em bedded qu er y lan gu age • M an u al over view ODM G OQL User Man u al 9 1 Introduction 1.1 System Overview Th e syst em ar ch it ect u r e of O2 is illu st r at ed in Figu r e 1.1. External Interfaces Development Tools Standard Dev. Tools O2 Dev. Tools OQL C O2 C C++ Java Database Engine O2Engine O2Store O2ODBC O2Corba O2DB Access O2Web Fi gu r e 1 .1 : O2 Sy st em Ar ch i t ect u r e Th e O 2 system can be viewed as con sist in g of t h r ee com pon en t s. Th e Database Engine pr ovides all t h e feat u r es of a D at abase syst em an d an object -or ien t ed syst em . Th is en gin e is accessed wit h Development Tools , su ch as var iou s pr ogr am m in g lan gu ages, O 2 developm en t t ools an d an y st an dar d developm en t tool. Nu m er ou s External Interfaces ar e pr ovided. All en com passin g, O 2 is a ver sat ile, por t able, dist r ibu t ed, h igh per for m an ce dyn am ic object -or ien ted dat abase system . Database En gin e: 10 • O2 St or e Th e dat abase m an agem en t syst em pr ovides low level facilit ies, t h r ou gh O2 Stor e API, t o access an d m an age a dat abase: disk volu m es, files, r ecor ds, in dices an d t r an sact ion s. • O2 En gin e Th e object dat abase en gin e pr ovides dir ect con t r ol of sch em as, classes, object s an d t r an sact ion s, t h r ou gh O2 En gin e API. It pr ovides fu ll t ext in dexin g an d sear ch capabilit ies wit h O 2 Sear ch an d spat ial in dexin g an d r et r ieval capabilit ies wit h O 2 Spat ial. It in clu des a Not ificat ion m an ager for in for m in g oth er clien t s con n ect ed t o t h e sam e O 2 ser ver t h at an even t h as occu r r ed, a Ver sion m an ager for h an dlin g m u lt iple object ver sion s an d a Replicat ion API for syn ch r on izin g m u lt iple copies of an O2 system . ODM G OQL User Man u al System Overview : Pr ogr am m in g Lan gu ages: O 2 object s m ay be cr eat ed an d m an aged u sin g t h e followin g pr ogr am m in g lan gu ages, u tilizin g all t h e featu r es available wit h O2 (per sist en ce, collection m an agem en t , t r an sact ion m an agem en t , OQL qu er ies, et c.) • C O2 fu n ct ion s can be in vok ed by C pr ogr am s. • C++ OD MG com plian t C++ bin din g. • J ava OD MG com plian t J ava bin din g. • O2 C A power fu l an d elegan t object-or ien t ed fou r t h gen er at ion lan gu age specialized for easy developm en t of object dat abase applicat ion s. • OQL OD MG st an dar d, easy-t o-u se SQL-lik e object qu er y lan gu age wit h special feat u r es for dealin g wit h com plex O2 object s an d m eth ods. O 2 Developm en t Tools: • O2 Gr aph Cr eat e, m odify an d edit an y t ype of object gr aph . • O2 Look Design an d develop gr aph ical u ser in ter faces, pr ovides in t er active m an ipu lation of com plex an d m u ltim edia object s. • O2 Kit Libr ar y of pr edefin ed classes an d m et h ods for fast er developm en t of u ser applicat ion s. • O2 Tools Com plet e gr aph ical pr ogr am m in g en vir on m en t t o design an d develop O 2 dat abase applicat ion s. St an dar d Developm en t Tools: All stan dar d pr ogr am m in g lan gu ages can be u sed wit h st an dar d en vir on m en t s (e.g. Visu al C++, Su n Spar cwor k s). Exter n al In t er faces: • O2 Cor ba Cr eat e an O2 / Or bix ser ver t o access an O2 dat abase wit h CORBA. • O2 DBAccess Con n ect O2 applicat ion s to r elation al dat abases on r em ot e h ost s an d in vok e SQL st at em en ts. • O2 OD BC Con n ect r em ot e ODBC clien t applicat ion s t o O2 dat abases. • O2 Web Cr eat e an O2 Wor ld Wide Web ser ver t o access an O2 dat abase th r ou gh t h e in t er n et n etwor k . ODM G OQL User Man u al 11 1 Introduction OQL OQL is an object-or ien t ed SQL-lik e qu er y lan gu age. OQL is t h e qu er y lan gu age of t h e ODM G-9 3 st an dar d 1 . It can be u sed in t wo differ en t ways eit h er as an em bedded fu n ct ion in a pr ogr am m in g lan gu age or as an ad h oc qu er y lan gu age. You can u se OQL as a fu n ct ion called fr om O 2 C, C, C++, Sm allt alk or J ava, in or der t o m an ipu lat e com plex valu es an d m et h ods. Each con st r u ct pr odu ces a r esu lt wh ich can t h en be u sed dir ect ly in t h e pr ogr am m in g lan gu age. Meth ods can be t r igger ed t o m odify t h e dat abase. You will fin d t h at pr ogr am m in g is easier becau se OQL can filter valu es u sin g com plex pr edicat es wh ose evalu at ion s ar e opt im ized by t h e OQL opt im izer in O2 . OQL can also be u sed in t er act ively as an ad h oc qu er y lan gu age allowin g dat abase qu er ies fr om bot h t ech n ical an d n on -t ech n ical u ser s. In t er act ive featu r es in clu de fast an d sim ple br owsin g of t h e dat abase. Browser Interface Th e br owser in t er face you see depen ds on t h e oper at in g syst em you ar e u sin g. • Unix In Un ix, th e O2 Look gr aph ical u ser in t er face gen er at or is u sed t o gen er at e t h e gr aph ical for m of OQL qu er y r esu lts. Figu r e 1.2 sh ows a t ypical qu er y r esu lt in gr aph ical for m , as gen er ated by O2 Look . 1. Th e Object Database St an dard: ODMG - 93. At wood, Bar ry, Du h l, Eastm an , Fer ran , J ordan , Loom is an d Wade. Edited by R.G.G. Cat tell. © 1996 Mor gan Kau fm an Pu blish er s. 12 ODM G OQL User Man u al System Overview : Browser Interface Er a ser bu t t on Object i con s Fi gu r e 1 .2 : T y p i ca l OQL qu er y r esu l t i n gr a p h i ca l f or m , a s gen er a t ed i n Un i x In addition t o t h e u su al Motif bu t ton s a gr aph ical qu er y r esu lt h as an Er aser bu t ton . Click in g on t h e Er aser bu t t on r em oves th e gr aph ical r esu lt . Th is qu er y r esu lt con sist s of a n u m ber of object s. Each object h as it s own pop-u p m en u wh ich is displayed by click in g t h e Object icon u sin g t h e r igh t m ou se bu t t on . Th is pop-u p m en u can be u sed t o access t h e pu blic m et h ods of each object . • Windows NT In Win dows NT, t h e qu er y r esu lt is displayed in a win dow in t ext u al for m con t ain in g h yper t ext lin k s. Each lin k r epr esen t s a su b-object . Th e label for a specific lin k m ay be obt ain ed by applyin g t h e title m et h od t o t h e su b-object r epr esen t ed by t h e lin k . Click in g on a h yper t ext lin k , with t h e r igh t m ou se bu t t on , r eplaces t h e con t en t s of t h e win dow wit h a r epr esen t at ion of t h e su b-object associat ed wit h th e lin k . Figu r e 1.3 sh ows a t ypical qu er y r esu lt in gr aph ical for m , as gen er at ed in Win dows NT. ODM G OQL User Man u al 13 1 Introduction Fi gu r e 1 .3 : Ty p i ca l OQL qu er y r esu l t i n gr a p h i ca l f or m , a s gen er a t ed i n Wi n d ow s NT Th e br owser sh own in Figu r e 1.3 h as th e followin g bu t t on s: Ba ck th is bu tt on displays t h e pr eviou s object . For w a r d th is bu tt on displays t h e n ext su b-object. It is on ly valid if th e Back bu t ton h as been act ivat ed at least on ce. New Wi n d ow Th is bu tt on displays t h e cu r r en t object in a n ew win dow. Each win dow is an in depen den t br owser . Qu i t Th is bu tt on closes t h e act ive win dow. Th e qu er y r esu lt is an object of t h e Per son class, wh ich h as a n am e, an age an d a spou se. A spou se is also an object of t h e Per son class, an d t h u s appear s in as a h yper t ext lin k . Left click in g displays t h e spou se object . Note Th e r est of th is m an u al will on ly sh ow gr aph ical displays fr om t h e Un ix plat for m . 1.2 Interactive and embedded query language It is becau se OQL is so easy t o u se in ter act ively t h at all k in ds of u ser s in clu din g n on -t ech n ical u ser s can br owse th e dat abase qu ick ly an d efficien t ly t o get t h e in for m at ion t h ey wan t . OQL can also be u sed as a fu n ct ion called fr om C, C++, J ava, O2 C an d O2 En gin e API. 14 ODM G OQL User Man u al Interactive and embedded query language : Interactive OQL Th e OQL in ter pr et er can be t r igger ed by t h e query com m an d of O2dba, O2dsa or O2 sh ells. Th e com m an d in t er pr et er pr om pt s you wit h t h e followin g m essage: type your command and end with ^D To r u n OQL, type: query ^D You m u st t ype ^D (Con t r ol - D) on a separ ate lin e. You n ow see: Query Interpreter type your query and end with ^D Type you r qu er y, en din g it wit h ^D. "this is a query" ^D Th e an swer is au tom at ically displayed an d th e system r etu r n s to t h e OQL pr om pt : type your query and end with ^D To leave t h e qu er y session type: ^D (or quit) You ar e n ow back in th e com m an d in t er pr et er an d you see t h e m essage: type your command and end with ^D You can also u se OQL in t h e O2 Tools pr ogr am m in g en vir on m en t (Refer t o t h e O2 Tools User M an u al). Note In a Win dows en vir on m en t ^Z (Con t r ol - Z) is u sed in stead of ^D (Con t r ol - D). Embedded OQL An y valid qu er y can be passed fr om O2 C code t o OQL u sin g t h e syst em su pplied fu n ction o2query. Th is is det ailed in t h e O2 C Refer en ce m an u al. Sim ilar ly, you can pass a qu er y t o a C++, C, Sm allt alk or J ava pr ogr am . Refer to t h e r espect ive m an u als for details. ODM G OQL User Man u al 15 1 Introduction Fin ally an OQL fu n ct ion exist s in O2 En gin e an d is descr ibed in t h e O 2 En gin e API Refer en ce M an u al. 1.3 Manual overview Th is m an u al is divided u p in t o th e followin g ch apt er s: • Ch apt er 1 - In t r odu ct ion Th is ch apt er in t r odu ces t h e O 2 system an d t h e OQL qu er y lan gu age. It ou t lin es t h e con cept s of t h e ad h oc qu er y lan gu age t h at allows you to br owse t h e dat abase qu ick ly an d efficien t ly t o get t h e in for m at ion you wan t , an d th e em bedded qu er y lan gu age t h at you can call fr om in side you r pr ogr am s. • Ch apt er 2 - OQL - Get t in g st ar t ed Th is ch apt er in t r odu ces t h e OQL lan gu age so you can st ar t t o u se OQL in or der t o obt ain t h e exact in for m ation you wan t fr om you r dat abase. It descr ibes an d illu st r at es basic an d “select ..fr om ..wh er e” qu er ies, details h ow t o con st r u ct r esu lts an d descr ibes t h e u se of oper at or s an d in dexes. To fu lly u n der st an d t h is ch apter , you m u st k n ow t h e ODMG dat a m odel. • Ch apt er 3 - OQL Rat ion ale Th is ch apt er in t r odu ces t h e ODMG stan dar d an d descr ibes t h e ODM G object m odel. It also gives an exam ple based pr esen t at ion of OQL. • Ch apt er 4 - OQL Refer en ce Th is ch apt er con t ain s th e ODMG r efer en ce m an u al for OQL 1.2. It is t h e sam e as t h e ODMG st an dar d wit h added n ot es an d explan at ion s on h ow t o u se OQL with O 2 . For each feat u r e of th e lan gu age, you get th e syn tax, in in for m al sem an t ics, an d an exam ple. Fin ally, t h e for m al syn t ax is given . 16 ODM G OQL User Man u al 2 G et ti n g S t art ed 2 AN OBJ ECT-ORIENTED DATABASE QUERY LANGUAGE So t h at you can obt ain t h e exact in for m ation you wan t fr om you r dat abase, O2 h as an object or ien t ed database qu er y lan gu age OQL. OQL is a power fu l an d easy-t o-u se SQL-lik e qu er y lan gu age with special feat u r es for dealin g wit h com plex objects, valu es an d m eth ods. Th is ch apt er in t r odu ces t h e OQL lan gu age an d is divided u p in t o t h e followin g sect ion s: • Basic qu er ies • Select ... fr om ... wh er e • Con str u ctin g r esu lt s • Oper ator s • Set oper at or s • Con ver sion s • Com bin in g oper at or s • In dexes • Ch apter Su m m ar y To u n der stan d t h is ch apt er you n eed to k n ow th e OD MG dat a m odel 1 . As an in t r odu ct ion t o t h e data m odel you can r efer t o ch apt er 3 of t h is m an u al or t h e O 2 C Begin n er ’s Gu ide. Exper ien ce of SQL, t h ou gh n ot a pr er equ isit e, will facilit at e t h e OQL lear n in g pr ocess. 1. Th e Object Dat abase Stan dard: ODM G - 93, r elease 1.2. Edit ed by R.G.G. Cat t ell. © 1996 M or gan Kau fm an Pu blish er s. ODM G OQL User Man u al 17 2 Getting Started 2.1 Basic queries All t h e exam ples sh own below ar e based on t h e followin g O2 sch em a: • In O2 C class o2_set_Employee public type unique set (Employee) end; class o2_list_Client public type list (Client) end; class Company public type tuple ( name: string, employees: o2_set_Employee, clients: o2_list_Client ) method public title: string end; class Client public type tuple ( name: string, order: list (tuple ( what: string, price: real)) ) end; class Employee public type tuple ( name: string, birthday: Date, position: string, salary: real) method age: integer end; 18 ODM G OQL User Man u al Basic queries • In C++ class Company { public: d_String name; d_Set<d_Ref<Employee> > employees; d_List<d_Ref<Client> > clients; char* title() {return name;} }; class item { d_String what; double price;}; class Client { public: d_String name; d_Array<item> order; }; class Employee { public: d_String name; d_Date birthday; d_String position; float salary; int age(); }; Two per sist en t r oot s ar e also defin ed: An object , Globe an d a collect ion the_employees. name Globe: Company; constant name the_employees: o2_set_Employee; ODM G OQL User Man u al 19 2 Getting Started Database entry points To qu er y an y dat abase you n eed var iou s en t r y poin t s. In O2 t h ese ar e th e n am ed object s an d n am ed valu es. For exam ple, Globe is an en tr y poin t. Th e sim plest OQL qu er y calls an en tr y poin t : Globe Th is r et u r n s: In an O2 dat abase, n am ed object s an d valu es can eit h er be valu es of an y type, or object s of an y class. Con sequ en tly, OQL allows you t o qu er y valu es or object s of an y type or class. Note Th e qu er y r esu lts sh own below ar e all given in th e Un ix gr aph ic for m . Simple queries Sim ple qu er ies can in volve differ en t t ypes of valu es: • Atomic values With at om ic valu es you can car r y ou t ar ith m et ic calcu lat ion s, e.g., 2 * 2 Th is is a qu er y wh ich r et u r n s t h e in t eger 4. 20 ODM G OQL User Man u al Basic queries : Simple queries • Struct values You can also con sider th e valu e of th e object Globe of class Company as a st r u ct (or t u ple) valu e wit h t h r ee at tr ibu t es. Th e on ly oper at ion you can car r y ou t on a st r u ct is extr actin g a field, e.g., Globe.name Th is r et u r n s t h e n am e of t h e Globe Com pan y. • List or array values A list is an or der ed collect ion t h at allows du plicates an d you can t h er efor e ext r act an y of it s elem en t s if you k n ow t h eir posit ion . For exam ple, you can ext r act t h e f i r st elem en t of t h e list in clients as follows. Globe.clients[0] In OQL, you cou n t list elem en t s fr om 0. For OQL, an ar r ay beh aves t h e sam e way as a list . ODM G OQL User Man u al 21 2 Getting Started • Call of a method To apply a m et h od t o an object is a base qu er y, e.g. Globe.title Th is applies t h e m et h od title to t h e object Globe an d r et u r n s t h e r esu lt of t h e m et h od title: 2.2 Select ... from ... where Th e select from where clau se en ables you to ext r act t h ose elem en t s m eetin g a specific con dition fr om a col l ec t i on . O2 collect ion s in clu de set , bag (a m u lt i-set or set wit h du plicates), l i st (an in ser t able an d dyn am ic ar r ay) or ar r ay . Th e OQL qu er y h as t h e followin g str u ctu r e: select: defin es t h e st r u ct u r e of t h e qu er y r esu lt from: in t r odu ces t h e collect ion s again st wh ich t h e qu er y r u n s. where: in t r odu ces a pr edicat e t h at filt er s t h e collect ion . Th is sect ion n ow descr ibes h ow t o u se t h is clau se. Set A set is a n on -or der ed collect ion . Th e m ost fr equ en t qu er y on a set is a f i l t er . Th is con sists of ex t r act i n g t h e el em en t s of a set wh ich h ave cer t ain ch ar act er istics. 22 ODM G OQL User Man u al Select ... from ... where : Set For exam ple: select e from e in Globe.employees where e.salary > 200.00 Th is qu er y r et u r n s t h ose em ployees wor k in g at t h e In t er n at ion al Globe wit h a salar y over 200: Th e select clau se defin es th e qu er y r esu lt as t h e em ployees an d t h e from clau se gives t h e set on wh ich t o r u n t h e qu er y. Th e var iable e r epr esen t s each of it s elem en t s in t u r n . Th e where clau se filt er s t h e em ployees so t h at t h ose ear n in g m or e t h an 200 ar e ext r act ed. Th is qu er y t h er efor e bu ilds a collect ion of em ployees. Th is collect ion is in fact a bag as du plicat es ar e accept ed. You can also add t h e k eywor d distinct to elim in at e an y du plicat es fr om t h e r esu lt in g bag an d t h en pr odu ce a t r u e set . M or eover , you can access fr om e an y at tr ibu t es, e.g. salary an d get a set of r eal n u m ber s. For exam ple: select distinct e.salary from e in Globe.employees where e.position = "Reporter" Th is gives a set of t h e salar ies of t h e Repor t er s: ODM G OQL User Man u al 23 2 Getting Started Join You can also u se a qu er y t o select fr om m or e t h an on e collect ion : select e from e in Globe.employees, c in Globe.clients where e.name = c.name Th is qu er y r et u r n s t h e set of em ployees wh o h ave t h e sam e n am e as a clien t . If t h er e is a clien t called Ken t an d an em ployee called Ken t , you see t h e followin g win dow: Path expressions Object s ar e r elat ed to ot h er object s, an d in or der t o get t o t h e data it n eeds, a qu er y can follow var iou s pat h s t h at st ar t fr om an y O2 object or collect ion . For exam ple, select distinct ord.what from cl in Globe.clients, ord in cl.order where cl.name = "Haddock" You obt ain t h e set of wh at t h e clien t (s) called Haddock bou gh t : 24 ODM G OQL User Man u al Select ... from ... where : Testing on nil Testing on nil Aft er you r applicat ion h as u pdat ed t h e database, you m ay fin d th at som e object s ar e n ow equ al t o n il. You can t est for t h is u sin g OQL. For exam ple, you can t est th at a clien t exist s an d if so, wh ich clien t h as t h r ee or der s: select c.name from c in Globe.clients where c!=nil and count (c.order) = 3 To sim plify pr ogr am m in g, OQL sk ips n il object s wh en t h ey ar e en cou n t er ed. If a pat h expr ession con tain s a nil object , a pr edicat e is always con sider ed false. Th is m ean s t h at th e pr eviou s expr ession can be r ewr it t en as follows: select c.name from c in Globe.clients where count (c.order) = 3 List or array A list or an ar r ay is an or der ed collect ion t h at can con tain du plicate elem en t s. Sin ce it is or der ed, you m ay ext r act an y of it s elem en t s if you k n ow th eir position . For exam ple: Globe.clients[2] Th is ext r act s t h e t h ir d elem en t of t h e list (t h e fir st elem en t is at posit ion 0). As with sets you can filt er a list . ODM G OQL User Man u al 25 2 Getting Started For exam ple: wh at ar e th e n am es of t h e clien t s wh o bu y t h e In t er n at ion al Globe n ewspaper ? select e.name from e in Globe.clients Th e r esu lt of t h is qu er y is a bag of t h e name of Globe clien ts: Note Th e qu er y r et u r n s a bag an d n ot a list . To r et u r n a list , you m u st defin e an or der . See “Or der by” on page 35. You can also add t h e k eywor d distinct t o a selection t o elim in at e an y du plicat es fr om t h e r esu lt in g set . Note You can m an ipu lat e ver y com plex st r u ct u r es. A list can be m ade u p of t u ples wh ich in t u r n can h ave a set at tr ibu t e, et c. Con sequ en t ly, you h ave access t o all t h e em bedded com pon en t s of an object . For m or e det ails, r efer t o Sect ion 2.3 for con str u ctin g qu er y r esu lts an d Sect ion 2.7 for com bin in g oper at or s. 26 ODM G OQL User Man u al Constructing results 2.3 Constructing results Th e st r u ct u r e of a qu er y r esu lt is ver y oft en im plicit . For exam ple, wh en you ext r act t h e age field of an em ployee, wh ich is of t ype in t eger , you obt ain an in t eger . Wh en you filt er a set, bag or list , you obtain a set , bag or list depen din g on wh at you select . H owever , you can also con str u ct a qu er y r esu lt wit h an explicit st r u ct u r e u sin g t h e struct, set, bag, list an d array con st r u ct or s. For exam ple, u sin g th e str u ct con st r u ct or : select struct (employee: e.name, position: e.position, salary: e.salary) from e in Globe.employees or sim ply: select e.name, e.position, e.salary from e in Globe.employees Th is qu er y gives th e n am e, posit ion an d salar y of t h e em ployees at t h e In t er n at ion al Globe n ewspaper : ODM G OQL User Man u al 27 2 Getting Started You can u se th e special "*" oper ator t o select all at tr ibu t es of t h e elem en t s of a collect ion . For exam ple: select * from Globe.employees Not e t h at in t h is exam ple you do n ot n eed t o defin e a var iable wit h from. You can also bu ild u p em bedded str u ct u r es sim ply by com bin in g struct oper at or s. For exam ple, t o get th e iden t it ies an d salar ies of all t h ose em ployees wor k in g as r epor ter s an d older t h an 22. select struct (employee: struct (name: e.name, age: e.age), salary: e.salary) from e in Globe.employees where e.position = "Reporter" and e.age > 22 28 ODM G OQL User Man u al Constructing results : Creating an object Th is qu er y gives a bag wit h on e elem en t : Creating an object You cr eat e valu es u sin g struct, list, array, bag an d set. In OQL, you can also cr eat e object s u sin g t h e class n am e an d by in it ializin g th e at t r ibu t es of you r ch oice. An y u n -in it ialized at t r ibu t es ar e set t o th e defau lt valu e. For exam ple, t o cr eat e an object of t h e class Client: Client (name: "Trent") Th is cr eat es a t em por ar y object wit h th e n am e at tr ibu t e in it ialized t o Trent. You can th en m ak e t h e object per sist en t in t h e u su al way (r efer t o t h e O 2 C, C++ an d J ava m an u als). Th e r esu lt of t h is qu er y is t h e n ew object . An object collect ion can be cr eated in t h e sam e way. For exam ple, u se t h e followin g qu er y t o cr eat e an o2 _l i st _Cl i en t collect ion . o2_list_Client (list(Client(name:"John"), (Client(name:"Jack"))) ODM G OQL User Man u al 29 2 Getting Started 2.4 Operators Th is sect ion ou t lin es t h e basic OQL oper ator s you can u se t o qu er y t h e dat abase. Count You can qu er y t h e dat abase u sin g t h e count clau se. For exam ple, t o fin d ou t h ow m an y em ployees t h er e ar e at t h e In t er n at ion al Globe n ewspaper : count (Globe.employees) Th is qu er y r et u r n s an in t eger . Oth er aggr egat e oper at or s ar e min, max, sum an d avg. Define You can n am e t h e r esu lt of a qu er y u sin g t h e define clau se. For exam ple, define MyEmployees as select e from e in Globe.employees where e.name like "Sp*" Th is n am es t h e r esu lt of t h e qu er y an d n ot th e qu er y itself. Th e n am e MyEmployees can t h en be u sed in ot h er qu er ies. Nam ed qu er ies gr eat ly im pr ove t h e legibilit y of com plex qu er ies. 30 ODM G OQL User Man u al Operators : Element Note You can on ly r eu se t h ese n am ed qu er ies in t h e sam e qu er y session , i.e., u p t o a com m i t or abor t poin t. Element Wh en you h ave a set or a bag t h at con t ain s a sin gle elem en t , you ext r act th e elem en t dir ect ly u sin g t h e element oper at or . For exam ple, element ( select e from e in Globe.employees where e.name = "Tintin") Th is qu er y gives t h e r esu lt: Exists You can add a n ew per sisten t n am e to cover all t h e differ en t com pan ies t h at exist : name TheCompanies: list (Company); You can n ow car r y ou t m or e com plex qu er ies, su ch as select in g wh ich com pan y h as at least on e em ployee u n der t h e age of 23: select c.name from c in TheCompanies where exists e in c.employees: e.age < 23 ODM G OQL User Man u al 31 2 Getting Started Th e an swer is a bag of n am es: Group by Th is oper at or gr ou ps t oget h er object s of a collect ion with t h e sam e valu e for par t icu lar att r ibu t es. For exam ple, select * from e in Globe.employees group by e.salary Th is gr ou ps t h e employees by salar y givin g a bag of two-at t r ibu t e t u ples: 32 ODM G OQL User Man u al Operators : Group by Th e fir st at tr ibu t e is th e salar y an d is called salary as specified. Th e secon d is t h e set of object s (em ployees) wit h t h e sam e salar y an d is called partition. Th u s, t h e type of th e r esu lt of th is qu er y is: bag (struct (salary: real, partition: bag (struct (e:Employee)))) You can wor k on a par t it ion valu e by com pu t in g statistics on each par tit ion . Th e followin g qu er y r et u r n s a bag of t wo-att r ibu t e t u ples with t h e salar y an d t h e n u m ber of em ployees ear n in g each of t h ese salar ies: select salary, number: count (partition) from e in Globe.employees group by e.salary ODM G OQL User Man u al 33 2 Getting Started You get t h e followin g type of win dow: Fin ally you can filt er t h e r esu lt of gr ou pin g by applyin g pr edicat es on aggr egat ive oper at ion s. You can select gr ou ps wit h con dit ion s on aver age, cou n t , su m , m axim u m an d m in im u m valu es of par tit ion s. You do th is u sin g t h e having clau se. For exam ple, if you wish t o select on ly gr ou ps wit h m or e t h an on e salar y: select salary, number: count (partition) from e in Globe.employees group by e.salary having count (partition) > 1 Th e followin g scr een is displayed. 34 ODM G OQL User Man u al Operators Like Th e like oper at or allows you t o t est par t of a ch ar act er st r in g. Th e "*" ch ar act er stan ds for an y st r in g in clu din g t h e em pt y st r in g. Th e qu er y: select distinct e.salary from e in Globe.employees where e.name like "Sp*" r et u r n s t h e salar ies of all em ployees wh ose n am es begin wit h Sp: Order by You can obt ain a sor t ed list u sin g t h e order by clau se. For exam ple, t o sor t t h e em ployees by n am e an d by age: select e from e in Globe.employees order by e.name, e.age Th e r esu lt of an order by oper ation is always a l i st , even t h ou gh t h e sou r ce of th e object s to sor t (t h e set employees, in th is case) m ay be a set. Th is qu er y r et u r n s a list of em ployees; t h eir or der is alph abet ical by n am e, an d t h en by age: ODM G OQL User Man u al 35 2 Getting Started 2.5 Set operators Th e st an dar d set oper at ion s ar e defin ed on set an d bag: union, intersect (in t er section ) an d except (differ en ce). You can also wr it e th ese oper at or s as + (u n ion ), * (in t er section ) an d (differ en ce). You can defin e an ot h er qu er y YourEmployees: define YourEmployees as select e from e in Globe.employees where e.name = "Tintin" Now you can com bin e t h e qu er ies by addin g t ogeth er t wo set s: MyEmployees + YourEmployees Th e sim ple addit ion (u n ion ) of t h e two set s of em ployees gives you a set con tain in g t h e an swer : 36 ODM G OQL User Man u al Conversions Th e pick oper at or is defin ed on a set or a bag. It r et u r n s an elem en t of t h e collect ion , ch osen ar bit r ar ily. For exam ple: pick (MyEmployees) 2.6 Conversions List to set To con ver t a list or ar r ay t o a set you u se t h e listtoset oper at or . Exam ple: listtoset (Globe.clients) intersect listtoset (TheCompanies[2].clients) Set to list To con ver t a set or bag t o a list you m u st or der it. ODM G OQL User Man u al 37 2 Getting Started For exam ple: select e from e in the_employees order by e.salary r et u r n s a list sor t ed by salar y. You can also u se "*" t o bu ild a list . Th is avoids a r eal sor t algor it h m an d sh ou ld be u sed wh en th e fin al or der of t h e list is u n im por t an t . select e from e in the_employees order by * r et u r n s a list of all em ployees in r an dom or der . Flatten To con ver t a collection of collect ion s in t o a flat t en ed collect ion you u se t h e flatten oper at or . For exam ple: flatten (select distinct c.clients from c in TheCompanies) r et u r n s a set of clien t s. 2.7 Combining operators OQL is a com plet e fu n ct ion al lan gu age in t h at ever y oper at or can be com bin ed wit h an y ot h er oper at or . You can u se com bin e an d bu ild u p oper at or s, u n iver sal an d exist en t ial qu an t ifier s, wild-car d oper at or s, st an dar d set oper at or s as well as list con cat en ation , or der in g an d gr ou pin g oper at or s on set s, bags an d list s. 38 ODM G OQL User Man u al Indexes : Flatten For exam ple: select cl.name, paid: sum (select p.price from p in cl.order) from cl in Globe.clients where count (cl.order) >2 order by sum (select p.price from p in cl.order) Th is sor t s all t h e clien t s, wit h m or e t h an t wo or der s, by h ow m u ch th ey h ave paid t o t h e com pan y: 2.8 Indexes Wh en OQL ext r act s on e or m or e elem en ts fr om a collection u sin g a specified pr edicate or or der oper at ion , it m u st scan th e wh ole collect ion t o fin d t h e r equ ir ed elem en t s. You can im pr ove per for m an ce if th e syst em is able t o dir ect ly access t h e m atch in g elem en t s. Th is is don e by est ablish in g an in dex on a collection . An in dex m aps a k ey t o on e or m or e elem en ts of a n am ed collect ion . Wh en ever a pr ogr am sear ch es for elem en t s of t h e collect ion u sin g t h e k ey, t h e syst em u ses th e in dex t o qu ick en t h e sear ch . Th is en tir e pr ocess is t ot ally t r an spar en t t o you as t h e pr ogr am m er . Th e absen ce or pr esen ce of an in dex h as n o effect on pr ogr am code, on ly on syst em per for m an ce. ODM G OQL User Man u al 39 2 Getting Started Th e ben efit s of in dexes in clu de t h e followin g: • Com plet e logical an d ph ysical in depen den ce You do n ot h ave to ch an ge you r qu er y t o u se in dexin g. In dexes ar e cr eat ed by adm in istr at ion com m an ds. • High per for m an ce du r in g u se an d m ain t en an ce Access fr om an in dex m ean s con st an t t im e access ir r egar dless of t h e size of t h e collect ion . Exam ple: • Defin in g an in dex for all em ployees: create index the_employees on salary; • Th e followin g qu er y will th en be opt im ized: select e from e in the_employees where e.salary ≥ 1000 and e.salary ≤ 5000 Display index The "display index" query allows you to see how OQL will use existing indexes in queries you will make. To stop this feature, execute "display index" again. Note Please r efer t o t h e Syst em Adm in ist r at ion Gu ide for det ails on h ow t o cr eat e an d m an age in dexes. 40 ODM G OQL User Man u al Chapter Summary : Display index 2.9 Chapter Summary Th is ch apt er h as cover ed t h e followin g poin t s: • Basic queries To qu er y an y database you n eed var iou s en t r y poin t s. In O2 t h ese ar e t h e n am ed i n st an ces — i.e. n am ed object s an d n am ed valu es. Sim ple qu er ies in clu de: callin g an en tr y poin t , applyin g a m et h od t o a n am ed object , ext r act in g a field, et c. • Select..from..where Th e select ... from ... where clau se en ables you to ext r act t h ose elem en t s m eet in g a specific con dition fr om a list or set . • Constructing results Th e st r u ct u r e of a qu er y r esu lt is ver y oft en im plicit. However , you can also con st r u ct a qu er y r esu lt wit h an explicit st r u ct u r e u sin g t h e struct, set an d list con st r u ctor s. • Operators OQL oper ator s in clu de define, element, order by, count, exists, group by an d like. Th ey can be com bin ed for com plex qu er ies. • Indexes Wh en OQL ext r act s on e or m or e elem en ts fr om a set or list it scan s t h e wh ole collect ion t o fin d t h e desir ed elem en t s. You can im pr ove per for m an ce if you t ell th e syst em exact ly wh er e t o look .Th is is don e by establish in g an in dex on a collect ion . An in dex m aps a k ey t o on e or m or e elem en t s of a n am ed collect ion . ODM G OQL User Man u al 41 2 42 Getting Started ODM G OQL User Man u al OQL Rationale 3 3 Most com m er cial object dat abase syst em s n ow h ave a com m on data m odel based on t h e OM G object m odel. Th is data m odel is defin ed in t h e OD MG 93 r epor t . Based on t h is OD MG m odel, t h e qu er y lan gu age OQL was defin ed an d adopt ed by t h e ODM G gr ou p. Th is ch apt er is divided as follows: • Th e ODMG st an dar d • Th e ODMG m odel • OQL by exam ple ODM G OQL User Man u al 43 3 OQL Rationale 3.1 The ODMG standard Th e ODMG st an dar d cover s t h e followin g poin t s: 1. an object m odel 2. an object defin it ion lan gu age for th is m odel, wit h it s own syn t ax, OD L or its expr ession t h r ou gh C++ an d Sm allt alk syn t ax 3. an object qu er y lan gu age for t h is m odel, OQL 4. a C++ bin din g allowin g C++ pr ogr am s to oper at e on a database com plian t t o t h e object m odel 5. a J ava bin din g allowin g J ava pr ogr am s t o oper ate on a dat abase com plian t t o t h e object m odel 3.2 The ODMG model Th e ODMG object m odel su ppor t s t h e n ot ion of classes, of object s wit h at t r ibu t es an d m et h ods, of in h er it an ce an d specializat ion . It offer s t h e classical t ypes t o deal with st r in g, dat e, t im e, t im e in t er val an d t im est am p. An d fin ally, it su ppor t s t h e n ot ion s of r el at i on sh i p s an d col l ect i on s . ODMG-93 in t r odu ces a set of pr edefin ed gen er ic collection classes: Set<T>, Bag<T> (a m u lt i-set , i.e., a set wit h r epeat ed elem en t s), Varray<T> (a var iable size ar r ay), List<T> (a var iable size an d in ser t able ar r ay). An object r efer s t o an oth er object t h r ou gh a Ref. A Ref beh aves as a C++ poin t er , bu t wit h m or e sem an tics: it is a per sist en t poin t er bu t r efer en tial in t egr it y can be expr essed in t h e sch em a an d m ain t ain ed by t h e syst em . Th is is don e by declar in g t h e r elat ion sh ip as sym m et r ic. Com bin in g r elat ion sh ips an d collect ion s, an object can r elat e t o m or e t h an on e object t h r ou gh a r elat ion sh ip. Th er efor e, 1-1 r elat ion sh ips, 1-n r elat ion sh ips an d n -m r elat ion sh ips can be su ppor ted wit h t h e sam e gu ar an t ee of r efer en t ial in t egr it y. ODMG-93 en ables explicit n am es t o be given t o an y object or collect ion . Fr om a n am e, an applicat ion can dir ectly r et r ieve t h e n am ed object an d 44 ODM G OQL User Man u al The ODMG model t h en oper at e on it or n avigat e t o oth er object s followin g t h e r elation sh ip lin k s. Let u s n ow pr esen t t h e m odel t h r ou gh a com plet e exam ple. We u se h er e C++ syn t ax for ou r object defin it ion lan gu age, followin g t h e ODM G C++ ODL bin din g (i.e., th e way of defin in g an OD MG sch em a u sin g t h e st an dar d C++ lan gu age). ODM G OQL User Man u al 45 3 OQL Rationale class Person{ d_String name; d_Date d_Set birthdate; < d_Ref<Person> > parents inverse children; d_List < d_Ref<Person> > children inverse parents; d_Ref<Apartment> lives_in inverse is_used_by; Meth ods Person(); int age(); Con st r u ct or : a n ew Per son is bor n Retu r n s an atom ic t ype void marriage( d_Ref<Person> spouse); Th is per son gets a spou se void birth( d_Ref<Person> child); Th is per son get s a ch ild d_Set< d_Ref<Person> > ancestors;; Set of an cest or s of t h is Per son virtual d_Set<d_String> activities(); A r edefin able m et h od }; class Employee: Person{ A su bclass of Per son float salary; M et h od virtual d_Set<d_String> activities(); Th is m eth od is r edefin ed }; 46 ODM G OQL User Man u al The ODMG model class Student: Person{ A su bclass of Per son d_String grade; M et h od virtual d_Set<d_String> activities(); Th e m et h od is r edefin ed }; ODM G OQL User Man u al 47 3 OQL Rationale class Address{ int number; d_String street; }; class Building{ Address address; A com plex valu e Address em bedded in t h is object d_List< <d_Ref<Apartment> > apartments inverse building; M et h od d_Ref<Apartment> less_expensive(); }; class Apartment{ int number; d_Ref<Building> building; d_Ref<Person> is_used_by inverse lives_in; }; d_Set< d_Ref<Person> > Persons; All per son s an d em ployees d_Set< d_Ref<Apartment> > Apartments; Th e Apar t em en t class ext en t d_Set< d_Ref<Apartment> > Vacancy; Th e set of vacan t appar t em en t s d_List< d_Ref<Apartment> > Directory; Th e list of appar t em en t s or der ed by t h eir n u m ber of r oom s }; 48 ODM G OQL User Man u al OQL by example : Path expressions 3.3 OQL by example Let u s n ow t u r n t o an exam ple based pr esen t at ion of OQL. We u se t h e dat abase descr ibed in t h e pr eviou s sect ion , an d in st ead of t r yin g to be exh au st ive, we give an over view of t h e m ost r elevan t feat u r es. Path expressions As explain ed above, on e can en t er a dat abase t h r ou gh a n am ed object , bu t m or e gen er ally as soon as on e get s an object (wh ich com es, for in st an ce, fr om a C++ expr ession ), on e n eeds a way t o “n avigat e” fr om it an d r each t h e r igh t dat a on e n eeds. To do t h is in OQL, we u se t h e “.” (or in differ en t ly “->”) n ot at ion wh ich en ables u s t o go in side com plex object s, as well as t o follow sim ple r elation sh ips. For in st an ce, given a Per son p t o k n ow t h e n am e of t h e st r eet wh er e th is per son lives, we u se t h e followin g OQL qu er y: p.lives_in.building.adddress.street Th is qu er y st ar t s fr om a Person, tr aver ses an Apartment, ar r ives in a Building an d goes in side t h e com plex at t r ibu te of t ype Address to get t h e st r eet n am e. Th is exam ple t r eated 1-1 r elation sh ip, let u s n ow look at n -p r elat ion sh ips. Assu m e we wan t t h e n am es of t h e ch ildr en of t h e per son p. We can n ot wr it e: p.children.name becau se children is a List of r efer en ces, so t h e in t er pr etation of t h e r esu lt of t h is qu er y wou ld be u n defin ed. In t u it ively, t h e r esu lt sh ou ld be a collect ion of n am es, bu t we n eed an u n am bigu ou s n otation t o t r aver se su ch a m u lt iple r elat ion sh ip an d we u se t h e select-from-where clau se t o h an dle collect ion s ju st as in SQL. select c.name from c in p.children Th e r esu lt of t h is qu er y is a valu e of type Bag<St r in g>. If we wan t t o get a Set , we sim ply dr op du plicat es, lik e in SQL by u sin g t h e distinct k eywor d. select distinct c.name from c in p.children ODM G OQL User Man u al 49 3 OQL Rationale Now we h ave a m ean s t o n avigat e fr om an y object to an y ot h er object followin g an y r elation sh ip an d en t er in g an y com plex su bvalu es of an object . For in st an ce, we wan t th e set of addr esses of t h e children of each Person of t h e dat abase. We k n ow t h e collection n am ed Persons con tain s all t h e per son s of th e dat abase. We h ave n ow to t r aver se t wo collect ion s: Persons an d Person::children. Lik e in SQL, t h e selectfrom oper at or allows u s t o qu er y m or e t h an on e collection . Th ese collect ion s th en appear in t h e from par t . In OQL, a collect ion in t h e from par t can be der ived fr om a pr eviou s on e by followin g a pat h wh ich st ar t s fr om it , an d t h e an swer is: select c.lives_in.building.address from p in Persons, c in p.children Th is qu er y in spect s all ch ildr en of all per son s. It s r esu lt is of t h e t ype Bag<Addr ess>. • Predicate Of cou r se, t h e where clau se can be u sed t o defin e an y pr edicat e wh ich t h en ser ves t o select th e dat a m atch in g t h e pr edicate. For in st an ce, t o r est r ict t h e pr eviou s r esu lt t o t h e people livin g on Main St r eet , an d h avin g at least 2 ch ildr en wh o do n ot live in t h e sam e apar tm en t as t h eir par en ts, t h e qu er y is: select c.lives_in.building.address from p in Persons, c in p.children where p.lives_in.building.address.street = "Main Street" and count(p.children) >= 2 and c.lives_in != p.lives_in • Join In t h e from clau se, collect ion s wh ich ar e n ot dir ect ly r elat ed can also be declar ed. As in SQL, t h is allows u s t o com pu t e “join s” between t h ese collect ion s. For in stan ce, t o fin d t h e people livin g in a st r eet an d h avin g t h e sam e n am e as t h is st r eet , we do th e followin g: th e Building ext en t is n ot defin ed in th e sch em a, so we h ave t o com pu t e it fr om t h e Apartments ext en t . To com pu te t h is in t er m ediat e r esu lt , we n eed a select-from oper ator again . So t h e join is don e as follows: 50 ODM G OQL User Man u al OQL by example : Data manipulation select p from p in Persons, b in (select distinct a.building from a in Apartments) where p.name = b.address.street Th is qu er y h igh ligh t s t h e n eed for an opt im izer . In t h is case, t h e in n er select su bqu er y m u st be com pu t ed on ce an d n ot for each per son ! Data manipulation A m ajor differ en ce bet ween OQL an d SQL is t h at an object qu er y lan gu age m u st m an ipu lat e com plex valu es. OQL can t h er efor e cr eate an y com plex valu e as a fin al r esu lt , or in side t h e qu er y as in ter m ediate com pu tation . To bu ild a com plex valu e, OQL u ses t h e con st r u ct or s struct, set, bag, list an d array. For exam ple, t o obt ain t h e addr esses of t h e ch ildr en of each per son , alon g wit h th e addr ess of th is per son , we u se t h e followin g qu er y: select struct(me: p.name, my_address: p.lives_in.building.address, my_children: (select struct( name: c.name, address: c.lives_in.building.address) from c in p.children)) from p in Persons ODM G OQL User Man u al 51 3 OQL Rationale Th is gives, for each per son , t h e n am e, t h e addr ess, an d t h e n am e an d addr ess of each ch ild. Th e t ype of t h e r esu lt is a bag of t h e followin g st r u ct : struct{ String me; Address my_address; Bag<struct{String name; Address address}> my_children; } OQL can also cr eat e com plex object s. For th is pu r pose, it u ses t h e n am e of a class as a con st r u ct or . At t r ibu tes of t h e object of t h is class can be in itialized explicit ly by an y valid expr ession . For in st an ce, t o cr eat e a n ew bu ildin g wit h 2 apar t m en t s, if t h er e is a t ype n am e in t h e sch em a, called List_apart, defin ed by: tydedef List<<Ref<Apartment> > List_apart; t h e qu er y is: Building( address: Address (number: 10, street: "Main street"), apartments: List_apart(list(Apartment(number: 1), Apartment(number: 2)))) Method invoking OQL allows m et h od calls wit h or wit h ou t par am et er s an ywh er e t h e r esu lt t ype of t h e m et h od m at ch es th e expect ed t ype in t h e qu er y. In case th e m et h od h as n o par am eter , t h e syn t ax for m eth od call is t h e sam e as for accessin g an at t r ibu te or t r aver sin g a r elat ion sh ip. If t h e m et h od h as par am eter s, t h ese ar e given bet ween par en t h esis. Th is flexible syn t ax fr ees t h e u ser fr om k n owin g wh eth er t h e pr oper t y is 52 ODM G OQL User Man u al OQL by example : Polymorphism st or ed (an at t r ibu t e) or com pu t ed (a m et h od). For in st an ce, t o get t h e age of t h e oldest ch ild of “Paul”, we wr it e t h e followin g qu er y: select max(select c.age from c in p.children) from p in Persons, where p.name = "Paul" Of cou r se, a m et h od can r etu r n a com plex object or a collect ion an d t h en its call can be em bedded in a com plex pat h expr ession . For in st an ce, in side a bu ildin g b, t o k n ow wh o in h abits t h ose least expen sive apar tm en t, we u se th e followin g pat h expr ession : b.less_expensive.is_used_by.name Alt h ou gh less_expensive is a m et h od we “t r aver se” it as if it wer e a r elat ion sh ip. Polymorphism A m ajor con t r ibu t ion of object tech n ology is th e possibilit y of m an ipu lat in g polym or ph ic collect ion s, an d t h an k s t o t h e “lat e bin din g” m ech an ism , t o car r y ou t gen er ic act ion s on th e elem en t s of t h ese collection s. For in st an ce, t h e set Persons con t ain s object s of class Person, Employee an d Student. So far , all th e qu er ies again st t h e Persons exten t dealt wit h th e t h r ee possible classes of objects of t h e collection . A qu er y is an expr ession wh ose oper ator s oper at e on t yped oper an ds. It is cor r ect if t h e type of oper an ds m at ch es t h ose r equ ir ed by t h e oper at or s. In th is sen se, OQL is a t yped qu er y lan gu age. Th is is a n ecessar y con dit ion for an efficien t qu er y optim izer . Wh en a polym or ph ic collect ion is filter ed (for in stan ce Persons), it s elem en t s ar e st at ically k n own t o be of th at class (for in stan ce Person). Th is m ean s t h at a pr oper t y of a su bclass (att r ibu t e or m et h od) can n ot be applied to su ch an elem en t , except in t wo im por tan t cases: lat e bin din g t o a m et h od, or explicit class in dicat ion . • Late binding To list th e act ivit ies of each per son , we u se t h e followin g qu er y: select p.activities from p in Persons ODM G OQL User Man u al 53 3 OQL Rationale activities is a m et h od wh ich h as 3 in car n at ion s, on e for Student, on e for Employee an d on e for gen er ic Person. Depen din g on t h e k in d of per son of t h e cu r r en t p, th e r igh t in car n ation is called. • Class indicator To go down t h e class h ier ar ch y, a u ser m ay explicit ly declar e th e class of an object t h at can n ot be in fer r ed st at ically. Th e in ter pr et er t h en h as to ch eck at r u n t im e, t h at t h is object act u ally belon gs t o t h e in dicated class (or on e of it s su bclasses). For exam ple, assu m in g we k n ow t h at on ly “st u den ts” spen d t h eir t im e in followin g a cou r se of st u dy, we can select t h ose per son s an d get t h eir gr ade. We explicit ly in dicat e in th e qu er y t h at t h ese per son s ar e st u den t s: select ((Student)p). grade from p in Persons where "course of study" in p.activities Operator composition OQL is a pu r ely fu n ct ion al lan gu age: all oper at or s can be com posed fr eely as lon g as t h e t ype syst em is r espect ed. Th is is wh y t h e lan gu age is so sim ple an d it s m an u al so sh or t . Th is ph ilosoph y is differ en t fr om SQL, wh ich is an ad-h oc lan gu age wh ose com posit ion r u les ar e n ot or t h ogon al t o t h e type syst em . Adopt in g a com plet e or th ogon alit y, m ak es t h e lan gu age easier t o lear n wit h ou t losin g t h e SQL st yle for sim ple qu er ies. Am on g t h e oper at or s offer ed by OQL bu t n ot yet in t r odu ced, we can m en t ion t h e set oper at or s (union, intersect, except), t h e u n iver sal (forall) an d exist en t ial qu an t ifier s (exists), t h e order by an d group by oper at or s an d t h e aggr egat ive oper at or s (count, sum, min, max an d avg). To illu st r at e t h is fr ee com posit ion of oper at or s, let u s wr it e a r at h er elabor at e qu er y. We wan t t o k n ow th e n am e of t h e str eet wh er e t h e set of em ployees livin g on t h at st r eet an d h ave t h e sm allest aver age salar y, com par ed t o t h e sets of em ployees livin g in oth er st r eet s. We pr oceed st ep by st ep an d u se t h e define OQL in str u ction t o evalu at e t em por ar y r esu lts. 54 ODM G OQL User Man u al OQL by example : Operator composition 1. Bu ild t h e ext en t of class Employee (n ot su ppor t ed dir ect ly by t h e sch em a) define Employees as select (Employee) p from p in Persons where "has a job" in p.activities 2. Gr ou p th e em ployees by st r eet an d com pu t e t h e aver age salar y in each st r eet 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 Th e group by oper at or split s t h e em ployees in to par t ition s, accor din g t o t h e cr it er ion (t h e n am e of t h e st r eet wh er e th is per son lives). Th e select clau se com pu tes, in each par t it ion , th e aver age of t h e salar ies of t h e em ployees belon gin g t o t h is par tit ion . Th e r esu lt of t h e qu er y is of t ype: Bag<struct{String street; float average_salary;}> 3. Sor t th is set by salar y define sorted_salary_map as select s from s in salary_map order by s.average_salary ODM G OQL User Man u al 55 3 OQL Rationale Th e r esu lt is of t ype: List<struct{String street; float average_salary;}> 4. Now get t h e sm allest salar y (t h e fir st in th e list) an d t ak e t h e cor r espon din g st r eet n am e. Th is is t h e fin al r esu lt . sorted_salary_map[0].street In a sin gle qu er y, we cou ld h ave wr it ten : (select street, average_salary: avg (select p.e.salary from partition p) from e in (select (Employee) p from p in Persons where "has a job" in p.activities) group by e.lives_in.building.address.street order by avg (select p.e.salary from partition p)) [0]. street 56 ODM G OQL User Man u al 4 O QL Re fer ence 4 Th is ch apt er gives t h e fu ll r efer en cial in for m at ion of t h e object qu er y lan gu age OQL. Th e ch apt er is divided in t o t h e followin g sect ion s: • In t r odu ct ion • Pr in ciples • Lan gu age D efin it ion • Syn t act ical Abbr eviation s • OQL BNF Th e in for m ation given below is th e sam e as th at of th e OD MG stan dar d 1 wit h n ot es added on h ow t o u se th is lan gu age wit h O2 . 1. Th e Object Dat abase St an dar d: ODMG - 93. At wood, Du h l, Ferr an , Loom is an d Wade. Edit ed by R.G.G. Catt ell. © 1996 M organ Kau fm an Pu blish ers. ODM G OQL User Man u al 57 4 4.1 Introduction In t h is ch apt er , a for m al an d com plet e defin it ion of t h e lan gu age is given . For each featu r e of t h e lan gu age, we give t h e syn t ax, it s sem an t ics, an d an exam ple. Alt er n at e syn t ax for som e feat u r es ar e descr ibed in Sect ion 4.4, wh ich com plet es OQL in or der t o accept an y syn t act ical for m of SQL. Th e ch apter en ds with t h e for m al syn t ax wh ich is given in Sect ion 4.5 4.2 Principles Ou r design is based on t h e followin g pr in ciples an d assu m pt ion s: • OQL r elies on t h e ODMG object m odel. • OQL is a su per set of t h e st an dar d SQL par t wh ich allows you t o qu er y a dat abase. Th u s, an y select SQL sen t en ce wh ich r u n s on r elat ion al t ables, wor k s with t h e sam e syn t ax an d sem an t ics on collect ion s of ODMG object s. Ext en sion s con cer n Object Or ien ted n ot ion s, lik e com plex object s, object iden t it y, pat h expr ession , polym or ph ism , oper at ion in vocat ion , lat e bin din g et c... • OQL pr ovides h igh -level pr im it ives t o deal wit h set s of object s bu t does n ot r est r ict it s at ten tion t o t h is collection con st r u ct . Th u s, it also pr ovides pr im it ives t o deal wit h str u ct u r es, list s, ar r ays, an d t r eat s all su ch con st r u ct s wit h t h e sam e efficien cy. • OQL is a fu n ct ion al lan gu age wh er e oper at or s can fr eely be com posed, as soon as t h e oper an ds r espect th e t ype syst em . Th is is a con sequ en ce of t h e fact t h at t h e r esu lt of an y qu er y h as a t ype wh ich belon gs t o t h e ODMG t ype m odel, an d t h u s can be qu er ied again . • OQL is n ot com pu t at ion ally com plet e. It is an easy to u se qu er y lan gu age wh ich pr ovides easy access t o an object database. • Based on t h e sam e t ype syst em , OQL can be in vok ed dir ect ly fr om wit h in pr ogr am m in g lan gu ages for wh ich an OD MG bin din g is defin ed, e.g., C++. Con ver sely, OQL can in vok e oper at ion s pr ogr am m ed in t h ese lan gu ages. • OQL does n ot pr ovide explicit u pdate oper at or s bu t r at h er can in vok e oper at ion s defin ed on objects for th at pu r pose, an d th u s does n ot br each t h e sem an t ics of an Object D at abase wh ich , by defin ition , is m an aged by t h e "m et h ods" defin ed on t h e objects. • OQL pr ovides declar at ive access to object s. Th u s OQL qu er ies can be easily opt im ized by vir t u e of t h is declar at ive n atu r e. • Th e for m al sem an t ics of OQL can easily be defin ed. 58 ODM G OQL User Man u al Language Definition : Query Program 4.3 Language Definition OQL is an "expr ession " lan gu age. A qu er y expr ession is bu ilt fr om t yped oper an ds com posed r ecu r sively by oper ator s. We will u se th e t er m expression t o design at e a valid qu er y in t h is sect ion . 4.3.1 Query Program A qu er y pr ogr am con sist s of a (possibly em pt y) set of qu er y defin it ion expr ession s followed by an expr ession , wh ich is evalu at ed as t h e qu er y it self. Th e set of qu er y defin it ion expr ession s is n on r ecu r sive (alt h ou gh a qu er y m ay call an oper ation wh ich issu es a qu er y r ecu r sively). For exam ple: define jones as select distinct x from Students x where x.name = "Jones"; select distinct student_id from jones Th is defin es th e set jones of st u den t s n am ed J on es, an d evalu ates t h e set of t h eir student_ids. O2 note Wit h t h e O2 qu er y in t er pr et er you u se CTRL -D (on Un ix) or CTRL-Z (On Win dows) bet ween t wo qu er ies r at h er t h at ";". 4.3.2 Named Query Definition If q is an iden t ifier an d e is a qu er y expr ession , t h en define q as e is a qu er y defin it ion expr ession wh ich defin es t h e qu er y wit h n am e q. Exam ple: define Does as select x from Student x where x.name ="Doe" ODM G OQL User Man u al 59 4 Th is st at em en t defin es Does as a qu er y r et u r n in g a bag con tain in g all t h e st u den t s wh ose n am e is Doe. define Doe as element(select x from Student x where x.name="Doe") Th is st at em en t defin es Doe as a qu er y wh ich r et u r n s t h e st u den t wh ose n am e is D oe (if t h er e is on ly on e, ot h er wise an except ion is r aised). O2 note • def i n e oper at ion is available on ly wit h t h e in ter act ive qu er y in t er pr et er . It h as n o m ean in g for OQL em bedded in pr ogr am m in g lan gu ages (C++, Sm allt alk , O 2 C) becau se st an dar d pr ogr am m in g lan gu age var iables can be u sed for t h at pu r pose. • A defin ed n am e is valid u p t o t h e n ext com m it or abor t • You can get t h e list of cu r r en t defin ed qu er ies by t ypin g t h e qu er y: d i spl ay qu er i es 4.3.3 Elementary Expressions 4.3.3.1 Atomic Literals If l is an at om ic lit er al, t h en l is an expr ession wh ose valu e is t h e lit er al it self. Lit er als h ave t h e u su al syn tax: • Object Lit er al: n il • Boolean Liter al: false, tr u e • In t eger Lit er al: sequ en ce of digit s, e.g, 27 • Float Lit er al: m an t issa/ expon en t . Th e expon en t is opt ion al, e.g., 3.14 or 314.16e-2 • Ch ar act er Lit er al: ch ar act er bet ween sim ple qu ot es, e.g., ’z’ • St r in g Lit er al: ch ar act er st r in g bet ween dou ble qu ot e, e.g.,"a st r in g" 4.3.3.2 Named Objects If e is a n am ed object , t h en e is an expr ession . It defin es th e en t it y at t ach ed t o th e n am e. 60 ODM G OQL User Man u al Language Definition : Construction Expressions Exam ple: Students Th is qu er y defin es t h e set of st u den t s. We h ave assu m ed h er e t h at t h e n am e Students exist s wh ich cor r espon ds t o t h e ext en t of objects of t h e class Student. 4.3.3.3 Iterator Variable If x is a var iable declar ed in a fr om par t of a select -fr om -wh er e..., t h en x is an expr ession wh ose valu e is t h e cu r r en t elem en t of t h e it er at ion over t h e cor r espon din g collect ion . 4.3.3.4 Named Query If define q as e is a qu er y defin it ion expr ession , t h en q is an expr ession . Exam ple: Doe Th is qu er y r et u r n s t h e st u den t wit h n am e Doe. It r efer s t o t h e qu er y defin ition expr ession declar ed in Sect ion 4.3.2. 4.3.4 Construction Expressions 4.3.4.1 Constructing Objects If t is a t ype n am e, p1, p2, ...,pn ar e pr oper t ies of t, an d e1, e2, ...,en ar e expr ession s, th en t ( p1: e1..., pn: en ) is an expr ession . Th is defin es a n ew object of t ype t wh ose pr oper t ies p1, p2, ...,pn ar e in it ialized wit h t h e expr ession s e1, e2, ...,en. Th e t ype of ei m u st be com pat ible wit h t h e t ype of pi. If t is a t ype n am e of a collection an d e is a collect ion lit er al, t h en t(e) is a collection object . Th e t ype of e m u st be com pat ible wit h t. Exam ples: Employee (name: "Peter", boss: Chairman) Th is cr eat es a m u t able Em ployee object . ODM G OQL User Man u al 61 4 vectint (set(1,3,10)) Th is cr eates a m u t able set object (assu m in g t h at vect i n t is t h e n am e of a class wh ose type is Bag<in t>). 4.3.4.2 Constructing Structures If p1, p2, ...,pn ar e pr oper t y n am es, an d e1, e2, ..., en ar e expr ession s, t h en struct (p1: e1, p2: e2, ..., pn: en) is an expr ession . It defin es t h e st r u ct u r e t ak in g valu es e1, e2, ..., en on pr oper ties p1, p2, ...,pn. Not e t h at t h is dyn am ically cr eates an in stan ce of t h e t ype struct(p1: t1, p2: t2, ..., pn: tn) if ti is t h e t ype of ei. Exam ple: struct(name: "Peter", age: 25); Th is r et u r n s a st r u ct u r e with t wo at t r ibu t es name an d age t ak in g r espect ive valu es Peter an d 25. See also abbr eviat ed syn tax in som e con t ext s, in Sect ion 4.4.1. 4.3.4.3 Constructing Sets If e1, e2, ..., en ar e expr ession s, t h en set(e1, e2, ..., en) is an expr ession . It defin es t h e set con tain in g t h e elem en t s e1, e2, ..., en. It cr eat es a set in st an ce. Exam ple: set(1,2,3) Th is r et u r n s a set con sist in g of t h e t h r ee elem en t s 1, 2, an d 3. 4.3.4.4 Constructing Lists If e1, e2, ..., en ar e expr ession s, t h en list(e1, e2, ..., en) or sim ply (e1, e2, ..., en) ar e expr ession s. Th ey defin e th e list h avin g elem en t s e1, e2, ..., en. Th ey cr eat e a list in st an ce. If m in , m ax ar e t wo expr ession s of in t eger or ch ar act er t ypes, su ch t h at m in < m ax, t h en 62 ODM G OQL User Man u al Language Definition : Construction Expressions list(min .. max) or sim ply (min .. max) ar e expr ession s wh ose valu e is: list (m in , m in +1, ... m ax-1, m ax) Exam ple: list(1,2,2,3) Th is r et u r n s a list of fou r elem en t s. Exam ple: list(3 .. 5) Th is r et u r n s t h e list (3,4,5) O2 note In O2 t h e k eywor d l i st is m an dat or y. 4.3.4.5 Constructing Bags If e1, e2, ..., en ar e expr ession s, t h en bag(e1, e2, ..., en) is an expr ession . It defin es t h e bag h avin g elem en t s e1, e2, ..., en. It cr eat es a bag in st an ce. Exam ple: bag(1,1,2,3,3) Th is r et u r n s a bag of five elem en t s. 4.3.4.6 Constructing Arrays If e1, e2, ..., en ar e expr ession s, t h en array(e1, e2, ..., en) is an expr ession . It defin es an ar r ay h avin g elem en t s e1, e2, ..., en. It cr eat es an ar r ay in st an ce. Exam ple: array(3,4,2,1,1) Th is r et u r n s an ar r ay of five elem en t s. ODM G OQL User Man u al 63 4 4.3.5 Atomic Types Expressions 4.3.5.1 Unary Expressions If e is an expr ession an d <op> is a u n ar y oper at ion valid for th e t ype of e, t h en <op> e is an expr ession . It defin es t h e r esu lt of applyin g <op> to e. Ar it h m et ic u n ar y oper at or s ar e: +, -, abs Boolean u n ar y oper at or is: n ot. Exam ple: not true Th is r et u r n s false. 4.3.5.2 Binary Expressions If e1 an d e2 ar e expr ession s an d <op> is a bin ar y oper at ion , t h en e1<op>e2 is an expr ession . It defin es th e r esu lt of applyin g <op> t o e1 an d e2. Ar it h m et ic in t eger bin ar y oper at or s ar e: +, -, *, / , m od (m odu lo) Float in g poin t bin ar y oper at or s ar e: +, -, *, / Relat ion al bin ar y oper at or s ar e: =, !=. <. <=, >, >= Th ese oper at or s ar e defin ed on all at om ic t ypes. Boolean bin ar y oper at or s ar e: an d, or Exam ple: count(Students) - count(TA) Th is r et u r n s t h e differ en ce bet ween t h e n u m ber of st u den t s an d th e n u m ber of TAs. 4.3.5.3 String Expressions If s1 an d s2 ar e expr ession s of type st r in g, th en s1 || s2, an d s1 + s2 ar e equ ivalen t expr ession s of type st r in g wh ose valu e is t h e con cat en ation of t h e t wo st r in gs. 64 ODM G OQL User Man u al Language Definition : Atomic Types Expressions O2 note In O2 t h e oper ator | | is n ot accept ed. To con cat en at e 2 st r in gs u se "+". If c is an expr ession of type ch ar act er , an d s an expr ession of t ype st r in g, t h en c in s is an expr ession of t ype boolean wh ose valu e is t r u e if t h e ch ar act er belon gs to t h e st r in g, else false. If s is an expr ession of type st r in g, an d i is an expr ession of t ype in t eger , t h en s[i] is an expr ession of t ype ch ar act er wh ose valu e is t h e i+1t h ch ar act er of t h e st r in g. If s is an expr ession of type st r in g, an d low an d up ar e expr ession s of t ype in t eger , t h en s[low:up] is an expr ession of t ype st r in g wh ose valu e is t h e su bst r in g of s fr om th e low+1 t h ch ar act er u p t o t h e u p+1 t h ch ar acter . If s is an expr ession of type st r in g, an d pattern a st r in g lit er al wh ich m ay in clu de t h e wildcar d ch ar act er s: "?" or "_", m ean in g an y ch ar act er , an d "*" or "%", m ean in g an y su bst r in g in clu din g an em pt y su bst r in g, t h en s like pattern is an expr ession of t ype boolean wh ose valu e is t r u e if s m at ch es t h e pat t er n , else false. Exam ple: ’a nice string’ like ’%nice%str_ng’ is t r u e. ODM G OQL User Man u al 65 4 O2 note In O2 t h e on ly su ppor ted wildcar d is "*". 4.3.6 Object Expressions 4.3.6.1 Comparison of Mutable Objects If e1 an d e2 ar e expr ession s wh ich den ot e m u t able object s (objects wit h iden tit y) of t h e sam e t ype, t h en e 1 = e2 an d e1 != e2 ar e expr ession s wh ich r et u r n a boolean . Th e secon d expr ession is equ ivalen t t o not(e1 = e2). e1 = e2 is t r u e if th ey design at e t h e sam e object . Exam ple: Doe = element(select s from Students s where s.name = "Doe") is tr u e. 4.3.6.2 Comparison of Immutable Objects If e1 an d e2 ar e expr ession s wh ich den ot e im m u t able object s (lit er als) of t h e sam e t ype, t h en e1 = e2 an d e1 != e2 ar e expr ession s wh ich r et u r n a boolean . th e secon d expr ession is equ ivalen t t o not(e1 = e2). e1 = e2 is t r u e if th e valu e e1 is equ al t o t h e valu e e2. 4.3.6.3 Extracting an Attribute or Traversing a Relationship from an Object If e is an expr ession , if p is a pr oper t y n am e, t h en e->p an d e.p ar e expr ession s. Th ese ar e alter n at e syn t ax t o extr act t h e pr oper t y p of an object e. If e h appen s t o design at e a delet ed or a n on exist in g object , i.e. nil, an at t em pt t o access t h e at t r ibu t e or t o t r aver se t h e r elat ion sh ip r aises an except ion . However , a qu er y m ay t est explicit ly if an object is differ en t fr om nil befor e accessin g a pr oper t y. 66 ODM G OQL User Man u al Language Definition : Object Expressions Exam ple: Doe.name Th is r et u r n s Doe. Exam ple: Doe->spouse != nil and Doe->spouse->name = "Carol" Th is r et u r n s true, if Doe h as a spou se wh ose n am e is Car ol, or else false. O2 note Accor din g to a r ecen t evolu t ion of t h e ODM G st an dar d, OQL does n ot n ow r aise an except ion wh en it t r aver ses a pat h wh ich con t ain s a nil. In st ead of t h is, a pr edicat e in volvin g su ch a pat h is always false. Th is m ean s th at OQL n ow sk ips su ch elem en t s an d th u s t h e explicit test t o nil is n ot yet m an dat or y. 4.3.6.4 Applying an Operation to an Object If e is an expr ession , if f is an oper at ion n am e, t h en e->f an d e.f ar e expr ession s. Th ese ar e alt er n ate syn t ax t o apply on oper at ion on an object . Th e valu e of t h e expr ession is t h e on e r et u r n ed by t h e oper ation or else t h e object nil, if t h e oper at ion r et u r n s n ot h in g. Exam ple: jones->number_of_students Th is applies th e oper at ion number_of_students to jones. 4.3.6.5 Applying an Operation with Parameters to an Object If e is an expr ession , if e1, e2 , ..., en ar e expr ession s, if f is an oper ation n am e, t h en e->f(e1, e2, ..., en) an d e.f(e1, e2, ..., en) ar e expr ession s t h at apply oper at ion f wit h par am et er s e1, e2, ..., en to object e. Th e valu e of t h e expr ession is t h e on e r et u r n ed by th e oper ation or else t h e object n il, if t h e oper at ion r et u r n s n ot h in g. In both cases, if e h appen s t o design at e a deleted or a n on exist in g object , i.e. nil, an at t em pt to apply an oper at ion t o it r aises an except ion . ODM G OQL User Man u al 67 4 H owever , a qu er y m ay t est explicit ly if an object is differ en t fr om nil befor e applyin g an oper at ion . Exam ple: Doe->apply_course("Maths", Turing)->number Th is qu er y calls t h e oper ation apply_course on class St u den t for t h e object D oe. It passes t wo par am et er s, a st r in g an d an object of class Pr ofessor . Th e oper at ion r et u r n s an object of t ype Cou r se an d t h e qu er y r et u r n s t h e n u m ber of t h is cou r se. 4.3.6.6 Dereferencing an Object If e is an expr ession wh ich den ot es an object wit h iden t ity (a m u t able object ), t h en *e is an expr ession wh ich deliver s t h e valu e of t h e object (a lit er al). Exam ple: Given t wo var iables of t ype Per son , p1 an d p2, th e pr edicat e p1 = p2 is tr u e if bot h var iables r efer t o t h e sam e object , wh ile *p1 =*p2 is t r u e if t h e objects h ave th e sam e valu es, even if t h ey ar e n ot th e sam e object s. 4.3.7 Collections Expressions 4.3.7.1 Universal Quantification If x is a var iable n am e, e1 an d e2 ar e expr ession s, e1 den ot es a collect ion an d e2 a pr edicat e, t h en for all x in e1: e2 is an expr ession . It r et u r n s true if all t h e elem en t s of collect ion e1 sat isfy e2 an d false ot h er wise. Exam ple: for all x in Students: x.student_id > 0 Th is r et u r n s true if all t h e object s in t h e Students set h ave a posit ive valu e for t h eir student_id att r ibu t e. Ot h er wise it r et u r n s false. 68 ODM G OQL User Man u al Language Definition : Collections Expressions 4.3.7.2 Existential Quantification If x is a var iable n am e, if e1 an d e2 ar e expr ession s, e1 den ot es a collection an d e2 a pr edicate, t h en exists x in e1: e2 is an expr ession . It r et u r n s true if t h er e is at least on e elem en t of collection e1 t h at sat isfies e2 an d false ot h er wise. Exam ple: exists x in Doe.takes: x.taught_by.name = "Turing" Th is r et u r n s true if at least on e cou r se Doe t ak es is t au gh t by som eon e n am ed Tu r in g. If e is a collect ion expr ession , t h en exists(e) an d unique(e) ar e expr ession s wh ich r et u r n a boolean valu e. Th e fir st on e r et u r n s t r u e if t h er e exist s at least on e elem en t in t h e collect ion , wh ile th e secon d on e r et u r n s t r u e, if t h er e exists on ly on e elem en t in t h e collect ion . Not ice t h at t h ese oper at or s allow th e accept an ce of t h e SQL syn t ax for n est ed qu er ies su ch as: select ... from col where exists ( select ... from col1 where predicate) Th e n est ed qu er y r et u r n s a bag to wh ich t h e oper at or exist s is applied. Th is is of cou r se t h e t ask of an opt im izer t o r ecogn ize t h at it is u seless to com pu te effect ively t h e in t er m ediat e bag r esu lt . O2 note In O 2 t h ese t wo last oper at ion s ar e n ot su ppor ted. On ly t h e for m "ex i st s x i n e1 : e2 " is valid. 4.3.7.3 Membership Testing If e1 an d e2 ar e expr ession s, e2 is a collect ion , e1 h as th e t ype of it s elem en t s, th en e1 in e2 is an expr ession . It r et u r n s true if elem en t e1 belon gs to collect ion e2. Exam ple: Doe in Does Th is r et u r n s true. ODM G OQL User Man u al 69 4 4.3.7.4 Aggregate Operators If e is an expr ession wh ich den ot es a collection , if <op> is an oper at or fr om {min, max, count, sum, avg}, t h en <op>(e) is an expr ession . Exam ple: max (select salary from Professors) Th is r et u r n s t h e m axim u m salar y of t h e Pr ofessor s. 4.3.8 Select From Where If e1, e2, ..., en ar e expr ession s wh ich den ot e collect ion s, an d x1, x2, ..., xn ar e var iable n am es, if e’ is an expr ession of t ype boolean , an d if projection is an expr ession or t h e ch ar act er *, t h en select projection from e1 as x1, e2 as x2 .., en as xn where e’ an d select distinct projection from e1 as x1, e2 as x2 ..., en xn where e as ar e expr ession s. Th e r esu lt of t h e qu er y is a set for a select distinct or a bag for a select. If you assu m e e1, e2, ..., en ar e all set an d bag expr ession s, t h en t h e r esu lt is obtain ed as follows: t ak e t h e car t esian pr odu ct 1 of th e set s e1, e2, ..., en; filt er t h at pr odu ct by expr ession e’ (i.e., elim in at e fr om t h e r esu lt all object s th at do n ot sat isfy boolean expr ession e’); apply t h e projection t o each on e of t h e elem en t s of t h is filt er ed set an d get t h e r esu lt. Wh en t h e r esu lt is a set (dist in ct case) du plicat es ar e au tom at ically elim in at ed. Th e situ at ion wh er e on e or m or e of t h e collect ion s e1, e2, ..., en is an in dexed collect ion is a lit t le m or e com plex. Th e select oper at or fir st con ver t s all th ese collect ion s in to set s an d applies t h e pr eviou s oper at ion . Th e r esu lt is a set (dist in ct case) or else a bag. So, in t h is case, we sim ply t r an sfor m each of t h e ei’s in t o a set an d apply t h e pr eviou s defin it ion . 4.3.8.1 Projection Befor e t h e pr oject ion , t h e r esu lt of t h e it er at ion over t h e from var iables is of t ype 1. Th e car tesian pr odu ct bet ween a set an d a bag is defin ed by fir st con vert in g t h e set in t o a bag, an d t h en get tin g t h e resu ltin g bag wh ich is t h e cart esian pr odu ct of th e t wo bags. 70 ODM G OQL User Man u al Language Definition : Select From Where bag< struct(x1: type_of(e1 elements), ... xn: type_of(en elements)) > Th e pr oject ion is con st r u ct ed by an expr ession wh ich can th en r efer im plicit ly t o th e "cu r r en t " elem en t of th is bag, u sin g t h e var iables xi. If for ei n eit h er explicit n or im plicit var iable is declar ed, t h en xi is given an in t er n al syst em n am e (wh ich is n ot accessible by t h e qu er y an yway). By con ven t ion , if t h e pr oject ion is sim ply "*", t h en t h e r esu lt of t h e select ion is t h e sam e as t h e r esu lt of th e it er at ion . If t h e pr oject ion is "dist in ct *", t h e r esu lt of t h e select is t h is bag con ver t ed in t o a set . In all ot h er cases, t h e pr oject ion is explicitly com pu t ed by t h e given expr ession . Exam ple: select couple(student: x.name, professor: z.name) from Students as x, x.takes as y, y.taught_by as z where z.rank = "full professor" Th is r et u r n s a bag of object s of t ype couple givin g st u den t n am es an d t h e n am es of t h e fu ll pr ofessor s fr om wh ich t h ey t ak e classes. Exam ple: select * from Students as x, x.takes as y, y.taught_by as z where z.rank = "full professor"select * Th is r et u r n s a bag of st r u ct u r es, givin g for each st u den t "object ", t h e sect ion object followed by t h e st u den t an d t h e fu ll pr ofessor "object " t each in g in t h is sect ion : bag< struct(x: Student, y: Section, z: Professor) > 4.3.8.2 Iterator Variables A var iable, xi, declar ed in t h e from par t r an ges over th e collect ion ei an d t h u s h as t h e t ype of t h e elem en t s of t h is collect ion . Su ch a var iable can be u sed in an y oth er par t of t h e qu er y t o evalu at e an y oth er expr ession s (see t h e Scope Ru les in Section 4.3.15). Syn t actical var iat ion s ar e ODM G OQL User Man u al 71 4 accept ed for declar in g t h ese var iables, exact ly as with SQL. Th e as k eywor d m ay be om it t ed. Mor eover , t h e var iable it self can be om it t ed, an d in t h is case, t h e n am e of t h e collect ion it self ser ves as a var iable n am e t o r an ge over it. Exam ple: select couple(student: Students.name, professor: z.name) from Students, Students.takes y, y.taught_by z where z.rank = "full professor" O2 note In O2 an addit ion al syn t ax is allowed t o declar e a var iable x: "... f r om x i n collect ion ...". Th is syn t ax will also be in clu ded in t h e n ext r elease of t h e ODM G stan dar d. 4.3.8.3 Predicate In a select -fr om -wh er e qu er y, t h e w here clau se can be om it t ed, wit h t h e m ean in g of a t r u e pr edicat e. 4.3.9 Group-by Operator If select_query is a select -fr om -wh er e qu er y, partition_attributes is a st r u ct u r e expr ession an d pred icate a boolean expr ession , t h en select_query group by partition_attributes is an expr ession an d select_query group by partition_attributes having pred icate is an expr ession . Th e car tesian pr odu ct visit ed by t h e select oper at or is split in t o par t it ion s. For each elem en t of t h e car t esian pr odu ct, t h e par t it ion at t r ibu t es ar e evalu at ed. All elem en t s wh ich m at ch t h e sam e valu es accor din g t o t h e given par t it ion at tr ibu t es, belon g t o t h e sam e par tit ion . Th u s t h e par t it ion ed set , aft er t h e gr ou pin g oper at ion is a set of st r u ct u r es: each st r u ct u r e h as t h e valu ed pr oper t ies for t h is par t ition (t h e valu ed partition_attributes), com plet ed by a pr oper t y wh ich is 72 ODM G OQL User Man u al Language Definition : Group-by Operator con ven tion ally called partition an d wh ich is t h e bag of all object s m atch in g t h is par t icu lar valu ed par t it ion . If t h e par t it ion att r ibu t es ar e: att1: e1, att2: e2, ... , attn: en , t h en t h e r esu lt of t h e gr ou pin g is of t ype set< struct(att1: type_of(e1), att2: type_of(e2),..., attn: type_of(en), partition: bag< type_of(grouped elements) >) Th e type of gr ou ped elem en t s is defin ed as follows. If t h e from clau se declar es t h e var iables v1 on collect ion col1, v2 on col2, ..., vn on coln, t h e gr ou ped elem en t s for m a st r u ct u r e wit h on e at t r ibu t e "vk" for each collect ion h avin g t h e type of th e elem en t s of t h e cor r espon din g collect ion . partition: bag< struct(v1: type_of(col1 elements), ... , vn: type_of(coln elements))>. If a collection colk h as n o var iable declar ed t h e cor r espon din g att r ibu t e h as an in t er n al syst em n am e. Th is par t it ion ed set m ay t h en be filt er ed by t h e pr edicate of a having clau se. Fin ally, th e r esu lt is com pu ted by evalu at in g t h e select clau se for t h is par t it ion ed an d filt er ed set . Th e having clau se can t h u s apply aggr egat e fu n ct ion s on partition, lik ewise t h e select clau se can r efer t o partition t o com pu t e t h e fin al r esu lt . Bot h clau ses can r efer also t o th e par t ition at t r ibu t es. Exam ple: select * from group by Employees e low: e.salary < 1000, medium: e.salary >= 1000 and salary < 10000, high: e.salary >= 10000 Th is gives a set of t h r ee elem en t s, each of wh ich h as a pr oper t y called partition wh ich con t ain s t h e bag of em ployees t h at en t er in t h is cat egor y. So t h e t ype of t h e r esu lt is: set<struct(low: boolean, medium: boolean, high: boolean, partition: bag<struct(e: Employee)>)> Th e secon d for m en h an ces th e fir st on e with a having clau se wh ich en ables you t o filt er t h e r esu lt u sin g aggr egat ive fu n ct ion s wh ich oper at e on each par t it ion . ODM G OQL User Man u al 73 4 Exam ple: select department, avg_salary: avg(select p.e.salary from partition p) from Employees e group by department: e.deptno having avg(select p.e.salary from partition p) > 30000 Th is gives a set of cou ples: depar t m en t an d aver age of t h e salar ies of t h e em ployees wor k in g in t h is depar t m en t , wh en t h is aver age is m or e th an 30000. So t h e t ype of t h e r esu lt is: bag<struct(department: integer, avg_salary: float)> O2 note In O2 t h e syn t ax of partition_attributes does n ot accept t h e k eywor d st r u c t an d t h u s is always given as a list of cr iter ia separ at ed by com m as. See Sect ion 4.4.1. 4.3.10 Order-by Operator If select_query is a select -fr om -wh er e or a select -fr om -wh er e-gr ou p_by qu er y, an d if e1, e2, ..., en ar e expr ession s, t h en select_query order by e1, e2, ..., en is an expr ession . It r et u r n s a list of t h e selected elem en t s sor t ed by th e fu n ct ion e1, an d in side each su bset yieldin g t h e sam e e1, sor t ed by e2, ... , an d t h e fin al su bsu b...set , sor t ed by en. Exam ple: select p from Persons p order by p.age, p.name Th is sor t s t h e set of per son s on t h eir age, t h en on t h eir n am e an d pu t s t h e sor t ed object s in t o th e r esu lt as a list . Each sor t expr ession cr it er ion can be followed by t h e k eywor d asc or d esc, specifyin g r espect ively an ascen din g or descen din g or der . Th e defau lt or der is t h at of t h e pr eviou s declar at ion . For t h e fir st expr ession , t h e defau lt is ascen din g. 74 ODM G OQL User Man u al Language Definition : Indexed Collection Exam ple: select * from p in Persons order by p.age desc, p.name asc, p.department 4.3.11 Indexed Collection Expressions 4.3.11.1 Getting the i-th Element of an Indexed Collection If e1 an d e2 ar e expr ession s, e1 is a list or an ar r ay, e2 is an in t eger , t h en e1[e2] is an expr ession . Th is ext r act s th e e2+1 th elem en t of t h e in dexed collect ion e1. Not ice t h at t h e fir st elem en t h as t h e r an k 0. Exam ple: list (a,b,c,d) [1] Th is r et u r n s b. Exam ple: element (select x from Courses x where x.name = "math" and x.number ="101").requires[2] Th is r et u r n s t h e t h ir d pr er equ isite of M at h 101. 4.3.11.2 Extracting a Subcollection of an Indexed Collection. If e1, e2, an d e3 ar e expr ession s, e1 is a list or an ar r ay, e2 an d e3 ar e in t eger s, th en e1[e2:e3] is an expr ession . Th is ext r act s th e su bcollect ion of e1 st ar t in g at posit ion e2 an d en din g at posit ion e3. Exam ple: list (a,b,c,d) [1:3] Th is r et u r n s list (b,c,d). ODM G OQL User Man u al 75 4 Exam ple: element (select x from Courses x where x.name="math" and x.number="101").requires[0:2] Th is r et u r n s t h e list con sist in g of t h e fir st t h r ee pr er equ isit es of Mat h 101. 4.3.11.3 Getting the First and Last Elements of an Indexed Collection If e is an expr ession , if <op> is an oper at or fr om {first, last}, e is a list or an ar r ay, th en <op>(e) is an expr ession . Th is extr acts t h e fir st an d last elem en t of a collection . Exam ple: first(element(select x from Courses x where x.name="math" and x.number="101").requires) Th is r et u r n s t h e fir st pr er equ isit e of Mat h 101. 4.3.11.4 Concatenating Two Indexed Collections If e1 an d e2 ar e expr ession s, if e1 an d e2 ar e bot h lists or both ar r ays, t h en e1+e2 is an expr ession . Th is com pu t es th e con cat en at ion of e1 an d e 2. Exam ple: list (1,2) + list( 2,3) Th is qu er y gen er at es list (1,2,2,3). 76 ODM G OQL User Man u al Language Definition : Binary Set Expressions 4.3.12 Binary Set Expressions 4.3.12.1 Union, Intersection, Difference If e1 an d e2 ar e expr ession s, if <op> is an oper at or fr om {union, except, intersect}, if e1 an d e2 ar e set s or bags, t h en e1 <op> e2 is an expr ession . Th is com pu t es set t h eor et ic oper ation s, u n ion , differ en ce, an d in t er sect ion on e1 an d e2, as defin ed in Ch apt er 2. Wh en th e collection k in ds of t h e oper an ds ar e differ en t (bag an d set ), t h e set is con ver t ed in t o a bag befor eh an d an d t h e r esu lt is a bag. Exam ples: Student except Ta Th is r et u r n s t h e set of st u den ts wh o ar e n ot Teach in g Assist an t s. bag(2,2,3,3,3) union bag(2,3,3,3) Th is bag expr ession r etu r n s bag(2,2,3,3,3,2,3,3,3) bag(2,2,3,3) intersect bag(2,3,3,3) Th e in ter sect ion of 2 bags yields a bag th at con t ain s th e m in im u m for each of th e m u lt iply valu es. So t h e r esu lt is: bag(2,3,3) bag(2,2,3,3,3) except bag(2,3,3,3) Th is bag expr ession r etu r n s bag(2) 4.3.12.2 Inclusion If e1 an d e2 ar e expr ession s wh ich den ote set s or bags, if <op> is an oper ator fr om {<, <=, >, >=}, th en e1 <op> e2 is an expr ession wh ose valu e is a boolean . Wh en t h e oper an ds ar e differ en t k in ds of collect ion s (bag an d set ), t h e set is fir st con ver t ed in t o a bag. ODM G OQL User Man u al 77 4 e1 < e2 is t r u e if e1 is in clu ded in t o e2 bu t n ot equ al t o e2 e1 <= e2 is t r u e if e1 is in clu ded in t o e2 Exam ple: set(1,2,3) < set(3,4,2,1) is t r u e. 4.3.13 Conversion Expressions 4.3.13.1 Extracting the Element of a Singleton If e is a collect ion -valu ed expr ession , element(e) is an expr ession . Th is t ak es t h e sin glet on e an d r et u r n s it s elem en t . If e is n ot a sin glet on t h is r aises an exception . Exam ple: element(select x from Professors x where x.name ="Turing") Th is r et u r n s t h e pr ofessor wh ose n am e is Turing (if t h er e is on ly on e). 4.3.13.2 Turning a List into a Set If e is a list expr ession , listtoset(e) is an expr ession . Th is con ver ts t h e list in t o a set, by for m in g th e set con t ain in g all t h e elem en t s of t h e list . Exam ple: listtoset (list(1,2,3,2)) Th is r et u r n s t h e set con t ain in g 1, 2, an d 3. O2 note To car r y ou t t h e r ever se oper at ion (set t o list ) you u se th e or der by oper at or . If you ar e n ot in t er est ed in a given or der you can u se "*" as sh own in t h e followin g qu er y: select e from e in aSet order by * 78 ODM G OQL User Man u al Language Definition : Conversion Expressions 4.3.13.3 Removing Duplicates If e is an expr ession wh ose valu e is a collect ion , t h en distinct(e) is an expr ession wh ose valu e is th e sam e collect ion aft er r em ovin g t h e du plicat ed elem en t s. If e is a bag, distinct(e) is a set . If e is an or der ed collection , t h e r elat ive or der in g of t h e r em ain in g elem en t s is pr eser ved. 4.3.13.4 Flattening Collection of Collections If e is a collect ion -valu ed expr ession , flatten(e) is an expr ession . Th is con ver t s a collect ion of collect ion s of t in t o a collect ion of t. So th is flat t en in g oper at es at t h e fir st level on ly. Assu m in g t h e type of e to be col1<col2<t>>, t h e r esu lt of flatten(e) is: • If col2 is a set (r esp. a bag), th e u n ion of all col2<t> is don e an d th e r esu lt is a set<t> (r esp. bag<t>) • If col2 is a list (r esp. an ar r ay) an d col1 is a list (r esp. an ar r ay) as well, t h e con cat en at ion of all col2<t> is don e followin g t h e or der in col1 an d t h e r esu lt is col2<t>, wh ich is t h u s a list (r esp. an ar r ay). Of cou r se du plicates, if an y, ar e m ain t ain ed by t h is oper ation . • If col2 is a list or an ar r ay an d col1 is a set or a bag, t h e list s or ar r ays ar e con ver t ed in to set s, t h e u n ion of all t h ese set s is don e an d th e r esu lt is a set<t>, t h er efor e with ou t du plicat es. Exam ples: flatten(list(set(1,2,3), set(3,4,5,6), set(7))) Th is r et u r n s t h e set con t ain in g 1,2,3,4,5,6,7. flatten(list(list(1,2), list(1,2,3))) Th is r et u r n s list(1,2,1,2,3). flatten(set(list(1,2), list(1,2,3))) ODM G OQL User Man u al 79 4 Th is r et u r n s t h e set con t ain in g 1,2,3. 4.3.13.5 Typing an Expression If e is an expr ession , if c is a t ype n am e, t h en (c)e is an expr ession . Th is asser ts t h at e is an object of class t ype c. If it tu r n s ou t t h at it is n ot t r u e, an except ion is r aised at r u n t im e. Th is is u sefu l to access a pr oper t y of an object wh ich is st at ically k n own t o be of a su per class of t h e specified class. Exam ple: select ((Employee) s).salary from Students s where s in (select sec.assistant from Sections sec) Th is r et u r n s t h e set of salar ies of all stu den t s wh o ar e teach in g assist an t s, assu m in g th at Students an d Sections ar e th e exten ts of t h e classes Student an d Section. 4.3.14 Function Call If f is a fu n ct ion n am e, if e1 , e2 , ..., en ar e expr ession s, t h en f() and f(e1, e2, ... , en) ar e expr ession s wh ose valu e is th e valu e r et u r n ed by t h e fu n ct ion , or t h e object nil, wh en t h e fu n ct ion does n ot r etu r n an y valu e. Th e fir st for m allows you to call a fu n ct ion wit h ou t a par am et er , wh ile t h e secon d on e calls a fu n ct ion wit h t h e par am et er s e1, e2, ..., en. OQL does n ot defin e in wh ich lan gu age t h e body of su ch a fu n ct ion is wr it t en . Th is feat u r e allows you t o sm ooth ly ext en d t h e fu n ct ion alit y of OQL wit h ou t ch an gin g t h e lan gu age. 4.3.15 Scope Rules Th e from par t of a select -fr om -wh er e qu er y in t r odu ces explicit or im plicit var iables t o r an ge over t h e filt er ed collection s. An exam ple of an explicit var iable is: select ... from Persons p ... wh ile an im plicit declar at ion wou ld be: select ... from Persons ... Th e scope of t h ese var iables r each es all par t s of t h e select -fr om -wh er e expr ession in clu din g n est ed su b-expr ession s. 80 ODM G OQL User Man u al Language Definition : Scope Rules Th e group by par t of a select-fr om -wh er e-gr ou p_by qu er y in t r odu ces t h e n am e partition alon g wit h possible explicit at t r ibu t e n am es wh ich ch ar act er ize t h e par t ition . Th ese n am es ar e visible in t h e cor r espon din g having an d select par ts, in clu din g n ested su b-expr ession s wit h in th ese par ts. In side a scope, you u se th ese var iable n am es t o con st r u ct path expr ession s an d r each pr oper t ies (at t r ibu t es an d oper at ion s) wh en t h ese var iables den ot e com plex object s. For in st an ce, in t h e scope of t h e fir st fr om clau se above, you access th e age of a per son by p.age. Wh en t h e var iable is im plicit , as in t h e secon d fr om clau se, you u se t h e n am e of t h e collection dir ect ly, Persons.age. However , wh en t h er e is n o am bigu it y, you can u se t h e pr oper ty n am e dir ect ly as a sh or tcu t , wit h ou t u sin g t h e var iable n am e t o open t h e scope (t h is is m ade im plicit ly), wr it in g sim ply: age. Th er e is n o am bigu ity wh en a pr oper t y n am e is defin ed for on e an d on ly on e object den ot ed by a visible var iable. To su m m ar ize, a n am e appear in g in a (n est ed) qu er y is look ed u p in th e followin g or der : • a var iable in th e cu r r en t scope, or • a n am ed qu er y in t r odu ced by t h e d efine clau se, or • a n am ed object , i.e., an en t r y poin t in t h e database, or • an at tr ibu t e n am e or an oper at ion n am e of a var iable in t h e cu r r en t scope, wh en t h er e is n o am bigu it y, i.e., t h is pr oper t y n am e belon gs t o on ly on e var iable in t h e scope. Exam ple: Assu m in g t h at in th e cu r r en t sch em a t h e n am es Per son s an d Cit ies ar e defin ed. select scop e1 from Persons, Cities c where exists(select scop e2 from children as child) or count (select scop e3 , (select scop e4 from partition) from children p, scop e5 v group by age: scop e6 ) In scope1 , we see t h e n am es: Per son s, c, Cit ies, all pr oper t y n am es of class Per son an d class Cit y as soon as t h ey ar e n ot pr esen t in bot h classes, an d t h ey ar e n ot called "Per son s", "c", n or "Cit ies". ODM G OQL User Man u al 81 4 In scope2 , we see t h e n am es: ch ild, Per son s, c, Cit ies, t h e pr oper t y n am es of th e class Cit y wh ich ar e n ot pr oper t y of t h e class Per son . No at t r ibu t e of t h e class Per son can be accessed dir ect ly sin ce th ey ar e am bigu ou s bet ween "ch ild" an d "Per son s". In scope3 , we see t h e n am es: age, par t it ion , an d t h e sam e n am es fr om scope1, except "age" an d "par t ition ", if th ey exist. In scope4 , we see t h e n am es: age, par t it ion , p, v, an d t h e sam e n am es fr om scope1, except "age", "par t ition ", "p" an d "v", if t h ey exist . In scope5 , we see t h e n am es: p, an d t h e sam e n am es fr om scope1, except "p", if it exist s. In scope6 , we see t h e n am es: p, v, Per son s, c, Cit ies, t h e pr oper t y n am es of t h e class Cit y wh ich ar e n ot pr oper t y of t h e class Per son . No att r ibu t e of t h e class Per son can be accessed dir ectly sin ce t h ey ar e am bigu ou s between "ch ild" an d "Per son s". O2 note Im plicit at t r ibu t e scope is n ot available wit h O2 . You m u st always access an at t r ibu t e with t h e dot n otation : v.att. 4.4 Syntactical Abbreviations OQL defin es an or t h ogon al expr ession lan gu age, in t h e sen se t h at all oper at or s can be com posed with each ot h er s as soon as t h e types of t h e oper an ds ar e cor r ect . To ach ieve t h is pr oper t y, we h ave defin ed a fu n ct ion al lan gu age with sim ple (lik e +) or com posit e oper at or s (lik e select fr om wh er e gr ou p_by or der _by) wh ich always deliver a r esu lt in t h e sam e t ype syst em an d wh ich t h u s can be r ecu r sively oper at ed wit h ot h er oper at ion s in t h e sam e qu er y. In or der t o accept th e wh ole DM L qu er y par t of SQL, as a valid syn t ax for OQL, OQL is added som e ad-h oc con st r u ct ion s each t im e SQL in t r odu ces a syn t ax wh ich can n ot en t er in t h e cat egor y of t r u e oper at or s. Th is sect ion gives t h e list of t h ese con st r u ct ion s t h at we call "abbr eviat ion s", sin ce t h ey ar e com plet ely equ ivalen t t o a fu n ction al OQL expr ession wh ich is also given . Doin g t h at , we t h u s give at t h e sam e tim e t h e sem an t ics of t h ese con st r u ct ion s, sin ce all oper ator s u sed for t h is descr iption h ave alr eady been defin ed. 82 ODM G OQL User Man u al Syntactical Abbreviations : Structure Construction 4.4.1 Structure Construction Th e st r u ct u r e con st r u ctor was in t r odu ced in Sect ion 4.3.4.2. Alt er n at e syn t ax ar e allowed in t wo con text s: select clau se an d gr ou p-by clau se. In both con t exts, t h e SQL syn t ax is accept ed, alon g wit h t h e on e alr eady defin ed. sel ec t projection {, projection } ... select ... gr ou p by projection {, projection } wh er e projection is in on e of t h e followin g for m s: • (i) expr ession as iden t ifier • (ii) iden t ifier : expr ession • (iii) expr ession Th is is an alt er n ate syn t ax for : st r u c t (iden t ifier : expr ession {, iden t ifier : expr ession }) If t h er e is on ly on e projection an d t h e syn t ax (iii) is u sed in a select clau se, t h en it is n ot in t er pr et ed as a st r u ct u r e con st r u ction bu t r at h er t h e expr ession st an ds as it is. Fu r t h er m or e, a (iii) expr ession is on ly valid if it is possible t o in fer t h e n am e of t h e cor r espon din g at t r ibu t e (t h e iden t ifier ). Th is r equ ir es t h at t h e expr ession den otes a path expr ession (possibly of len gt h on e) en din g in a pr oper t y wh ose n am e is th en ch osen as t h e iden t ifier . Exam ple: select p.name, salary, from Professors p, student_id p.teaches Th is qu er y r et u r n s a bag of st r u ct u r es: bag<struct(name: string, salary: float, student_id: integer)> O2 note O2 accept s t h e 3 alt er n atives of t h e projection syn t ax in th e select par t , as well as t h e struct syn t ax. In t h e group by par t , O 2 accepts t h e 3 alt er n at ives bu t does n ot accept t h e struct syn t ax. ODM G OQL User Man u al 83 4 4.4.2 Aggregate Operators Th ese oper at or s wer e in t r odu ced in Sect ion 4.3.7.4. SQL adopt s a n ot at ion wh ich is n ot fu n ction n al for t h em . So OQL accept s t h is syn t ax t oo. If we defin e aggregate as on e of m i n , m ax , cou n t , su m an d av g, select count(*) from ... is equ ivalen t t o: count(select * from ...) select aggregate(query) from ... is equ ivalen t t o: aggregate(select query from ...) select aggregate(distinct query) from ... is equ ivalen t t o: aggregate(distinct( select query from ...) O2 note O2 does n ot su ppor t Aggr egat e Oper at or abbr eviat ion s. 4.4.3 Composite Predicates If e1 an d e2 ar e expr ession s, e2 is a collect ion , e1 h as t h e t ype of it s elem en t s, if relation is a r elat ion al oper at or (=, !=, <, <=, > , >=), th en e1 relation some e2 an d e1 relation any e2 an d e1 relation all e2 ar e expr ession s wh ose valu e is a boolean . Th e t wo fir st pr edicat es ar e equ ivalen t t o: exists x in e2: e1 relation x Th e last pr edicat e is equ ivalen t t o: for all x in e2: e1 relation x Exam ple: 10 < some (8,15, 7, 22) is t r u e 84 ODM G OQL User Man u al OQL BNF : String Literal O2 note In O2 Com posite Pr edicat e abbr eviat ion s ar e n ot su ppor t ed. 4.4.4 String Literal OQL accept s sim ple qu ot es as well t o delim it a st r in g (see Sect ion 4.3.3.1), as SQL does. Th is in t r odu ces an am bigu it y for a st r in g wit h on e ch ar act er wh ich t h en h as t h e sam e syn t ax as a ch ar act er lit er al. Th is am bigu it y is solved by con t ext . O2 note In O2 a st r in g m u st be delim it ed by dou ble qu ot es. 4.5 OQL BNF Th e OQL gr am m ar is given u sin g a BNF-lik e n ot at ion . • { sym bol } m ean s a sequ en ce of 0 or m or e sym bol(s). • [sym bol ] m ean s an opt ion al sym bol. Do n ot con fu se wit h t h e separ at or s [ ] • keyword is a t er m in al of t h e gr am m ar . Keywor ds ar e n ot case sen sitive. • xxx_n am e h as t h e syn t ax of an iden t ifier • xxx_lit er al is self explan at or y, e.g., "a st r in g" is a st r in g_lit er al • bin d_ar gu m en t stan ds for a par am et er wh en em bedded in a pr ogr am m in g lan gu age, e.g., $3i. Th e n on t er m in al query st an ds for a valid qu er y expr ession . Th e gr am m ar is pr esen t ed as r ecu r sive r u les pr odu cin g valid qu er ies. Th is explain s wh y m ost of t h e t im e t h is n on t er m in al appear s on t h e left side of ::=. Of cou r se, all oper at or s expect t h eir "qu er y" oper an ds t o be of t h e r igh t t ype. Type con st r ain t s wer e discu ssed in t h e pr eviou s sect ion s. Th ese r u les m u st be com pleted by t h e pr ior it y of OQL oper at or s wh ich is given aft er t h e gr am m ar . Som e syn t act ical am bigu it ies ar e solved sem an t ically fr om t h e t ypes of t h e oper an ds. ODM G OQL User Man u al 85 4 4.5.1 Grammar 4.5.1.1 Axiom (see Sections 4.3.1, 4.3.2) query_program ::={define_query;} query define_query ::=define identifier as query 4.5.1.2 Basic (see Section 4.3.3) query ::= nil query ::= true query ::= false query ::= integer_literal query ::= float_literal query ::= character_literal query ::= string_literal query ::= entry_name query ::= query_name query ::= bind_argument1 query ::= from_variable_name query ::= (query) 4.5.1.3 Simple Expression (see Section 4.3.5) query ::= query + query2 query ::= query - query query ::= query * query query ::= query / query query ::= - query query ::= query mod query query ::= abs (query) query ::= query || query 4.5.1.4 Comparison (see Section 4.3.5) query ::= query comparison_operator query query ::= query like string_literal comparison_operator ::= = comparison_operator ::= != comparison_operator ::= > comparison_operator ::= < comparison_operator ::= >= 1. A bin d ar gu m en t allows to bin d expr ession s fr om a pr ogr am m in g lan gu age to a qu er y wh en em bedded in to th is lan gu age (see Ch apters on lan gu age bin din gs). 2. Th e oper at or + is also u sed for list an d ar ray con cat en at ion . 86 ODM G OQL User Man u al OQL BNF : Grammar comparison_operator ::= <= 4.5.1.5 Boolean Expression (see Section 4.3.5) query ::= not query query ::= query and query query ::= query or query 4.5.1.6 Constructor (see Section 4.3.4) query ::= type_name ([query] ) query ::= type_name (identifier:query {, identifier: query}) query ::= struct (identifier: query {, identifier: query}) query ::= set ([query {, query}]) query ::= bag ([query {,query}]) query ::= list ([query {,query}]) query ::= (query, query {, query}) query ::=[ list](query .. query) query ::= array ([query {,query}]) 4.5.1.7 Accessor (see Sections 4.3.6, 4.3.11, 4.3.14, 4.3.15) query ::= query dot attribute_name query ::= query dot relationship_name query ::= query dot operation_name query ::= query dot operation_name( query {,query} ) dot ::= . | -> query ::= * query query ::= query [query] query ::= query [query:query] query ::= first (query) query ::= last (query) query ::= function_name( [ query {,query} ] ) 4.5.1.8 Collection Expression (see Sections 4.3.7, 4.4.3) query ::= for all identifier in query: query query ::= exists identifier in query: query query ::= exists(query) query ::= unique(query) query ::= query in query query ::= query comparison_operator quantifier query quantifier ::= some quantifier ::= any quantifier ::= all query ::= count (query) ODM G OQL User Man u al 87 4 query ::= count (*) query ::= sum (query) query ::= min (query) query ::= max (query) query ::= avg (query) 4.5.1.9 Select Expression (see Sections 4.3.8, 4.3.9, 4.3.10) query ::= select [ distinct ] projection_attributes from variable_declaration {, variable_declaration} [where query] [group by partition_attributes] [having query] [order by sort_criterion {, sort_criterion}] projection_attributes ::= projection {, projection} projection_attributes ::= * projection ::= query projection ::= identifier: query projection ::= query as identifier variable_declaration ::= query [ [ as ] identifier] partition_attributes ::= projection {, projection} sort_criterion ::= query [ordering] ordering ::= asc ordering ::= desc 4.5.1.10 Set Expression (see Section 4.3.12) query ::= query intersect query query ::= query union query query ::= query except query 4.5.1.11 Conversion (see Section 4.3.13) query ::= listtoset (query) query ::= element (query) query ::= distinct(e) query ::= flatten (query) query ::= (class_name) query 4.5.2 Operator Priorities Th e followin g oper at or s ar e sor t ed by decr easin g pr ior it y. Oper at or s on t h e sam e lin e h ave t h e sam e pr ior it y an d gr ou p left -t o-r igh t . () 88 [] . -> ODM G OQL User Man u al OQL BNF : Operator Priorities n ot - (u n ar y) + (u n ar y) in * / m od i n t er sect + - u n i on ex cep t < > <= oper ator s) = != an d >= || < som e < an y < al l (et c ... for all com par ison lik e ex i st s for all or .. : , (iden t ifier ) th is is t h e cast oper at or order having group by where from select ODM G OQL User Man u al 89 4 90 ODM G OQL User Man u al IN DEX ODM G OQL User Man u al 91 INDEX Sym bol s C + 36 C 11 C++ In t er face 11 A Accessor 87 Addit ion of set s 36 Aggr egat ive oper at or s 54 Ar ch itect u r e O2 10 Ar it h m etic 86 Ar r ay 21, 22, 25 Con st r u ctin g 63 Set con ver sion 37 array 27, 29, 51 Ar r ay valu e 21 Atom ic valu e 20 Att r ibu t e 66 avg 30, 54, 70 C++ bin din g 44, 45 Class in dicat or 54 Collection 22, 44, 49, 79 in dexed expr ession 75 Nam ed 39 Collection expr ession 87 Com bin in g oper at or s 28, 38 Com par ison 86 con cat en ation 76 Con st r u ction Ar r ay 51 Bag 51 List 51 Set 51 Str u ct 51 Con st r u ctor 27, 51, 87 Con ver sion 37, 78, 88 count 30, 54, 70 Cr eat in g object s 29 B D Bag 22, 29 Con st r u ctin g 63 Data m an ipu lat ion 51 Database en t r y poin t 20 define 30, 36, 54 differ en ce 77 distinct 23, 49 bag 27, 29, 51 Boolean 87 Br owser In t er face 12 Un ix 12 Win dows NT 13 by 35 92 ODM G OQL User Man u al INDEX E I element 31, 78 except 36, 54, 77 Exist en t ial qu an t ificat ion 38, 54, 69 exists 31, 54 intersect 36, 54, 77 in t er sect ion 77 J F first 76 flatten 38 Flatt en in g 79 forall ... in 54 from 50 G group ... by 32, 54, 55, 72 H H yper t ext lin k s 13 J ava 11 J ava bin din g 44 J oin 50 J oin qu er y 24 L last 76 Lat e bin din g 53 like 35 List 21, 22, 25 Con st r u ct in g 62 Set con ver sion 37 Valu es 21 list 27, 51, 78 listtoset 37 M max 30, 54, 70 Mem ber sh ip 69 ODM G OQL User Man u al 93 INDEX M et h od call 22, 52 M et h od in vok in g 52 min 30, 54, 70 M ot if 13 N name 31 Nam ed Collection 39 Object s 20 Qu er y 30 Valu es 20 O O2 Ar ch it ectu r e 10 O 2 C 11 O 2 Cor ba 11 O 2 DBAccess 11 O 2 En gin e 10 O 2 Gr aph 11 O 2 Kit 11 O 2 Look 11, 12 O 2 OD BC 11 O 2 Stor e 10 O 2 Tools 11 O 2 Web 11 Object Cr eat ion 29 Nam ed 20 Oper at ion 67 Oper at or 30, 54 - 36 * 36 + 36 Aggr egat ive 54 avg 30, 54, 70 Com bin in g 28, 38 Com position 54 count 30, 54, 70 define 30, 54 element 31 except 36, 54 exists 31, 54 flatten 38 forall...in 54 group...by 32, 54, 55, 72 intersect 36, 54 like 35 max 30, 54, 70 min 30, 54, 70 order by 35, 74 Set 36, 38, 54 sum 30, 54, 70 union 36, 54 Wild-car d 38 OQL 11, 12, 17 Oper at or s 30 Rat ion al 43 Resu lt 27 or der by 35, 54, 74 P partition 33 Pat h expr ession s 24, 49 Polym or ph ism 53 Pr edicat e 50 Object s 61 ODMG m odel 44 ODMG stan dar d 44, 57 94 ODM G OQL User Man u al INDEX Q T Qu er y Basic 18, 86 Nam ed 30 Resu lt 27, 30 Test in g on n il 25 Typin g 80 U R Ref 44 Relation sh ip 44, 49, 66 S select 55 Select fr om wh er e 70 select from where 22, 49 Set 22, 22 Con st r u ctin g 62 List con ver sion 37 Oper at or s 36, 54 set 27, 29, 51, 77, 78 Set expr ession 88 struct 27, 29, 29, 51 St r u ct valu e 21 st r u ct u r e 62 Su bcollect ion 75, 75 sum 30, 54, 70 Syst em Ar ch it ectu r e 10 union 36, 54, 77 Un iver sal qu an t ificat ion 38, 54, 68 Un ix 12 V Valu e Ar r ay 21 At om ic 20 List 21 Nam ed 20 Str u ct 21 W where 50 Win dows NT 13 ODM G OQL User Man u al 95