DBIx::DataModel Classes and UML-style associations on top of DBI - - PowerPoint PPT Presentation

dbix datamodel
SMART_READER_LITE
LIVE PREVIEW

DBIx::DataModel Classes and UML-style associations on top of DBI - - PowerPoint PPT Presentation

DBIx::DataModel Classes and UML-style associations on top of DBI (just an appetizer...) laurent.dami@justice.ge.ch LD, PJ-GE, july 2006 2 Agenda Introduction ORMs Design issues Unified Modelling Language (UML) Tables


slide-1
SLIDE 1

DBIx::DataModel

Classes and UML-style associations on top of DBI (just an appetizer...)

laurent.dami@justice.ge.ch

slide-2
SLIDE 2

LD, PJ-GE, july 2006 2

Agenda

Introduction

ORMs Design issues

Unified Modelling Language (UML) Tables

Declaration Usage

Associations

Declaration Usage

slide-3
SLIDE 3

Introduction

ORMs Design issues

slide-4
SLIDE 4

LD, PJ-GE, july 2006 4

ORM : Object-Relational Mapping

r1 r2 ... c1 c2 c3 ... c3 c4 +c1 +c2 +c3 r1 : class1 DBMS r2 : class1 RAM table1 table2

slide-5
SLIDE 5

LD, PJ-GE, july 2006 5

What for ?

[catalyst list] On Thu, 2006-06-08, Steve wrote:

Not intending to start any sort of rancorous discussion, but I was wondering whether someone could illuminate me a little? I'm comfortable with SQL, and with DBI. I write basic SQL that runs just fine on all databases, or more complex SQL when I want to target a single database (ususally postgresql). What value does an ORM add for a user like me?

slide-6
SLIDE 6

LD, PJ-GE, july 2006 6

ORM useful for ...

navigation between tables data conversions generate complex SQL queries from Perl

datastructures

expansion of tree data structures coded in

the relational model

transaction encapsulation data validation auto-filling some columns at update

See Also : http://lists.rawmode.org/pipermail/catalyst/2006-June

slide-7
SLIDE 7

LD, PJ-GE, july 2006 7

ORMs in CPAN : TIMTOWTDI !

Class::DBI DBIx::Class Tangram Alzabo Rose::DB::Object SPOPS Data::ObjectDriver DBIx::RecordSet ORM DBIx::SQLEngine Class::PObject DBIx::DataModel ... ... ... ...

slide-8
SLIDE 8

LD, PJ-GE, july 2006 8

Some design issues

DRY : Don't Repeat Yourself When to fetch column values, and which Pure objects versus Perl datastructures Encapsulation / collaboration with lower-

level layers

Fine SQL tuning Caching

slide-9
SLIDE 9

Unified Modeling Language

slide-10
SLIDE 10

LD, PJ-GE, july 2006 10

UML conceptual Model : example

Author Distribution Module

  • ► depends on
  • ► contains

Package child parent

slide-11
SLIDE 11

LD, PJ-GE, july 2006 11

UML terminology

Author Distribution Module

  • ► depends on
  • ► contains

Package

  • multiplicity

association name role name class association

slide-12
SLIDE 12

LD, PJ-GE, july 2006 12

Model implementation

author_id author_name e_mail

  • Author

distrib_id module_id

Dependency

distrib_id distrib_name d_release

Distribution

module_id module_name

Module

  • link table for

n-to-n association

slide-13
SLIDE 13

Tables [ and Views ]

declaration usage

slide-14
SLIDE 14

LD, PJ-GE, july 2006 14

Schema and table declarations

  • !"#$"%$

&'()*"" *+ &'()*""*+ &'()*"" *+

slide-15
SLIDE 15

LD, PJ-GE, july 2006 15

Don't Repeat Yourself

DBMS Prog. Lang. generate Data Definition Statements get metadata from DBMS schema

slide-16
SLIDE 16

LD, PJ-GE, july 2006 16

Design philosophy

DBMS Prog. Lang. keep intersection to a strict minimum DBMS tools for declaring schema, datatypes, integrity rules, etc. Perl idioms : dual nature of Perl objects, dynamic typing, multiple inheritance, etc. freedom responsability

slide-17
SLIDE 17

LD, PJ-GE, july 2006 17

Fetching data : example 1

,,-$%$ $,./0 $12, 3,#444 )$1%.1/2516

  • 71%1/87

9 444: ; ,,$,

  • 1"
slide-18
SLIDE 18

LD, PJ-GE, july 2006 18

Fetching data : example 2

  • $1"2

2'()*""*+. ) 26" 2621"99. $ 2"<!.

  • ,$1=1"64449
slide-19
SLIDE 19

LD, PJ-GE, july 2006 19

Select API

&>?) 28=. >@28=. ) 285). 3-$ 28=3-3. /3 285. $ 28=. , 2$.

  • < 28A-<"%.
  • < 28A-<"%.

2)BBBB( BB See Also : SQL::Abstract

slide-20
SLIDE 20

LD, PJ-GE, july 2006 20

Retrieving columns

programmer decides which 28=$, 27;.C73 2707 , no delayed fetching

  • bjects have variable size !

runtime error if missing keys

for following joins for updates and deletes

slide-21
SLIDE 21

Associations

declaration usage

slide-22
SLIDE 22

LD, PJ-GE, july 2006 22

Association declarations

!"-4:"%$

  • '()*;"*+.

'()*0"*+

  • '()*;"*+.

'()*0"*+ Author Distrib Module

slide-23
SLIDE 23

LD, PJ-GE, july 2006 23

will generate LEFT OUTER JOINS

Associations

  • '()*;"*+.

'()*0"*+

creates method Distrib::author which returns a single object creates method Author::distribs which returns an arrayref creates method Author::insert_into_distribs

slide-24
SLIDE 24

LD, PJ-GE, july 2006 24

Role methods

,$1=1"6 $1216"9 $121 $1"21 ) 26" 26D2199. 2

  • 9

same API as TableOrView::select() role methods

slide-25
SLIDE 25

LD, PJ-GE, july 2006 25

Follow several roles at once

1)2 ?)E@()** ) 2444 1)21 E@()** ) 2444 Author Distrib Module aRow : AuthorDistribModule DBIx::DataModel::View new class created on the fly

slide-26
SLIDE 26

LD, PJ-GE, july 2006 26

Generated SQL

1)21 E@()** 2'()*""*+. ) 26" 26D219 <F<!&"." E@> F<E&>G&<@H>I >I4"24" JK<@<4"216"9 I"D1

slide-27
SLIDE 27

LD, PJ-GE, july 2006 27

n-to-n Associations

,;% '()*;"*+. '()*-$-0"*+ ,C% '()*;"*+. '()*-$-0"*+ ) '()*0-*+. '()*0- *+

slide-28
SLIDE 28

LD, PJ-GE, july 2006 28

Not covered here

updates and transactions tree expansions and exports (XML, Json) column handlers for

data conversions (scalar or object) data validation

adding ad hoc methods criteria combinations : preselectWhere Views ...

See Also : DBIx::DataModel manual