Download ODMG OQL User Manual
Transcript
ODMG OQL User Manual Release 5.0 - February 1998 In for m at ion in t h is docu m en t is su bject t o ch an ge wit h ou t n ot ice an d sh ou ld n ot be con st r u ed as a com m itm en t by O2 Tech n ology. Th e softwar e descr ibed in t h 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 t h e agr eem en t . It is again st th e law t o copy th is soft war e to m agn etic t ape, disk , or any oth er m ediu m for an y pu r pose oth er t h an th e pu r ch aser ’s own u se. Copyr igh t 1992-1998 O 2 Tech n ology. All r igh t s r eser ved. No par t of th is pu blication can be r epr odu ced, stor ed in a r etr ieval syst em or t r an sm it t ed in an y for m or by an y m eans, elect r on ic, m ech an ical, ph ot ocopy with ou t pr ior wr itt en per m ission of O 2 Tech n ology. O2 , O2 En gin e API, O2 C, O2 DBAccess, O2 En gin e, O2 Gr aph , O2 Kit , O 2 Look , O2 Stor e, O 2 Tools, an d O 2 Web ar e r egister ed tr adem ar k s of O 2 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 egister ed t r adem ar k s of Su n Micr osyst em s, In c. X Win dow Syst em is a r egist er ed tr adem ar k of th e Massach u set ts In st itu te of Tech n ology. Un ix is a r egister ed tr adem ar k of Un ix System Labor at or ies, In c. HPUX is a r egister ed tr adem ar k of Hewlet t-Pack ar d Com pan y. BOSX is a r egister ed tr 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 tr adem ar k of th e NeXT Com pu t er , In c. Pu r ify, Qu an t ify ar e r egist er ed t r adem ar k s of Pu r e Softwar e In c. Win dows is a r egist er ed t r adem ar k of Micr osoft Cor por at ion . All ot h er com pan y or pr odu ct n am es qu oted ar e t r adem ar k s or r egister 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 ODMG st an dar d. Th is m an u al descr ibes h ow to u se OQL as an em bedded fu n ction in a pr ogr am m in g lan gu age (e.g. O 2 C, C, C++, or Sm alltalk ) 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 O2 syst em . Oth er docu m en ts available ar e ou t lin ed, click below. See O2 Documentation set. TAB L E OF CONTE NTS 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 ted 3 - OQL Rat ion ale 4 - OQL Refer en ce ODMG OQL User Man u al i TABLE OF CONTENTS 1 INTRODUCTION 1-1 1.1 System Overview.....................................................................1-2 OQL ........................................................................................... 1-4 Browser Interface ....................................................................... 1-5 1.2 Interactive and embedded query language ..........................1-7 Interactive OQL ......................................................................... 1-7 Embedded OQL ......................................................................... 1-8 1.3 Manual overview......................................................................1-8 2 GETTING STARTED 2-1 2.1 Basic queries ...........................................................................2-2 Database entry points ................................................................. 2-4 Simple queries............................................................................ 2-5 2.2 Select ... from ... where ........................................................2-6 Set............................................................................................... 2-7 Join ............................................................................................. 2-8 Path expressions ......................................................................... 2-8 Testing on nil ............................................................................. 2-9 List or array................................................................................ 2-9 2.3 Constructing results .............................................................2-10 Creating an object .................................................................... 2-12 2.4 Operators ...............................................................................2-13 Count ........................................................................................ 2-13 Define....................................................................................... 2-14 Element .................................................................................... 2-14 Exists ........................................................................................ 2-15 Group by .................................................................................. 2-15 Like .......................................................................................... 2-19 Order by ................................................................................... 2-19 2.5 Set operators .........................................................................2-20 2.6 Conversions...........................................................................2-21 List to set .................................................................................. 2-21 Set to list................................................................................... 2-21 ii ODMG OQL User Man u al TABLE OF CONTENTS Flatten ....................................................................................... 2-21 2.7 Combining operators ........................................................... 2-22 2.8 Indexes .................................................................................. 2-22 Display index............................................................................ 2-23 2.9 Chapter Summary................................................................. 2-24 3 OQL RATIONALE 3-1 3.1 The ODMG standard ............................................................... 3-2 3.2 The ODMG model.................................................................... 3-3 3.3 OQL by example ..................................................................... 3-7 Path expressions ......................................................................... 3-7 Data manipulation ...................................................................... 3-9 Method invoking ...................................................................... 3-10 Polymorphism........................................................................... 3-11 Operator composition ............................................................... 3-12 4 OQL REFERENCE 4-1 4.1 Introduction............................................................................. 4-2 4.2 Principles................................................................................. 4-2 4.3 Language Definition ............................................................... 4-3 4.3.1 Query Program.............................................................................4-3 4.3.2 Named Query Definition .............................................................4-3 4.3.3 Elementary Expressions...............................................................4-5 4.3.3.1 Atomic Literals .............................................................4-5 4.3.3.2 Named Objects..............................................................4-5 4.3.3.3 Iterator Variable ............................................................4-5 4.3.3.4 Named Query ................................................................4-5 4.3.4 Construction Expressions ............................................................4-6 4.3.4.1 Constructing Objects.....................................................4-6 4.3.4.2 Constructing Structures.................................................4-6 4.3.4.3 Constructing Sets ..........................................................4-7 ODMG OQL User Man u al iii TABLE OF CONTENTS 4.3.4.4 Constructing Lists......................................................... 4-7 4.3.4.5 Constructing Bags ........................................................ 4-8 4.3.4.6 Constructing Arrays ..................................................... 4-8 4.3.5 Atomic Types Expressions .......................................................... 4-8 4.3.5.1 Unary Expressions........................................................ 4-8 4.3.5.2 Binary Expressions....................................................... 4-9 4.3.5.3 String Expressions ........................................................ 4-9 4.3.6 Object Expressions ................................................................... 4-11 4.3.6.1 Comparison of Mutable Objects................................. 4-11 4.3.6.2 Comparison of Immutable Objects............................. 4-11 4.3.6.3 Extracting an Attribute or Traversing a Relationship from an Object ..................................................................... 4-11 4.3.6.4 Applying an Operation to an Object........................... 4-12 4.3.6.5 Applying an Operation with Parameters to an Object 4-12 4.3.6.6 Dereferencing an Object............................................. 4-14 4.3.7 Collections Expressions ............................................................ 4-14 4.3.7.1 Universal Quantification............................................. 4-14 4.3.7.2 Existential Quantification........................................... 4-14 4.3.7.3 Membership Testing ................................................... 4-15 4.3.7.4 Aggregate Operators................................................... 4-15 4.3.8 Select From Where .................................................................... 4-16 4.3.8.1 Projection.................................................................... 4-16 4.3.8.2 Iterator Variables ........................................................ 4-17 4.3.8.3 Predicate ..................................................................... 4-18 4.3.9 Group-by Operator .................................................................... 4-18 4.3.10 Order-by Operator ................................................................... 4-20 4.3.11 Indexed Collection Expressions ............................................. 4-21 4.3.11.1 Getting the i-th Element of an Indexed Collection... 4-21 4.3.11.2 Extracting a Subcollection of an Indexed Collection.4-21 4.3.11.3 Getting the First and Last Elements of an Indexed Collection .................................................................................... 4-22 iv ODMG OQL User Man u al TABLE OF CONTENTS 4.3.11.4 Concatenating Two Indexed Collections ..................4-22 4.3.12 Binary Set Expressions ...........................................................4-22 4.3.12.1 Union, Intersection, Difference................................4-23 4.3.12.2 Inclusion....................................................................4-23 4.3.13 Conversion Expressions.........................................................4-24 4.3.13.1 Extracting the Element of a Singleton ......................4-24 4.3.13.2 Turning a List into a Set............................................4-24 4.3.13.3 Removing Duplicates................................................4-25 4.3.13.4 Flattening Collection of Collections .........................4-25 4.3.13.5 Typing an Expression................................................4-26 4.3.14 Function Call...........................................................................4-26 4.3.15 Scope Rules.............................................................................4-26 4.4 Syntactical Abbreviations................................................... 4-28 4.4.1 Structure Construction ..............................................................4-29 4.4.2 Aggregate Operators .................................................................4-30 4.4.3 Composite Predicates................................................................4-30 4.4.4 String Literal .............................................................................4-31 4.5 OQL BNF............................................................................... 4-32 4.5.1 Grammar ...................................................................................4-32 4.5.1.1 Axiom (see Sections 4.3.1, 4.3.2) .............................4-32 4.5.1.2 Basic (see Section 4.3.3)............................................4-32 4.5.1.3 Simple Expression (see Section 4.3.5).......................4-33 4.5.1.4 Comparison (see Section 4.3.5) .................................4-33 4.5.1.5 Boolean Expression (see Section 4.3.5).....................4-33 4.5.1.6 Constructor (see Section 4.3.4)..................................4-33 4.5.1.7 Accessor (see Sections 4.3.6, 4.3.11, 4.3.14, 4.3.15) .... .....................................................................................4-34 4.5.1.8 Collection Expression (see Sections 4.3.7, 4.4.3).... 4-34 4.5.1.9 Select Expression (see Sections 4.3.8, 4.3.9, 4.3.10)4-34 ODMG OQL User Man u al v TABLE OF CONTENTS 4.5.1.10 Set Expression (see Section 4.3.12) ....................... 4-35 4.5.1.11 Conversion (see Section 4.3.13) ............................. 4-35 4.5.2 Operator Priorities .................................................................... 4-35 INDEX vi ODMG OQL User Man u al I-i 1 1 I N T RO D U C T I O N Con gr at u lation s! You ar e n ow a u ser of t he object -or ien t ed qu er y lan gu age OQL. O2 is a r evolu t ion ar y system t h at is par t icu lar ly well adapt ed for developin g lar ge-scale clien t / ser ver applicat ion s in both fields of bu sin ess an d tech n ical softwar e developm en t. Th is ch apter in t r odu ces t he O 2 system an d t h e OQL qu er y lan gu age. It is divided u p in to th e followin g section s : • Syst em Over view • In t er act ive and em bedded qu er y lan gu age • Man u al over view ODMG OQL User Man u al 1-1 1 INTRODUCTION 1.1 System Overview Th e system ar ch itect u r e of O 2 is illu st r ated in Figu r e 1.1. External Interfaces Development Tools O2 Dev. Tools Standard Dev. Tools OQL C O 2C C++ Java O2ODBC O2Corba Database Engine O2DBA O2Engine O2Store O2Web Fi gu r e 1 .1 : O2 Sy st em Ar ch i t ect u r e Th e O2 syst em divided in t o t h r ee com pon en ts. Th e Database Engine pr ovides all th e feat u r es of a Database syst em an d an object-or ien t ed syst em . Th is en gin e is accessed with 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 stan dar d developm en t t ool. Nu m er ou s External Interfaces ar e pr ovided. All en com passin g, O 2 is a ver satile, por table, dist r ibu t ed, h igh per for m an ce dyn am ic object -or ient ed database syst em . Dat abase En gin e: 1-2 • O2 St or e Th e dat abase m an agem en t syst em pr ovides low level facilit ies, th r ou gh O2 St or 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 database en gin e pr ovides dir ect con t r ol of sch em at a, classes, objects 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 with O2 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 to th 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 ltiple object ver sion s an d a Replication API for syn ch r on izin g m u lt iple copies of an O2 syst em . ODMG OQL User Man u al System Overview : Pr ogr am m in g Lan gu ages: O2 object s m ay be cr eat ed an d m an aged u sin g th e followin g pr ogr am m in g lan gu ages, u tilizin g all th e featu r es available with O2 (per sist en ce, collect ion m an agem en t , t r an saction 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++ ODMG com plian t C++ bin din g. • J ava ODMG com plian t J ava bin din g. • O2 C A power fu l an d elegan t object -or ien ted fou r th gener ation lan gu age specialized for easy developm en t of object dat abase application s. • OQL ODMG stan dar d, easy-t o-u se SQL-lik e object qu er y langu age wit h special featu r es for dealin g wit h com plex O2 objects an d m eth ods. O2 Developm ent 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 act ive m an ipu lat ion of com plex an d m u lt im 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 application s. • O2 Tools Complete graphical programming environment to design and develop O2 database applications. Stan dar d Developm en t Tools: All st an dar d pr ogr am m in g lan gu ages can be u sed wit h stan dar d en vir on m en t s (e.g. Visu al C++, Su n Spar cwor k s). Ext er n al In t er faces: • O2 Cor ba Cr eat e an O2 / Or bix ser ver t o access an O 2 dat abase wit h CORBA. • O2 DBAccess Con n ect O2 applicat ion s t o r elation al databases on r em ot e h osts an d in vok e SQL st at em en t s. • O2 ODBC Con n ect r em ot e ODBC clien t applicat ion s t o O 2 dat abases. • O2 Web Cr eat e an O2 Wor ld Wide Web ser ver t o access an O 2 dat abase t h r ou gh th e in t er n et n etwor k . ODMG OQL User Man u al 1-3 1 INTRODUCTION OQL OQL is an object -or ien ted SQL-lik e qu er y lan gu age. OQL is th e qu er y lan gu age of th 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 ction 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 ction 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. Met h ods can be t r igger ed t o m odify th e database. You will fin d t h at pr ogr am m in g is easier becau se OQL can filt er valu es u sin g com plex pr edicat es wh ose evalu ation s ar e opt im ized by t h e OQL optim izer in O 2 . OQL can also be u sed in ter act ively as an ad h oc qu er y lan gu age allowing database qu er ies fr om bot h t ech n ical an d n on -tech n ical u ser s. In t er act ive feat u r es in clu de fast an d sim ple br owsin g of t h e database. 1. Th e Object Dat abase St an dar d: ODMG - 93. At wood, Bar r y, Du h l, East m an , Fer r an , J or dan , Loom is an d Wade. Edit ed by R.G.G. Cat t ell. © 1996 Mor gan Kau fm an Pu blish er s. 1-4 ODMG OQL User Man u al System Overview : Browser Interface Browser Interface Th e br owser in ter face you see depen ds on th e oper at in g syst em you ar e u sin g. • Unix In Un ix, th e O 2 Look gr aph ical u ser in t er face gen er ator is u sed to 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 at ed by O 2 Look . Er a ser bu t t on Obj ect i con s Fi gu r e 1 .2 : Typ 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 addit ion t o t h e u su al Motif bu tt on s a gr aph ical qu er y r esu lt h as an Er aser bu t ton . Click in g on th e Er aser bu t t on r em oves t h e gr aphical r esu lt. Th is qu er y r esu lt con sists 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 th e Object icon u sin g t h e r igh t m ou se bu tt on . Th is pop-u p m enu can be u sed t o access th e pu blic m et hods of each object . ODMG OQL User Man u al 1-5 1 INTRODUCTION • Windows NT In Win dows NT, t h e qu er y r esu lt is displayed in a win dow in t extu 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 to th e su b-object r epr esent ed by t h e lin k . Click in g on a h yper t ext lin k , wit h t h e r igh t m ou se bu t ton , r eplaces th e con t en t s of t h e win dow wit h a r epr esen tation of th e su b-object associated wit h t h 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. 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 ton s: Ba ck th is bu t t on displays th e pr eviou s object . For w a r d th is bu t t on displays th e n ext su b-object . It is on ly valid if th e Back bu t ton has been act ivated at least on ce. New Wi n d ow Th is bu t t on displays th 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 t t on closes t h e active 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 th u s appear s in as a h yper t ext lin k . Left click in g displays th 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-6 ODMG OQL User Man u al Interactive and embedded query language : 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 ction called fr om C, C++, Sm allt alk , J ava, O2 C an d O2 En gin e API. Interactive OQL Th e OQL in t er pr eter can be tr igger ed by th e query com m an d of O2dba, O2dsa or O2 sh ells. Th e com m an d in ter pr et er pr om pt s you with t h e followin g m essage: type your command and end with ^D To r u n OQL, t ype: query ^D You m u st type ^D (Con tr ol - D) on a separ at e 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 with ^D. "this is a query" ^D Th e an swer is au tom at ically displayed an d t h e system r et u r n s t o t h e OQL pr om pt: type your query and end with ^D To leave th e qu er y session t ype: ^D (or quit) You ar e n ow back in t h e com m an d in t er pr eter an d you see th e m essage: type your command and end with ^D You can also u se OQL in th e O 2 Tools pr ogr am m in g envir on m en t (Refer t o t h e O 2 Tools User Man 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 tr ol - D). ODMG OQL User Man u al 1-7 1 INTRODUCTION Embedded OQL An y valid qu er y can be passed fr om O 2 C code t o OQL u sin g t h e system su pplied fu n ct ion o2query. Th is is detailed in th e O 2 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 t o t h e r espect ive m an u als for det ails. Fin ally an OQL fu n ct ion exist s in O 2 Engin e an d is descr ibed in t h e O2 En gin e API Refer en ce Man u al. 1.3 Manual overview Th is m an u al is divided u p in t o t h e followin g ch apter s: • Ch apt er 1 - In tr odu ct ion Th is chapt er in tr odu ces t h e O 2 syst em an d th e OQL qu er y lan gu age. It ou t lin es th e con cept s of t h e ad h oc qu er y lan gu age th at allows you t o br owse th e dat abase qu ick ly an d efficien tly t o get th e in for m ation you wan t , an d t h 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 chapt er in tr odu ces t h e OQL lan gu age so you can st ar t t o u se OQL in or der to obt ain th 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 to con st r u ct r esu lt s an d descr ibes th e u se of oper ator s an d in dexes. To fu lly u n der st an d th is ch apter , you m u st k now th e ODMG data m odel. • Ch apt er 3 - OQL Ration ale Th is chapt er in tr odu ces t h e ODMG standar d an d descr ibes t h e ODMG 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 t h e ODMG r efer en ce m an u al for OQL 1.2. It is th e sam e as t h e ODMG st an dar d with added n ot es an d explanation s on h ow to u se OQL wit h O 2 . For each featu r e of th e lan gu age, you get t h e syn t ax, in in for m al sem an tics, an d an exam ple. Fin ally, th e for m al syn tax is given . 1-8 ODMG OQL User Man u al GE TTI NG ST A RT E D 2 2 AN OBJ ECT-ORIENTED DATABASE QUERY LANGUAGE So t h at you can obt ain th 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 wit h special feat u r es for dealin g wit h com plex objects, valu es an d m eth ods. Th is ch apter in t r odu ces t he 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 st r u ctin g r esu lts • Oper at or s • Set oper at or s • Con ver sion s • Com bin in g oper at or s • In dexes • Ch apt er Su m m ar y To u n der st an d th is ch apter you n eed to k n ow t h e ODMG dat a m odel 1 . As an in tr odu ct ion t o t h e dat a m odel you can r efer t o ch apt er 3 of th is m an u al or t h e O2 C Begin n er ’s Gu ide. Exper ien ce of SQL, th ou gh n ot a pr er equ isite, will facilit ate t h e OQL lear n in g pr ocess. 1. Th e Object Dat abase St an dar d: ODMG - 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. ODMG OQL User Man u al 2-1 2 GETTING STARTED 2.1 Basic queries All t h e exam ples sh own below ar e based on th e followin g O 2 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; 2-2 ODMG 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; ODMG OQL User Man u al 2-3 2 GETTING STARTED Database entry points To qu er y an y dat abase you n eed var iou s en tr y poin t s. In O2 t h ese ar e t h e n am ed object s an d n am ed valu es. For exam ple, Globe is an en t r 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 t ype, or object s of an y class. Con sequ en tly, OQL allows you to qu er y valu es or object s of an y t ype or class. Note Th e qu er y r esu lt s sh own below ar e all given in t h e Un ix gr aph ic for m . 2-4 ODMG OQL User Man u al Basic queries : Simple queries Simple queries Sim ple qu er ies can in volve differ en t types of valu es: • Atomic values Wit h 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 etu r n s th e in t eger 4. • Struct values You can also con sider th e valu e of t h e object Globe of class Company as a st r u ct (or tu ple) valu e with 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 ext r act in 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. ODMG OQL User Man u al 2-5 2 GETTING STARTED • List or array values A list is an or der ed collect ion t h at allows du plicat es an d you can th er efor e ext r act an y of it s elem en t s if you k n ow t h eir position . For exam ple, you can extr act t h e fi 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 ts fr om 0. For OQL, an ar r ay beh aves th e sam e way as a list. • 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 t o t h e object Globe an d r et u r n s t h e r esu lt of th e m et h od title: 2.2 Select ... from ... where Th e select from where clau se en ables you t o ext r act t h ose elem en t s m eetin g a specific con dit ion fr om a col l ect i on . O2 collect ion s in clu de set , bag (a m u lt i-set or set with du plicat es), l i st (an in ser table 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 st r u ctu r e: 2-6 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. ODMG OQL User Man u al Select ... from ... where : Set where: in t r odu ces a pr edicat e th at filter s t h e collect ion . Th is sect ion n ow descr ibes h ow t o u se th 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 fi l t er . Th is con sist s 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. 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 hose em ployees wor k in g at t h e In ter n ation al Globe wit h a salar y over 200: Th e select clau se defin es t h e qu er y r esu lt as t h e em ployees an d th e from clau se gives th 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 filter 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 accepted. You can also add t h e k eywor d distinct t o elim in at e an y du plicat es fr om t h e r esu ltin g bag an d t h en pr odu ce a t r u e set . Mor eover , you can access fr om e an y att r 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: ODMG OQL User Man u al 2-7 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 etu r n s th e set of em ployees wh o h ave t h e sam e n am e as a clien t . If th 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 Objects ar e r elat ed to ot h er object s, an d in or der t o get to th e dat a it n eeds, a qu er y can follow var iou s path s t h at st ar t fr om an y O 2 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 th e set of wh at th e clien t (s) called Haddock bou gh t : 2-8 ODMG OQL User Man u al Select ... from ... where : Testing on nil Testing on nil Aft er you r applicat ion h as u pdated t he dat abase, you m ay fin d t h at som e object s ar e n ow equ al t o n il. You can test for t his u sin g OQL. For exam ple, you can t est t h at a clien t exists 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 objects wh en t h ey ar e en cou n ter ed. If a path expr ession con t ain s a nil object, a pr edicate 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 ten 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 cont ain du plicate elem en t s. Sin ce it is or der ed, you m ay ext r act an y of its elem en ts if you k n ow t h eir position . For exam ple: Globe.clients[2] Th is extr acts th e th ir d elem en t of th e list (th e fir st elem en t is at position 0). As wit h set s you can filter a list . ODMG OQL User Man u al 2-9 2 GETTING STARTED For exam ple: wh at ar e t h e n am es of th e clien t s wh o bu y th 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 th is qu er y is a bag of t h e name of Globe clien t s: 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 “Order by” on page 19. You can also add th e k eywor d distinct t o a select ion t o elim in at e an y du plicat es fr om t h e r esu ltin g set. Note You can m an ipu lat e ver y com plex str u ct u r es. A list can be m ade u p of tu ples wh ich in t u r n can h ave a set att r ibu t e, etc. Con sequ en t ly, you h ave access to all th e em bedded com pon en t s of an object. For m or e details, r efer to Sect ion 2.3 for con st r u ct in g qu er y r esu lt s an d Sect ion 2.7 for com bin in g oper at or s. 2.3 Constructing results Th e str 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 extr act t h e age field of an em ployee, wh ich is of type in t eger , you obt ain an in teger . Wh en you filter a set , bag or list , you obt ain a set , bag or list depen din g on wh at you select. 2-10 ODMG OQL User Man u al Constructing results However , you can also con st r u ct a qu er y r esu lt with 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 st r 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 th e em ployees at t h e In t er n at ion al Globe n ewspaper : You can u se t h 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 . ODMG OQL User Man u al 2-11 2 GETTING STARTED For exam ple: select * from Globe.employees Not e th at in th is exam ple you do n ot n eed t o defin e a var iable with from. You can also bu ild u p em bedded str u ctu r es sim ply by com bin in g struct oper at or s. For exam ple, t o get th e iden t ities an d salar ies of all t hose em ployees wor k in g as r epor ter s an d older th 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 Th is qu er y gives a bag with on e elem en t: Creating an object You cr eate valu es u sin g struct, list, array, bag an d set. In OQL, you can also cr eate object s u sin g t h e class n am e an d by in it ializin g t h e at t r ibu t es of you r ch oice. An y u n -in itialized att r ibu t es ar e set to th e defau lt valu e. For exam ple, to 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 t h e n am e at tr ibu t e in itialized t o Trent. 2-12 ODMG OQL User Man u al Operators : Count You can t h 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 O2 C, C++, Sm allt alk an d J ava m an u als). Th e r esu lt of t h is qu er y is th e n ew object. An object collect ion can be cr eat ed in t h e sam e way. For exam ple, u se t h e followin g qu er y to cr eat e an o2 _l i st _Cl i en t collection . o2_list_Client (list(Client(name:"John"), (Client(name:"Jack"))) 2.4 Operators Th is sect ion ou tlin es t h e basic OQL oper ator s you can u se to 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 teger . Oth er aggr egat e oper at or s ar e min, max, sum an d avg. ODMG OQL User Man u al 2-13 2 GETTING STARTED Define You can n am e th e r esu lt of a qu er y u sing 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 th e qu er y an d n ot t h 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 th e legibilit y of com plex qu er ies. 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 extr act th e elem en t dir ectly 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: 2-14 ODMG OQL User Man u al Operators : Exists Exists You can add a n ew per sist en t n am e t o 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 th e age of 23: select c.name from c in TheCompanies where exists e in c.employees: e.age < 23 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 objects of a collect ion wit h 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 t wo-at tr ibu t e t u ples: ODMG OQL User Man u al 2-15 2 GETTING STARTED Th e fir st at t r ibu te is t h e salar y an d is called salary as specified. Th e secon d is t h e set of objects (em ployees) with t h e sam e salar y an d is called partition. Th u s, th e t ype of t h e r esu lt of t h is qu er y is: bag (struct (salary: real, partition: bag (struct (e:Employee)))) 2-16 ODMG OQL User Man u al Operators : Group by You can wor k on a par tit ion valu e by com pu t in g statist ics on each par tit ion . Th e followin g qu er y r et u r n s a bag of t wo-at t r ibu te tu ples wit h t h e salar y an d th 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 You get th 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 t his u sin g t h e having clau se. ODMG OQL User Man u al 2-17 2 GETTING STARTED For exam ple, if you wish to select on ly gr ou ps wit h m or e th 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. 2-18 ODMG OQL User Man u al Operators Like Th e like oper ator allows you t o t est par t of a ch ar act er str in g. Th e "*" ch ar acter st an ds for an y str in g in clu din g th e em pty 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 th e salar ies of all em ployees wh ose n am es begin with Sp: Order by You can obt ain a sor t ed list u sin g th e order by clau se. For exam ple, t o sor t th 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 th ou gh t h e sou r ce of t h e object s t o 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 th en by age: ODMG OQL User Man u al 2-19 2 GETTING STARTED 2.5 Set operators Th e stan dar d set oper ation 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 ite t h ese oper at or s as + (u n ion ), * (in t er sect ion ) an d (differ en ce). You can defin e an oth 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 th e qu er ies by addin g toget h er two set s: MyEmployees + YourEmployees Th e sim ple addit ion (u nion ) of th e t wo sets of em ployees gives you a set con tainin g t h e an swer : 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 th e collection , ch osen ar bit r ar ily. For exam ple: pick (MyEmployees) 2-20 ODMG OQL User Man u al Conversions 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 ator . Exam ple: listtoset (Globe.clients) intersect listtoset (TheCompanies[2].clients) Set to list To con ver t a set or bag to a list you m u st or der it . 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 "*" to bu ild a list . This avoids a r eal sor t algor ith 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 collect ion of collect ion s in t o a flat t en ed collect ion you u se t h e flatten oper ator . For exam ple: flatten (select distinct c.clients from c in TheCompanies) r et u r n s a set of clien t s. ODMG OQL User Man u al 2-21 2 GETTING STARTED 2.7 Combining operators OQL is a com plete fu n ction al lan gu age in th at ever y oper ator can be com bined wit h an y ot h er oper ator . 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 ator s, stan dar d set oper at or s as well as list con cat en at ion , or der in g an d gr ou pin g oper at or s on set s, bags and list s. 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 th e clien t s, with m or e t h an t wo or der s, by h ow m u ch t h 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 t s fr om a collect ion u sin g a specified pr edicat e or or der oper ation , it m u st scan t h e wh ole collect ion to fin d t h e r equ ir ed elem en t s. You can im pr ove per for m an ce if t h e syst em is able to dir ect ly access th e m at ch in g elem en t s. Th is is don e by est ablish 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 . 2-22 ODMG OQL User Man u al Indexes : Display index Wh en ever a pr ogr am sear ch es for elem en t s of th e collection u sin g t h e k ey, t h e syst em u ses t h e in dex t o qu ick en t h e sear ch. Th is en t ir e pr ocess is t ot ally t r an spar en t to you as th e pr ogr am m er . Th e absence or pr esen ce of an in dex h as n o effect on pr ogr am code, only on system per for m an ce. 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 eated 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 ten 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 ation Gu ide for det ails on h ow t o cr eat e an d m an age in dexes. ODMG OQL User Man u al 2-23 2 GETTING STARTED 2.9 Chapter Summary Th is chapt er h as cover ed th e followin g poin t s: • Basic queries To qu er y an y dat abase you n eed var iou s en tr y poin t s. In O 2 th ese ar e th 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 t r y poin t , applyin g a m et h od to a n am ed object, extr actin g a field, et c. • Select..from..where Th e select ... from ... where clau se en ables you t o ext r act t h ose elem en ts m eet in g a specific con dit ion fr om a list or set . • Constructing results Th e str u ctu r e of a qu er y r esu lt is ver y oft en im plicit . However , you can also constr u ct a qu er y r esu lt wit h an explicit str u ct u r e u sin g t h e struct, set an d list con str u ct or s. • Operators OQL oper at or 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 t s fr om a set or list it scan s t h e wh ole collection to fin d t h e desir ed elem en ts. You can im pr ove per for m an ce if you t ell th e syst em exact ly wh er e to look .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 t s of a n am ed collection . 2-24 ODMG OQL User Man u al OQL RA T I O N A L E 3 3 Most com m er cial object database system s n ow h ave a com m on dat a m odel based on th e OMG object m odel. Th is dat a m odel is defin ed in th e ODMG 93 r epor t . Based on t h is ODMG m odel, t h e qu er y lan gu age OQL was defin ed an d adopt ed by th e ODMG grou p. Th is ch apter is divided u p as follows: • Th e ODMG stan dar d • Th e ODMG m odel • OQL by exam ple ODMG OQL User Man u al 3-1 3 OQL RATIONALE 3.1 The ODMG standard Th e ODMG stan dar d cover s th e followin g poin t s: 3-2 1. an object m odel 2. an object defin it ion lan gu age for t h is m odel, wit h it s own syn t ax, ODL or its expr ession th r ou gh C++ an d Sm allt alk syn tax 3. an object qu er y lan gu age for th is m odel, OQL 4. a C++ bin din g allowin g C++ pr ogr am s t o oper ate on a database com plian t t o t h e object m odel 5. a Sm allt alk bin din g allowin g Sm allt alk pr ogr am s t o oper at e on a database com plian t t o t h e object m odel ODMG OQL User Man u al The ODMG model 3.2 The ODMG model Th e ODMG object m odel su ppor t s th e n otion of classes, of objects wit h at t r ibu t es an d m eth ods, of in h er it an ce an d specialization . It offer s t h e classical t ypes t o deal with st r in g, date, t im e, tim e in ter val an d t im est am p. An d fin ally, it su ppor ts t h e n ot ion s of r el at i on sh i ps an d col l ect i on s. ODMG-93 in tr odu ces a set of pr edefined gen er ic collection classes: Set<T>, Bag<T> (a m u lt i-set , i.e., a set with r epeat ed elem en ts), 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 to an oth er object th r ou gh a Ref . A Ref beh aves as a C++ poin t er , bu t with m or e sem an tics: it is a per sisten t poin t er bu t r efer en tial in t egr it y can be expr essed in t h e schem 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 etr ic. Com bin in g r elat ion ships an d collect ion s, an object can r elate t o m or e t h an on e object th 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 t ed wit h t h e sam e gu ar an tee of r efer en tial in t egr ity. ODMG-93 en ables explicit n am es to be given t o an y object or collect ion . Fr om a n am e, an application can dir ectly r et r ieve th e n am ed object an d t h en oper ate on it or n avigate t o ot h er object s followin g th e r elat ion 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 plete exam ple. We u se h er e C++ syn t ax for ou r object defin ition lan gu age, followin g th e ODMG C++ ODL bin din g (i.e., th e way of defin in g an ODMG sch em a u sin g t h e st an dar d C++ lan gu age). ODMG OQL User Man u al 3-3 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 str u ct or : a n ew Per son is bor n Ret u r n s an atom ic t ype void marriage( d_Ref<Person> spouse); Th is per son get s 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 his Per son virtual d_Set<d_String> activities(); A r edefin able m eth od }; class Employee: Person{ A su bclass of Per son float salary; Meth od virtual d_Set<d_String> activities(); Th is m eth od is r edefin ed }; 3-4 ODMG OQL User Man u al The ODMG model class Student: Person{ A su bclass of Per son d_String grade; Met h od virtual d_Set<d_String> activities(); Th e m eth od is r edefin ed }; ODMG OQL User Man u al 3-5 3 OQL RATIONALE class Address{ int number; d_String street; }; class Building{ Address address; A com plex valu e Address em bedded in th is object d_List< <d_Ref<Apartment> > apartments inverse building; Meth 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 exten t d_Set< d_Ref<Apartment> > Vacancy; Th e set of vacan t appar t em en ts d_List< d_Ref<Apartment> > Directory; Th e list of appar t em en t s or der ed by th eir n u m ber of r oom s }; 3-6 ODMG 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 section , an d in st ead of tr 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 ter a database t h r ou gh a n am ed object , bu t m or e gen er ally as soon as on e gets 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 th e r igh t dat a on e n eeds. To do th 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 objects, as well as to follow sim ple r elat ion sh ips. For in stan ce, given a Per son p t o k n ow th e n am e of t h e str eet wh er e t h 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 th e com plex at tr ibu t e of t ype Address to get t h e str eet n am e. Th is exam ple tr eated 1-1 r elat ion sh ip, let u s n ow look at n -p r elat ion sh ips. Assu m e we wan t th 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 th e in t er pr et at ion 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 otat ion t o t r aver se su ch a m u ltiple r elation sh ip an d we u se th 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 th is qu er y is a valu e of t ype Bag<St r in g>. If we wan t t o get a Set , we sim ply dr op du plicates, lik e in SQL by u sin g t h e distinct k eywor d. select distinct c.name from c in p.children 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. ODMG OQL User Man u al 3-7 3 OQL RATIONALE 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 database. We k n ow t h e collect ion n am ed Persons con tains all t h e per son s of t h e dat abase. We h ave n ow to t r aver se two collect ion s: Persons an d Person::children. Lik e in SQL, t h e selectfrom oper ator allows u s to qu er y m or e th an on e collection . Th ese collect ion s t h en appear in t h e from par t. In OQL, a collection in t h e from par t can be der ived fr om a pr eviou s on e by followin g a path wh ich st ar ts fr om it , an d th 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. Its r esu lt is of t h e t ype Bag<Addr ess>. • Predicate Of cou r se, th e where clau se can be u sed to defin e an y pr edicat e wh ich th en ser ves t o select th e dat a m at ch in g th e pr edicat e. For in st an ce, t o r est r ict th e pr eviou s r esu lt to th 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 t m en t as t heir par en ts, th 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, collection s wh ich ar e n ot dir ectly r elated can also be declar ed. As in SQL, t h is allows u s t o com pu te “join s” bet ween t h ese collect ion s. For in stan ce, t o fin d th e people livin g in a str eet an d h avin g th e sam e n am e as t h is str eet, we do th e followin g: t h e Building ext en t is n ot defin ed in t h e schem a, so we h ave to com pu t e it fr om t h e Apartments ext en t . To com pu t e t h is int er m ediate r esu lt , we n eed a select-from oper ator again . So t h e join is don e as follows: 3-8 ODMG 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 optim izer . In t his 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 th at an object qu er y lan gu age m u st m an ipu late 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 t er m ediat e com pu t at ion . 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, to obt ain t h e addr esses of t h e ch ildr en of each per son , alon g wit h t h e addr ess of t h is per son , we u se th 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 ODMG OQL User Man u al 3-9 3 OQL RATIONALE Th is gives, for each per son , t h e n am e, t h e addr ess, an d th e n am e an d addr ess of each ch ild. Th e type 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 eate com plex object s. For t h is pu r pose, it u ses th e n am e of a class as a con st r u ct or . At tr ibu t es of t h e object of t his class can be in itialized explicit ly by an y valid expr ession . For in st an ce, t o cr eate a n ew bu ildin g wit h 2 apar t m en t s, if th er e is a type n am e in t h e sch em a, called List_apart, defin ed by: tydedef List<<Ref<Apartment> > List_apart; th 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 th e r esu lt type of t h e m et h od m at ch es th e expect ed type in t h e qu er y. In case th e m eth od h as n o par am et er , t h e syn t ax for m eth od call is t h e sam e as for accessin g an at tr ibu t e 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, th ese ar e given bet ween par en th esis. Th is flexible syn t ax fr ees t h e u ser fr om k n owin g wh et h er th e pr oper ty is 3-10 ODMG OQL User Man u al OQL by example : Polymorphism st or ed (an at tr ibu t e) or com pu t ed (a m et h od). For in stan ce, to get t h e age of t h e oldest ch ild of “Paul”, we wr it e th 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 eth od can r et u r n a com plex object or a collect ion an d t h en it s call can be em bedded in a com plex path expr ession . For in st an ce, in side a bu ildin g b, to k n ow wh o in h abits t h ose least expen sive apar t m en t, we u se t h e followin g pat h expr ession : b.less_expensive.is_used_by.name Alth ou gh less_expensive is a m et h od we “tr aver se” it as if it wer e a r elat ion sh ip. Polymorphism A m ajor con tr ibu t ion of object t ech n ology is t h e possibilit y of m an ipu lat in g polym or ph ic collect ion s, an d th an k s t o t h e “lat e bin din g” m ech an ism , t o carr y ou t gen eric act ion s on t h e elem en t s of th ese collect ion s. For in st an ce, th e set Persons con tain s object s of class Person, Employee an d Student. So far , all t h e qu er ies again st t h e Persons ext en t dealt wit h t h e t h r ee possible classes of object s of t h e collect ion . 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 atch es t hose 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 dition for an efficien t qu er y opt im izer . Wh en a polym or ph ic collect ion is filt er ed (for in st an ce Persons), its elem en ts ar e st at ically k n own to be of t h at class (for in st an ce Person). Th is m ean s th at a pr oper ty of a su bclass (at t r ibu te or m eth od) can n ot be applied t o su ch an elem en t, except in t wo im por t an t cases: lat e bin din g t o a m et h od, or explicit class in dicat ion . • Late binding To list t h e activities of each per son , we u se th e followin g qu er y: select p.activities from p in Persons ODMG OQL User Man u al 3-11 3 OQL RATIONALE activities is a m eth od wh ich h as 3 incar 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 th e cu r r en t p, t h e r igh t in car n at ion is called. • Class indicator To go down t h e class hier ar ch y, a u ser m ay explicitly declar e t h e class of an object th at can n ot be in fer r ed st at ically. Th e in t er pr et er th en h as to ch eck at r u n t im e, th at th is object actu ally belon gs t o th e in dicat ed 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 t s” spen d th eir t im e in followin g a cou r se of st u dy, we can select th ose per son s and get t h eir gr ade. We explicitly in dicat e in t h e qu er y t h at th 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 type syst em is r espected. Th is is wh y th e lan gu age is so sim ple an d it s m an u al so sh or t . This 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 to th e t ype syst em . Adopt in g a com plete or th ogon ality, 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 th e oper at or s offer ed by OQL bu t n ot yet in tr 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 ator s an d t h e aggr egat ive oper ator s (count, sum, min, max an d avg). To illu st r at e th is fr ee com position 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 to k n ow th e n am e of th e str eet wh er e th e set of em ployees livin g on t h at str eet an d have t h e sm allest aver age salar y, com par ed t o t h e set s of em ployees living in ot h er st r eet s. We pr oceed st ep by st ep an d u se t h e define OQL in st r u ction to evalu at e t em por ar y r esu lts. 3-12 ODMG OQL User Man u al OQL by example : Operator composition 1. Bu ild t h e exten t of class Employee (n ot su ppor ted 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 str 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 splits t h e em ployees in t o par t ition s, accor din g t o t h e cr iter ion (t h e n am e of th e st r eet wh er e th is per son lives). Th e select clau se com pu t es, in each par t ition , t h e aver age of th e salar ies of t h e em ployees belon gin g t o t h is par t it 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 t h is set by salar y define sorted_salary_map as select s from s in salary_map order by s.average_salary Th e r esu lt is of t ype: List<struct{String street; float average_salary;}> ODMG OQL User Man u al 3-13 3 OQL RATIONALE 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 have wr itt en : (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 3-14 ODMG OQL User Man u al 4 OQL RE F E RE N C E 4 Th is ch apter gives th e fu ll r efer en cial in for m ation of th e object qu er y lan gu age OQL. It is divided in to th e followin g section s: • In t r odu ction • Pr in ciples • Lan gu age Defin it ion • Syn tact ical Abbr eviat ion s • OQL BNF Th e in for m ation given below is th e sam e as th at of th e ODMG st an dar d 1 wit h n ot es added on h ow to u se t h is lan gu age wit h O 2 . 1. Th e Object Dat abase St an dar d: ODM G - 93. At wood, Du h l, Fer r an , Loom is an d Wade. Edit ed by R.G.G. Cat t ell. © 1996 Mor gan Kau fm an Pu blish er s. ODMG OQL User Man u al 4-1 4 4.1 Introduction In th 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 tax, its sem an tics, 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 apt er en ds with th 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 ption s: • OQL r elies on th e ODMG object m odel. • OQL is a su per set of th 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 ten ce wh ich r u n s on r elation al t ables, wor k s with t he sam e syn tax an d sem an tics 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 objects, object iden t ity, path expr ession , polym or ph ism , oper ation in vocat ion, lat e bin din g etc... • OQL pr ovides h igh -level pr im itives t o deal with sets of object s bu t does n ot r est r ict it s att en t ion t o t h is collect ion con str u ct. Th u s, it also pr ovides pr im it ives t o deal wit h st r u ctu r es, list s, ar r ays, an d t r eat s all su ch con str u ct s with t h e sam e efficien cy. • OQL is a fu n ct ion al lan gu age wh er e oper ator 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 to th e ODMG t ype m odel, an d t h u s can be qu er ied again . • OQL is n ot com pu tation ally com plet e. It is an easy t o u se qu er y lan gu age wh ich pr ovides easy access t o an object dat abase. • Based on t h e sam e t ype system , 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 ODMG bin din g is defin ed, e.g., C++ and Sm allTalk . Con ver sely, OQL can in vok e oper ation s pr ogr am m ed in th ese lan gu ages. • OQL does n ot pr ovide explicit u pdat e oper at or s bu t r at h er can in vok e oper ation s defin ed on object s 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 Database wh ich , by defin it ion , is m an aged by t h e "m eth ods" defin ed on th e object s. • OQL pr ovides declar at ive access t o object s. Th u s OQL qu er ies can be easily opt im ized by vir tu e of t h is declar at ive n at u r e. • Th e for m al sem an t ics of OQL can easily be defin ed. 4-2 ODMG 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 typed oper an ds com posed r ecu r sively by oper at or s. We will u se t h e t er m expression t o design at e a valid qu er y in t h is section . 4.3.1 Query Program A qu er y pr ogr am consist 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 th e qu er y it self. Th e set of qu er y defin ition expr ession s is n on r ecu r sive (alt h ou gh a qu er y m ay call an oper at ion 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 t h e set jones of stu den ts n am ed J on es, an d evalu ates th e set of th eir student_ids. O2 note With t h e O2 qu er y in ter pr et er you u se CTRL-D (on Un ix) or CTRL-Z (On Windows) between t wo qu er ies r ath er th at ";". 4.3.2 Named Query Definition If q is an iden t ifier an d e is a qu er y expr ession , th 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" Th is st at em en t defin es Does as a qu er y r etu r n ing a bag con t ain in g all th 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 statem en t defin es Doe as a qu er y wh ich r etu r n s t h e st u den t wh ose n am e is Doe (if t h er e is on ly on e, oth er wise an exception is r aised). ODMG OQL User Man u al 4-3 4 O2 note • def i n e oper ation is available on ly with t h e in t er active 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 , O2 C) becau se st an dar d pr ogr am m in g lan gu age var iables can be u sed for th 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 he qu er y: di spl ay qu er i es 4-4 ODMG OQL User Man u al Language Definition : Elementary Expressions 4.3.3 Elementary Expressions 4.3.3.1 Atomic Literals If l is an at om ic liter al, th en l is an expr ession wh ose valu e is t h e liter al it self. Lit er als h ave t h e u su al syn tax: • Object Liter al: n il • Boolean Liter al: false, t r u e • In t eger Lit er al: sequ en ce of digit s, e.g, 27 • Float Lit er al: m an tissa/ expon en t. Th e expon en t is opt ion al, e.g., 3.14 or 314.16e-2 • Ch ar acter Lit er al: ch ar act er bet ween sim ple qu ot es, e.g., ’z’ • Str in g Lit er al: ch ar acter 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 tit y at t ach ed to th e n am e. Exam ple: Students Th is qu er y defin es t he set of stu 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 to t h e exten t of object s of th 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..., th 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 hen q is an expr ession . Exam ple: Doe Th is qu er y r et u r n s t he stu den t wit h nam e Doe. It r efer s t o t h e qu er y definit ion expr ession declar ed in Sect ion 4.3.2. ODMG OQL User Man u al 4-5 4 4.3.4 Construction Expressions 4.3.4.1 Constructing Objects If t is a type n am e, p1, p2, ...,pn ar e pr oper t ies of t, an d e1, e2, ...,en ar e expr ession s, t h 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 ties p1, p2, ...,pn ar e in itialized wit h t h e expr ession s e1, e2, ...,en. The t ype of ei m u st be com pat ible with t h e t ype of pi. If t is a type n am e of a collect ion an d e is a collect ion lit er al, t h en t(e) is a collect ion object . The t ype of e m u st be com patible with t. Exam ples: Employee (name: "Peter", boss: Chairman) Th is cr eates a m u table Em ployee object. vectint (set(1,3,10)) Th is cr eates a m u table set object (assu m ing th at vect i n t is th e n am e of a class wh ose t ype 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 th e str u ct u r e tak in g valu es e1, e2, ..., en on pr oper t ies p1, p2, ...,pn. Not e th at th is dyn am ically cr eat es an in st an ce of th e t ype struct(p1: t1, p2: t2, ..., pn: tn) if ti is th e t ype of ei. Exam ple: struct(name: "Peter", age: 25); Th is r et u r n s a st r u ctu r e wit h two at tr ibu t es name an d age tak in g r espect ive valu es Peter an d 25. See also abbr eviat ed syn t ax in som e cont ext s, in Sect ion 4.4.1. 4-6 ODMG OQL User Man u al Language Definition : Construction Expressions 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 t ain in g t h e elem en ts 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 sistin g of t h e th 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 t h e list h avin g elem en t s e1, e2, ..., en. Th ey cr eat e a list in stan ce. If m in , m ax ar e two expr ession s of in t eger or ch ar acter types, su ch th at m in < m ax, t h en 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. ODMG OQL User Man u al 4-7 4 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 ts 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 ts. 4.3.4.6 Constructing Arrays If e1, e2, ..., en ar e expr ession s, th en array(e1, e2, ..., en) is an expr ession . It defin es an ar r ay h avin g elem en ts 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 ts. 4.3.5 Atomic Types Expressions 4.3.5.1 Unary Expressions If e is an expr ession and <op> is a u n ar y oper at ion valid for t h e t ype of e, t h en <op> e is an expr ession . It defin es th 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 ator is: n ot . Exam ple: not true Th is r et u r n s false. 4-8 ODMG OQL User Man u al Language Definition : Atomic Types Expressions 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 , th en e1<op>e2 is an expr ession . It defin es th e r esu lt of applyin g <op> to 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 ator s ar e: +, -, *, / Relation al bin ar y oper at or s ar e: =, !=. <. <=, >, >= Th ese oper ator s ar e defin ed on all at om ic types. Boolean bin ar y oper ator 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 + s 2 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 th e t wo st r in gs. O2 note In O2 t h e oper at or | | is n ot accept ed. To con caten at e 2 st r in gs u se "+". If c is an expr ession of t ype ch ar acter , an d s an expr ession of t ype st r in g, th en c in s is an expr ession of t ype boolean wh ose valu e is tr u e if th e ch ar act er belongs t o t h e st r in g, else false. ODMG OQL User Man u al 4-9 4 If s is an expr ession of t ype str in g, an d i is an expr ession of type in teger , th 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 acter of th e str in g. If s is an expr ession of t ype str in g, an d low an d up ar e expr ession s of type in teger , 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 th ch ar acter u p to th e u p+1 t h char act er . If s is an expr ession of t ype str in g, an d pattern a str in g lit er al wh ich m ay in clu de t h e wildcar d ch ar acter 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, th en s like pattern is an expr ession of t ype boolean wh ose valu e is t r u e if s m atch es t h e patt er n , else false. Exam ple: ’a nice string’ like ’%nice%str_ng’ is t r u e. O2 note In O2 t h e on ly su ppor t ed wildcar d is "*". 4-10 ODMG OQL User Man u al Language Definition : Object Expressions 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 (object s wit h iden tit y) of th e sam e t ype, t h en e 1 = e2 e 1 != e2 an d 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 ate t h e sam e object. Exam ple: Doe = element(select s from Students s where s.name = "Doe") is t r u e. 4.3.6.2 Comparison of Immutable Objects If e1 an d e2 ar e expr essions wh ich den ote 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, th en e->p an d e.p ar e expr ession s. Th ese ar e alter n ate syn tax to extr act t h e pr oper t y p of an object e. If e h appen s t o design ate a delet ed or a n on existin g object , i.e. nil, an at t em pt t o access th e at t r ibu te 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 test explicit ly if an object is differ en t fr om nil befor e accessin g a pr oper ty. Exam ple: Doe.name Th is r et u r n s Doe. ODMG OQL User Man u al 4-11 4 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 t o a r ecen t evolu t ion of th e ODMG stan 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 path is always false. Th is m ean s t h at OQL n ow sk ips su ch elem en t s an d t h u s th e explicit t est to nil is n ot yet m an dator 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 alter n at e syn tax 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 at ion or else t h e object nil, if t h e oper ation r et u r n s n ot h in g. Exam ple: jones->number_of_students Th is applies t h e oper at ion number_of_students t o 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 at ion 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 t o 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 t h e oper at ion or else th e object n il, if t h e oper at ion r et u r n s n ot h in g. In bot h cases, 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 apply an oper at ion to it r aises an exception . However , a qu er y m ay test explicit ly if an object is differ en t fr om nil befor e applyin g an oper at ion . 4-12 ODMG OQL User Man u al Language Definition : Object Expressions Exam ple: Doe->apply_course("Maths", Turing)->number Th is qu er y calls th e oper ation apply_course on class St u den t for t h e object Doe. 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 ation 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 th e n u m ber of t h is cou r se. ODMG OQL User Man u al 4-13 4 4.3.6.6 Dereferencing an Object If e is an expr ession wh ich den otes an object with iden tit y (a m u table object), t h en *e is an expr ession wh ich deliver s t h e valu e of th e object (a liter al). Exam ple: Given two var iables of type Per son , p1 an d p2, t h e pr edicate 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 tr u e if th e object s h ave th e sam e valu es, even if t h ey ar e n ot t h e sam e objects. 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 otes a collection an d e2 a pr edicate, 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 ts 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 objects in t he Students set h ave a posit ive valu e for t h eir student_id at t r ibu te. Ot h er wise it r et u r n s false. 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 otes a collect ion an d e2 a pr edicat e, t h en exists x in e1: e2 is an expr ession . It r et u r n s true if th er e is at least on e elem en t of collect ion e1 th at satisfies e2 an d false oth er wise. Exam ple: exists x in Doe.takes: x.taught_by.name = "Turing" 4-14 ODMG OQL User Man u al Language Definition : Collections Expressions Th is r et u r n s true if at least on e cou r se Doe t ak es is tau gh t by som eon e n am ed Tu r in g. If e is a collection expr ession , th 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 tr u e if t h er e exists at least on e elem en t in t h e collect ion , while t h e secon d on e r et u r n s tr u e, if th er e exist s 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 ested qu er ies su ch as: select ... from col where exists ( select ... from col1 where predicate) Th e nest ed qu er y r et u r n s a bag t o 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 t o com pu t e effect ively t h e in ter m ediat e bag r esu lt . O2 note In O2 th ese t wo last oper ation 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 t h e t ype of it s elem en t s, t h en e1 in e2 is an expr ession . It r et u r n s true if elem en t e1 belongs t o collect ion e2. Exam ple: Doe in Does Th is r et u r n s true. 4.3.7.4 Aggregate Operators If e is an expr ession wh ich den ot es a collection , if <op> is an oper ator 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. ODMG OQL User Man u al 4-15 4 4.3.8 Select From Where If e1, e2, ..., en ar e expr ession s wh ich den ot e collect ions, 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 th e ch ar act er *, th 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, th en th e r esu lt is obt ain ed as follows: t ak e th e car t esian pr odu ct 1 of t h e set s e1, e2, ..., en; filt er t h at pr odu ct by expr ession e’ (i.e., elim in ate fr om th e r esu lt all object s th at do n ot sat isfy boolean expr ession e’); apply th e projection t o each on e of t h e elem en ts of t h is filter ed set an d get t h e r esu lt. Wh en t h e r esu lt is a set (distin 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 tle m or e com plex. Th e select oper ator fir st con ver ts all t h ese collect ion s in t o set s an d applies t h e pr eviou s oper at ion . Th e r esu lt is a set (distin ct case) or else a bag. So, in th is case, we sim ply t r an sfor m each of t h e ei’s in to a set an d apply th e pr eviou s defin it ion . 4.3.8.1 Projection Befor e t h e pr ojection , th e r esu lt of t h e it er at ion over t h e from var iables is of type bag< struct(x1: type_of(e1 elements), ... xn: type_of(en elements)) > Th e pr ojection is con st r u ct ed by an expr ession wh ich can t h en r efer im plicitly t o t h e "cu r r en t" elem en t of t h is bag, u sin g th 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 ter n al system n am e (wh ich is n ot accessible by th e qu er y an yway). By con ven t ion , if t h e project ion is sim ply "*", t h en t h e resu lt of th e select ion is th e sam e as t h e r esu lt of t h e iter ation . If t h e pr oject ion is "distin ct *", th e r esu lt of t h e select is t h is bag con ver ted in t o a set. 1. Th e car t esian pr odu ct bet ween a set an d a bag is defin ed by fir st con ver t in g t h e set in t o a bag, an d t h en get t in g t h e r esu lt in g bag wh ich is t h e car t esian pr odu ct of t h e t wo bags. 4-16 ODMG OQL User Man u al Language Definition : Select From Where In all ot h er cases, th e pr ojection is explicit ly com pu t ed by th 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 objects of t ype couple givin g st u den t n am es an d t h e nam 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, giving for each stu den t "object", t h e sect ion object followed by th e stu den t an d th e fu ll pr ofessor "object " t eachin g in th is sect ion : bag< struct(x: Student, y: Section, z: Professor) > 4.3.8.2 Iterator Variables A var iable, xi, declar ed in th e from par t r an ges over t h e collection ei an d t h u s h as t h e type of th e elem en t s of th is collect ion . Su ch a var iable can be u sed in an y ot h er par t of t h e qu er y to evalu ate an y ot h er expr ession s (see th e Scope Ru les in Sect ion 4.3.15). Syn t act ical var iation s ar e accepted for declar in g t h ese var iables, exact ly as wit h SQL. Th e as k eywor d m ay be om it ted. 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. ODMG OQL User Man u al 4-17 4 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 to declar e a var iable x: "... fr 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 th e ODMG 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 ted, with t h e m ean ing of a t r u e pr edicate. 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 predicate a boolean expr ession, th en select_query group by partition_attributes is an expr ession an d select_query group by partition_attributes having predicate is an expr ession . Th e car t esian pr odu ct visit ed by th 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, th e par tit ion at tr 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 th e given par tit ion at tr ibu t es, belon g t o th e sam e par t it ion . Th u s t he par t ition ed set , aft er t h e gr ou pin g oper ation 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 tit ion (th e valu ed partition_attributes), com plet ed by a pr oper t y wh ich is con ven t ion ally called partition an d wh ich is t h e bag of all object s m at ch in g th is par t icu lar valu ed par tit ion . 4-18 ODMG OQL User Man u al Language Definition : Group-by Operator If th e par tit ion att r ibu tes 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 th e from clau se declar es t h e var iables v1 on collect ion col1, v2 on col2, ..., vn on coln, th 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 collection h avin g th e t ype of th e elem en t s of t h e cor r espon din g collection . partition: bag< struct(v1: type_of(col1 elements), ... , vn: type_of(coln elements))>. If a collect ion colk h as n o var iable declar ed th e cor r espon din g at t r ibu te h as an in t er n al syst em n am e. Th is par t ition ed set m ay th en be filter ed by t h e pr edicat e of a having clau se. Fin ally, t h e r esu lt is com pu t ed by evalu at in g th e select clau se for t h is par t ition ed an d filter ed set. Th e having clau se can th u s apply aggr egate fu n ct ion s on partition, lik ewise t h e select clau se can r efer t o partition to com pu t e th e fin al r esu lt. Bot h clau ses can r efer also to t h e par tit ion att r ibu tes. 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 ts, 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 th at en t er in t h is categor 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 han ces t h e fir st on e wit h a having clau se wh ich en ables you t o filter 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 . ODMG OQL User Man u al 4-19 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 tm 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 t h 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 tax of partition_attributes does not accept th e k eywor d st r u ct an d th u s is always given as a list of cr it er 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 th e select ed elem en ts sor t ed by th e fu n ct ion e1, an d in side each su bset yielding t h e sam e e1, sor t ed by e2, ... , an d t h e fin al su bsu b...set , sor ted by en. Exam ple: select p from Persons p order by p.age, p.name Th is sor ts t h e set of per son s on th eir age, th en on th eir n am e an d pu t s th e sor ted object s in to th e r esu lt as a list. Each sor t expr ession cr it er ion can be followed by th e k eywor d asc or desc, specifyin g r espect ively an ascen din g or descen din g or der . Th e defau lt or der is th at of th e pr eviou s declar at ion . For t he fir st expr ession , th e defau lt is ascen din g. 4-20 ODMG 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 t h e e2+1 th elem en t of t h e in dexed collection 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 isit e of Math 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, t h en e1[e2:e3] is an expr ession . Th is ext r acts t h e su bcollect ion of e1 star t in g at posit ion e2 an d en din g at position e3. Exam ple: list (a,b,c,d) [1:3] Th is r et u r n s list (b,c,d). ODMG OQL User Man u al 4-21 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 sistin g of t h e fir st t h r ee pr er equ isites of Math 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, t h en <op>(e) is an expr ession . Th is ext r act s 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 Math 101. 4.3.11.4 Concatenating Two Indexed Collections If e1 and e2 ar e expr ession s, if e1 an d e2 ar e bot h lists or bot h ar r ays, th en e1+e2 is an expr ession . Th is com pu tes t h e con caten at ion of e1 an d e2 . Exam ple: list (1,2) + list( 2,3) Th is qu er y gen er at es list (1,2,2,3). 4.3.12 4-22 Binary Set Expressions ODMG OQL User Man u al Language Definition : 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 sets or bags, t h en e1 <op> e2 is an expr ession . Th is com pu t es set th eor et ic oper ation s, u n ion , differ en ce, an d in ter sect ion on e1 and e2, as defin ed in Ch apter 2. Wh en th e collect ion k in ds of th e oper an ds ar e differ en t (bag an d set ), th 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 t s wh o ar e n ot Teach in g Assistan ts. bag(2,2,3,3,3) union bag(2,3,3,3) Th is bag expr ession r et u 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 t er sect ion of 2 bags yields a bag t h at con tain s th e m in im u m for each of t h 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 et u r n s bag(2) 4.3.12.2 Inclusion If e1 an d e2 ar e expr ession s wh ich den ot e sets or bags, if <op> is an oper at or fr om {<, <=, >, >=}, t h en e1 <op> e2 is an expr ession wh ose valu e is a boolean . Wh en th e oper an ds ar e differ en t k in ds of collection s (bag an d set), th e set is fir st con ver t ed in t o a bag. 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 ODMG OQL User Man u al 4-23 4 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 tak es t h e sin glet on e an d r etu r ns it s elem en t. If e is n ot a sin glet on th is r aises an except ion . 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 her 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 th e list in to a set , by for m in g t h e set con tain in g all t h e elem en t s of th e list . Exam ple: listtoset (list(1,2,3,2)) Th is r et u r n s t h e set cont 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 t h e or der by oper ator . If you ar e not in t er ested 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 * 4-24 ODMG 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 , th en distinct(e) is an expr ession wh ose valu e is t h 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 collect ion , 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 collection s of t in t o a collect ion of t. So t h is flat ten in g oper ates at t h e fir st level on ly. Assu m in g th e t ype of e t o be col1<col2<t>>, t h e r esu lt of flatten(e) is: • If col2 is a set (r esp. a bag), t h e u n ion of all col2<t> is don e an d t h 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 th e or der in col1 an d th e r esu lt is col2<t>, which 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 at ion . • If col2 is a list or an ar r ay an d col1 is a set or a bag, t he lists or ar r ays ar e con ver ted in t o sets, 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>, th er efor e wit h 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 tain 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))) Th is r et u r n s t h e set con tain in g 1,2,3. ODMG OQL User Man u al 4-25 4 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 tim e. Th is is u sefu l t o access a pr oper ty of an object wh ich is statically 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 ts wh o ar e teach in g assist an ts, assu m in g t h at Students an d Sections ar e t h e ext en t s 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, th en f() and f(e1, e2, ... , en) ar e expr ession s wh ose valu e is t h e valu e r et u r n ed by th e fu n ct ion , or t h e object nil, wh en t h e fu n ct ion does n ot r et u r n an y valu e. Th e fir st for m allows you t o call a fu n ct ion wit h ou t a par am eter , wh ile t h e secon d on e calls a fu n ction with t he 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 ction is wr it t en . Th is feat u r e allows you to sm oot h ly exten d t h e fu n ct ion ality of OQL wit h ou t ch an gin g th 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 th e filter 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 th e select -fr om -wh er e expr ession in clu din g n ested su b-expr ession s. 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 th e n am e partition alon g wit h possible explicit at t r ibu te n am es wh ich ch ar acter ize th e par t ition . These n am es ar e visible in t h e cor r espon din g having an d select par t s, in clu din g n est ed su b-expr ession s wit h in t h ese par t s. 4-26 ODMG OQL User Man u al Language Definition : Scope Rules In side a scope, you u se t h ese var iable n am es to con str u ct pat h expr ession s an d r each pr oper ties (at tr 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 stan ce, in t h e scope of th e fir st fr om clau se above, you access t h e age of a per son by p.age. Wh en th e var iable is im plicit, as in th 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 th er e is n o am bigu it y, you can u se t h e pr oper t y n am e dir ectly as a sh or t cu t , with ou t u sin g t h e var iable n am e t o open th 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 it y wh en a pr oper t y n am e is defin ed for on e an d on ly on e object den oted by a visible var iable. To su m m ar ize, a n am e appear in g in a (n ested) qu er y is look ed u p in t h e followin g or der : • a var iable in t h e cu r r ent scope, or • a n am ed qu er y in t r odu ced by th e define clau se, or • a n am ed object, i.e., an en tr y poin t in t he dat abase, or • an at tr ibu t e n am e or an oper ation n am e of a var iable in th 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 ty n am e belon gs to 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 defined. 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 ) select In scope1 , we see t h e n am es: Per son s, c, Cities, all pr oper ty 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 th ey ar e n ot called "Per son s", "c", n or "Cities". In scope2 , we see t h e n am es: ch ild, Per son s, c, Cities, t h e pr oper t y n am es of th e class City wh ich ar e n ot pr oper t y of th 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". ODMG OQL User Man u al 4-27 4 In scope3 , we see t h e nam es: age, par tit ion , an d t h e sam e n am es fr om scope1, except "age" an d "par tit ion ", if t h ey exist . In scope4 , we see t h e nam es: age, par tit ion , p, v, an d t he sam e n am es fr om scope1, except "age", "par tit ion ", "p" an d "v", if t h ey exist. In scope5 , we see th e n am es: p, an d t h e sam e n am es fr om scope1, except "p", if it exists. In scope6 , we see t h e n am es: p, v, Per son s, c, Cit ies, th 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 at t r ibu te of th e class Per son can be accessed dir ect ly sin ce t h ey ar e am bigu ou s between "ch ild" an d "Per son s". O2 note Im plicit att r ibu te scope is n ot available wit h O2 . You m u st always access an att r ibu te wit h th e dot n ot at ion : v.att. 4.4 Syntactical Abbreviations OQL defin es an or th ogon al expr ession lan gu age, in t h e sen se th at all oper at or s can be com posed with each oth er s as soon as t h e types of th e oper an ds ar e cor r ect. To ach ieve th is pr oper ty, 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 hu 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 t h e wh ole DML 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 ction s each t im e SQL in t r odu ces a syn t ax wh ich can n ot en ter in th e cat egor y of tr u e oper at or s. Th is section gives th e list of t h ese con st r u ct ion s th 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 ct ion 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 t im e t h e sem an tics of th ese con st r u ct ion s, sin ce all oper at or s u sed for t h is descr ipt ion h ave alr eady been defin ed. 4-28 ODMG OQL User Man u al Syntactical Abbreviations : Structure Construction 4.4.1 Structure Construction Th e st r u ctu r e con st r u ct or was in t r odu ced in Section 4.3.4.2. Alt er n ate syn t ax ar e allowed in t wo con t ext s: select clau se an d gr ou p-by clau se. In bot h con text s, th e SQL syn t ax is accept ed, alon g wit h th e on e alr eady defined. sel ect projection {, projection } ... select ... gr ou p by projection {, projection } wh er e projection is in on e of th 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 alter n ate syn t ax for : st r u ct (iden tifier : 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 ter pr eted as a str u ct u r e con st r u ct ion bu t r ath er t h e expr ession st an ds as it is. Fu r th er m or e, a (iii) expr ession is on ly valid if it is possible to in fer th e n am e of t h e cor r espon din g at tr ibu t e (t h e iden tifier ). Th is r equ ir es th at t h e expr ession den otes a pat h expr ession (possibly of len gt h on e) en din g in a pr oper t y wh ose n am e is t h en ch osen as t h e iden tifier . 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 str u ct u r es: bag<struct(name: string, salary: float, student_id: integer)> O2 note O2 accepts t h e 3 alt er n at ives of th e projection syn t ax in t h e select par t , as well as t h e struct syn t ax. In t h e group by par t, O 2 accept s t h e 3 alt er n atives bu t does n ot accept t h e struct syn tax. ODMG OQL User Man u al 4-29 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 ct ion n al for t h em . So OQL accepts t h is syn t ax too. 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 egate Oper at or abbr eviation 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 he t ype of its elem en ts, if relation is a r elation al oper ator (=, !=, <, <=, > , >=), t h 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 4-30 ODMG OQL User Man u al Syntactical Abbreviations : String Literal O2 note In O2 Com posit e Pr edicat e abbr eviation s ar e n ot su ppor t ed. 4.4.4 String Literal OQL accepts sim ple qu ot es as well t o delim it a st r in g (see Section 4.3.3.1), as SQL does. Th is in tr odu ces an am bigu ity for a st r in g with on e ch ar acter wh ich t h en h as th e sam e syn t ax as a ch ar acter lit er al. Th is am bigu ity is solved by con t ext. O2 note In O2 a str in g m u st be delim ited by dou ble qu ot es. ODMG OQL User Man u al 4-31 4 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 ence of 0 or m or e sym bol(s). • [sym bol ] m ean s an opt ional sym bol. Do n ot con fu se wit h t h e separ ator s [ ] • keyword is a t er m in al of th e gr am m ar . Keywor ds ar e n ot case sen sit ive. • xxx_n am e h as th e syn tax of an iden tifier • xxx_lit er al is self explan ator y, e.g., "a str in g" is a st r in g_lit er al • bin d_ar gu m en t st an ds for a par am eter 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 explains wh y m ost of th e tim 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 to be of t h e r igh t type. 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 after t h e gr am m ar . Som e syn tact ical am bigu ities ar e solved sem an tically fr om th e t ypes of th e oper an ds. 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) 1. A bin d ar gu m en t allows t o bin d expr ession s fr om a pr ogr am m in g lan gu age t o a qu er y when em bedded in t o t h is lan gu age (see Ch apt er s on lan gu age bin din gs). 4-32 ODMG OQL User Man u al OQL BNF : Grammar 4.5.1.3 Simple Expression (see Section 4.3.5) query ::= query + query1 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 ::= >= 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}]) 1. Th e oper at or + is also u sed for list an d ar r ay con cat en at ion . ODMG OQL User Man u al 4-33 4 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) 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] 4-34 ODMG OQL User Man u al OQL BNF : Operator Priorities 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 ator 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 he sam e pr ior it y an d gr ou p left -t o-r igh t . () [] n ot . -> - (u n ar y) + (u n ar y) in * / m od i n t er sect + - u n i on ex cept < > <= oper at or s) = != an d >= || < som e < an y < al l (et c ... for all com par ison like ex i st s for all or .. : , (iden tifier ) t h is is t h e cast oper at or order having group by where from select ODMG OQL User Man u al 4-35 4 4-36 ODMG OQL User Man u al I ND EX ODMG OQL User Man u al I-i INDEX Sym bol s C + 2-20, 2-20 C 1-3 C++ In ter face 1-3 A Accessor 4-34 Addition of set s 2-20 Aggr egative oper at or s 3-12 Ar ch it ectu r e O2 1-2 Ar it h m et ic 4-33 Ar r ay 2-6, 2-6, 2-9 Con st r u ct in g 4-8 Set con ver sion 2-21 array 2-11, 2-12, 3-9 Ar r ay valu e 2-6 At om ic valu e 2-5 At t r ibu te 4-11 avg 2-13, 3-12, 4-15 C++ bin din g 3-2, 3-3 Class in dicat or 3-12 Collect ion 2-6, 3-3, 3-7, 4-25 in dexed expr ession 4-21 Nam ed 2-22 Collect ion expr ession 4-34 Com bin in g oper at or s 2-12, 2-22 Com par ison 4-33 con cat en at ion 4-22 Con st r u ct ion Ar r ay 3-9 Bag 3-9 List 3-9 Set 3-9 St r u ct 3-9 Con st r u ct or 2-11, 3-9, 4-33 Con ver sion 2-21, 4-24, 4-35 count 2-13, 3-12, 4-15 Cr eat in g objects 2-12 B D Bag 2-6, 2-12 Con st r u ct in g 4-8 Data m an ipu lat ion 3-9 Database en t r y poin t 2-4 define 2-14, 2-20, 3-12 differ en ce 4-23 distinct 2-7, 3-7 bag 2-11, 2-12, 3-9 Boolean 4-33 Br owser In t er face 1-5 Un ix 1-5 Win dows NT 1-6 by 2-19 I-ii ODMG OQL User Man u al INDEX E I element 2-14, 4-24 except 2-20, 3-12, 4-23 Existen tial qu an t ificat ion 2-22, 3-12, 4-14 exists 2-15, 3-12 intersect 2-20, 3-12, 4-23 in ter sect ion 4-23 J F J ava 1-3 J oin 3-8 J oin qu er y 2-8 first 4-22 flatten 2-21 Flat t en in g 4-25 forall ... in 3-12 from 3-8 L G group ... by 2-15, 3-12, 3-13, 4-18 H Hyper t ext lin k s 1-6 last 4-22 Lat e bin din g 3-11 like 2-19 List 2-6, 2-6, 2-9 Con st r u ct in g 4-7 Set con ver sion 2-21 Valu es 2-6 list 2-11, 3-9, 4-24 listtoset 2-21 M max 2-13, 3-12, 4-15 Mem ber sh ip 4-15 Meth od call 2-6, 3-10 ODMG OQL User Man u al I-iii INDEX Meth od in vok in g 3-10 min 2-13, 3-12, 4-15 Mot if 1-5 N name 2-15 Nam ed Collect ion 2-22 Object s 2-4 Qu er y 2-14 Valu es 2-4 O O2 Ar ch it ect u r e 1-2 O2 C 1-3 O2 Cor ba 1-3 O2 DBAccess 1-3 O2 Engin e 1-2 O2 Gr aph 1-3 O2 Kit 1-3 O2 Look 1-3, 1-5 O2 Repor t 1-3 O2 Stor e 1-2 O2 Tools 1-3 O2 Web 1-3 Object Cr eat ion 2-12 Nam ed 2-4 Oper at or 2-13, 3-12 - 2-20 * 2-20 + 2-20 Aggr egat ive 3-12 avg 2-13, 3-12, 4-15 Com bin in g 2-12, 2-22 Com posit ion 3-12 count 2-13, 3-12, 4-15 define 2-14, 3-12 element 2-14 except 2-20, 3-12 exists 2-15, 3-12 flatten 2-21 forall...in 3-12 group...by 2-15, 3-12, 3-13, 4-18 intersect 2-20, 3-12 like 2-19 max 2-13, 3-12, 4-15 min 2-13, 3-12, 4-15 order by 2-19, 4-20 Set 2-20, 2-22, 3-12 sum 2-13, 3-12, 4-15 union 2-20, 3-12 Wild-car d 2-22 OQL 1-3, 1-4, 2-1 Oper at or s 2-13 Ration al 3-1 Resu lt 2-10 or der by 2-19, 3-12, 4-20 P partition 2-16 Path expr ession s 2-8, 3-7 Polym or ph ism 3-11 Pr edicat e 3-8 Objects 4-6 ODMG m odel 3-3 ODMG st an dar d 3-2, 4-1 Oper at ion 4-12 I-iv ODMG OQL User Man u al INDEX Q T Qu er y Basic 2-2, 4-32 Nam ed 2-14 Resu lt 2-10, 2-14 Test in g on n il 2-9 Typin g 4-26 U R Ref 3-3 Relat ion sh ip 3-3, 3-7, 4-11 S select 3-13 Select fr om wh er e 4-16 select from where 2-6, 3-7 Set 2-6, 2-7 Con st r u ct in g 4-7 List con ver sion 2-21 Oper at or s 2-20, 3-12 set 2-11, 2-12, 3-9, 4-22, 4-24 Set expr ession 4-35 Sm allt alk 1-3 Sm allt alk bin din g 3-2 struct 2-11, 2-12, 2-12, 3-9 Str u ct valu e 2-5 st r u ct u r e 4-6 Su bcollection 4-21, 4-21 sum 2-13, 3-12, 4-15 Syst em Ar ch it ect u r e 1-2 union 2-20, 3-12, 4-23 Un iver sal qu an t ificat ion 2-22, 3-12, 4-14 Un ix 1-5 V Valu e Ar r ay 2-6 At om ic 2-5 List 2-6 Nam ed 2-4 St r u ct 2-5 W where 3-8 Win dows NT 1-6 ODMG OQL User Man u al I-v INDEX I-vi ODMG OQL User Man u al