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