Commit | Line | Data |
f54428ab |
1 | #!/usr/bin/env perl |
0c337847 |
2 | |
f54428ab |
3 | use warnings; |
0c337847 |
4 | use strict; |
5 | |
f54428ab |
6 | use MyDatabase::Main; |
7 | |
0c337847 |
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>. |
11 | |
12 | get_tracks_by_cd('Bad'); |
13 | get_tracks_by_artist('Michael Jackson'); |
14 | |
15 | get_cd_by_track('Stan'); |
16 | get_cds_by_artist('Michael Jackson'); |
17 | |
18 | get_artist_by_track('Dirty Diana'); |
19 | get_artist_by_cd('The Marshall Mathers LP'); |
20 | |
21 | |
22 | sub get_tracks_by_cd { |
23 | my $cdtitle = shift; |
24 | print "get_tracks_by_cd($cdtitle):\n"; |
25 | my $rs = $schema->resultset('Track')->search( |
26 | { |
27 | 'cd.title' => $cdtitle |
28 | }, |
29 | { |
30 | join => [qw/ cd /], |
0c337847 |
31 | } |
32 | ); |
33 | while (my $track = $rs->next) { |
34 | print $track->title . "\n"; |
35 | } |
36 | print "\n"; |
37 | } |
38 | |
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( |
43 | { |
44 | 'artist.name' => $artistname |
45 | }, |
46 | { |
47 | join => { |
48 | 'cd' => 'artist' |
49 | }, |
50 | } |
51 | ); |
52 | while (my $track = $rs->next) { |
53 | print $track->title . "\n"; |
54 | } |
55 | print "\n"; |
56 | } |
57 | |
0c337847 |
58 | sub get_cd_by_track { |
59 | my $tracktitle = shift; |
60 | print "get_cd_by_track($tracktitle):\n"; |
61 | my $rs = $schema->resultset('Cd')->search( |
62 | { |
63 | 'tracks.title' => $tracktitle |
64 | }, |
65 | { |
66 | join => [qw/ tracks /], |
67 | } |
68 | ); |
69 | my $cd = $rs->first; |
70 | print $cd->title . "\n\n"; |
71 | } |
72 | |
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( |
77 | { |
78 | 'artist.name' => $artistname |
79 | }, |
80 | { |
81 | join => [qw/ artist /], |
0c337847 |
82 | } |
83 | ); |
84 | while (my $cd = $rs->next) { |
85 | print $cd->title . "\n"; |
86 | } |
87 | print "\n"; |
88 | } |
89 | |
0c337847 |
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( |
94 | { |
95 | 'tracks.title' => $tracktitle |
96 | }, |
97 | { |
98 | join => { |
99 | 'cds' => 'tracks' |
100 | } |
101 | } |
102 | ); |
103 | my $artist = $rs->first; |
104 | print $artist->name . "\n\n"; |
105 | } |
106 | |
0c337847 |
107 | sub get_artist_by_cd { |
108 | my $cdtitle = shift; |
109 | print "get_artist_by_cd($cdtitle):\n"; |
110 | my $rs = $schema->resultset('Artist')->search( |
111 | { |
112 | 'cds.title' => $cdtitle |
113 | }, |
114 | { |
115 | join => [qw/ cds /], |
116 | } |
117 | ); |
118 | my $artist = $rs->first; |
119 | print $artist->name . "\n\n"; |
120 | } |