Chapter 8 Object-oriented Query Languages

4,462 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,462
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
200
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Chapter 8 Object-oriented Query Languages

  1. 1. Chapter 8 Object-oriented Query Languages
  2. 2. Object-oriented query language <ul><li>OQL (Object Query Language) </li></ul><ul><ul><li>ODMG (Object Database Management Group) </li></ul></ul><ul><ul><li>ODMG-93 release 1.2 (1993) </li></ul></ul><ul><ul><li>The Object Database Standard: ODMG 2.0, Ed. R.G.G. Cattell and D.K. Barry, Morgan Kaufmann, 1997 </li></ul></ul><ul><li>SQL3 </li></ul><ul><ul><li>ISO/IEC JTC1 SC21 WG3 </li></ul></ul><ul><ul><li>http://speckle.ncsl.nist.edu/~ftp/ </li></ul></ul><ul><li>While OQL is trying to bring the best of SQL into the object-oriented world, SQL3 is trying to bring the best of objet-orientation into the relational world </li></ul>
  3. 3. Using an OODBMS Declarations in ODL or PL ODL Application Source in [extended] PL Declaration Preprocessor PL Compiler ODBMS Runtime Application Binary Linker Running Application Database data access metadata
  4. 4. ODL <ul><li>The coupling of ODL definitions and the host language is tighter </li></ul><ul><ul><li>The host language should be object-oriented (C++, Smalltalk) </li></ul></ul><ul><ul><li>the ODL declarations can be translated directly into declarations of the host language </li></ul></ul>Copy and Translation Application Data Structures Relational Representation RDBMS Transparent ODBMS Data Transfer
  5. 5. Method signature in ODL <ul><li>ODL allows method as property </li></ul><ul><ul><li>A function associated with a class </li></ul></ul><ul><ul><li>Applied to an object and may also take one or more other arguments </li></ul></ul><ul><li>declare the method names and function parameters </li></ul><ul><ul><li>The actual code for a method would be written in the host language </li></ul></ul><ul><ul><li>Function name can be overloaded </li></ul></ul><ul><li>Signature syntax </li></ul><ul><ul><li>Functions parameters are specified to be in, out or inout </li></ul></ul><ul><ul><li>The function may have a return value </li></ul></ul><ul><ul><li>Functions may raise exceptions </li></ul></ul>
  6. 6. Adding method signature to the Movie class <ul><ul><li>interface Movie (extent Movies key (title, year)) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>attribute string title; </li></ul></ul><ul><ul><li>attribute integer year; </li></ul></ul><ul><ul><li>attribute integer length; </li></ul></ul><ul><ul><li>attribute enumeration {color, blackAndWhite} filmType; </li></ul></ul><ul><ul><li>relationship set <Star> stars inverse Star::starredIn; </li></ul></ul><ul><ul><li>relationship Studio ownedBy inverse Studio::owns; </li></ul></ul><ul><ul><li>float lengthInHours() raises (noLengthFound); </li></ul></ul><ul><ul><li>starNames(out set <string>); </li></ul></ul><ul><ul><li>otherMovies(in Star, out set <Movie>) raises (noSuchStar); </li></ul></ul><ul><ul><li>}; </li></ul></ul>
  7. 7. The extent of a class <ul><li>The extent of a type is the set of all instances of the type within a particular database </li></ul><ul><ul><li>If an object is an instance of the type A, then it will of necessity to be member of the extent of A </li></ul></ul><ul><ul><li>If a type A is a subtype of type B, then the extent of A is a subset of the extent of B </li></ul></ul><ul><li>Declaration format </li></ul><ul><ul><li>EXTENT <extent name> </li></ul></ul><ul><ul><li>Must appear immediately after the declaration of the interface name </li></ul></ul><ul><li>OQL queries refer to the extent of a class, not to the class name itself </li></ul>
  8. 8. An object-oriented movie schema
  9. 9. The OQL type system <ul><li>There are no limits in OQL on the depth of nesting of type constructors </li></ul><ul><li>Constants (immutable objects) are constructed as below: </li></ul><ul><ul><li>Basic types </li></ul></ul><ul><ul><ul><li>atomic types: integers, floats, characters, strings, and Booleans </li></ul></ul></ul><ul><ul><ul><li>enumerations: the values in an enumeration are actually declared in ODL. Any one of these values may be used as a constant </li></ul></ul></ul><ul><ul><li>Complex types </li></ul></ul><ul><ul><ul><li>Set (…), Bag (…), List (…), Array (…), Struct (…) </li></ul></ul></ul><ul><ul><ul><li>The first four of these are called collection types </li></ul></ul></ul><ul><li>Example </li></ul><ul><ul><li>struct(foo: bag(1,2,1), bar: “baz”) </li></ul></ul>
  10. 10. Path expression <ul><li>If a denotes an object belonging to class C, and p is some property of the class, then a.p denotes the result of “applying p to a ”. </li></ul><ul><ul><li>if p is an attribute, then a.p is the value of that attribute in object a </li></ul></ul><ul><ul><li>if p is a relationship. Then a.p is the object or collection of objects related to a by relationship p </li></ul></ul><ul><ul><li>if p is a method, then a.p is the result of applying p to a </li></ul></ul><ul><li>OQL uses the arrow (  ) as a synonym for the dot </li></ul><ul><li>For example </li></ul><ul><ul><li>Suppose myMovie is a host-language variable for the Movie object </li></ul></ul><ul><ul><li>myMovie.length </li></ul></ul><ul><ul><li>myMovie.lengthInHours() </li></ul></ul><ul><ul><li>myMovie.stars </li></ul></ul><ul><ul><li>myMovie.starNames(myStars) </li></ul></ul>
  11. 11. Select-from-where expressions in OQL <ul><li>OQL’s select-from-where is similar to SQL’s </li></ul><ul><ul><li>Possible to use any collect-producing expression such as another select-from-where expression (for example, in the FROM clause, in the SELECT clause) </li></ul></ul><ul><ul><li>Use != rather than <> </li></ul></ul><ul><li>The query produces a bag of objects </li></ul><ul><li>Example </li></ul><ul><ul><li>SELECT m.year FROM Movies m WHERE m.title = “Gone with the Wind” </li></ul></ul>
  12. 12. Example of select-from-where expressions <ul><li>Find the names of the stars of Casablanca </li></ul><ul><ul><li>select s.name </li></ul></ul><ul><ul><li>from Movies m, m.stars s </li></ul></ul><ul><ul><li>where m.title = “Casablanca”; </li></ul></ul><ul><ul><li>Evaluation can be carried out this way: </li></ul></ul><ul><ul><li>for each m in Movies do </li></ul></ul><ul><ul><li> for each s in m.stars do </li></ul></ul><ul><ul><li>if m.title = “Casablanca” then </li></ul></ul><ul><ul><li>add s.name to the output bag </li></ul></ul>
  13. 13. Eliminating duplicates <ul><li>Technically a query produces a bag, rather than a set as an answer </li></ul><ul><li>To eliminate duplicates, use DISTINCT following SELECT </li></ul><ul><li>Example </li></ul><ul><ul><li>SELECT DISTINCT s.name </li></ul></ul><ul><ul><li>FROM Movies m, m.stars s </li></ul></ul><ul><ul><li>WHERE m.ownedBy.name = “Disney”; </li></ul></ul>
  14. 14. Complex output types <ul><li>The expression(s) in the SELECT clause can be any expression, including expressions built using type constructors. </li></ul><ul><li>Example </li></ul><ul><ul><li>Find the set of pairs of stars living at the same address </li></ul></ul><ul><ul><li>select star1: s1, star2: s2 </li></ul></ul><ul><ul><li>from Stars s1, Stars s2 </li></ul></ul><ul><ul><li>where s1.addr = s2.addr and s1.name < s2.name; </li></ul></ul>
  15. 15. Subqueries <ul><li>A select-from-where expression can be present in FROM clause </li></ul><ul><li>The similar capability will be a part of the proposed SQL3 </li></ul><ul><li>Example </li></ul><ul><ul><li>Retrieve the stars of the movies made by Disney </li></ul></ul><ul><ul><li>select distinct s.name </li></ul></ul><ul><ul><li>from Movies m, m.stars s </li></ul></ul><ul><ul><li>where m.ownedBy.name = “Disney” </li></ul></ul><ul><ul><li>select distinct s.name </li></ul></ul><ul><ul><li>from (select m </li></ul></ul><ul><ul><li> from Movies m </li></ul></ul><ul><ul><li> where m.ownedBy.name = “Disney”) d, d.stars s </li></ul></ul>
  16. 16. Ordering the result <ul><li>We can make the output be a list with an ORDR BY clause, while the result of OQL is normally either a bag or (if distinct is used) a set </li></ul><ul><li>Example </li></ul><ul><ul><li>find the set of Disney movies, but let the result be a list of movies, ordered by length. If there are ties, let the movies of equal length be ordered alphabetically. </li></ul></ul><ul><ul><li>Select m </li></ul></ul><ul><ul><li>from Movies m </li></ul></ul><ul><ul><li>where m.ownedBy.name = “Disney” </li></ul></ul><ul><ul><li>order by m.length, m.title; </li></ul></ul><ul><li>ASC vs DESC </li></ul>
  17. 17. Quantifier expressions <ul><li>We can test whether all, or at least one, member of a set satisfies some condition </li></ul><ul><ul><li>FOR ALL x IN S : C(x) </li></ul></ul><ul><ul><li>EXISTS x IN S : C(x) </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>Find all the stars of Disney movies select s </li></ul></ul><ul><ul><li>from Stars s </li></ul></ul><ul><ul><li>where exists m in s.starredIn : m.ownedBy.name = “Disney” </li></ul></ul><ul><ul><li>Find stars that have appeared only in Disney movies select s </li></ul></ul><ul><ul><li>from Stars s </li></ul></ul><ul><ul><li>where for all m in s.starredIn : m.ownedBy.name = “Disney”; </li></ul></ul>
  18. 18. Aggregations <ul><li>OQL uses the same five aggregation operators that SQL does </li></ul><ul><ul><li>AVG, COUNT, SUM, MIN and MAX </li></ul></ul><ul><ul><li>the operators in OQL apply to collections whose members are of a suitable type </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>To compute the average length of all movies </li></ul></ul><ul><ul><li>AVG (select m.length from Movies m) </li></ul></ul>
  19. 19. Group-by expressions (1) <ul><li>The form of GROUP BY clause </li></ul><ul><ul><li>GROUP BY f1:e1, f2:e2, …, fn:en </li></ul></ul><ul><li>When there is only one variable x in the FROM clause </li></ul><ul><ul><li>The value of x ranges over some collection C </li></ul></ul><ul><ul><li>The actual value returned by the GROUP BY is a set of structures </li></ul></ul><ul><ul><li>Struct ( f1:v1, f2:v2, …, fn:vn , partition: P ) </li></ul></ul><ul><ul><li>v1, v2, …, vn are a list of values that comes from evaluating e 1 ( i ), e 2 ( i ), …, e n ( i ) for at least one value of i in the collection C </li></ul></ul><ul><ul><li>The last field has the special name Partition. P is a bag consisting of structures of the form </li></ul></ul><ul><ul><ul><li>Struct (x:i) </li></ul></ul></ul><ul><ul><li>The SELECT clause may refer only to the fields in the result of the GROUP BY, namely f1, f2, …, fn , partition </li></ul></ul>
  20. 20. Group-by expressions (2) <ul><li>The variables appearing in the FROM clause are x 1 , x 2 , …, x k </li></ul><ul><ul><li>All variables x 1 , x 2 , …, x k may be used in the expressions e 1 , e 2 , …, e n of the GROUP BY clause </li></ul></ul><ul><ul><li>Structures in the bag that is the value of the partition field have fields named x 1 , x 2 , …, x k </li></ul></ul><ul><ul><li>Suppose i 1 , i 2 , …, i k are values for x 1 , x 2 , …, x k , respectively, that make the WHERE clause true. Then there is a structure in the set that is the result of the GROUP BY of the form Struct( f 1 :e 1 (i 1 , i 2 , …, i k ), …, f n :e n (i 1 , i 2 , …, i k ), partition P) and in bag P is the structure: Struct(x 1 :i 1 , x 2 :i 2 , …, x k :i k ) </li></ul></ul>
  21. 21. Example of group-by expressions <ul><li>Build a table of the total length of movies for each studio and for each year </li></ul><ul><ul><li>select std, yr, sumLength: SUM(select p.m.length from partition p) </li></ul></ul><ul><ul><li>from Movies m </li></ul></ul><ul><ul><li>group by std: m.ownedBy.name, yr: m.year </li></ul></ul><ul><ul><li>Result of group by: a set of structures </li></ul></ul>
  22. 22. Having clauses <ul><li>To eliminate some of the groups created by the GROUP BY, use a HAVING clause </li></ul><ul><ul><li>HAVING <condition> </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>Compute the sum of the lengths of movies for only those studios and years such that the studio produced at least one movie of 120 minutes </li></ul></ul><ul><ul><li>select std, yr, sumLength: SUM (select p.m.length from partition p) </li></ul></ul><ul><ul><li>from Movies m </li></ul></ul><ul><ul><li>group by std : m.ownedBy.name, yr : m.year </li></ul></ul><ul><ul><li>having max (select p.m.length from partition p) > 120 </li></ul></ul>
  23. 23. Set operators <ul><li>The operators UNION, INTERSECT, EXCEPT operate on either sets or bags </li></ul><ul><ul><li>Set-oriented process takes place only if both operands are sets </li></ul></ul><ul><li>Rules for operating on bags </li></ul><ul><ul><li>Suppose an object x appears n 1 and n 2 times in B 1 and B 2 respectively </li></ul></ul><ul><ul><li>In B 1  B 2 , x appears n 1 +n 2 times </li></ul></ul><ul><ul><li>In B 1  B 2 , x appears min (n 1 , n 2 ) times </li></ul></ul><ul><ul><li>In B 1  B 2 , x appears </li></ul></ul><ul><ul><ul><li>if n 1  n 2 , then 0 times </li></ul></ul></ul><ul><ul><ul><li>if n 1 > n 2 , then n 1 - n 2 times </li></ul></ul></ul>
  24. 24. Example of set operators <ul><li>Find the set of movies starring Harrison Ford that were not made by Disney </li></ul><ul><ul><li>(select distinct m from Movies m, m.stars s where s.name = “Harrison Ford”) except (select distinct m from Movies m where m.ownedBy.name = “Disney”) </li></ul></ul>
  25. 25. Assigning values to host-language variables <ul><li>It is possible to assign result of OQL expressions to host-language variable of the proper type </li></ul><ul><li>Example </li></ul><ul><ul><li>Retrieve all movies made before 1920 </li></ul></ul><ul><ul><li>select distinct m </li></ul></ul><ul><ul><li>from Movies m </li></ul></ul><ul><ul><li>where m.year < 1920 </li></ul></ul><ul><ul><li>if oldMovies is a host-language variable of set<Movie> </li></ul></ul><ul><ul><li>oldMovies = select distinct m </li></ul></ul><ul><ul><li> from Movies m </li></ul></ul><ul><ul><li> where m.year < 1920; </li></ul></ul>
  26. 26. Extracting elements of collections <ul><li>OQL provides the operator ELEMENT to turn a singleton set or bag into its lone member </li></ul><ul><li>Example </li></ul><ul><ul><li>gwtw = element (select m </li></ul></ul><ul><ul><li> from Movies m </li></ul></ul><ul><ul><li> where m.title = “Gone With The Wind” </li></ul></ul><ul><ul><li> ); </li></ul></ul><ul><ul><li>The SELECT statement must return a bag with one element only </li></ul></ul>
  27. 27. Obtaining each member of a collection <ul><li>Need to make a set (bag) into a list, using ORDER BY </li></ul><ul><li>Can access i th element by L[i-1] </li></ul><ul><li>Example </li></ul><ul><ul><li>A C++ function that prints the title, year, and length of each movie </li></ul></ul><ul><ul><li>movieList = select m from Movies m order by m.title, m.year; </li></ul></ul><ul><ul><li>numberOfMovies = count(Movies); </li></ul></ul><ul><ul><li>for (i = 0; i < numberOfMovies; i++) { </li></ul></ul><ul><ul><li>movie = movieList[i]; </li></ul></ul><ul><ul><li>cout << movie.title << “ “ << movie.year << “ “ << movie.length << “ ”; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  28. 28. Creating new objects <ul><li>OQL expressions allow us to create new object </li></ul><ul><ul><li>In place of the triangular brackets that are used describe types, the round parentheses are used when we are constructing values </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>x = Struct (a:1, b:2); </li></ul></ul><ul><ul><li>y = Bag (x, x, Struct (a:3, b:4)); </li></ul></ul><ul><ul><li>Suppose that the type StarPair is struct{star1: Star, star2: Star} </li></ul></ul><ul><ul><li>Select distinct StarPair(star1: s1, star2: s2) </li></ul></ul><ul><ul><li>from Stars s1, Stars s2 </li></ul></ul><ul><ul><li>where s1.addr = s2.addr and s1.name < s2.name </li></ul></ul><ul><ul><li>The query returns Set<StarPair> </li></ul></ul>
  29. 29. Tuple objects in SQL3 <ul><li>OQL has no specific notion of relation, objects in SQL3 keep relations as a central concept </li></ul><ul><li>Objects in SQL3 </li></ul><ul><ul><li>Row objects </li></ul></ul><ul><ul><li>Abstract Data Types (ADT, value ADT) </li></ul></ul>
  30. 30. Row types <ul><li>In SQL3, one can define a row type T </li></ul><ul><ul><li>CREATE ROW TYPE T ( <component declarations> ) </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>Create row type AddressType ( </li></ul></ul><ul><ul><li>street char(50), city char(20) </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>Create row type StarType ( </li></ul></ul><ul><ul><li>name char(30), address AddressType </li></ul></ul><ul><ul><li>); </li></ul></ul>
  31. 31. Declaring relations with a row type <ul><li>May declare one or more relations whose tuples are of a row type </li></ul><ul><ul><li>CREATE TABLE <table_name> OF TYPE < row type name> </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>Create table MovieStar of type StarType; </li></ul></ul><ul><li>To access a component, use a double-dot notation that corresponds closely to the single-dot notation of OQL or C </li></ul><ul><li>Example </li></ul><ul><ul><li>Find the name and street address of each star living in Beverly Hills </li></ul></ul><ul><ul><li>select MovieStar.name, MovieStar.address..street </li></ul></ul><ul><ul><li>from MovieStar </li></ul></ul><ul><ul><li>where MovieStar.address..city = ‘Beverly Hills’; // “MovieStar.” is not necessary </li></ul></ul>
  32. 32. References <ul><li>If T is a row type, then REF(T) is the type of a reference to a tuple of type T </li></ul><ul><li>Example </li></ul><ul><ul><li>Create row type MovieType ( </li></ul></ul><ul><ul><li>title char(30), year integer, inColor bit(1) ); </li></ul></ul><ul><ul><li>Create table Movie of type MovieType; </li></ul></ul><ul><ul><li>Create row type StarType ( </li></ul></ul><ul><ul><li>name char(30), address AddressType, </li></ul></ul><ul><ul><li>bestMovie ref(MovieType)); // reference to the star’s best movie </li></ul></ul>
  33. 33. Example of references <ul><li>Represent the stars-in relationship (M:N) between movies and stars </li></ul><ul><ul><li>Is likely that use of collection types as attribute types will be deferred to SQL4 </li></ul></ul><ul><ul><li>Hence, need to create a separate relation for the M:N relationship </li></ul></ul><ul><ul><li>Create row type MovieType ( </li></ul></ul><ul><ul><li>title char(30), year integer, inColor bit(1)); </li></ul></ul><ul><ul><li>Create row type AddressType (street char(50), city char(20)); </li></ul></ul><ul><ul><li>Create row type StarType (name char(30), address AddressType); </li></ul></ul><ul><ul><li>Create row type StarsInType ( star ref(StarType), movie ref(MovieType)); </li></ul></ul><ul><ul><li>Create table Movie of type MovieType; </li></ul></ul><ul><ul><li>Create table MovieStar of type StarType; </li></ul></ul><ul><ul><li>Create table StarsIn of type StarsInType; </li></ul></ul>
  34. 34. Following references <ul><li>In SQL3, -> is used for dereferencing (same meaning as in C) </li></ul><ul><li>if x is a reference to a tuple t and a is an attribute of t , then x -> a is the value of the attribute a in tuple t </li></ul><ul><li>Example </li></ul><ul><ul><li>Find the titles of all the movies in which Mel Gibson starred </li></ul></ul><ul><ul><li>Select movie->title </li></ul></ul><ul><ul><li>from StarsIn </li></ul></ul><ul><ul><li>where star->name = ‘Mel Gibson’; </li></ul></ul>
  35. 35. Dereferencing and component-extraction <ul><li>The dot and -> operators are synonyms in OQL </li></ul><ul><ul><li>each applies to an OQL object that is a tuple and returns a component of that object </li></ul></ul><ul><li>In SQL3 </li></ul><ul><ul><li>“->” is applicable to a reference to tuple </li></ul></ul><ul><ul><li>dot operator is applicable to tuple variable </li></ul></ul><ul><ul><li>if r is a reference to a tuple t , then r->a yields the same value as t..a </li></ul></ul>
  36. 36. Domains and row types <ul><li>Major differences between domains and row types </li></ul><ul><ul><li>Domains define types for components, while row types are types for entire tuples </li></ul></ul><ul><ul><li>Two domains can represent the same type and values from those domains will not be distinguished </li></ul></ul><ul><ul><li>However, suppose two row types T 1 and T 2 have identical definitions. Then tuples from relations with those types cannot be interchanged </li></ul></ul><ul><ul><ul><li>An attribute whose type is a reference to a T 1 cannot be made to refer to a tuple whose type is T 2 </li></ul></ul></ul>
  37. 37. Scopes of references <ul><li>SQL3 provides a mechanism for specifying which relation a reference attribute refers to </li></ul><ul><ul><li>SCOPE FOR <attribute> IS <relation> </li></ul></ul><ul><ul><li>The named attribute whose type must be a reference, always refers to a tuple of the named relation </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>Create row type StarsInType ( </li></ul></ul><ul><ul><li>star ref(StarType), movie ref(MovieType) ); </li></ul></ul><ul><ul><li>Create table StarsIn of type StarsInType </li></ul></ul><ul><ul><li>scope for star is MovieStar, </li></ul></ul><ul><ul><li>scope for movie is Movie; </li></ul></ul><ul><ul><li>Useful when there are other relations declared to be type StarType or MovieType </li></ul></ul>
  38. 38. Object identifiers as values <ul><li>In SQL3, we can refer to object IDs explicilty </li></ul><ul><ul><li>VALUES FOR <attribute> ARE SYSTEM GENERATED </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>create row type MovieType ( </li></ul></ul><ul><ul><li>movie_id ref(MovieType), </li></ul></ul><ul><ul><li>title char(30), year integer, inColor bit(1)); </li></ul></ul><ul><ul><li>Create table Movie of type MovieType </li></ul></ul><ul><ul><li>values for movie_id are system generated; </li></ul></ul>
  39. 39. Abstract data types in SQL3 <ul><li>Row types are not encapsulated ! </li></ul><ul><li>SQL3 has another “class” definition, abstract data type (ADT) , that does support encapsulation </li></ul><ul><ul><li>Objects of an ADT are used as components of tuples, not as tuples themselves </li></ul></ul>
  40. 40. Defining ADT’s <ul><li>ADT definition : </li></ul><ul><ul><li>CREATE TYPE <type name> ( </li></ul></ul><ul><ul><li>list of attributes and their types </li></ul></ul><ul><ul><li>optional declaration of = and < functions for the type </li></ul></ul><ul><ul><li>declaration of functions (methods) for the type ); </li></ul></ul><ul><li>SQL3 provides certain “built-in” functions with every ADT, and these do not need to be declared or defined </li></ul><ul><ul><li>Constructor function that returns a new object of the type </li></ul></ul><ul><ul><li>Observer functions for each attribute that return the value of this attribute </li></ul></ul><ul><ul><li>Mutator functions for each attribute that set the value of that attribute to a new value </li></ul></ul>
  41. 41. Example of defining ADT’s <ul><ul><li>Create type AddressADT ( </li></ul></ul><ul><ul><li>street char(50), city char(20), </li></ul></ul><ul><ul><li>equals addrEq, less than addrLT, </li></ul></ul><ul><ul><li>other function could be declared here ); </li></ul></ul><ul><ul><li>Create type Mpeg ( </li></ul></ul><ul><ul><li>video blob, length integer, </li></ul></ul><ul><ul><li>copyright varchar(255), </li></ul></ul><ul><ul><li>equals default, less than none, </li></ul></ul><ul><ul><li>definitions of functions go here ); </li></ul></ul>
  42. 42. Defining methods for ADT’s <ul><li>After the attribute list of an ADT, we can append any list of function declarations </li></ul><ul><ul><li>FUNCTION <name> ( <arguments> ) RETURNS <type> ; </li></ul></ul><ul><li>Functions are of two types: internal and external </li></ul><ul><ul><li>external: written in host language and only their signature appears in the definition of the ADT </li></ul></ul><ul><ul><li>internal: written in an extended SQL </li></ul></ul><ul><ul><li>some options </li></ul></ul><ul><ul><ul><li>:= assignment operator </li></ul></ul></ul><ul><ul><ul><li>A variable local can be declared by giving its name, preceded by a colon and followed by its type </li></ul></ul></ul><ul><ul><ul><li>The dot operator is used to access components of a structure </li></ul></ul></ul><ul><ul><ul><li>Boolean values can be expressed as in WHERE clauses </li></ul></ul></ul><ul><ul><ul><li>BEGIN and END are used to collect several arguments into the body of a function </li></ul></ul></ul>
  43. 43. ADT definition example (1/2) <ul><ul><li>Function AddressADT (:s char(50), :c char(20)) returns AddressADT; </li></ul></ul><ul><ul><li>:a AddressADT; </li></ul></ul><ul><ul><li>begin </li></ul></ul><ul><ul><li>:a := AddressADT(); // built-in constructor </li></ul></ul><ul><ul><li>:a.street := :s; </li></ul></ul><ul><ul><li>:a.city := :c; </li></ul></ul><ul><ul><li>return :a; </li></ul></ul><ul><ul><li>end; </li></ul></ul><ul><ul><li>Function addrEq (:a1 AddressADT, :a2 AddressADT) returns boolean; </li></ul></ul><ul><ul><li>return(:a1.street = :a2.street and :a1.city = :a2.city); </li></ul></ul>
  44. 44. ADT definition example (2/2) <ul><ul><li>Function addrLT (:a1 AddressADT, :a2 AddressADT) returns boolean; return ((:a1.city < :a2.city) or (:a1.city = :a2.city) and (:a1.street < :a2.street) ); </li></ul></ul><ul><ul><li>Function fullAddr (:a AddressADT) returns char(82); :z char(10); </li></ul></ul><ul><ul><li>begin </li></ul></ul><ul><ul><li>:z = findZip (:a.street, :a.city); </li></ul></ul><ul><ul><li>return(:a.street || ‘ ‘ || :a.city || ‘ ‘ || :z); </li></ul></ul><ul><ul><li>end; </li></ul></ul>
  45. 45. External functions <ul><li>ADTs may also have methods that are written in some host language rather than in SQL3 </li></ul><ul><ul><li>DECLARE EXTERNAL <function name> <signature> LANGUAGE <language name> </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>DECLARE EXTERNAL findZip </li></ul></ul><ul><ul><li>char(50) char(20) RETURNS char(10) </li></ul></ul><ul><ul><li>LANGUAGE C; </li></ul></ul>
  46. 46. Comparison of ODL/OQL and SQL3

×