6 my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
7 # for other DSNs, e.g. MySql, see the perldoc for the relevant dbd
8 # driver, e.g perldoc L<DBD::mysql>.
10 get_tracks_by_cd('Bad');
11 get_tracks_by_artist('Michael Jackson');
13 get_cd_by_track('Stan');
14 get_cds_by_artist('Michael Jackson');
16 get_artist_by_track('Dirty Diana');
17 get_artist_by_cd('The Marshall Mathers LP');
20 sub get_tracks_by_cd {
22 print "get_tracks_by_cd($cdtitle):\n";
23 my $rs = $schema->resultset('Track')->search(
25 'cd.title' => $cdtitle
31 while (my $track = $rs->next) {
32 print $track->title . "\n";
37 sub get_tracks_by_artist {
38 my $artistname = shift;
39 print "get_tracks_by_artist($artistname):\n";
40 my $rs = $schema->resultset('Track')->search(
42 'artist.name' => $artistname
50 while (my $track = $rs->next) {
51 print $track->title . "\n";
58 my $tracktitle = shift;
59 print "get_cd_by_track($tracktitle):\n";
60 my $rs = $schema->resultset('Cd')->search(
62 'tracks.title' => $tracktitle
65 join => [qw/ tracks /],
69 print $cd->title . "\n\n";
72 sub get_cds_by_artist {
73 my $artistname = shift;
74 print "get_cds_by_artist($artistname):\n";
75 my $rs = $schema->resultset('Cd')->search(
77 'artist.name' => $artistname
80 join => [qw/ artist /],
83 while (my $cd = $rs->next) {
84 print $cd->title . "\n";
91 sub get_artist_by_track {
92 my $tracktitle = shift;
93 print "get_artist_by_track($tracktitle):\n";
94 my $rs = $schema->resultset('Artist')->search(
96 'tracks.title' => $tracktitle
104 my $artist = $rs->first;
105 print $artist->name . "\n\n";
109 sub get_artist_by_cd {
111 print "get_artist_by_cd($cdtitle):\n";
112 my $rs = $schema->resultset('Artist')->search(
114 'cds.title' => $cdtitle
120 my $artist = $rs->first;
121 print $artist->name . "\n\n";