8 my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
9 # for other DSNs, e.g. MySql, see the perldoc for the relevant dbd
10 # driver, e.g perldoc L<DBD::mysql>.
12 get_tracks_by_cd('Bad');
13 get_tracks_by_artist('Michael Jackson');
15 get_cd_by_track('Stan');
16 get_cds_by_artist('Michael Jackson');
18 get_artist_by_track('Dirty Diana');
19 get_artist_by_cd('The Marshall Mathers LP');
22 sub get_tracks_by_cd {
24 print "get_tracks_by_cd($cdtitle):\n";
25 my $rs = $schema->resultset('Track')->search(
27 'cd.title' => $cdtitle
33 while (my $track = $rs->next) {
34 print $track->title . "\n";
39 sub get_tracks_by_artist {
40 my $artistname = shift;
41 print "get_tracks_by_artist($artistname):\n";
42 my $rs = $schema->resultset('Track')->search(
44 'artist.name' => $artistname
52 while (my $track = $rs->next) {
53 print $track->title . "\n";
59 my $tracktitle = shift;
60 print "get_cd_by_track($tracktitle):\n";
61 my $rs = $schema->resultset('Cd')->search(
63 'tracks.title' => $tracktitle
66 join => [qw/ tracks /],
70 print $cd->title . "\n\n";
73 sub get_cds_by_artist {
74 my $artistname = shift;
75 print "get_cds_by_artist($artistname):\n";
76 my $rs = $schema->resultset('Cd')->search(
78 'artist.name' => $artistname
81 join => [qw/ artist /],
84 while (my $cd = $rs->next) {
85 print $cd->title . "\n";
90 sub get_artist_by_track {
91 my $tracktitle = shift;
92 print "get_artist_by_track($tracktitle):\n";
93 my $rs = $schema->resultset('Artist')->search(
95 'tracks.title' => $tracktitle
103 my $artist = $rs->first;
104 print $artist->name . "\n\n";
107 sub get_artist_by_cd {
109 print "get_artist_by_cd($cdtitle):\n";
110 my $rs = $schema->resultset('Artist')->search(
112 'cds.title' => $cdtitle
118 my $artist = $rs->first;
119 print $artist->name . "\n\n";