91
Object Databases Object/Relational Databases XML Databases Database Systems Object Databases H. Turgut Uyar ¸ Sule ¨ ud¨ uc¨ u 2005-2012 H. Turgut Uyar, ¸ Sule ¨ ud¨ uc¨ u Database Systems

Database Systems - Object Databases

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

Database SystemsObject Databases

H. Turgut Uyar Sule Oguducu

2005-2012

H. Turgut Uyar, Sule Oguducu Database Systems

Page 2: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

License

c©2005-2012 T. Uyar, S. Oguducu

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 anyway 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 onlyunder the same or similar license to this one.

Legal code (the full license):http://creativecommons.org/licenses/by-nc-sa/3.0/

H. Turgut Uyar, Sule Oguducu Database Systems

Page 3: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

Topics

1 Object DatabasesIntroductionObject IdentifiersExample: db4o

2 Object/Relational DatabasesIntroductionExample: Persist

3 XML DatabasesIntroductionXQueryExample: DBXML

H. Turgut Uyar, Sule Oguducu Database Systems

Page 4: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Topics

1 Object DatabasesIntroductionObject IdentifiersExample: db4o

2 Object/Relational DatabasesIntroductionExample: Persist

3 XML DatabasesIntroductionXQueryExample: DBXML

H. Turgut Uyar, Sule Oguducu Database Systems

Page 5: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Object-Orientation

mismatch between the data model and the software model

data: relation, tuple, foreign key, . . .software: object, method, . . .

H. Turgut Uyar, Sule Oguducu Database Systems

Page 6: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Mismatch 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)

)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 7: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Mismatch 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)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 8: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Mismatch 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);}

}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 9: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Mismatch 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);

H. Turgut Uyar, Sule Oguducu Database Systems

Page 10: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Topics

1 Object DatabasesIntroductionObject IdentifiersExample: db4o

2 Object/Relational DatabasesIntroductionExample: Persist

3 XML DatabasesIntroductionXQueryExample: DBXML

H. Turgut Uyar, Sule Oguducu Database Systems

Page 11: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Object 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 12: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Object 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 13: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Object 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 14: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Containment Hierarchy Example

Example

H. Turgut Uyar, Sule Oguducu Database Systems

Page 15: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Containment Hierarchy Example

Example

H. Turgut Uyar, Sule Oguducu Database Systems

Page 16: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Object Databases

persistent objects are stored as objects, not as relations

write: object → internal format (serialization)

read: internal format → object (deserialization)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 17: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

Topics

1 Object DatabasesIntroductionObject IdentifiersExample: db4o

2 Object/Relational DatabasesIntroductionExample: Persist

3 XML DatabasesIntroductionXQueryExample: DBXML

H. Turgut Uyar, Sule Oguducu Database Systems

Page 18: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o

an object database system that can work embedded

query using conditions

query by example

create an object of the class to be queriedset the desired properties, leave the others blanksearch for similar objects

the objects to be updated or deleted have to be retrievedfrom the database (object identifier)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 19: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o

an object database system that can work embedded

query using conditions

query by example

create an object of the class to be queriedset the desired properties, leave the others blanksearch for similar objects

the objects to be updated or deleted have to be retrievedfrom the database (object identifier)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 20: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o

an object database system that can work embedded

query using conditions

query by example

create an object of the class to be queriedset the desired properties, leave the others blanksearch for similar objects

the objects to be updated or deleted have to be retrievedfrom the database (object identifier)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 21: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Interface

connecting to database (embedded mode):Db4oEmbedded.openFile(filePath) → ObjectContainer

insert and update:ObjectContainer.store(object)

delete:ObjectContainer.delete(object)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 22: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Interface

connecting to database (embedded mode):Db4oEmbedded.openFile(filePath) → ObjectContainer

insert and update:ObjectContainer.store(object)

delete:ObjectContainer.delete(object)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 23: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Interface

all instances of a class:ObjectContainer.query(Class.class) → List<Class>

query by example:ObjectContainer.queryByExample(Class prototype)→ ObjectSet<Class>

H. Turgut Uyar, Sule Oguducu Database Systems

Page 24: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Interface

all instances of a class:ObjectContainer.query(Class.class) → List<Class>

query by example:ObjectContainer.queryByExample(Class prototype)→ ObjectSet<Class>

H. Turgut Uyar, Sule Oguducu Database Systems

Page 25: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Interface

query condition: Predicate<Class>

implement the match method:public boolean match(Class object)

query:ObjectContainer.query(Predicate<Class> predicate)→ List<Class>

H. Turgut Uyar, Sule Oguducu Database Systems

Page 26: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Interface

query condition: Predicate<Class>

implement the match method:public boolean match(Class object)

query:ObjectContainer.query(Predicate<Class> predicate)→ List<Class>

H. Turgut Uyar, Sule Oguducu Database Systems

Page 27: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Examples

Example (connecting to the database)

ObjectContainer db = Db4oEmbedded.openFile("imdb.db4o"

);

H. Turgut Uyar, Sule Oguducu Database Systems

Page 28: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Examples

Example (query: all movies)

List <Movie > movies = db.query(Movie.class);for (Movie movie : movies) {

...}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 29: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o 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 ();...

}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 30: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o 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;

}});

H. Turgut Uyar, Sule Oguducu Database Systems

Page 31: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Examples

Example (insert)

Movie m = new Movie("Casablanca");m.setYear (1942);db.store(m);db.commit ();

H. Turgut Uyar, Sule Oguducu Database Systems

Page 32: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o 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 ();

H. Turgut Uyar, Sule Oguducu Database Systems

Page 33: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

db4o Examples

Example (delete)

Movie prototype = new Movie("Casablanca");ObjectSet <Movie > result =

db.queryByExample(prototype );Movie found = result.next ();db.delete(found);db.commit ();

H. Turgut Uyar, Sule Oguducu Database Systems

Page 34: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionObject IdentifiersExample: db4oReferences

References

Required Reading: Date

Chapter 25: Object Databases

H. Turgut Uyar, Sule Oguducu Database Systems

Page 35: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Topics

1 Object DatabasesIntroductionObject IdentifiersExample: db4o

2 Object/Relational DatabasesIntroductionExample: Persist

3 XML DatabasesIntroductionXQueryExample: DBXML

H. Turgut Uyar, Sule Oguducu Database Systems

Page 36: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Object/Relational Mapping

software is object-oriented

database is relational

map software components to database components

H. Turgut Uyar, Sule Oguducu Database Systems

Page 37: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Topics

1 Object DatabasesIntroductionObject IdentifiersExample: db4o

2 Object/Relational DatabasesIntroductionExample: Persist

3 XML DatabasesIntroductionXQueryExample: DBXML

H. Turgut Uyar, Sule Oguducu Database Systems

Page 38: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Example: Persist

wraps a JDBC connection

translates the object database interface into SQL statements

H. Turgut Uyar, Sule Oguducu Database Systems

Page 39: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Persist Interface

database connection: Connection connectionPersist(connection) → Persist

insert:Persist.insert(object)

update:Persist.update(object)

delete:Persist.delete(object)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 40: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Persist Interface

database connection: Connection connectionPersist(connection) → Persist

insert:Persist.insert(object)

update:Persist.update(object)

delete:Persist.delete(object)

H. Turgut Uyar, Sule Oguducu Database Systems

Page 41: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Persist Interface

query: all instances of a classPersist.readList(Class.class) → List<Class>

query using SQL: similar to prepared statementsPersist.readList(Class.class, String query, params)→ List<Class>

H. Turgut Uyar, Sule Oguducu Database Systems

Page 42: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Persist Examples

Example (database connection)

Connection connection =DriverManager.getConnection(jdbcURL );

Persist db = new Persist(connection );

H. Turgut Uyar, Sule Oguducu Database Systems

Page 43: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Persist Examples

Example (query: all movies)

List <Movie > movies = db.readList(Movie.class);for (Movie movie : movies) {

...}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 44: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Persist Examples

Example (query using SQL: all movies in 1977)

List <Movie > movies = db.readList(Movie.class ,"SELECT * FROM MOVIE WHERE (YEAR = ?)",1977);

H. Turgut Uyar, Sule Oguducu Database Systems

Page 45: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Persist Examples

Example (insert)

Movie m = new Movie("Casablanca");m.setYear (1942);db.insert(m);

H. Turgut Uyar, Sule Oguducu Database Systems

Page 46: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Persist 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);

H. Turgut Uyar, Sule Oguducu Database Systems

Page 47: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

Persist Examples

Example (delete)

List <Movie > movies = db.readList(Movie.class ,"SELECT * FROM MOVIE WHERE (TITLE = ?)","Casablanca");

Movie found = movies.get (0);db.delete(found);

H. Turgut Uyar, Sule Oguducu Database Systems

Page 48: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionExample: PersistReferences

References

Required Reading: Date

Chapter 26: Object/Relational Databases

H. Turgut Uyar, Sule Oguducu Database Systems

Page 49: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

Topics

1 Object DatabasesIntroductionObject IdentifiersExample: db4o

2 Object/Relational DatabasesIntroductionExample: Persist

3 XML DatabasesIntroductionXQueryExample: DBXML

H. Turgut Uyar, Sule Oguducu Database Systems

Page 50: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML

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, ...

H. Turgut Uyar, Sule Oguducu Database Systems

Page 51: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML

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, ...

H. Turgut Uyar, Sule Oguducu Database Systems

Page 52: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML

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, ...

H. Turgut Uyar, Sule Oguducu Database Systems

Page 53: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML Structure

an XML document forms a tree

nodes: elements

root node: document elementleaves: character data, self-closing elements

opening/closing tags

attributes

H. Turgut Uyar, Sule Oguducu Database Systems

Page 54: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML Structure

an XML document forms a tree

nodes: elements

root node: document elementleaves: character data, self-closing elements

opening/closing tags

attributes

H. Turgut Uyar, Sule Oguducu Database Systems

Page 55: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML 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>

H. Turgut Uyar, Sule Oguducu Database Systems

Page 56: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML Example

Example (DocBook)

<book lang="en"><title>Foobar Report </title><bookinfo >...</bookinfo ><chapter >...</chapter ><chapter >...</chapter >...

</book>

H. Turgut Uyar, Sule Oguducu Database Systems

Page 57: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML Example

Example (DocBook)

<bookinfo ><author >

<firstname >John</firstname ><surname >Foobar </surname >

</author ><date>2007</date>

</bookinfo >

H. Turgut Uyar, Sule Oguducu Database Systems

Page 58: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML Example

Example (DocBook)

<chapter ><title>Introduction </title ><section >

<title>Description </title><para>Foobar is ...</para>

</section >...

</chapter >

H. Turgut Uyar, Sule Oguducu Database Systems

Page 59: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML Example

Example (movies)

<movies ><movie color="Color">

<title>Usual Suspects </title>...

</movie><movie color="Color">

<title>Being John Malkovich </title>...

</movie>...

</movies >

H. Turgut Uyar, Sule Oguducu Database Systems

Page 60: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XML 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>

H. Turgut Uyar, Sule Oguducu Database Systems

Page 61: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

Topics

1 Object DatabasesIntroductionObject IdentifiersExample: db4o

2 Object/Relational DatabasesIntroductionExample: Persist

3 XML DatabasesIntroductionXQueryExample: DBXML

H. Turgut Uyar, Sule Oguducu Database Systems

Page 62: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XQuery

XPath: selecting nodes and data from XML documents

XQuery: XPath + update operations

H. Turgut Uyar, Sule Oguducu Database Systems

Page 63: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath

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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 64: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath

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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 65: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

Location Steps

location step structure:axis::node_selector[predicate]

axis: where to search

selector: what to search

predicate: under which conditions

H. Turgut Uyar, Sule Oguducu Database Systems

Page 66: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

Location Steps

location step structure:axis::node_selector[predicate]

axis: where to search

selector: what to search

predicate: under which conditions

H. Turgut Uyar, Sule Oguducu Database Systems

Page 67: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

Axes

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

...

H. Turgut Uyar, Sule Oguducu Database Systems

Page 68: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

Node Selectors

node tag

node attribute

node text: text()

all children: *

H. Turgut Uyar, Sule Oguducu Database Systems

Page 69: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 70: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 71: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 72: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 73: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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"]

H. Turgut Uyar, Sule Oguducu Database Systems

Page 74: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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"]

H. Turgut Uyar, Sule Oguducu Database Systems

Page 75: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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"]

H. Turgut Uyar, Sule Oguducu Database Systems

Page 76: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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"]

H. Turgut Uyar, Sule Oguducu Database Systems

Page 77: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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"]

H. Turgut Uyar, Sule Oguducu Database Systems

Page 78: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

XPath 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"]

H. Turgut Uyar, Sule Oguducu Database Systems

Page 79: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

Topics

1 Object DatabasesIntroductionObject IdentifiersExample: db4o

2 Object/Relational DatabasesIntroductionExample: Persist

3 XML DatabasesIntroductionXQueryExample: DBXML

H. Turgut Uyar, Sule Oguducu Database Systems

Page 80: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

Example: 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 81: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML Interface

creating a database:

create an XmlManager objectXmlManager.createContainer(name) → XmlContainerput a document element:XmlContainer.putDocument(namespace, xml_string,

configuration)

connecting to an existing database:

create an XmlManager objectif XmlManager.existsContainer(name) != 0XmlManager.openContainer(name) → XmlContainer

H. Turgut Uyar, Sule Oguducu Database Systems

Page 82: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML Interface

creating a database:

create an XmlManager objectXmlManager.createContainer(name) → XmlContainerput a document element:XmlContainer.putDocument(namespace, xml_string,

configuration)

connecting to an existing database:

create an XmlManager objectif XmlManager.existsContainer(name) != 0XmlManager.openContainer(name) → XmlContainer

H. Turgut Uyar, Sule Oguducu Database Systems

Page 83: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 84: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 85: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML 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

H. Turgut Uyar, Sule Oguducu Database Systems

Page 86: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML 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);

}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 87: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML 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 ();

}

H. Turgut Uyar, Sule Oguducu Database Systems

Page 88: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML 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;

}H. Turgut Uyar, Sule Oguducu Database Systems

Page 89: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML 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 ();

H. Turgut Uyar, Sule Oguducu Database Systems

Page 90: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

DBXML 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 ();

H. Turgut Uyar, Sule Oguducu Database Systems

Page 91: Database Systems - Object Databases

Object DatabasesObject/Relational Databases

XML Databases

IntroductionXQueryExample: DBXMLReferences

References

Required Reading: Date

Chapter 27: The World Wide Web and XML

H. Turgut Uyar, Sule Oguducu Database Systems