7 use DBIx::Class::_Util 'parent_dir';
9 my $db_fn = parent_dir( $INC{'MyApp/Schema.pm'} ) . '../db/example.db';
11 # for other DSNs, e.g. MySql, see the perldoc for the relevant dbd
12 # driver, e.g perldoc L<DBD::mysql>.
13 my $schema = MyApp::Schema->connect("dbi:SQLite:$db_fn");
15 get_tracks_by_cd('Bad');
16 get_tracks_by_artist('Michael Jackson');
18 get_cd_by_track('Stan');
19 get_cds_by_artist('Michael Jackson');
21 get_artist_by_track('Dirty Diana');
22 get_artist_by_cd('The Marshall Mathers LP');
25 sub get_tracks_by_cd {
27 print "get_tracks_by_cd($cdtitle):\n";
28 my $rs = $schema->resultset('Track')->search(
30 'cd.title' => $cdtitle
36 while (my $track = $rs->next) {
37 print $track->title . "\n";
42 sub get_tracks_by_artist {
43 my $artistname = shift;
44 print "get_tracks_by_artist($artistname):\n";
45 my $rs = $schema->resultset('Track')->search(
47 'artist.name' => $artistname
55 while (my $track = $rs->next) {
56 print $track->title . " (from the CD '" . $track->cd->title
63 my $tracktitle = shift;
64 print "get_cd_by_track($tracktitle):\n";
65 my $rs = $schema->resultset('Cd')->search(
67 'tracks.title' => $tracktitle
70 join => [qw/ tracks /],
74 print $cd->title . " has the track '$tracktitle'.\n\n";
77 sub get_cds_by_artist {
78 my $artistname = shift;
79 print "get_cds_by_artist($artistname):\n";
80 my $rs = $schema->resultset('Cd')->search(
82 'artist.name' => $artistname
85 join => [qw/ artist /],
88 while (my $cd = $rs->next) {
89 print $cd->title . "\n";
94 sub get_artist_by_track {
95 my $tracktitle = shift;
96 print "get_artist_by_track($tracktitle):\n";
97 my $rs = $schema->resultset('Artist')->search(
99 'tracks.title' => $tracktitle
107 my $artist = $rs->first;
108 print $artist->name . " recorded the track '$tracktitle'.\n\n";
111 sub get_artist_by_cd {
113 print "get_artist_by_cd($cdtitle):\n";
114 my $rs = $schema->resultset('Artist')->search(
116 'cds.title' => $cdtitle
122 my $artist = $rs->first;
123 print $artist->name . " recorded the CD '$cdtitle'.\n\n";