Database Systems - Object Databases

806 views
735 views

Published on

Object databases, object/relational mapping. XML, XPath, XQuery.

Published in: Education
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
806
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
55
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

Database Systems - Object Databases

  1. 1. Object Databases Object/Relational Databases XML Databases Database Systems Object Databases H. Turgut Uyar ¨ g¨ u u Sule O˘ud¨c¨ ¸ 2005-2012 ¸ ¨ g¨ u uH. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  2. 2. Object Databases Object/Relational Databases XML DatabasesLicense ¸ ¨ g¨ u u c 2005-2012 T. Uyar, S. O˘ud¨c¨ You are free: to Share – to copy, distribute and transmit the work to Remix – to adapt the work Under the following conditions: Attribution – You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Noncommercial – You may not use this work for commercial purposes. Share Alike – If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one. Legal code (the full license): http://creativecommons.org/licenses/by-nc-sa/3.0/ ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  3. 3. Object Databases Object/Relational Databases XML DatabasesTopics 1 Object Databases Introduction Object Identifiers Example: db4o 2 Object/Relational Databases Introduction Example: Persist 3 XML Databases Introduction XQuery Example: DBXML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  4. 4. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesTopics 1 Object Databases Introduction Object Identifiers Example: db4o 2 Object/Relational Databases Introduction Example: Persist 3 XML Databases Introduction XQuery Example: DBXML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  5. 5. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesObject-Orientation mismatch between the data model and the software model data: relation, tuple, foreign key, . . . software: object, method, . . . ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  6. 6. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesMismatch Example Example (adding an actor to a movie - SQL definitions) CREATE TABLE MOVIE ( ID INTEGER PRIMARY KEY , TITLE VARCHAR (80) NOT NULL ) CREATE TABLE PERSON ( ID INTEGER PRIMARY KEY , NAME VARCHAR (40) NOT NULL ) CREATE TABLE CASTING ( MOVIEID INTEGER REFERENCES MOVIE , ACTORID INTEGER REFERENCES PERSON , PRIMARY KEY ( MOVIEID , ACTORID ) ) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  7. 7. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesMismatch Example Example (adding an actor to a movie - SQL operations) INSERT INTO MOVIE ( ID , TITLE ) VALUES (110 , ’ Sleepy Hollow ’) INSERT INTO PERSON ( ID , NAME ) VALUES (26 , ’ Johnny Depp ’) INSERT INTO CASTING ( MOVIEID , ACTORID ) VALUES (110 , 26) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  8. 8. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesMismatch Example Example (adding an actor to a movie - Java definitions) public class Movie { ... private List < Person > cast ; ... public void addActor ( Person p ) { this . cast . add ( p ); } } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  9. 9. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesMismatch Example Example (adding an actor to a movie - Java operations) Movie m = new Movie ( " Sleepy Hollow " , ...); Person p = new Person ( " Johnny Depp " , ...); m . addActor ( p ); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  10. 10. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesTopics 1 Object Databases Introduction Object Identifiers Example: db4o 2 Object/Relational Databases Introduction Example: Persist 3 XML Databases Introduction XQuery Example: DBXML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  11. 11. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesObject Identifiers every object has an identifier object identifiers don’t change even if object attributes change different from primary key primary key is visible (user-defined) value of primary key can change correspond to references in programming languages object identifiers can refer to other objects: containment hierarchy ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  12. 12. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesObject Identifiers every object has an identifier object identifiers don’t change even if object attributes change different from primary key primary key is visible (user-defined) value of primary key can change correspond to references in programming languages object identifiers can refer to other objects: containment hierarchy ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  13. 13. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesObject Identifiers every object has an identifier object identifiers don’t change even if object attributes change different from primary key primary key is visible (user-defined) value of primary key can change correspond to references in programming languages object identifiers can refer to other objects: containment hierarchy ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  14. 14. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesContainment Hierarchy Example Example ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  15. 15. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesContainment Hierarchy Example Example ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  16. 16. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesObject Databases persistent objects are stored as objects, not as relations write: object → internal format (serialization) read: internal format → object (deserialization) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  17. 17. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesTopics 1 Object Databases Introduction Object Identifiers Example: db4o 2 Object/Relational Databases Introduction Example: Persist 3 XML Databases Introduction XQuery Example: DBXML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  18. 18. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o an object database system that can work embedded query using conditions query by example create an object of the class to be queried set the desired properties, leave the others blank search for similar objects the objects to be updated or deleted have to be retrieved from the database (object identifier) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  19. 19. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o an object database system that can work embedded query using conditions query by example create an object of the class to be queried set the desired properties, leave the others blank search for similar objects the objects to be updated or deleted have to be retrieved from the database (object identifier) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  20. 20. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o an object database system that can work embedded query using conditions query by example create an object of the class to be queried set the desired properties, leave the others blank search for similar objects the objects to be updated or deleted have to be retrieved from the database (object identifier) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  21. 21. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Interface connecting to database (embedded mode): Db4oEmbedded.openFile(filePath) → ObjectContainer insert and update: ObjectContainer.store(object) delete: ObjectContainer.delete(object) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  22. 22. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Interface connecting to database (embedded mode): Db4oEmbedded.openFile(filePath) → ObjectContainer insert and update: ObjectContainer.store(object) delete: ObjectContainer.delete(object) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  23. 23. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Interface all instances of a class: ObjectContainer.query(Class.class) → List<Class> query by example: ObjectContainer.queryByExample(Class prototype) → ObjectSet<Class> ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  24. 24. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Interface all instances of a class: ObjectContainer.query(Class.class) → List<Class> query by example: ObjectContainer.queryByExample(Class prototype) → ObjectSet<Class> ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  25. 25. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Interface query condition: Predicate<Class> implement the match method: public boolean match(Class object) query: ObjectContainer.query(Predicate<Class> predicate) → List<Class> ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  26. 26. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Interface query condition: Predicate<Class> implement the match method: public boolean match(Class object) query: ObjectContainer.query(Predicate<Class> predicate) → List<Class> ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  27. 27. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Examples Example (connecting to the database) ObjectContainer db = Db4oEmbedded . openFile ( " imdb . db4o " ); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  28. 28. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Examples Example (query: all movies) List < Movie > movies = db . query ( Movie . class ); for ( Movie movie : movies ) { ... } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  29. 29. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Examples Example (query by example: movies in 1977) Movie prototype = new Movie ( null ); prototype . setYear (1977); ObjectSet < Movie > movies = db . queryByExample ( prototype ); while ( movies . hasNext ()) { Movie m = movies . next (); ... } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  30. 30. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Examples Example (query by condition: movies after 1977) List < Movie > movies = db . query ( new Predicate < Movie >() { public boolean match ( Movie movie ) { return movie . getYear () > 1977; } }); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  31. 31. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Examples Example (insert) Movie m = new Movie ( " Casablanca " ); m . setYear (1942); db . store ( m ); db . commit (); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  32. 32. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Examples Example (update) Movie prototype = new Movie ( " Casablanca " ); ObjectSet < Movie > result = db . queryByExample ( prototype ); Movie found = result . next (); found . setYear (1943); db . store ( found ); db . commit (); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  33. 33. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases Referencesdb4o Examples Example (delete) Movie prototype = new Movie ( " Casablanca " ); ObjectSet < Movie > result = db . queryByExample ( prototype ); Movie found = result . next (); db . delete ( found ); db . commit (); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  34. 34. Introduction Object Databases Object Identifiers Object/Relational Databases Example: db4o XML Databases ReferencesReferences Required Reading: Date Chapter 25: Object Databases ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  35. 35. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesTopics 1 Object Databases Introduction Object Identifiers Example: db4o 2 Object/Relational Databases Introduction Example: Persist 3 XML Databases Introduction XQuery Example: DBXML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  36. 36. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesObject/Relational Mapping software is object-oriented database is relational map software components to database components ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  37. 37. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesTopics 1 Object Databases Introduction Object Identifiers Example: db4o 2 Object/Relational Databases Introduction Example: Persist 3 XML Databases Introduction XQuery Example: DBXML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  38. 38. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesExample: Persist wraps a JDBC connection translates the object database interface into SQL statements ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  39. 39. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesPersist Interface database connection: Connection connection Persist(connection) → Persist insert: Persist.insert(object) update: Persist.update(object) delete: Persist.delete(object) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  40. 40. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesPersist Interface database connection: Connection connection Persist(connection) → Persist insert: Persist.insert(object) update: Persist.update(object) delete: Persist.delete(object) ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  41. 41. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesPersist Interface query: all instances of a class Persist.readList(Class.class) → List<Class> query using SQL: similar to prepared statements Persist.readList(Class.class, String query, params) → List<Class> ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  42. 42. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesPersist Examples Example (database connection) Connection connection = DriverManager . getConnection ( jdbcURL ); Persist db = new Persist ( connection ); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  43. 43. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesPersist Examples Example (query: all movies) List < Movie > movies = db . readList ( Movie . class ); for ( Movie movie : movies ) { ... } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  44. 44. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesPersist Examples Example (query using SQL: all movies in 1977) List < Movie > movies = db . readList ( Movie . class , " SELECT * FROM MOVIE WHERE ( YEAR = ?) " , 1977); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  45. 45. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesPersist Examples Example (insert) Movie m = new Movie ( " Casablanca " ); m . setYear (1942); db . insert ( m ); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  46. 46. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesPersist Examples Example (update) List < Movie > movies = db . readList ( Movie . class , " SELECT * FROM MOVIE WHERE ( TITLE = ?) " , " Casablanca " ); Movie found = movies . get (0); found . setYear (1943); db . update ( found ); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  47. 47. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesPersist Examples Example (delete) List < Movie > movies = db . readList ( Movie . class , " SELECT * FROM MOVIE WHERE ( TITLE = ?) " , " Casablanca " ); Movie found = movies . get (0); db . delete ( found ); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  48. 48. Object Databases Introduction Object/Relational Databases Example: Persist XML Databases ReferencesReferences Required Reading: Date Chapter 26: Object/Relational Databases ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  49. 49. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesTopics 1 Object Databases Introduction Object Identifiers Example: db4o 2 Object/Relational Databases Introduction Example: Persist 3 XML Databases Introduction XQuery Example: DBXML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  50. 50. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML XML is not a language itself framework for defining languages XML-based languages XHTML, DocBook, SVG, MathML, WML, XMI, ... XML-related languages XPath, XQuery, XSL Transforms, SOAP, XLink, ... ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  51. 51. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML XML is not a language itself framework for defining languages XML-based languages XHTML, DocBook, SVG, MathML, WML, XMI, ... XML-related languages XPath, XQuery, XSL Transforms, SOAP, XLink, ... ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  52. 52. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML XML is not a language itself framework for defining languages XML-based languages XHTML, DocBook, SVG, MathML, WML, XMI, ... XML-related languages XPath, XQuery, XSL Transforms, SOAP, XLink, ... ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  53. 53. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML Structure an XML document forms a tree nodes: elements root node: document element leaves: character data, self-closing elements opening/closing tags attributes ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  54. 54. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML Structure an XML document forms a tree nodes: elements root node: document element leaves: character data, self-closing elements opening/closing tags attributes ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  55. 55. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML Example Example (HTML) < html > < head > < title > Foo Bar </ title > </ head > < body > < h1 > Welcome to Foo Bar ! </ h1 > <p > You can get more information from the <a href = " http: // www . foobar . net / " > foobar page </ a >. </ p > < img src = " logo . jpg " alt = " Foo Bar logo " / > </ body > </ html > ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  56. 56. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML Example Example (DocBook) < book lang = " en " > < title > Foobar Report </ title > < bookinfo > ... </ bookinfo > < chapter > ... </ chapter > < chapter > ... </ chapter > ... </ book > ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  57. 57. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML Example Example (DocBook) < bookinfo > < author > < firstname > John </ firstname > < surname > Foobar </ surname > </ author > < date > 2007 </ date > </ bookinfo > ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  58. 58. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML Example Example (DocBook) < chapter > < title > Introduction </ title > < section > < title > Description </ title > < para > Foobar is ... </ para > </ section > ... </ chapter > ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  59. 59. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML Example Example (movies) < movies > < movie color = " Color " > < title > Usual Suspects </ title > ... </ movie > < movie color = " Color " > < title > Being John Malkovich </ title > ... </ movie > ... </ movies > ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  60. 60. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXML Example Example (movies) < movie color = " Color " > < title > Usual Suspects </ title > < year > 1995 </ year > < score > 8.7 </ score > < votes > 35027 </ votes > < director > Bryan Singer </ director > < cast > < actor > Gabriel Byrne </ actor > < actor > Benicio Del Toro </ actor > </ cast > </ movie > ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  61. 61. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesTopics 1 Object Databases Introduction Object Identifiers Example: db4o 2 Object/Relational Databases Introduction Example: Persist 3 XML Databases Introduction XQuery Example: DBXML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  62. 62. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXQuery XPath: selecting nodes and data from XML documents XQuery: XPath + update operations ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  63. 63. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath path of nodes to find: chain of location steps starting from the root (absolute) starting from the current node (relative) location steps are separated by / symbols Example /movies/movie cast/actor or ./cast/actor ../../year ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  64. 64. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath path of nodes to find: chain of location steps starting from the root (absolute) starting from the current node (relative) location steps are separated by / symbols Example /movies/movie cast/actor or ./cast/actor ../../year ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  65. 65. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesLocation Steps location step structure: axis::node_selector[predicate] axis: where to search selector: what to search predicate: under which conditions ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  66. 66. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesLocation Steps location step structure: axis::node_selector[predicate] axis: where to search selector: what to search predicate: under which conditions ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  67. 67. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesAxes child: all children, one level (default axis) descendant: all children, recursively (shorthand: //) parent: parent node, one level ancestor: parent nodes, up to document element attribute: attributes (shorthand: @) following-sibling: siblings that come later preceding-sibling: siblings that come earlier ... ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  68. 68. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesNode Selectors node tag node attribute node text: text() all children: * ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  69. 69. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Examples Example names of all directors: /movies/movie/director/text() //director/text() all actors in this movie: ./cast/actor .//actor colors of all movies: //movie/@color scores of movies after this one: ./following-sibling::movie/score ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  70. 70. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Examples Example names of all directors: /movies/movie/director/text() //director/text() all actors in this movie: ./cast/actor .//actor colors of all movies: //movie/@color scores of movies after this one: ./following-sibling::movie/score ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  71. 71. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Examples Example names of all directors: /movies/movie/director/text() //director/text() all actors in this movie: ./cast/actor .//actor colors of all movies: //movie/@color scores of movies after this one: ./following-sibling::movie/score ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  72. 72. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Examples Example names of all directors: /movies/movie/director/text() //director/text() all actors in this movie: ./cast/actor .//actor colors of all movies: //movie/@color scores of movies after this one: ./following-sibling::movie/score ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  73. 73. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Predicates testing node position: [position] testing existence of a child: [child_tag] testing value of a child: [child_tag="value"] testing existence of an attribute: [@attribute] testing value of an attribute: [@attribute="value"] ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  74. 74. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Predicates testing node position: [position] testing existence of a child: [child_tag] testing value of a child: [child_tag="value"] testing existence of an attribute: [@attribute] testing value of an attribute: [@attribute="value"] ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  75. 75. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Predicates testing node position: [position] testing existence of a child: [child_tag] testing value of a child: [child_tag="value"] testing existence of an attribute: [@attribute] testing value of an attribute: [@attribute="value"] ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  76. 76. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Examples Example the title of the first movie: /movies/movie[1]/title all movies in the year 1997: movie[year="1997"] black-and-white movies: movie[@color="BW"] ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  77. 77. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Examples Example the title of the first movie: /movies/movie[1]/title all movies in the year 1997: movie[year="1997"] black-and-white movies: movie[@color="BW"] ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  78. 78. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesXPath Examples Example the title of the first movie: /movies/movie[1]/title all movies in the year 1997: movie[year="1997"] black-and-white movies: movie[@color="BW"] ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  79. 79. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesTopics 1 Object Databases Introduction Object Identifiers Example: db4o 2 Object/Relational Databases Introduction Example: Persist 3 XML Databases Introduction XQuery Example: DBXML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  80. 80. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesExample: Oracle Berkeley DBXML an embedded XML database stores XML documents manipulates data using XQuery can be used via its own client has bindings for several languages ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  81. 81. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Interface creating a database: create an XmlManager object XmlManager.createContainer(name) → XmlContainer put a document element: XmlContainer.putDocument(namespace, xml_string, configuration) connecting to an existing database: create an XmlManager object if XmlManager.existsContainer(name) != 0 XmlManager.openContainer(name) → XmlContainer ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  82. 82. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Interface creating a database: create an XmlManager object XmlManager.createContainer(name) → XmlContainer put a document element: XmlContainer.putDocument(namespace, xml_string, configuration) connecting to an existing database: create an XmlManager object if XmlManager.existsContainer(name) != 0 XmlManager.openContainer(name) → XmlContainer ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  83. 83. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Interface XmlManager.createQueryContext() → XmlQueryContext XmlQueryContext.setNamespace(namespace, URL) query string: collection(name)/xpath_expression running the query: XmlManager.query(query, context) → XmlResults each element of the XmlResults iterator is an XmlValue getFirstChild(), getLastChild(), getNextSibling(), ... character data: getNodeValue() → String attributes: XmlValue.getAttributes() → XmlResults ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  84. 84. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Interface XmlManager.createQueryContext() → XmlQueryContext XmlQueryContext.setNamespace(namespace, URL) query string: collection(name)/xpath_expression running the query: XmlManager.query(query, context) → XmlResults each element of the XmlResults iterator is an XmlValue getFirstChild(), getLastChild(), getNextSibling(), ... character data: getNodeValue() → String attributes: XmlValue.getAttributes() → XmlResults ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  85. 85. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Interface XmlManager.createQueryContext() → XmlQueryContext XmlQueryContext.setNamespace(namespace, URL) query string: collection(name)/xpath_expression running the query: XmlManager.query(query, context) → XmlResults each element of the XmlResults iterator is an XmlValue getFirstChild(), getLastChild(), getNextSibling(), ... character data: getNodeValue() → String attributes: XmlValue.getAttributes() → XmlResults ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  86. 86. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Examples Example (database connection) db = new XmlManager (); XmlContainer container = null ; if ( db . existsContainer ( " imdb . dbxml " ) != 0) container = db . openContainer ( " imdb . dbxml " ); else { container = db . createContainer ( " imdb . dbxml " ); container . putDocument ( " movies " , " < movies / > " , ( XmlDocumentConfig ) null ); } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  87. 87. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Examples Example (converting a movie object into an XML string) public static String toXml ( Movie movie ) { StringBuffer buffer = new StringBuffer (); buffer . append ( " < movie > " ); buffer . append ( " < title > " + movie . getTitle () + " </ title > " ); buffer . append ( " < year > " + movie . getYear (). toString () + " </ year > " ); buffer . append ( " </ movie > " ); return buffer . toString (); } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  88. 88. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Examples Example (converting an XML node into a movie object) private static Movie fromNode ( XmlValue node ) throws XmlException { XmlValue tn = node . getFirstChild (); String title = tn . getFirstChild (). getNodeValue (); XmlValue yn = tn . getNextSibling (); String yearValue = yn . getFirstChild (). getNodeValue (); Integer year = Integer . parseInt ( yearValue ); Movie movie = new Movie ( title ); movie . setYear ( year ); return movie ; } ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  89. 89. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Examples Example (query: all movies) XmlQueryContext context = ...; context . setNamespace (...); String query = " collection (" imdb . dbxml ")/ movies / movie " ; XmlResults results = db . query ( query , context ); if ( results . hasNext ()) { XmlValue node = results . next (); Movie movie = fromNode ( node ); ... } results . delete (); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  90. 90. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesDBXML Examples Example (insert) Movie m = new Movie ( " Casablanca " ); m . setYear (1942); XmlQueryContext context = ...; context . setNamespace (...); String query = " insert nodes " + toXml ( m ) + " into collection (" imdb . dbxml ")/ movies " ; XmlResults results = db . query ( query , context ); results . delete (); ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems
  91. 91. Introduction Object Databases XQuery Object/Relational Databases Example: DBXML XML Databases ReferencesReferences Required Reading: Date Chapter 27: The World Wide Web and XML ¸ ¨ g¨ u u H. Turgut Uyar, Sule O˘ud¨c¨ Database Systems

×