README
1NAME
2 Class::DBI - Simple Database Abstraction
3
4SYNOPSIS
5 package Music::DBI;
6 use base 'Class::DBI';
7 Music::DBI->set_db('Main', 'dbi:mysql', 'username', 'password');
8
9 package Artist;
10 use base 'Music::DBI';
11 Artist->table('artist');
12 Artist->columns(All => qw/artistid name/);
13 Artist->has_many('cds', 'CD' => artist);
14
15 package CD;
16 use base 'Music::DBI';
17 CD->table('cd');
18 CD->columns(All => qw/cdid artist title year/);
19 CD->has_many('tracks', 'Track' => 'cd', { sort => 'position' });
20 CD->has_a(artist => 'CD::Artist');
21 CD->has_a(reldate => 'Time::Piece',
22 inflate => sub { Time::Piece->strptime(shift => "%Y-%m-%d") },
23 deflate => 'ymd',
24 }
25
26 CD->might_have(liner_notes => LinerNotes => qw/notes/);
27
28 package Track;
29 use base 'Music::DBI';
30 Track->table('track');
31 Track->columns(All => qw/trackid cd position title/);
32
33 #-- Meanwhile, in a nearby piece of code! --#
34
35 my $artist = Artist->create({ artistid => 1, name => 'U2' });
36
37 my $cd = $artist->add_to_cds({
38 cdid => 1,
39 title => 'October',
40 year => 1980,
41 });
42
43 # Oops, got it wrong.
44 $cd->year(1981);
45 $cd->commit;
46
47 # etc.
48
49 while (my $track = $cd->tracks) {
50 print $track->position, $track->title
51 }
52
53 $cd->delete; # also deletes the tracks
54
55 my $cd = CD->retrieve(1);
56 my @cds = CD->retrieve_all;
57 my @cds = CD->search(year => 1980);
58 my @cds = CD->search_like(title => 'October%');
59
60DESCRIPTION
61 Class::DBI provides a convenient abstraction layer to a database.
62
63 It not only provides a simple database to object mapping layer, but can
64 be used to implement several higher order database functions (triggers,
65 referential integrity, cascading delete etc.), at the application level,
66 rather than at the database.
67
68 This is particularly useful when using a database which doesn't support
69 these (such as MySQL), or when you would like your code to be portable
70 across multiple databases which might implement these things in
71 different ways.
72
73 In short, Class::DBI aims to make it simple to introduce 'best practice'
74 when dealing with data stored in a relational database.
75
76PRE-REQUISITES
77
78 Class::DBI requires on several other modules. Installing via the
79 CPAN shell should take care of all these for you. But if you need to
80 install by hand you need to have the following:
81
82 To run the module:
83
84 Class::Accessor
85 Class::Data::Inheritable
86 Class::Trigger
87 Ima::DBI
88 (which in turn requires DBI itself, and Class::WhiteHole)
89
90 To test the installation:
91 Test::More
92 DBD::SQLite
93 DBD::mysql
94 DBD::Pg
95