Top Banner
How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1
173

How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Aug 01, 2018

Download

Documents

hatruc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

How to MoveObject-Relational Mapping

into the DatabaseDavid Wheeler

KineticodeOSCON 2005

1

Page 2: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PolymorphicDatabase Design

David WheelerKineticode

OSCON 2005

2

Page 3: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Executive summary

3

Page 4: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Executive summary

Look at simple object-oriented examples

3

Page 5: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Executive summary

Look at simple object-oriented examples

Examine typical database serialization approach

3

Page 6: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Executive summary

Look at simple object-oriented examples

Examine typical database serialization approach

Make the database do the work

3

Page 7: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Executive summary

Look at simple object-oriented examples

Examine typical database serialization approach

Make the database do the work

Let’s give it a try

3

Page 8: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Start with a class

4

Page 9: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Start with a classCD::Album

titleartistpublisherisbn

4

Page 10: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Start with a class

Simple UML is good UML

CD::Album

titleartistpublisherisbn

4

Page 11: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Standard Approach

5

Page 12: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Standard ApproachRules:

5

Page 13: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Standard ApproachRules:

Class == Table

5

Page 14: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Standard ApproachRules:

Class == Table

Object == Row

5

Page 15: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Standard ApproachRules:

Class == Table

Object == Row

Attribute == Column

5

Page 16: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Standard ApproachRules:

Class == Table

Object == Row

Attribute == Column

Methodology

5

Page 17: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Standard ApproachRules:

Class == Table

Object == Row

Attribute == Column

Methodology

Create Table

5

Page 18: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Standard ApproachRules:

Class == Table

Object == Row

Attribute == Column

Methodology

Create Table

Create SELECT query5

Page 19: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create a Table

6

Page 20: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create a TableCREATE TABLE album ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT, artist TEXT, publisher TEXT, isbn TEXT);

6

Page 21: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Not much to see here

Create a TableCREATE TABLE album ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT, artist TEXT, publisher TEXT, isbn TEXT);

6

Page 22: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Not much to see here

Use primary keys!

Create a TableCREATE TABLE album ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT, artist TEXT, publisher TEXT, isbn TEXT);

6

Page 23: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query

7

Page 24: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SELECT id, title, artist, publisher, isbnFROM album;

Write SELECT query

7

Page 25: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SELECT id, title, artist, publisher, isbnFROM albumWHERE id = ?;

Write SELECT query

7

Page 26: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Gee, that was easy

SELECT id, title, artist, publisher, isbnFROM albumWHERE id = ?;

Write SELECT query

7

Page 27: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Gee, that was easy

Inflate objects from each row

SELECT id, title, artist, publisher, isbnFROM albumWHERE id = ?;

Write SELECT query

7

Page 28: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Gee, that was easy

Inflate objects from each row

Let’s see if it works…

SELECT id, title, artist, publisher, isbnFROM albumWHERE id = ?;

Write SELECT query

7

Page 29: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Examine the outputid title artist publisher isbn ---------- ------------------------ ------------------------- ---------- ----------1 Rage Against the Machine Rage Against the Machine Atlantic 012848939 2 OK Computer Radiohead Atlantic 934293249 3 Elephant The White Stripes BMG 000864P55 4 Get Behind Me Satan The White Stripes BMG 949394595 5 Purple Rain Prince and the Revolution Warner Bro 638594823 6 Roots of a Revolution James Brown Polydor 496758395 7 Blood Sugar Sex Magik Red Hot Chili Peppers Warnter Br 957483845 8 Frizzle Fry Primus Atlantic 685968345

8

Page 30: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Examine the output

Hey cool!

id title artist publisher isbn ---------- ------------------------ ------------------------- ---------- ----------1 Rage Against the Machine Rage Against the Machine Atlantic 012848939 2 OK Computer Radiohead Atlantic 934293249 3 Elephant The White Stripes BMG 000864P55 4 Get Behind Me Satan The White Stripes BMG 949394595 5 Purple Rain Prince and the Revolution Warner Bro 638594823 6 Roots of a Revolution James Brown Polydor 496758395 7 Blood Sugar Sex Magik Red Hot Chili Peppers Warnter Br 957483845 8 Frizzle Fry Primus Atlantic 685968345

8

Page 31: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Examine the output

Hey cool!

Single row == single object

id title artist publisher isbn ---------- ------------------------ ------------------------- ---------- ----------1 Rage Against the Machine Rage Against the Machine Atlantic 012848939 2 OK Computer Radiohead Atlantic 934293249 3 Elephant The White Stripes BMG 000864P55 4 Get Behind Me Satan The White Stripes BMG 949394595 5 Purple Rain Prince and the Revolution Warner Bro 638594823 6 Roots of a Revolution James Brown Polydor 496758395 7 Blood Sugar Sex Magik Red Hot Chili Peppers Warnter Br 957483845 8 Frizzle Fry Primus Atlantic 685968345

8

Page 32: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Examine the output

Hey cool!

Single row == single object

Single column == single attribute

id title artist publisher isbn ---------- ------------------------ ------------------------- ---------- ----------1 Rage Against the Machine Rage Against the Machine Atlantic 012848939 2 OK Computer Radiohead Atlantic 934293249 3 Elephant The White Stripes BMG 000864P55 4 Get Behind Me Satan The White Stripes BMG 949394595 5 Purple Rain Prince and the Revolution Warner Bro 638594823 6 Roots of a Revolution James Brown Polydor 496758395 7 Blood Sugar Sex Magik Red Hot Chili Peppers Warnter Br 957483845 8 Frizzle Fry Primus Atlantic 685968345

8

Page 33: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Let’s add a subclassCD::Album

titleartistpublisherisbn

9

Page 34: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Let’s add a subclass

CD::Music::Classical

composerconductorsoloistorchestra

CD::Album

titleartistpublisherisbn

9

Page 35: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Let’s add a subclass

Piece of cake, right?

CD::Music::Classical

composerconductorsoloistorchestra

CD::Album

titleartistpublisherisbn

9

Page 36: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Typical Subclassing Approach

10

Page 37: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Typical Subclassing Approach

Create another table

10

Page 38: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Typical Subclassing Approach

Create another table

Reference the parent class’ table

10

Page 39: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Typical Subclassing Approach

Create another table

Reference the parent class’ table

Write a two queries for each table

10

Page 40: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Typical Subclassing Approach

Create another table

Reference the parent class’ table

Write a two queries for each table

Write a JOIN query for both tables

10

Page 41: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the new table

11

Page 42: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the new tableCREATE TABLE classical ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, album_id INTEGER NOT NULL REFERENCES album(id), composer TEXT, conductor TEXT, orchestra TEXT);

11

Page 43: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the new table

Hrm…not bad

CREATE TABLE classical ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, album_id INTEGER NOT NULL REFERENCES album(id), composer TEXT, conductor TEXT, orchestra TEXT);

11

Page 44: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the new table

Hrm…not bad

You use foreign keys, right?

CREATE TABLE classical ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, album_id INTEGER NOT NULL REFERENCES album(id), composer TEXT, conductor TEXT, orchestra TEXT);

11

Page 45: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the new table

Hrm…not bad

You use foreign keys, right?

What about the SELECT statement?

CREATE TABLE classical ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, album_id INTEGER NOT NULL REFERENCES album(id), composer TEXT, conductor TEXT, orchestra TEXT);

11

Page 46: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query

12

Page 47: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query

SELECT m.id, title, artist, publisher, isbn, c.id composer, conductor, orchestraFROM album m JOIN classical c ON m.id = c.album_id;

12

Page 48: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query

Also not too bad

SELECT m.id, title, artist, publisher, isbn, c.id composer, conductor, orchestraFROM album m JOIN classical c ON m.id = c.album_id;

12

Page 49: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query

Also not too bad

Let’s see how it works…

SELECT m.id, title, artist, publisher, isbn, c.id composer, conductor, orchestraFROM album m JOIN classical c ON m.id = c.album_id;

12

Page 50: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What’s wrong here?id title id composer ---------- ------------------ ---------- ------------9 Emperor Concerto 1 Beethoven 10 Eroica Symphony 2 Beethoven 11 Amadeus Soundtrack 3 Mozart

13

Page 51: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What’s wrong here?

Two IDs? That’s annoying

id title id composer ---------- ------------------ ---------- ------------9 Emperor Concerto 1 Beethoven 10 Eroica Symphony 2 Beethoven 11 Amadeus Soundtrack 3 Mozart

13

Page 52: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What’s wrong here?

Two IDs? That’s annoying

Different ID for the same object

id title id composer ---------- ------------------ ---------- ------------9 Emperor Concerto 1 Beethoven 10 Eroica Symphony 2 Beethoven 11 Amadeus Soundtrack 3 Mozart

13

Page 53: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What’s wrong here?

Two IDs? That’s annoying

Different ID for the same object

Which to use?

id title id composer ---------- ------------------ ---------- ------------9 Emperor Concerto 1 Beethoven 10 Eroica Symphony 2 Beethoven 11 Amadeus Soundtrack 3 Mozart

13

Page 54: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What’s wrong here?

Two IDs? That’s annoying

Different ID for the same object

Which to use?

Stick to one and avoid the problem.

id title id composer ---------- ------------------ ---------- ------------9 Emperor Concerto 1 Beethoven 10 Eroica Symphony 2 Beethoven 11 Amadeus Soundtrack 3 Mozart

13

Page 55: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the table again

14

Page 56: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the table againCREATE TABLE classical ( id INTEGER NOT NULL PRIMARY KEY REFERENCES album (id), composer TEXT, conductor TEXT, orchestra TEXT);

14

Page 57: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the table again

Gee, that’s simpler

CREATE TABLE classical ( id INTEGER NOT NULL PRIMARY KEY REFERENCES album (id), composer TEXT, conductor TEXT, orchestra TEXT);

14

Page 58: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the table again

Gee, that’s simpler

The primary key is also a foreign key

CREATE TABLE classical ( id INTEGER NOT NULL PRIMARY KEY REFERENCES album (id), composer TEXT, conductor TEXT, orchestra TEXT);

14

Page 59: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Create the table again

Gee, that’s simpler

The primary key is also a foreign key

You still use the foreign key constraint, right?

CREATE TABLE classical ( id INTEGER NOT NULL PRIMARY KEY REFERENCES album (id), composer TEXT, conductor TEXT, orchestra TEXT);

14

Page 60: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query again

15

Page 61: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query againSELECT m.id AS id, title, artist, publisher, isbn, composer, conductor, orchestraFROM album m JOIN classical c ON m.id = c.id;

15

Page 62: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query again

That got a bit simpler, too

SELECT m.id AS id, title, artist, publisher, isbn, composer, conductor, orchestraFROM album m JOIN classical c ON m.id = c.id;

15

Page 63: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query again

That got a bit simpler, too

Disambiguate the ID column

SELECT m.id AS id, title, artist, publisher, isbn, composer, conductor, orchestraFROM album m JOIN classical c ON m.id = c.id;

15

Page 64: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Write SELECT query again

That got a bit simpler, too

Disambiguate the ID column

Let’s see if it works…

SELECT m.id AS id, title, artist, publisher, isbn, composer, conductor, orchestraFROM album m JOIN classical c ON m.id = c.id;

15

Page 65: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Now we’re talkin’!id title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

16

Page 66: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Now we’re talkin’!

Just one ID

id title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

16

Page 67: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Now we’re talkin’!

Just one ID

But why must we write a JOIN query?

id title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

16

Page 68: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Now we’re talkin’!

Just one ID

But why must we write a JOIN query?

Couldn’t it be even simpler?

id title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

16

Page 69: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Now we’re talkin’!

Just one ID

But why must we write a JOIN query?

Couldn’t it be even simpler?

Can’t we truly have one class == one table?

id title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

16

Page 70: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Yes, it can!

SELECT m.id AS id, title, artist, publisher, isbn, composer, conductor, orchestraFROM album m JOIN classical c ON m.id = c.id;

17

Page 71: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Yes, it can!CREATE VIEW classical ASSELECT m.id AS id, title, artist, publisher, isbn, composer, conductor, orchestraFROM album m JOIN album_classical c ON m.id = c.id;

17

Page 72: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Yes, it can!

Now we have a single “table”

CREATE VIEW classical ASSELECT m.id AS id, title, artist, publisher, isbn, composer, conductor, orchestraFROM album m JOIN album_classical c ON m.id = c.id;

17

Page 73: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Yes, it can!

Now we have a single “table”

Rename subclass table to represent inheritance

CREATE VIEW classical ASSELECT m.id AS id, title, artist, publisher, isbn, composer, conductor, orchestraFROM album m JOIN album_classical c ON m.id = c.id;

17

Page 74: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Yes, it can!

Now we have a single “table”

Rename subclass table to represent inheritance

Supported by PostgreSQL, SQLite, and MySQL

CREATE VIEW classical ASSELECT m.id AS id, title, artist, publisher, isbn, composer, conductor, orchestraFROM album m JOIN album_classical c ON m.id = c.id;

17

Page 75: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Query the VIEW

18

Page 76: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Query the VIEWSELECT id, title, artist, publisher, isbn, composer, conductor, orchestraFROM classical;

18

Page 77: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Query the VIEW

Also a bit simpler

SELECT id, title, artist, publisher, isbn, composer, conductor, orchestraFROM classical;

18

Page 78: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Query the VIEW

Also a bit simpler

The database handles inheritance transparently

SELECT id, title, artist, publisher, isbn, composer, conductor, orchestraFROM classical;

18

Page 79: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Query the VIEW

Also a bit simpler

The database handles inheritance transparently

The VIEW is compiled, thus faster

SELECT id, title, artist, publisher, isbn, composer, conductor, orchestraFROM classical;

18

Page 80: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Query the VIEW

Also a bit simpler

The database handles inheritance transparently

The VIEW is compiled, thus faster

Let’s see if it works…

SELECT id, title, artist, publisher, isbn, composer, conductor, orchestraFROM classical;

18

Page 81: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Get the same outputid title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

19

Page 82: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Get the same output

Hey, awesome!

id title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

19

Page 83: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Get the same output

Hey, awesome!

Returns the same records as before

id title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

19

Page 84: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Get the same output

Hey, awesome!

Returns the same records as before

It’s faster, too

id title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

19

Page 85: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Get the same output

Hey, awesome!

Returns the same records as before

It’s faster, too

But what about INSERTs, UPDATEs, and DELETEs?

id title composer conductor ---------- ------------------ ------------ ----------9 Emperor Concerto Beethoven Ozawa 10 Eroica Symphony Beethoven Bernstein 11 Amadeus Soundtrack Mozart Neville Ma

19

Page 86: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Back to the base class

20

Page 87: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Back to the base classINSERT INTO album (title, artist, publisher, isbn)VALUES ('Kid B', 'Radiohed', 'Polygramm', '0000');

20

Page 88: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Back to the base classINSERT INTO album (title, artist, publisher, isbn)VALUES ('Kid B', 'Radiohed', 'Polygramm', '0000');

UPDATE albumSET title = 'Kid A', artist = 'Radiohead', publisher = 'Polygram', isbn = '4959'WHERE id = 12;

20

Page 89: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Back to the base classINSERT INTO album (title, artist, publisher, isbn)VALUES ('Kid B', 'Radiohed', 'Polygramm', '0000');

UPDATE albumSET title = 'Kid A', artist = 'Radiohead', publisher = 'Polygram', isbn = '4959'WHERE id = 12;

DELETE FROM albumWHERE id = 12;

20

Page 90: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Back to the base class

Seems pretty straight-forward

INSERT INTO album (title, artist, publisher, isbn)VALUES ('Kid B', 'Radiohed', 'Polygramm', '0000');

UPDATE albumSET title = 'Kid A', artist = 'Radiohead', publisher = 'Polygram', isbn = '4959'WHERE id = 12;

DELETE FROM albumWHERE id = 12;

20

Page 91: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Back to the base class

Seems pretty straight-forward

Let’s try it…

INSERT INTO album (title, artist, publisher, isbn)VALUES ('Kid B', 'Radiohed', 'Polygramm', '0000');

UPDATE albumSET title = 'Kid A', artist = 'Radiohead', publisher = 'Polygram', isbn = '4959'WHERE id = 12;

DELETE FROM albumWHERE id = 12;

20

Page 92: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

And now the subclass

21

Page 93: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

And now the subclassINSERT INTO classical (title, artist, publisher, isbn, composer, conductor, orchestra)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323', 'Verious', 'Jon Williams', 'Pittsburg Symphony');

21

Page 94: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

And now the subclassINSERT INTO classical (title, artist, publisher, isbn, composer, conductor, orchestra)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323', 'Verious', 'Jon Williams', 'Pittsburg Symphony');

UPDATE classicalSET title = 'Cinema Serenade', artist = 'Itzak Perlman', publisher = 'BMG', isbn = '2764', composer = 'Various', conductor = 'John Williams', orchestra = 'Pittsburgh Symphony'WHERE id = 13;

21

Page 95: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

And now the subclassINSERT INTO classical (title, artist, publisher, isbn, composer, conductor, orchestra)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323', 'Verious', 'Jon Williams', 'Pittsburg Symphony');

UPDATE classicalSET title = 'Cinema Serenade', artist = 'Itzak Perlman', publisher = 'BMG', isbn = '2764', composer = 'Various', conductor = 'John Williams', orchestra = 'Pittsburgh Symphony'WHERE id = 13;

DELETE FROM classicalWHERE id = 13;

21

Page 96: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

And now the subclass

Let’s see if these work…

INSERT INTO classical (title, artist, publisher, isbn, composer, conductor, orchestra)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323', 'Verious', 'Jon Williams', 'Pittsburg Symphony');

UPDATE classicalSET title = 'Cinema Serenade', artist = 'Itzak Perlman', publisher = 'BMG', isbn = '2764', composer = 'Various', conductor = 'John Williams', orchestra = 'Pittsburgh Symphony'WHERE id = 13;

DELETE FROM classicalWHERE id = 13;

21

Page 97: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

D’oh! What now?

INSERT INTO classical (title, artist, publisher, isbn, composer, conductor, orchestra)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323', 'Verious', 'Jon Williams', 'Pittsburg Symphony');

SQL error: cannot modify classical because it is a view

22

Page 98: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

D’oh! What now?

VIEWs are not updatable

INSERT INTO classical (title, artist, publisher, isbn, composer, conductor, orchestra)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323', 'Verious', 'Jon Williams', 'Pittsburg Symphony');

SQL error: cannot modify classical because it is a view

22

Page 99: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

D’oh! What now?

VIEWs are not updatable

Must modify the two tables separately

INSERT INTO classical (title, artist, publisher, isbn, composer, conductor, orchestra)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323', 'Verious', 'Jon Williams', 'Pittsburg Symphony');

SQL error: cannot modify classical because it is a view

22

Page 100: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

INSERT classical object

23

Page 101: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

INSERT classical objectINSERT INTO album (title, artist, publisher, isbn)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323');

INSERT INTO album_classical (id, composer, conductor, orchestra)VALUES (last_insert_rowid(), 'Verious', 'Jon Williams', 'Pittsburg Symphony');

23

Page 102: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

INSERT classical object

We have to modify the two table separately

INSERT INTO album (title, artist, publisher, isbn)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323');

INSERT INTO album_classical (id, composer, conductor, orchestra)VALUES (last_insert_rowid(), 'Verious', 'Jon Williams', 'Pittsburg Symphony');

23

Page 103: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

INSERT classical object

We have to modify the two table separately

Use function to populate subclass ID

INSERT INTO album (title, artist, publisher, isbn)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323');

INSERT INTO album_classical (id, composer, conductor, orchestra)VALUES (last_insert_rowid(), 'Verious', 'Jon Williams', 'Pittsburg Symphony');

23

Page 104: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

INSERT classical object

We have to modify the two table separately

Use function to populate subclass ID

SQLite: last_insert_rowid()

INSERT INTO album (title, artist, publisher, isbn)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323');

INSERT INTO album_classical (id, composer, conductor, orchestra)VALUES (last_insert_rowid(), 'Verious', 'Jon Williams', 'Pittsburg Symphony');

23

Page 105: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

INSERT classical object

We have to modify the two table separately

Use function to populate subclass ID

SQLite: last_insert_rowid()

MySQL: last_insert_id()

INSERT INTO album (title, artist, publisher, isbn)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323');

INSERT INTO album_classical (id, composer, conductor, orchestra)VALUES (last_insert_rowid(), 'Verious', 'Jon Williams', 'Pittsburg Symphony');

23

Page 106: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

INSERT classical object

We have to modify the two table separately

Use function to populate subclass ID

SQLite: last_insert_rowid()

MySQL: last_insert_id()

PostgreSQL: CURRVAL('album_id_seq');

INSERT INTO album (title, artist, publisher, isbn)VALUES ('Cinema Serenad', 'Itzack Perlman', 'BMC', '2323');

INSERT INTO album_classical (id, composer, conductor, orchestra)VALUES (last_insert_rowid(), 'Verious', 'Jon Williams', 'Pittsburg Symphony');

23

Page 107: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

UPDATE classical object

24

Page 108: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

UPDATE classical objectUPDATE albumSET title = 'Cinema Serenade', artist = 'Itzak Perlman', publisher = 'BMG', isbn = '2764'WHERE id = 13;

UPDATE album_classicalSET composer = 'Various', conductor = 'John Williams', orchestra = 'Pittsburgh Symphony'WHERE id = 13;

24

Page 109: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

UPDATE classical object

Again, modify the two table separately

UPDATE albumSET title = 'Cinema Serenade', artist = 'Itzak Perlman', publisher = 'BMG', isbn = '2764'WHERE id = 13;

UPDATE album_classicalSET composer = 'Various', conductor = 'John Williams', orchestra = 'Pittsburgh Symphony'WHERE id = 13;

24

Page 110: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

DELETE classical object

25

Page 111: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

DELETE classical objectDELETE FROM album_classicalWHERE id = 13;

DELETE FROM albumWHERE id = 13;

25

Page 112: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

DELETE classical object

And again, modify the two table separately

DELETE FROM album_classicalWHERE id = 13;

DELETE FROM albumWHERE id = 13;

25

Page 113: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

DELETE classical object

And again, modify the two table separately

Unless your foreign key is ON DELETE CASCADE

DELETE FROM album_classicalWHERE id = 13;

DELETE FROM albumWHERE id = 13;

25

Page 114: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

DELETE classical object

And again, modify the two table separately

Unless your foreign key is ON DELETE CASCADE

Let’s see how these queries work…

DELETE FROM album_classicalWHERE id = 13;

DELETE FROM albumWHERE id = 13;

25

Page 115: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Is there no other way?

26

Page 116: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Is there no other way?

The more complex your relations, the more queries

26

Page 117: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Is there no other way?

The more complex your relations, the more queries

All because you can’t update VIEWs

26

Page 118: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Is there no other way?

The more complex your relations, the more queries

All because you can’t update VIEWs

Or can you?

26

Page 119: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Updatable views

27

Page 120: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Updatable viewsSQLite supports triggers on VIEWs

27

Page 121: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Updatable viewsSQLite supports triggers on VIEWs

PostgreSQL supports rules on VIEWs

27

Page 122: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Updatable viewsSQLite supports triggers on VIEWs

PostgreSQL supports rules on VIEWs

With work, you can INSERT, UPDATE, and DELETE on VIEWs

27

Page 123: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite INSERT trigger

28

Page 124: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite INSERT triggerCREATE TRIGGER insert_classicalINSTEAD OF INSERT ON classicalFOR EACH ROW BEGIN INSERT INTO album (title, artist, publisher, isbn) VALUES (NEW.title, NEW.artist, NEW.publisher, NEW.isbn);

INSERT INTO album_classical (id, composer, conductor, orchestra) VALUES (last_insert_rowid(), NEW.composer, NEW.conductor, NEW.orchestra);END;

28

Page 125: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite INSERT trigger

Use NEW variable to populate values

CREATE TRIGGER insert_classicalINSTEAD OF INSERT ON classicalFOR EACH ROW BEGIN INSERT INTO album (title, artist, publisher, isbn) VALUES (NEW.title, NEW.artist, NEW.publisher, NEW.isbn);

INSERT INTO album_classical (id, composer, conductor, orchestra) VALUES (last_insert_rowid(), NEW.composer, NEW.conductor, NEW.orchestra);END;

28

Page 126: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite INSERT trigger

Use NEW variable to populate values

Use last_insert_rowid() for classical ID

CREATE TRIGGER insert_classicalINSTEAD OF INSERT ON classicalFOR EACH ROW BEGIN INSERT INTO album (title, artist, publisher, isbn) VALUES (NEW.title, NEW.artist, NEW.publisher, NEW.isbn);

INSERT INTO album_classical (id, composer, conductor, orchestra) VALUES (last_insert_rowid(), NEW.composer, NEW.conductor, NEW.orchestra);END;

28

Page 127: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL INSERT rule

29

Page 128: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL INSERT ruleCREATE RULE insert_classical ASON INSERT TO classical DO INSTEAD ( INSERT INTO album (title, artist, publisher, isbn) VALUES (NEW.title, NEW.artist, NEW.publisher, NEW.isbn);

INSERT INTO album_classical (id, composer, conductor, orchestra) VALUES (CURRVAL('seq_album_id_seq'), NEW.composer, NEW.conductor, NEW.orchestra););

29

Page 129: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL INSERT rule

Use NEW variable to populate values

CREATE RULE insert_classical ASON INSERT TO classical DO INSTEAD ( INSERT INTO album (title, artist, publisher, isbn) VALUES (NEW.title, NEW.artist, NEW.publisher, NEW.isbn);

INSERT INTO album_classical (id, composer, conductor, orchestra) VALUES (CURRVAL('seq_album_id_seq'), NEW.composer, NEW.conductor, NEW.orchestra););

29

Page 130: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL INSERT rule

Use NEW variable to populate values

Use CURRVAL() to populate classical ID

CREATE RULE insert_classical ASON INSERT TO classical DO INSTEAD ( INSERT INTO album (title, artist, publisher, isbn) VALUES (NEW.title, NEW.artist, NEW.publisher, NEW.isbn);

INSERT INTO album_classical (id, composer, conductor, orchestra) VALUES (CURRVAL('seq_album_id_seq'), NEW.composer, NEW.conductor, NEW.orchestra););

29

Page 131: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite UPDATE trigger

30

Page 132: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite UPDATE triggerCREATE TRIGGER update_classicalINSTEAD OF UPDATE ON classicalFOR EACH ROW BEGIN UPDATE album SET title = NEW.title, artist = NEW.artist, publisher = NEW.publisher, isbn = NEW.isbn WHERE id = OLD.id;

UPDATE album_classical SET composer = NEW.composer, conductor = NEW.conductor, orchestra = NEW.orchestra WHERE id = OLD.id;END;

30

Page 133: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite UPDATE trigger

Use OLD.id variable to reference existing rows

CREATE TRIGGER update_classicalINSTEAD OF UPDATE ON classicalFOR EACH ROW BEGIN UPDATE album SET title = NEW.title, artist = NEW.artist, publisher = NEW.publisher, isbn = NEW.isbn WHERE id = OLD.id;

UPDATE album_classical SET composer = NEW.composer, conductor = NEW.conductor, orchestra = NEW.orchestra WHERE id = OLD.id;END;

30

Page 134: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL UPDATE rule

31

Page 135: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL UPDATE ruleCREATE RULE update_classical ASON UPDATE TO classical DO INSTEAD ( UPDATE album SET title = NEW.title, artist = NEW.artist, publisher = NEW.publisher, isbn = NEW.isbn WHERE id = OLD.id;

UPDATE album_classical SET composer = NEW.composer, conductor = NEW.conductor, orchestra = NEW.orchestra WHERE id = OLD.id;);

31

Page 136: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL UPDATE rule

Use OLD.id variable to reference existing rows

CREATE RULE update_classical ASON UPDATE TO classical DO INSTEAD ( UPDATE album SET title = NEW.title, artist = NEW.artist, publisher = NEW.publisher, isbn = NEW.isbn WHERE id = OLD.id;

UPDATE album_classical SET composer = NEW.composer, conductor = NEW.conductor, orchestra = NEW.orchestra WHERE id = OLD.id;);

31

Page 137: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite DELETE trigger

32

Page 138: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite DELETE trigger

CREATE TRIGGER delete_classicalINSTEAD OF DELETE ON classicalFOR EACH ROW BEGIN DELETE FROM album_classical WHERE id = OLD.id;

DELETE FROM album WHERE id = OLD.id;END;

32

Page 139: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

SQLite DELETE trigger

Use OLD.id to delete the proper row

CREATE TRIGGER delete_classicalINSTEAD OF DELETE ON classicalFOR EACH ROW BEGIN DELETE FROM album_classical WHERE id = OLD.id;

DELETE FROM album WHERE id = OLD.id;END;

32

Page 140: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL DELETE rule

33

Page 141: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL DELETE rule

CREATE RULE delete_classical ASON DELETE TO classical DO INSTEAD ( DELETE FROM album_classical WHERE id = OLD.id;

DELETE FROM album WHERE id = OLD.id;);

33

Page 142: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL DELETE rule

Use OLD.id to delete the proper row

CREATE RULE delete_classical ASON DELETE TO classical DO INSTEAD ( DELETE FROM album_classical WHERE id = OLD.id;

DELETE FROM album WHERE id = OLD.id;);

33

Page 143: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

PostgreSQL DELETE rule

Use OLD.id to delete the proper row

Let’s see how they work…

CREATE RULE delete_classical ASON DELETE TO classical DO INSTEAD ( DELETE FROM album_classical WHERE id = OLD.id;

DELETE FROM album WHERE id = OLD.id;);

33

Page 144: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Trigger/Rule Advantages

34

Page 145: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Trigger/Rule Advantages

Queries are pre-compiled

34

Page 146: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Trigger/Rule Advantages

Queries are pre-compiled

Much simpler client-side code

34

Page 147: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Trigger/Rule Advantages

Queries are pre-compiled

Much simpler client-side code

Fewer queries sent to the database

34

Page 148: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Trigger/Rule Advantages

Queries are pre-compiled

Much simpler client-side code

Fewer queries sent to the database

Reduced network overhead

34

Page 149: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Trigger/Rule Advantages

Queries are pre-compiled

Much simpler client-side code

Fewer queries sent to the database

Reduced network overhead

Maintains normalization

34

Page 150: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

One-to-many relationships

35

Page 151: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

One-to-many relationshipsOften have one-to-many relationships

35

Page 152: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

One-to-many relationshipsOften have one-to-many relationships

Let’s we add track objects

35

Page 153: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

One-to-many relationshipsOften have one-to-many relationships

Let’s we add track objects

Each refers to a single album object

35

Page 154: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

One-to-many relationshipsOften have one-to-many relationships

Let’s we add track objects

Each refers to a single album object

Often need to know album information for the track

35

Page 155: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

One-to-many relationshipsOften have one-to-many relationships

Let’s we add track objects

Each refers to a single album object

Often need to know album information for the track

Generally requires two queries, one for each object

35

Page 156: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

One-to-many relationshipsOften have one-to-many relationships

Let’s we add track objects

Each refers to a single album object

Often need to know album information for the track

Generally requires two queries, one for each objectSELECT id, title, numb, album_idFROM track;

SELECT id, title, artist, publisher, isbnFROM albumWHERE id = $album_id;

35

Page 157: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

JOIN composite objectsSELECT track.id AS id, track.title AS title, numb, album.id AS album__id, album.title AS album__title, artist AS album__artist, publisher AS album__publisher, isbn AS album__isbnFROM track JOIN album ON track.album_id = album.id;

36

Page 158: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

JOIN composite objects

Or, do a JOIN, instead

SELECT track.id AS id, track.title AS title, numb, album.id AS album__id, album.title AS album__title, artist AS album__artist, publisher AS album__publisher, isbn AS album__isbnFROM track JOIN album ON track.album_id = album.id;

36

Page 159: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

JOIN composite objects

Or, do a JOIN, instead

Saves network overhead

SELECT track.id AS id, track.title AS title, numb, album.id AS album__id, album.title AS album__title, artist AS album__artist, publisher AS album__publisher, isbn AS album__isbnFROM track JOIN album ON track.album_id = album.id;

36

Page 160: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

JOIN composite objects

Or, do a JOIN, instead

Saves network overhead

But might as well make a view for it…

SELECT track.id AS id, track.title AS title, numb, album.id AS album__id, album.title AS album__title, artist AS album__artist, publisher AS album__publisher, isbn AS album__isbnFROM track JOIN album ON track.album_id = album.id;

36

Page 161: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

CREATE VIEW track ASSELECT _track.id AS id, _track.title AS title, numb, album.id AS album__id, album.title AS album__title, artist AS album__artist, publisher AS album__publisher, isbn AS album__isbnFROM _track JOIN album ON _track.album_id = album.id;

JOIN composite objects

Or, do a JOIN, instead

Saves network overhead

But might as well make a view for it…

36

Page 162: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What about MySQL?

37

Page 163: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What about MySQL?MySQL 5 supports views

37

Page 164: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What about MySQL?MySQL 5 supports views

Can INSERT, UPDATE, & DELETE on single table views

37

Page 165: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What about MySQL?MySQL 5 supports views

Can INSERT, UPDATE, & DELETE on single table views

Cannot on multi-table (JOIN) views

37

Page 166: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What about MySQL?MySQL 5 supports views

Can INSERT, UPDATE, & DELETE on single table views

Cannot on multi-table (JOIN) views

MySQL supports triggers

37

Page 167: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What about MySQL?MySQL 5 supports views

Can INSERT, UPDATE, & DELETE on single table views

Cannot on multi-table (JOIN) views

MySQL supports triggers

Cannot assign triggers to views

37

Page 168: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

What about MySQL?MySQL 5 supports views

Can INSERT, UPDATE, & DELETE on single table views

Cannot on multi-table (JOIN) views

MySQL supports triggers

Cannot assign triggers to views

No rules

37

Page 169: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Resources

38

Page 170: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Resources

“PostgreSQL: Introduction and Concepts” by Bruce Momjianhttp://xrl.us/gy3a

38

Page 171: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Resources

“PostgreSQL: Introduction and Concepts” by Bruce Momjianhttp://xrl.us/gy3a

SQLite: http://www.sqlite.org/

38

Page 172: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Resources

“PostgreSQL: Introduction and Concepts” by Bruce Momjianhttp://xrl.us/gy3a

SQLite: http://www.sqlite.org/

MySQL: http://dev.mysql.com/doc/mysql/en/views.html

38

Page 173: How to Move Object-Relational Mapping into the Database · How to Move Object-Relational Mapping into the Database David Wheeler Kineticode OSCON 2005 1. ... 5 Purple Rain Prince

Thank youDavid Wheeler

KineticodeOSCON 2005

[email protected]

39