Streamline ::ResultSource::* inheritance, similar to d009cb7d
[dbsrgits/DBIx-Class-Historic.git] / examples / Schema / testdb.pl
CommitLineData
f54428ab 1#!/usr/bin/env perl
0c337847 2
f54428ab 3use warnings;
0c337847 4use strict;
5
a5bd5d88 6use MyApp::Schema;
e48635f7 7use DBIx::Class::_Util 'parent_dir';
f54428ab 8
e48635f7 9my $db_fn = parent_dir( $INC{'MyApp/Schema.pm'} ) . '../db/example.db';
15ff1933 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 13my $schema = MyApp::Schema->connect("dbi:SQLite:$db_fn");
0c337847 14
15get_tracks_by_cd('Bad');
16get_tracks_by_artist('Michael Jackson');
17
18get_cd_by_track('Stan');
19get_cds_by_artist('Michael Jackson');
20
21get_artist_by_track('Dirty Diana');
22get_artist_by_cd('The Marshall Mathers LP');
23
24
25sub 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
42sub 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) {
1f9ae1a3 56 print $track->title . " (from the CD '" . $track->cd->title
57 . "')\n";
0c337847 58 }
59 print "\n";
60}
61
0c337847 62sub get_cd_by_track {
63 my $tracktitle = shift;
64 print "get_cd_by_track($tracktitle):\n";
65 my $rs = $schema->resultset('Cd')->search(
66 {
67 'tracks.title' => $tracktitle
68 },
69 {
70 join => [qw/ tracks /],
71 }
72 );
73 my $cd = $rs->first;
1f9ae1a3 74 print $cd->title . " has the track '$tracktitle'.\n\n";
0c337847 75}
76
77sub get_cds_by_artist {
78 my $artistname = shift;
79 print "get_cds_by_artist($artistname):\n";
80 my $rs = $schema->resultset('Cd')->search(
81 {
82 'artist.name' => $artistname
83 },
84 {
85 join => [qw/ artist /],
0c337847 86 }
87 );
88 while (my $cd = $rs->next) {
89 print $cd->title . "\n";
90 }
91 print "\n";
92}
93
0c337847 94sub get_artist_by_track {
95 my $tracktitle = shift;
96 print "get_artist_by_track($tracktitle):\n";
97 my $rs = $schema->resultset('Artist')->search(
98 {
99 'tracks.title' => $tracktitle
100 },
101 {
102 join => {
103 'cds' => 'tracks'
104 }
105 }
106 );
107 my $artist = $rs->first;
1f9ae1a3 108 print $artist->name . " recorded the track '$tracktitle'.\n\n";
0c337847 109}
110
0c337847 111sub get_artist_by_cd {
112 my $cdtitle = shift;
113 print "get_artist_by_cd($cdtitle):\n";
114 my $rs = $schema->resultset('Artist')->search(
115 {
116 'cds.title' => $cdtitle
117 },
118 {
119 join => [qw/ cds /],
120 }
121 );
122 my $artist = $rs->first;
1f9ae1a3 123 print $artist->name . " recorded the CD '$cdtitle'.\n\n";
0c337847 124}