Example doesn't work without 'year' column
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Manual / Example.pod
1 =head1 NAME
2
3 DBIx::Class::Manual::Example - Simple CD database example
4
5 =head1 DESCRIPTION
6
7 This tutorial will guide you through the process of setting up and
8 testing a very basic CD database using SQLite, with DBIx::Class::Schema
9 as the database frontend.
10
11 The database consists of the following:
12
13   table 'artist' with columns:  artistid, name
14   table 'cd'     with columns:  cdid, artist, title, year
15   table 'track'  with columns:  trackid, cd, title
16
17
18 And these rules exists:
19
20   one artist can have many cds
21   one cd belongs to one artist
22   one cd can have many tracks
23   one track belongs to one cd
24
25
26 =head2 Installation
27
28 Install DBIx::Class via CPAN should be sufficient.
29
30 =head3 Create the database/tables
31
32 First make and change the directory:
33
34   mkdir app
35   cd app
36   mkdir db
37   cd db
38
39 This example uses SQLite which is a dependency of DBIx::Class, so you
40 shouldn't have to install extra software.
41
42 Save the following into a example.sql in the directory db
43
44   CREATE TABLE artist (
45     artistid INTEGER PRIMARY KEY,
46     name TEXT NOT NULL
47   );
48
49   CREATE TABLE cd (
50     cdid INTEGER PRIMARY KEY,
51     artist INTEGER NOT NULL REFERENCES artist(artistid),
52     title TEXT NOT NULL,
53     year TEXT
54   );
55
56   CREATE TABLE track (
57     trackid INTEGER PRIMARY KEY,
58     cd INTEGER NOT NULL REFERENCES cd(cdid),
59     title TEXT NOT NULL
60   );
61
62 and create the SQLite database file:
63
64   sqlite3 example.db < example.sql
65
66 =head3 Set up DBIx::Class::Schema
67
68 Change directory back from db to the directory app:
69
70   cd ../
71
72 Now create some more directories:
73
74   mkdir MyApp
75   mkdir MyApp/Schema
76   mkdir MyApp/Schema/Result
77   mkdir MyApp/Schema/ResultSet
78
79 Then, create the following DBIx::Class::Schema classes:
80
81 MyApp/Schema.pm:
82
83   package MyApp::Schema;
84   use base qw/DBIx::Class::Schema/;
85   __PACKAGE__->load_namespaces;
86
87   1;
88
89
90 MyApp/Schema/Result/Artist.pm:
91
92   package MyApp::Schema::Result::Artist;
93   use base qw/DBIx::Class::Core/;
94   __PACKAGE__->table('artist');
95   __PACKAGE__->add_columns(qw/ artistid name /);
96   __PACKAGE__->set_primary_key('artistid');
97   __PACKAGE__->has_many('cds' => 'MyApp::Schema::Result::Cd');
98
99   1;
100
101
102 MyApp/Schema/Result/Cd.pm:
103
104   package MyApp::Schema::Result::Cd;
105   use base qw/DBIx::Class::Core/;
106   __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
107   __PACKAGE__->table('cd');
108   __PACKAGE__->add_columns(qw/ cdid artist title year/);
109   __PACKAGE__->set_primary_key('cdid');
110   __PACKAGE__->belongs_to('artist' => 'MyApp::Schema::Result::Artist');
111   __PACKAGE__->has_many('tracks' => 'MyApp::Schema::Result::Track');
112
113   1;
114
115
116 MyApp/Schema/Result/Track.pm:
117
118   package MyApp::Schema::Result::Track;
119   use base qw/DBIx::Class::Core/;
120   __PACKAGE__->table('track');
121   __PACKAGE__->add_columns(qw/ trackid cd title /);
122   __PACKAGE__->set_primary_key('trackid');
123   __PACKAGE__->belongs_to('cd' => 'MyApp::Schema::Result::Cd');
124
125   1;
126
127
128 =head3 Write a script to insert some records
129
130 insertdb.pl
131
132   #!/usr/bin/perl
133
134   use strict;
135   use warnings;
136
137   use MyApp::Schema;
138
139   my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
140
141   my @artists = (['Michael Jackson'], ['Eminem']);
142   $schema->populate('Artist', [
143      [qw/name/],
144      @artists,
145   ]);
146
147   my %albums = (
148     'Thriller' => 'Michael Jackson',
149     'Bad' => 'Michael Jackson',
150     'The Marshall Mathers LP' => 'Eminem',
151   );
152
153   my @cds;
154   foreach my $lp (keys %albums) {
155     my $artist = $schema->resultset('Artist')->find({
156       name => $albums{$lp}
157     });
158     push @cds, [$lp, $artist->id];
159   }
160
161   $schema->populate('Cd', [
162     [qw/title artist/],
163     @cds,
164   ]);
165
166
167   my %tracks = (
168     'Beat It'         => 'Thriller',
169     'Billie Jean'     => 'Thriller',
170     'Dirty Diana'     => 'Bad',
171     'Smooth Criminal' => 'Bad',
172     'Leave Me Alone'  => 'Bad',
173     'Stan'            => 'The Marshall Mathers LP',
174     'The Way I Am'    => 'The Marshall Mathers LP',
175   );
176
177   my @tracks;
178   foreach my $track (keys %tracks) {
179     my $cdname = $schema->resultset('Cd')->find({
180       title => $tracks{$track},
181     });
182     push @tracks, [$cdname->id, $track];
183   }
184
185   $schema->populate('Track',[
186     [qw/cd title/],
187     @tracks,
188   ]);
189
190 =head3 Create and run the test scripts
191
192 testdb.pl:
193
194   #!/usr/bin/perl
195
196   use strict;
197   use warnings;
198
199   use MyApp::Schema;
200
201   my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
202   # for other DSNs, e.g. MySQL, see the perldoc for the relevant dbd
203   # driver, e.g perldoc L<DBD::mysql>.
204
205   get_tracks_by_cd('Bad');
206   get_tracks_by_artist('Michael Jackson');
207
208   get_cd_by_track('Stan');
209   get_cds_by_artist('Michael Jackson');
210
211   get_artist_by_track('Dirty Diana');
212   get_artist_by_cd('The Marshall Mathers LP');
213
214
215   sub get_tracks_by_cd {
216     my $cdtitle = shift;
217     print "get_tracks_by_cd($cdtitle):\n";
218     my $rs = $schema->resultset('Track')->search(
219       {
220         'cd.title' => $cdtitle
221       },
222       {
223         join     => [qw/ cd /],
224       }
225     );
226     while (my $track = $rs->next) {
227       print $track->title . "\n";
228     }
229     print "\n";
230   }
231
232   sub get_tracks_by_artist {
233     my $artistname = shift;
234     print "get_tracks_by_artist($artistname):\n";
235     my $rs = $schema->resultset('Track')->search(
236       {
237         'artist.name' => $artistname
238       },
239       {
240         join => {
241           'cd' => 'artist'
242         },
243       }
244     );
245     while (my $track = $rs->next) {
246       print $track->title . "\n";
247     }
248     print "\n";
249   }
250
251
252   sub get_cd_by_track {
253     my $tracktitle = shift;
254     print "get_cd_by_track($tracktitle):\n";
255     my $rs = $schema->resultset('Cd')->search(
256       {
257         'tracks.title' => $tracktitle
258       },
259       {
260         join     => [qw/ tracks /],
261       }
262     );
263     my $cd = $rs->first;
264     print $cd->title . "\n\n";
265   }
266
267   sub get_cds_by_artist {
268     my $artistname = shift;
269     print "get_cds_by_artist($artistname):\n";
270     my $rs = $schema->resultset('Cd')->search(
271       {
272         'artist.name' => $artistname
273       },
274       {
275         join     => [qw/ artist /],
276       }
277     );
278     while (my $cd = $rs->next) {
279       print $cd->title . "\n";
280     }
281     print "\n";
282   }
283
284
285
286   sub get_artist_by_track {
287     my $tracktitle = shift;
288     print "get_artist_by_track($tracktitle):\n";
289     my $rs = $schema->resultset('Artist')->search(
290       {
291         'tracks.title' => $tracktitle
292       },
293       {
294         join => {
295           'cds' => 'tracks'
296         }
297       }
298     );
299     my $artist = $rs->first;
300     print $artist->name . "\n\n";
301   }
302
303   sub get_artist_by_cd {
304     my $cdtitle = shift;
305     print "get_artist_by_cd($cdtitle):\n";
306     my $rs = $schema->resultset('Artist')->search(
307       {
308         'cds.title' => $cdtitle
309       },
310       {
311         join     => [qw/ cds /],
312       }
313     );
314     my $artist = $rs->first;
315     print $artist->name . "\n\n";
316   }
317
318
319
320 It should output:
321
322   get_tracks_by_cd(Bad):
323   Dirty Diana
324   Smooth Criminal
325   Leave Me Alone
326
327   get_tracks_by_artist(Michael Jackson):
328   Beat it
329   Billie Jean
330   Dirty Diana
331   Smooth Criminal
332   Leave Me Alone
333
334   get_cd_by_track(Stan):
335   The Marshall Mathers LP
336
337   get_cds_by_artist(Michael Jackson):
338   Thriller
339   Bad
340
341   get_artist_by_track(Dirty Diana):
342   Michael Jackson
343
344   get_artist_by_cd(The Marshall Mathers LP):
345   Eminem
346
347 =head1 Notes
348
349 A reference implementation of the database and scripts in this example
350 are available in the main distribution for DBIx::Class under the
351 directory F<examples/Schema>.
352
353 With these scripts we're relying on @INC looking in the current
354 working directory.  You may want to add the MyApp namespaces to
355 @INC in a different way when it comes to deployment.
356
357 The F<testdb.pl> script is an excellent start for testing your database
358 model.
359
360 This example uses L<DBIx::Class::Schema/load_namespaces> to load in the
361 appropriate L<Result|DBIx::Class::Manual::ResultClass> classes from the
362 C<MyApp::Schema::Result> namespace, and any required
363 L<ResultSet|DBIx::Class::ResultSet> classes from the
364 C<MyApp::Schema::ResultSet> namespace (although we created the directory
365 in the directions above we did not add, or need to add, any resultset
366 classes).
367
368 =head1 FURTHER QUESTIONS?
369
370 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
371
372 =head1 COPYRIGHT AND LICENSE
373
374 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
375 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
376 redistribute it and/or modify it under the same terms as the
377 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
378
379 =cut