Commit | Line | Data |
f54428ab |
1 | #!/usr/bin/env perl |
0c337847 |
2 | |
f54428ab |
3 | use warnings; |
0c337847 |
4 | use strict; |
5 | |
a5bd5d88 |
6 | use MyApp::Schema; |
f54428ab |
7 | |
15ff1933 |
8 | use Path::Class 'file'; |
9 | my $db_fn = file($INC{'MyApp/Schema.pm'})->dir->parent->file('db/example.db'); |
10 | |
0c337847 |
11 | # for other DSNs, e.g. MySql, see the perldoc for the relevant dbd |
12 | # driver, e.g perldoc L<DBD::mysql>. |
15ff1933 |
13 | my $schema = MyApp::Schema->connect("dbi:SQLite:$db_fn"); |
0c337847 |
14 | |
15 | get_tracks_by_cd('Bad'); |
16 | get_tracks_by_artist('Michael Jackson'); |
17 | |
18 | get_cd_by_track('Stan'); |
19 | get_cds_by_artist('Michael Jackson'); |
20 | |
21 | get_artist_by_track('Dirty Diana'); |
22 | get_artist_by_cd('The Marshall Mathers LP'); |
23 | |
24 | |
25 | sub get_tracks_by_cd { |
26 | my $cdtitle = shift; |
27 | print "get_tracks_by_cd($cdtitle):\n"; |
28 | my $rs = $schema->resultset('Track')->search( |
29 | { |
30 | 'cd.title' => $cdtitle |
31 | }, |
32 | { |
33 | join => [qw/ cd /], |
0c337847 |
34 | } |
35 | ); |
36 | while (my $track = $rs->next) { |
37 | print $track->title . "\n"; |
38 | } |
39 | print "\n"; |
40 | } |
41 | |
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( |
46 | { |
47 | 'artist.name' => $artistname |
48 | }, |
49 | { |
50 | join => { |
51 | 'cd' => 'artist' |
52 | }, |
53 | } |
54 | ); |
55 | while (my $track = $rs->next) { |
56 | print $track->title . "\n"; |
57 | } |
58 | print "\n"; |
59 | } |
60 | |
0c337847 |
61 | sub get_cd_by_track { |
62 | my $tracktitle = shift; |
63 | print "get_cd_by_track($tracktitle):\n"; |
64 | my $rs = $schema->resultset('Cd')->search( |
65 | { |
66 | 'tracks.title' => $tracktitle |
67 | }, |
68 | { |
69 | join => [qw/ tracks /], |
70 | } |
71 | ); |
72 | my $cd = $rs->first; |
73 | print $cd->title . "\n\n"; |
74 | } |
75 | |
76 | sub get_cds_by_artist { |
77 | my $artistname = shift; |
78 | print "get_cds_by_artist($artistname):\n"; |
79 | my $rs = $schema->resultset('Cd')->search( |
80 | { |
81 | 'artist.name' => $artistname |
82 | }, |
83 | { |
84 | join => [qw/ artist /], |
0c337847 |
85 | } |
86 | ); |
87 | while (my $cd = $rs->next) { |
88 | print $cd->title . "\n"; |
89 | } |
90 | print "\n"; |
91 | } |
92 | |
0c337847 |
93 | sub get_artist_by_track { |
94 | my $tracktitle = shift; |
95 | print "get_artist_by_track($tracktitle):\n"; |
96 | my $rs = $schema->resultset('Artist')->search( |
97 | { |
98 | 'tracks.title' => $tracktitle |
99 | }, |
100 | { |
101 | join => { |
102 | 'cds' => 'tracks' |
103 | } |
104 | } |
105 | ); |
106 | my $artist = $rs->first; |
107 | print $artist->name . "\n\n"; |
108 | } |
109 | |
0c337847 |
110 | sub get_artist_by_cd { |
111 | my $cdtitle = shift; |
112 | print "get_artist_by_cd($cdtitle):\n"; |
113 | my $rs = $schema->resultset('Artist')->search( |
114 | { |
115 | 'cds.title' => $cdtitle |
116 | }, |
117 | { |
118 | join => [qw/ cds /], |
119 | } |
120 | ); |
121 | my $artist = $rs->first; |
122 | print $artist->name . "\n\n"; |
123 | } |